What mean by this fitness coding? I'm not understand. What is "ones" means?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
This is the coding
clear
clc
n = 50; % Size of the swarm " no of birds "
bird_step = 50; % Maximum number of "birds steps"
dim = 10; % Dimension of the problem
c2 =1.2; % PSO parameter C1
c1 = 0.12; % PSO parameter C2
w =0.9; % pso momentum or inertia
fitness=0*ones(n,bird_step); %fitness population
%**********************************%
% Initialize the parameter
%**********************************%
R1 = rand(dim, n);
R2 = rand(dim, n);
current_fitness =0*ones(1,n);
%****************************************************%
% Initializing swarm and velocities and position
%****************************************************%
%Initialize random number of generator
current_position = 10*(rand(dim, n)-.5);
velocity = .3*randn(dim, n) ;
local_best_position = current_position ;
%**********************************%
% Evaluate initial population
%**********************************%
for i = 1:n
current_fitness(i) = rosenbrock(current_position(:,i));
end
local_best_fitness = current_fitness ;
[global_best_fitness,g] = min(local_best_fitness) ;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g) ;
end
%**********************************%
% Optimization
%**********************************%
%------VELOCITY UPDATE------%
velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position));
%------SWARMUPDATE------%
current_position = current_position + velocity ;
%------EVALUATE NEW SWARM------%
global_best_fitness = inf;
%% MAIN LOOP %%
iter = 0 ; % Iterationscounter
while ( iter < bird_step )
iter = iter + 1;
for i = 1:n,
current_fitness(i) = rosenbrock(current_position(:,i)) ;
end
for i = 1 : n
if current_fitness(i) < local_best_fitness(i)
local_best_fitness(i) = current_fitness(i);
local_best_position(:,i) = current_position(:,i) ;
end
end
[current_global_best_fitness,g] = min(local_best_fitness);
if current_global_best_fitness < global_best_fitness
global_best_fitness = current_global_best_fitness;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g);
end
end
velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position));
current_position = current_position + velocity;
x=current_position(1,:);
y=current_position(2,:);
clf
plot(x, y , 'h')
axis([-5 5 -5 5]);
pause(.3)
end % end of while loop its mean the end of all step that the birds move it
[Jbest_min,I] = min(current_fitness) % minimum fitness
current_position(:,I) % best solution
%
What i dont understand is...
fitness=0*ones(n,bird_step); %fitness population
What means by zero multiply by ones?
below is the rosenbrock.m
function f=ui(X)
%fitness function
n=length(X);
f=0;
for i=1:n-1
f=f+100*(X(i,1)*X(i,1)-X(i+1,1))^2+(X(i,1)-1)^2;%summation function f = f + ...
end
1 commentaire
Walter Roberson
le 23 Juin 2022
Modifié(e) : Walter Roberson
le 23 Juin 2022
it would have been clearer if the user had coded
fitness=zeros(n,bird_step); %fitness population
There are some very subtle efficiency differences in using 0*ones(), and some differences in internal preallocation that make no difference unless you are using an array larger than half of your RAM.
Mostly, some people prefer 0*ones() by analogy to something like 3.7*ones() which would be a strategy to set all the entries to 3.7. They feel that it is inconsistent to write zeros() for the case of setting to 0 while having to use Constant*ones() for setting the value to Constant.
Réponses (0)
Voir également
Catégories
En savoir plus sur Particle Swarm dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!