-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
79 lines (58 loc) · 1.79 KB
/
run.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
from matplotlib import pyplot as plt
import numpy as np
from PSO import PSO
import imageio
import os
def rosenbrock(x, y):
a = 0
b = 10
return (a - x) ** 2 + b * (y - x ** 2) ** 2
def rastrigin(x, y):
return 10 * 2 + (x ** 2 - 10 * np.cos(2 * np.pi * x)) + (y ** 2 - 10 * np.cos(2 * np.pi * y))
def plotFunction(range, evaluation):
steps = 70
x = np.linspace(-range, range, steps)
y = np.linspace(-range, range, steps)
z = np.array([evaluation(i, j) for j in y for i in x])
X, Y = np.meshgrid(x, y)
Z = z.reshape(steps, steps)
plt.contourf(X, Y, Z, steps)
plt.colorbar()
def findMinimum(r, evaluation, iterations):
filenames = []
particles = PSO(r, evaluation)
# Plot starting points
plotFunction(r, evaluation)
for particle in particles.particles:
plt.plot(particle.x, particle.y, 'wo')
filename = f'start.png'
filenames.append(filename)
plt.savefig(filename)
plt.close()
a = 0.9
step = (0.9 - 0.4)/iterations
# Plot every iteration
for iteration in range(iterations):
plotFunction(r, evaluation)
particles.move(a)
for particle in particles.particles:
plt.plot(particle.x, particle.y, 'wo')
filename = f'{iteration}.png'
filenames.append(filename)
plt.savefig(filename)
plt.close()
a -= step
return filenames
def main():
# Create pictures of every iteration
filenames = findMinimum(5, rastrigin, 20)
# Create GIF
with imageio.get_writer('POS.gif', mode='I') as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
# Remove files
for filename in set(filenames):
os.remove(filename)
if __name__ == '__main__':
main()