-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
205 lines (180 loc) · 8.13 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
MIT License
Copyright (c) 2020 Bachtiar Herdianto
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import numpy as np
import random
import matplotlib.pyplot as plt
import time
def Sphere (x): # Sphere function
total = 0
for i in range(len(x)):
total += x[i]**2
return total
def Rastrigin (x): # Rastrigin function
total = 10*len(x)
for i in range(len(x)):
total += x[i]**2 - (10*np.cos(2*np.pi*x[i]))
return total
def grad_Sphere (x): # Derivative of Sphere function
gradient_coordinate = []
for i in range(len(x)):
total = 2*x[i]
gradient_coordinate.append(total)
return np.array(gradient_coordinate)
def grad_Rastrigin (x): # Derivative of Rastrigin function
gradient_coordinate = []
for i in range(len(x)):
total = 2*x[i] + 10*2*np.pi*x[i]*np.sin(2*np.pi*x[i])
gradient_coordinate.append(total)
return np.array(gradient_coordinate)
class Particle:
def __init__(self, dim, minx, maxx, error):
self.position = np.random.uniform(low=minx, high=maxx, size=dim)
self.velocity = np.random.uniform(low=minx, high=maxx, size=dim)
self.best_part_pos = self.position.copy()
self.error = error(self.position)
self.best_part_err = self.error.copy()
def setPos(self, pos, error):
self.position = pos
self.error = error(pos)
if self.error < self.best_part_err:
self.best_part_err = self.error
self.best_part_pos = pos
def controlPos(self, bounds):
for i in range(len(bounds)):
if self.position[i] < bounds[i][0]:
self.position[i] = bounds[i][0]
if self.position[i] > bounds[i][1]:
self.position[i] = bounds[i][1]
class PSO:
def __init__(self, dims, numOfParticles, numOfEpochs, lower, upper, funct, grad):
self.swarm_list = [Particle(dims, lower, upper, funct) for i in range(numOfParticles)]
self.numOfEpochs = numOfEpochs
self.best_swarm_position = np.random.uniform(low=lower, high=upper, size=dims)
self.dimension = dims
self.upper = upper
self.lower = lower
self.best_swarm_error = -1
self.function = funct
self.gradien = grad
self.boundaries = []
for i in range(dims):
self.boundaries.append((lower, upper))
""" Optimize Function """
def optimize_signature(self, w, c1, c2):
r1 = random.random()
r2 = random.random()
funct = self.function
X_epoch = []
Y_error = []
boundaries = self.boundaries
for i in range(self.numOfEpochs):
for j in range(len(self.swarm_list)):
current_particle = self.swarm_list[j]
Vcog = r1*c1*(current_particle.best_part_pos - current_particle.position)
Vsos = r2*c2*(self.best_swarm_position - current_particle.position)
deltaV = w*current_particle.velocity + Vcog + Vsos # calculate deltaV
new_position = current_particle.position + deltaV # calculate the new position
self.swarm_list[j].setPos(new_position, funct)
self.swarm_list[j].velocity = deltaV
self.swarm_list[j].controlPos(boundaries)
# check the position if it is best for swarm
if funct(new_position) < self.best_swarm_error or self.best_swarm_error == -1:
self.best_swarm_position = new_position
self.best_swarm_error = funct(new_position)
X_epoch.append(i)
Y_error.append(self.best_swarm_error)
a, b = np.array(X_epoch), np.array(Y_error)
plt.plot(a, b)
plt.xlabel('Epoch'), plt.ylabel('Objective value (Error)'), plt.title('Report Optimization'), plt.show()
print('----------------------------\nTotal Epoch: {0} \nBest position: \n[{1}, {2}, {3}, {4}, {5}, {6}] \nBest known error: {7}'.format(
i + 1, self.best_swarm_position[0], self.best_swarm_position[1], self.best_swarm_position[2],
self.best_swarm_position[3], self.best_swarm_position[4], self.best_swarm_position[5], self.best_swarm_error))
def optimize_grad(self, w, c1, c2, lr):
funct = self.function
grad_funct = self.gradien
boundaries = self.boundaries
X_epoch = []
Y_error = []
for i in range(self.numOfEpochs):
for j in range(len(self.swarm_list)):
current_particle = self.swarm_list[j] # get current particle
Vcurr = grad_funct(current_particle.position) # calculate current velocity of particle
Vcog = c1*(current_particle.best_part_pos - current_particle.position)
Vsos = c2*(self.best_swarm_position - current_particle.position)
deltaV = w*Vcurr + Vcog + Vsos # calculate deltaV
new_position = self.swarm_list[j].position - lr*deltaV # calculate the new position
self.swarm_list[j].setPos(new_position, funct) # update the position of particle
self.swarm_list[j].controlPos(boundaries)
# check the position if it is best for swarm
if funct(new_position) < self.best_swarm_error or self.best_swarm_error == -1:
self.best_swarm_position = new_position
self.best_swarm_error = funct(new_position)
X_epoch.append(i)
Y_error.append(self.best_swarm_error)
a, b = np.array(X_epoch), np.array(Y_error)
plt.plot(a, b)
plt.xlabel('Epoch'), plt.ylabel('Objective value (Error)'), plt.title('Report Optimization'), plt.show()
print('----------------------------\nTotal Epoch: {0} \nBest position: \n[{1}, {2}, {3}, {4}, {5}, {6}] \nBest known error: {7}'.format(
i + 1, self.best_swarm_position[0], self.best_swarm_position[1], self.best_swarm_position[2],
self.best_swarm_position[3], self.best_swarm_position[4], self.best_swarm_position[5], self.best_swarm_error))
""" Running Code
General setting:
Number of dimmensions: 6
Number of particles: 30
Number of Max Iterations: 500
Setting no. 1:
Objective Funct.: Rastrigin
Setting no. 2:
Objective Funct.: Sphere """
setting01 = PSO(
dims=6,
numOfParticles=30,
numOfEpochs=500,
lower=-500,
upper=500,
funct=Rastrigin,
grad=grad_Rastrigin
)
setting02 = PSO(
dims=6,
numOfParticles=30,
numOfEpochs=500,
lower=-500,
upper=500,
funct=Sphere,
grad=grad_Sphere
)
def main():
print('Original PSO using setting no. 1:')
start1 = time.time()
optimize_signature(setting01, w=0.729, c1=1.49445, c2=1.49445)
end1 = time.time()
print("processing time:", end1-start1, 'second\n---------------------\n')
print('Original PSO using setting no. 2:')
start2 = time.time()
optimize_signature(setting02, w=1, c1=2, c2=2)
end2 = time.time()
print("processing time:", end2-start2, 'second\n---------------------\n')
print('Modified Weight-Addictive PSO:')
start = time.time()
optimize_grad(setting01, w=0.729, c1=1.49445, c2=1.49445, lr=0.01)
end = time.time()
print("processing time:", end - start, 'second\n')
main()