-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdateVelocity.m
91 lines (76 loc) · 2.46 KB
/
updateVelocity.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
function velocity = UpdateVelocity(particle,index,velocity,pbest,iteration)
%UPDATEV Summary of this function goes here
% Detailed explanation goes here
w = 0.7;
c = 2;
%velocity=zeros(max_iteration,swarm_size,particle_size,particle_size,1);
for x=1:101
for y=1:101
if( velocity(iteration,index,x,y,1)*w>1)
velocity(iteration,index,x,y,1)=1;
else
velocity(iteration,index,x,y,1)=velocity(iteration,index,x,y,1)*w;
end
end
end
self_position=zeros(101,2); %记录每个点的上一个顾客和下一个顾客
for x=1:101
for y=1:101
if(particle(iteration,index,x,y)==1)
self_position(y,1)=x;
self_position(x,2)=y;
break;
% elseif(y==102)
% disp(111111111111111111);
% disp(x);
end
end
end
best_order=zeros(10,101,2); %记录每个点的上一个顾客和下一个顾客
for size=1:10
for x=1:101
for y=1:101
if(pbest(iteration,size,x,y)==1)
best_order(size,y,1)=x;
best_order(size,x,2)=y;
% elseif(y==102)
% disp(222222222222);
% disp(x);
end
end
end
end
best_position=zeros(101,2);
for dimension=2:101
num=rand();%用于粒子的选取
num2=rand();%用於更新概率
if(num<0.3)
for x=1:101
for y=1:2
best_position(x,y)=best_order(index,x,y);
end
end
else
choose=round(rand()*9+1); %这里的10是swarm_size
for x=1:101
for y=1:2
best_position(x,y)=best_order(choose,x,y);
end
end
end
it=iteration+1;
if( best_position(dimension,1)~=0&&best_position(dimension,1)~= self_position(dimension,1))
if(c*num2>1)
velocity(it,index,best_position(dimension,1),dimension,1)=1;
elseif(c*num2>velocity(iteration,index,best_position(dimension,1),dimension,1))
velocity(it,index,best_position(dimension,1),dimension,1)=c*num2;
end
end
if( best_position(dimension,2)~=0&&best_position(dimension,2)~= self_position(dimension,2))
if(c*num2>1)
velocity(it,index,dimension,best_position(dimension,2),1)=1;
elseif(c*num2>velocity(iteration,index,dimension,best_position(dimension,2),1))
velocity(it,index,dimension,best_position(dimension,2),1)=c*num2;
end
end
end