-
Notifications
You must be signed in to change notification settings - Fork 0
/
mopso.m
147 lines (113 loc) · 4.08 KB
/
mopso.m
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
%% Multiple Objective Particle Swarm Optimization
% Modified according to known best practice.
% Application of Computational Intelligence - As Taught Professor I. T. Yang
% National Taiwan University of Science and Technology
% Created by : Danny Gho
clc; clear;
w_init = 1;c1 = 2; c2 =2;
pop_size = 100; iteration = 10;
% Boundary Limit for the Particles, Please change it to match your condition.
ub = [5 3];
lb = [0 0];
vub = [0.5 0.5];
vlb = [-0.5 -0.5];
n_var = length(ub);
mo_fit = 'NNDI';
%% Particle Initialization
x = rand(pop_size, n_var) .* (ub-lb) + lb;
%% Initial Fitness Evaluation
fv = [];
for pop_iter=1:pop_size
fv(pop_iter,:) = objFunction(x(pop_iter,:));
end
pbestfv = fv;
pbest = x;
%%% Check for Domination
[pdominant, pdominantfv] = sortDomination(x, fv, pop_size)
% Determine the Fitness of the dominant
% Nearest Neighbor Density Estimator
pdominantfv_NNDE = distNNDE(pdominantfv);
[value, index] = max(pdominantfv_NNDE);
gbestNNDE = value;
gbestfv = pdominantfv(index,:);
gbest = pdominant(index,:);
v = (rand(pop_size, n_var)) .* (vub-vlb) + vlb;
%% Population Iteration
for iterator = 1:iteration
w = w_init - (0.5/iteration * iterator);
for pop_iter=1:pop_size
%Determine the Velocity
v(pop_iter,:) = w * v(pop_iter,:) + rand(1,n_var) .* c1 .* (pbest(pop_iter,:)-x(pop_iter,:)) + rand(1,n_var) .* c2 .* (gbest - x(pop_iter,:));
% Calculate the new Position
x(pop_iter,:) = x(pop_iter,:) + v(pop_iter,:) ;
% Fixing the Boundary
bindex_up = x(pop_iter,:) > ub;
bindex_down = x(pop_iter,:) < lb;
x(pop_iter,bindex_up)=ub(bindex_up);
x(pop_iter,bindex_down)=lb(bindex_down);
% Calculate the New Fitness Value
fv(pop_iter,:) = objFunction(x(pop_iter,:));
% Damping the Velocity
vbindex_up = v(pop_iter,:) > vub;
vbindex_down = v(pop_iter,:) < vlb;
v(pop_iter,vbindex_up)=vub(vbindex_up);
v(pop_iter,vbindex_down)=vlb(vbindex_down);
dominated = checkDomination(fv(pop_iter,:), pbestfv(pop_iter));
valid = checkValid(x(pop_iter,:),lb,ub);
if (dominated == 0 & valid == 1)
pbestfv(pop_iter,:) = fv(pop_iter,:);
pbest(pop_iter,:) = x(pop_iter,:);
end
end
% Expand the Domination Matrix
[pdominant, pdominantfv] = sortDomination([pdominant; x], [pdominantfv; fv], pop_size+size(pdominantfv,1))
%% Determine the GBest
pdominantfv_NNDE = distNNDE(pdominantfv);
[value, index] = max(pdominantfv_NNDE);
if value > gbestNNDE
gbestfv = pdominantfv(index,:);
gbest = pdominant(index,:);
end
end
if (nvar == 2)
scatter(pdominantfv(:,1), pdominantfv(:,2))
end
function fitness = objFunction(var)
%%% Test Case from http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.46.8661
fitness(1) = 4*var(1)^2 + 4*var(2)^2;
fitness(2) = (var(1) -5)^2 + (var(2)-5)^2;
end
function valid = checkValid(particle, lb, ub)
% Check for if the particle range is under UB and over LB
break_boundary = sum(particle>=ub) + sum(particle<=lb);
% TO DO
% Handle Constraint!!!
if (break_boundary == 0)
valid = 1;
else
valid = 0;
end
end
function [dominated, dominating] = checkDomination(particle,particles)
dominated = sum(sum((particles<=particle),2) == length(particle));
dominating = sum(sum((particles>=particle),2) == length(particle));
end
function [pdominant, pdominantfv] = sortDomination(x, fv, pop_size)
pdominant = [];
pdominantfv = [];
for pop_iter=1:pop_size
fv_iter = fv;
fv_iter(pop_iter,:) = [];
dominated = checkDomination(fv(pop_iter,:), fv_iter);
if (dominated == 0)
pdominantfv = [pdominantfv; fv(pop_iter,:)];
pdominant = [pdominant; x(pop_iter,:)];
end
end
end
function pdominantfv_NNDE = distNNDE(pdominantfv)
pdominantfv_NNDE = []
for iterator=1:size(pdominantfv,1)
pdominantfv_NNDE = [pdominantfv_NNDE; sum(mink(sqrt(sum((pdominantfv(iterator,:)-pdominantfv(:,:)).^2,2)),3))];
end
end