-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSO.py
executable file
·98 lines (85 loc) · 3.46 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
__author__ = 'Arneau Jacques van der Merwe'
import numpy as np
from logger_setup.logger import logger
def pso(s: object, x_min: object, x_max: object, y_min: object, y_max: object, inertia: object, c_1: object, c_2: object, obj_f: object) -> object:
particles = np.array(np.zeros((s, 2)))
particles_new = np.array(np.zeros((s, 2)))
velocities = np.array(np.zeros((s, 2)))
PBest = np.array(np.zeros((s, 3)))
GBest = np.array([0, 0, 0])
# GBestPast = 0
# delta = 0
# delta1 = 0
# delta2 = 0
# Initialize Swarm
min_ = 1000000
max_no_change = 0
for i in list(range(s)):
particles[i, 0] = np.random.uniform(x_min, x_max, 1)
PBest[i, 0] = particles[i, 0]
particles[i, 1] = np.random.uniform(y_min, y_max, 1)
PBest[i, 1] = particles[i, 1]
PBest[i, 2] = eval(obj_f.format(particles[i, 0], particles[i, 1]))
if PBest[i, 2] <= min_:
min_ = PBest[i, 2]
GBest[0] = PBest[i, 0]
GBest[1] = PBest[i, 1]
GBest[2] = PBest[i, 2]
velocities[i, 0] = np.random.uniform(-(x_max - x_min), (x_max - x_min), 1)
velocities[i, 1] = np.random.uniform(-(y_max - y_min), (y_max - y_min), 1)
GBestPast = GBest[2]
# Start Particle swarm
while max_no_change < 10:
for i in list(range(s)):
delta = 1
delta1 = 1
delta2 = 1
for d in list(range(2)):
rP = np.random.uniform(1, 0, 1)
rG = np.random.uniform(1, 0, 1)
velocities[i, d] = inertia*velocities[i, d] + c_1*rP*(PBest[i, d] - particles[i, d]) + c_2*rG*(GBest[d] - particles[i, d])
particles_new[i, d] = particles[i, d] + velocities[i, d]
if particles_new[i, 0] < x_min:
delta1 = (x_min - particles[i, 0])/velocities[i, 0]
elif particles_new[i, 0] > x_max:
delta1 = (x_max - particles[i, 0])/velocities[i, 0]
if particles_new[i, 1] < y_min:
delta2 = (y_min - particles[i, 1])/velocities[i, 1]
elif particles_new[i, 1] > y_max:
delta2 = (y_max - particles[i, 1])/velocities[i, 1]
delta = min(delta1, delta2)
for d in list(range(2)):
velocities[i, d] = velocities[i, d] * delta
particles[i, d] = particles[i, d] + velocities[i, d]
if eval(obj_f.format(particles[i, 0], particles[i, 1])) < PBest[i, 2]:
PBest[i, 0] = particles[i, 0]
PBest[i, 1] = particles[i, 1]
PBest[i, 2] = eval(obj_f.format(particles[i, 0], particles[i, 1]))
if PBest[i, 2] < GBest[2]:
GBest[0] = PBest[i, 0]
GBest[1] = PBest[i, 1]
GBest[2] = PBest[i, 2]
logger.info(GBest)
if GBestPast == GBest[2]:
max_no_change += 1
else:
max_no_change = 0
GBestPast = GBest[2]
return GBestPast
def main():
S = 50
C_1 = 1
C_2 = 1
inertia = 1
# C_1 = 1.62421000e+00
# C_2 = 2.59158586e+00
# inertia = 6.05988443e-01
objective_function = '-({1}+47)*np.sin(np.sqrt(np.abs({1}+({0}/2)+47)))-{0}*np.sin(np.sqrt(np.abs({0}-({1}+47))))'
x_min = -512
y_min = -512
x_max = 512
y_max = 512
p = pso(s=S, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max, inertia=inertia, c_1=C_1, c_2=C_2,
obj_f=objective_function)
if __name__ == '__main__':
main()