New Post

Markov Chains || Step-By-Step || ~xRay Pixy

Image
Learn Markov Chains step-by-step using real-life examples. Click Here   Video Link Video Chapters: Markov Chains 00:00 Introduction 00:19 Topics Covered 01:49 Markov Chains Applications 02:04 Markov Property 03:18 Example 1 03:54 States, State Space, Transition Probabilities 06:17 Transition Matrix 08:17 Example 02 09:17 Example 03 10:26 Example 04 12:25 Example 05 14:16 Example 06 16:49 Example 07 18:11 Example 08 24:56 Conclusion In computer science, Markov problems are typically associated with Markov processes or Markov models . These are related to topics involving stochastic processes and probabilistic systems where future states depend only on the current state, not on the sequence of states that preceded it. Artificial Intelligence (AI): Markov Decision Processes (MDP): Used in decision-making problems, especially in reinforcement learning. Hidden Markov Models (HMM): Widely used in speech recognition, handwriting recognition, and natural language processing. Machine Le...

Whale Optimization Algorithm Code Implementation || WOA CODE || ~xRay Pixy

Whale Optimization Algorithm Code Implementation


Whale Optimization Algorithm Code Files


function obj_fun(test_fun)
switch test_fun
    case 'F1'
        x = -100:2:100; y=x;
    case 'F2'
        x = -10:2:10; y=x;
end
end


function [LB,UB,D,FitFun]=test_fun_info(C)
switch C
    case 'F1'
        FitFun = @F1;
        LB = -100; 
        UB = 100;
        D = 30;
    case 'F2'
        FitFun = @F2;
        LB = -10;
        UB = 10;
        D = 30;
end
% F1 Test Function
    function r = F1(x)
        r = sum(x.^2);
    end
% F2 Test Function
    function r = F2(x)
        r = sum(abs(x))+prod(abs(x));
    end
end

function Position = initialize(Pop_Size,D,UB,LB)
SS_Bounds = size(UB,2);

if SS_Bounds == 1
    Position = rand(Pop_Size,D).*(UB-LB)+LB;
end

if SS_Bounds>1
    for i = 1:D
        UB_i = UB(i);
        LB_i = LB(i);
        Position(:,i) = rand(Pop_Size,1).*(UB_i-LB_i)+LB_i; 
    end
end
end

function [Best_Val,Best_Pos,Convergence_Curve]=WOA(Pop_Size,MaxT,LB,UB,D,FitFun)
Best_Pos = zeros(1,D);
Best_Val = inf;

Position = initialize(Pop_Size,D,UB,LB);
Convergence_Curve = zeros(1,MaxT);

T = 0;

while T<MaxT
    for i = 1:size(Position,1)
        CheckUB = Position(i,:)>UB;
        CheckLB = Position(i,:)<LB;
        Position(i,:) = (Position(i,:).*(~(CheckUB+CheckLB)))+UB.*CheckUB+LB.*CheckLB;
        %Calculate Fitness Values
        Fitness_Val = FitFun(Position(i,:));
        %Compare Fitness Values
        if Fitness_Val<Best_Val
            Best_Val = Fitness_Val;
            Best_Pos = Position(i,:);
        end
    end
    a = 2-T*((2)/MaxT);
    a2 = -1+T*((-1)/MaxT);
    
    %Agents Position Update (New Positions)
    for i=1:size(Position,1)
        r1=rand();
        r2=rand();
        A = 2*a*r1-a;
        C = 2 * r2;
        b = 1;
        l = (a2-1)*rand+1;
        p = rand();
        for j = 1:size(Position,2)
            if p<0.5
                if abs(A)>1
                    rand_best_index=floor(Pop_Size*rand()+1);
                    X_rand = Position(rand_best_index,:);
                    D_X_rand = abs(C*X_rand(j)-Position(i,j));
                    Position(i,j) = X_rand(j)-A*D_X_rand;
                elseif abs(A)<1
                    D_Best = abs(C*Best_Pos(j)-Position(i,j));
                    Position(i,j) = Best_Pos(j)-A*D_Best;
                end
            elseif p>=0.5
                distance2Best = abs(Best_Pos(j)-Position(i,j));
                Position(i,j) = distance2Best * exp(b.*1).*cos(1.*2*pi)+Best_Pos(j);    
            end
        end  
    end
    T = T + 1;  %Counter Increment
    Convergence_Curve(T) = Best_Val;
    [T Best_Val]
end
end


clc
clear all
Pop_Size = 100;
Objective_Fun = 'F2';
MaxT = 500;

[LB,UB,D,FitFun] = test_fun_info(Objective_Fun);
[Best_Val,Best_Pos,Sol_Convergence]=WOA(Pop_Size,MaxT,LB,UB,D,FitFun);


subplot(1,1,1);
semilogy(Sol_Convergence,'Color','r');
title('Convergence Curve');
xlabel('Iteration');
ylabel('Best Value');
axis tight
grid on
box on
legend ('WOA')

display(['Best Position',num2str(Best_Pos)]);
display(['Best_Value ',num2str(Best_Val)]);









Comments

Popular Post

PARTICLE SWARM OPTIMIZATION ALGORITHM NUMERICAL EXAMPLE

Cuckoo Search Algorithm for Optimization Problems

Particle Swarm Optimization (PSO)

PSO (Particle Swarm Optimization) Example Step-by-Step

how is the LBP |Local Binary Pattern| values calculated? Step-by-Step with Example

PSO Python Code || Particle Swarm Optimization in Python || ~xRay Pixy

Grey Wolf Optimization Algorithm

Bat algorithm Explanation Step by Step with example

Grey Wolf Optimization Algorithm Numerical Example