-
Notifications
You must be signed in to change notification settings - Fork 18
/
PSO_lbest.m
73 lines (72 loc) · 2.07 KB
/
PSO_lbest.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
function [costs, bestSol] = PSO_lbest(jobs, m, n, particals, iterations, costFunc)
c1 = 1.4944;
c2 = 1.4944;
w = 0.9;
gbests = ones(1, n);
gbest = costFunc(gbests, jobs, m, n);
lbests = randi(m, particals, n);
lbest = ones(1, particals);
nbests = lbests;
nbest = lbest;
nsize = 2;
costs = ones(iterations, 1);
costsEnd = 0;
for i = 1:particals
lbest(i) = costFunc(lbests(i, :), jobs, m, n);
%if lbest(i) < gbest
% gbest = lbest(i);
% gbests = lbests(i, :);
%end
nbest(i) = lbest(i);
end
for i = 1:particals
for k = (i-nsize-1):(i+nsize-1)
if lbest(i) < nbest(mod(k, particals) + 1)
nbest(mod(k, particals) + 1) = lbest(i);
nbests(mod(k, particals) + 1, :) = lbests(i, :);
end
end
end
x = lbests;
v = zeros(particals, n);
for i = 1:iterations
ibest = costFunc(ones(particals, n), jobs, m, n);
r1 = rand(particals, 1);
r2 = rand(particals, 1);
%v = w*v + c1 * bsxfun(@times, r1, lbests - x) + ...
% c2 * bsxfun(@times, r2, (bsxfun(@minus, gbests, x)));
v = w*v + c1 * bsxfun(@times, r1, lbests - x) + ...
c2 * bsxfun(@times, r2, nbests - x);
x = round(x + v);
%x = bsxfun(@mod, x-1, m) + 1;
x(x < 1) = 1;
x(x > m) = m;
for j = 1:particals
c = costFunc(x(j, :), jobs, m, n);
if c < ibest
ibest = c;
end
if c < lbest(j)
lbest(j) = c;
lbests(j, :) = x(j, :);
for k = (j-nsize-1):(j+nsize-1)
if lbest(j) < nbest(mod(k, particals) + 1)
nbest(mod(k, particals) + 1) = lbest(j);
nbests(mod(k, particals) + 1, :) = lbests(j, :);
end
end
end
end
costsEnd = costsEnd + 1;
costs(costsEnd) = ibest;
[ilbest, idx] = min(lbest);
%ibest
ilbests = lbests(idx, :);
if ilbest < gbest
gbest = ilbest;
gbests = ilbests;
end
end
%v
bestSol = gbests;
end