-
Notifications
You must be signed in to change notification settings - Fork 1
/
pso.py
160 lines (107 loc) · 5.23 KB
/
pso.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
import numpy as np
import random
import statistics
import generate
import aqem
def optimization(K, N, P, G, R, threshold, alpha, beta, w, vmax, phi, input, mu, sigma, loss, visibility):
'''Particle Swarm Optimization. This function returns the best policy generated by the PSO algorithm.'''
position = np.array([np.array([np.array([0. for i in range(N)]) for j in range(P)]) for k in range(G)])
velocity = np.array([np.array([np.array([0. for i in range(N)]) for j in range(P)]) for k in range(G)])
for i in range(P):
position[0][i] = np.array(generate.policy(N))
outcome = np.array([np.array([0. for i in range(P)]) for j in range(G)])
best_pos = np.array([np.array([0. for i in range(N)]) for j in range(P)])
best_val = np.array([0. for i in range(P)])
aux = 1000
gbest = 0
count = 0
for g in range(G-1):
print("Generation:", g)
aux_counter = 0
if g % 10 == 0 and g > 0:
vmax = vmax / 2
for i in range(P):
outcome[g][i] = aqem.simulate(K, N, R, phi, input, position[g][i], mu, sigma, loss, visibility)
if g == 0:
best_val[i] = outcome[g][i]
best_pos[i] = position[g][i]
if g > 0:
if outcome[g][i] < best_val[i]:
best_val[i] = outcome[g][i]
best_pos[i] = position[g][i]
if outcome[g][i] < aux:
aux = outcome[g][i]
gbest = i
for i in range(P):
for j in range(N):
a = best_pos[i][j]-position[g][i][j]
if a > 1*np.pi:
a = a - 2*np.pi
if a < -1*np.pi:
a = a + 2*np.pi
b = best_pos[gbest][j]-position[g][i][j]
if b > 1*np.pi:
b = b - 2*np.pi
if b < -1*np.pi:
b = b + 2*np.pi
velocity[g+1][i][j] = velocity[g][i][j] + alpha*random.uniform(0,1)*a + beta*random.uniform(0,1)*b
position[g+1][i][j] = position[g][i][j] + w * velocity[g+1][i][j]
if position[g+1][i][j] < 0:
a = abs(position[g+1][i][j])
b = a // (2*np.pi) + 1
position[g+1][i][j] = 2*b*np.pi - a
if position[g+1][i][j] >= 2*np.pi:
a = position[g+1][i][j]
b = a // (2*np.pi)
position[g+1][i][j] = a - 2*b*np.pi
if velocity[g+1][i][j] <= -2*np.pi:
a = velocity[g+1][i][j]
b = a // (2*np.pi) + 1
velocity[g+1][i][j] = a - 2*b*np.pi
if velocity[g+1][i][j] >= 2*np.pi:
a = velocity[g+1][i][j]
b = a // (2*np.pi)
velocity[g+1][i][j] = a - 2*b*np.pi
if velocity[g+1][i][j] > 1*np.pi:
velocity[g+1][i][j] = velocity[g+1][i][j] - 2*np.pi
if velocity[g+1][i][j] < -1*np.pi:
velocity[g+1][i][j] = velocity[g+1][i][j] + 2*np.pi
if velocity[g+1][i][j] > vmax*2*np.pi:
velocity[g+1][i][j] = vmax*2*np.pi
if velocity[g+1][i][j] < -vmax*2*np.pi:
velocity[g+1][i][j] = -vmax*2*np.pi
count = count + 1
c = np.array([0. for i in range(N)])
s = np.array([0. for i in range(N)])
for i in range(P):
for j in range(N):
c[j] = c[j] + np.real(np.cos(position[g][i][j])) / P
s[j] = s[j] + np.real(np.sin(position[g][i][j])) / P
best_policy = np.array([0. for i in range(N)])
for i in range(N):
if c[i] > 0 and s[i] > 0:
best_policy[i] = np.real(np.arctan(s[i]/c[i]))
if c[i] < 0:
best_policy[i] = np.real(np.arctan(s[i]/c[i]) + 1*np.pi)
if c[i] > 0 and s[i] < 0:
best_policy[i] = np.real(np.arctan(s[i]/c[i]) + 2*np.pi)
dispersion = np.array([0. for i in range(N)])
for i in range(N):
for j in range(P):
if abs(best_policy[i] - position[g][j][i]) < 1*np.pi:
dispersion[i] = dispersion[i] + abs(best_policy[i] - position[g][j][i])
else:
dispersion[i] = dispersion[i] + 2*np.pi - abs(best_policy[i] - position[g][j][i])
dispersion[i] = dispersion[i] / P
if dispersion[i] < threshold * 2*np.pi:
aux_counter = aux_counter + 1
if aux_counter == N:
break
if count < G-1:
print("The algorithm converged in", count, "iterations.\n")
else:
print("The algorithm didn't converge in", G, "iterations.\n")
dispersion_avg = statistics.mean(dispersion)
dispersion_std = statistics.stdev(dispersion)
print("Dispersion Average: ", dispersion_avg, " Dispersion Standard Deviation: ", dispersion_std, "\n")
return best_policy