-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSO.asv
66 lines (63 loc) · 2.22 KB
/
PSO.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function [Xoptima,FitnessOptimum] = PSO(dimensionSize,landscape,boundary,populationSize,simulationLimit)
%Particle Swarm Optimization
%dimensionSize --- is the size of each individual; landscape -- is the fitness function
%boundary --- is the value area of the individual;
%velocity adjustment: v = w*v + c1*rand()*(pbest - present) + c2*rand()*(pbest[gbest] - present)
%position adjustment: x = x+v;
w = 1;
c1 = 2;
c2 = 2;
resolution = 100;
Vmax = (boundary(2)-boundary(1))/resolution;
pbest = [];
present = [];
fitnessPoul = [];
v = [];
gbest = 1;% the subscript of pbest to specify the best in pbest;
%initialize the population,and it's speed
for i = 1 : populationSize,
individual = [];
soloSpeed = [];
for j = 1 : dimensionSize,
individual = [individual,boundary(1)+(boundary(2)-boundary(1))*rand()];
soloSpeed = [soloSpeed,Vmax*rand()];
end
present = [present;individual];
v = [v;soloSpeed];
end
%initialize the pbest and present
pbest = present;
%intialize the gbest
for i = 1 : populationSize,
fitnessPoul = [fitnessPoul;landscape(pbest(i,:))];
end
tempGbest = find(fitnessPoul == max(fitnessPoul));
gbest = tempGbest(1);
%evoluation
for i = 1 : simulationLimit,%per simulation
for j = 1 : populationSize,%per individual
for k = 1 : dimensionSize,%per character
present(j,k) = present(j,k) + v(j,k); % update the postion of the specific dimension of the individual
v(j,k) = w*v(j,k) + c1*rand()*(pbest(j,k) - present(j,k)) + c2*rand()*(pbest(gbest,k) - present(j,k));%update the speed in the specific dimension of the individual
end
%calculate the fitness ot the updated individual
fitness = landscape(present(j,:));
%update the pbest if this individual perform better then before
if fitness > fitnessPoul(j),
fitnessPoul(j) = fitness;
%pbest(k,:) = present(k,:);
pbest(j,:) = present(j,:);
end
end
%update the global optimum
tempGbest = find(fitnessPoul == max(fitnessPoul));
if gbest < tempGbest(1),
gbest = tempGbest(1);
end
end
fprintf('Result:\n\tpopulation:\n');
disp(present);
fprintf('\tGlobal best fitness:\n');
disp(gbest);
disp(fitnessPoul(gbest));
end