Home > Back-end >  How to relate the objective function and algorithm matlab programming, I want to change a target fun
How to relate the objective function and algorithm matlab programming, I want to change a target fun

Time:09-22

The function [best, fmin N_iter]=fpa_demo (para)

% parametersIf nargin<1,
Para=0.8 [20];
End

N=para (1); % size
P=para (2);
% probability

% iteration parametersN_iter=500; % of the total of iterative


% search variablesD=3;
Lb=2 * ones (1 d);
Ub=2 * ones (1 d);


% initialization groupFor I=1: n,
Sol (I, :)=Lb + (Ub - Lb). * rand (1 d);
The Fitness (I)=Fun (Sol (I, :));
End

Find the best %
[fmin, I]=min (Fitness);
Best=Sol (I);
S=Sol;


% start iterationFor t=1: N_iter,
% Loop over all bats/solutions
For I=1: n,
% Pollens are carried by insects and thus can move in
% large scale, large short.
% This L should replace by Levy flights
% Formula: x_i ^ {t + 1}=x_i ^ t + L (x_i ^ t - gbest)
If rand> P
% % L=rand;
L=Levy (d);
DS=l. * (Sol (I, :) - best);
S (I, :)=Sol (I, :) + dS;

% check line
S (I, :)=simplebounds (S (I, :), Lb, Ub);

% If not, then the local pollenation of neighbor flowers
The else
Epsilon=rand;

% random flowersJK=randperm (n);
% As they are random, the first two entries of the random
% If the flower are the same or similar species, then
% they can be pollenated, otherwise, no action.
% Formula: x_i ^ {t + 1} + epsilon * (x_j ^ t - x_k ^ t)
S (I, :)=S (I, :) + epsilon * (Sol (JK (1), :) - Sol (JK (2), :));
% Check if the simple limits/bounds are OK
S (I, :)=simplebounds (S (I, :), Lb, Ub);
End

% to evaluate new
Fnew=Fun (S (I, :));
If the % is a better solution, we update the
If (Fnew<=Fitness (I)),
Sol (I, :)=S (I, :);
The Fitness (I)=Fnew;
End

% update current best
If FnewBest=S (I);
Fmin=Fnew;
End
End

% per 100 iterations resultsIf round (100) t/t/100==,
The best;
Fmin;
End

End

% o
Disp ([' Total number of evaluations: ' ', num2str (N_iter * n)));
Disp ([' Best solution=', num2str (Best), 'fmin=', num2str (fmin)]);



% simple constraintsThe function s=simplebounds (s, Lb, Ub)
% set lower limit
Ns_tmp=s;
I=ns_tmp & lt; Lb.
Ns_tmp (I)=Lb (I);

% cap
J=ns_tmp & gt; Ub;
Ns_tmp (J)=Ub (J);
% Update this new move
S=ns_tmp;


% levy flight
The function of L=Levy (d)
Beta=3/2;
Sigma=(gamma (1 + beta) * sin (PI * beta/2)/(gamma ((1 + beta)/2) * beta * 2 ^ ((beta - 1)/2))) ^ (1/beta);
U=randn (1 d) * sigma;
V=randn (1 d);
Step=u./abs (v). ^ (1/beta);
L=0.01 * step;

% Objective function and here we have 2 Rosenbrock 's 3 d function
The function of z=Fun (u)
Z=(1 - u (1)) ^ 2 + 100 * (u (2), u (1) ^ 2) ^ 2 + 100 * (u (3), u (2) ^ 2) ^ 2;
  • Related