-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
179 lines (150 loc) · 6.86 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
import numpy as np
import matplotlib.pyplot as plt
from test_function import Griewank, Rosenbrock, Sphere, Ackley, Rastrigin
from constant import seed_number, bound_lower, bound_upper, max_evals, F_scale, cross_prob
from constant import sigma_init, c_inc, c_dec, popsize, max_evals, dimension
from constant import num_elite, sigma_init, extra_std
from algorithm import DE, ES, CEM, CEMv2
from celluloid import Camera
def DifferentialEvolution(test_function, dimension, bound_lower, bound_upper, F_scale, cross_prob, popsize, max_evals):
np.random.rand(seed_number)
results, all_pops, generation_count = DE(test_function, dimension, [(bound_lower, bound_upper)]*dimension, F_scale, cross_prob, popsize, max_evals)
bound_lower = -6
bound_upper = 6
x = np.linspace(bound_lower, bound_upper, 100)
y = np.linspace(bound_lower, bound_upper, 100)
X, Y = np.meshgrid(x, y)
Z = test_function([X, Y])
fig = plt.figure(figsize=(12, 12))
camera = Camera(fig)
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(0, 0, marker='*')
for generation in range(generation_count):
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(0, 0, marker='*')
plt.scatter(all_pops[generation][:, 0], all_pops[generation][:, 1], c='#ff0000', marker='o')
plt.plot()
plt.pause(0.01)
camera.snap()
plt.show()
anim = camera.animate()
#save the animation as a gif file
anim.save('DE/Griewank' + "/" + 'Griewank' + "-DE-" + str(popsize) + ".gif",writer="pillow")
def EvolutionStrategies(test_function, bounds, sigma_init, c_inc, c_dec, popsize, max_evals, dimension):
np.random.rand(seed_number)
all_pops = []
generation_count = 0
results = []
results, all_pops, generation_count = ES(test_function, bounds, sigma_init, c_inc, c_dec, popsize, max_evals, dimension)
bound_lower = -33
bound_upper = 33
x = np.linspace(bound_lower, bound_upper, 100)
y = np.linspace(bound_lower, bound_upper, 100)
X, Y = np.meshgrid(x, y)
Z = test_function([X, Y])
fig = plt.figure(figsize=(12, 12))
camera = Camera(fig)
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(0, 0, marker='*')
for generation in range(generation_count):
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(0, 0, marker='*')
plt.scatter(all_pops[generation][:, 0], all_pops[generation][:, 1], c='#ff0000', marker='o')
plt.plot()
plt.pause(0.01)
camera.snap()
plt.show()
anim = camera.animate()
#save the animation as a gif file
anim.save('ES/Rastrigin' + "/" + 'Rastrigin' + "-ES-" + str(popsize) + ".gif",writer="pillow")
# The same format as EvolutionStrategies but use CEM instead of ES
def CrossEntropyMethod(test_function, dimensions, bounds, popsize, num_elite, sigma_init, seed_number, max_evals):
np.random.rand(seed_number)
# all_pops = []
generation_count = 0
results = []
cov_matrix = []
max_evals = 2500 if popsize < 512 else 10000
results, cov_matrix, ind, fitness, evals, generation_count = CEM(test_function, dimensions, bounds, popsize, num_elite, sigma_init, seed_number, max_evals)
# print(results)
# print(cov_matrix)
bound_lower = -1000
bound_upper = 1000
x = np.linspace(bound_lower, bound_upper, 100)
y = np.linspace(bound_lower, bound_upper, 100)
X, Y = np.meshgrid(x, y)
Z = test_function([X, Y])
fig = plt.figure(figsize=(12, 12))
camera = Camera(fig)
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(0, 0, marker='*')
for generation in range(len(results)):
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(0, 0, marker='*')
plt.scatter(results[generation][:, 0], results[generation][:, 1], c='#ff0000', marker='o')
plt.scatter(cov_matrix[generation][:, 0], cov_matrix[generation][:, 1], c='#ff1493', marker='x')
plt.plot()
plt.pause(0.1)
camera.snap()
plt.show()
anim = camera.animate()
#save the animation as a gif file
anim.save('CEM/Ackley' + "/" + 'Ackley' + "-CEM-" + str(popsize) + ".gif",writer="pillow")
def CrossEntropyMethodv2(test_function, dimensions, bounds, popsize, num_elite, sigma_init, extra_std, seed_number, max_evals):
np.random.rand(seed_number)
# all_pops = []
generation_count = 0
results = []
cov_matrix = []
max_evals = 5000 if popsize < 512 else 10000
results, cov_matrix, ind, fitness, evals, generation_count = CEMv2(test_function, dimensions, bounds, popsize, num_elite, sigma_init, extra_std, seed_number, max_evals)
# print(results)
# print(cov_matrix)
bound_lower = -30
bound_upper = 30
x = np.linspace(bound_lower, bound_upper, 100)
y = np.linspace(bound_lower, bound_upper, 100)
X, Y = np.meshgrid(x, y)
Z = test_function([X, Y])
fig = plt.figure(figsize=(12, 12))
camera = Camera(fig)
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(1, 1, marker='*')
for generation in range(len(results)):
plt.contourf(X, Y, Z, popsize, cmap='viridis')
plt.axis('square')
plt.scatter(1, 1, marker='*')
plt.scatter(results[generation][:, 0], results[generation][:, 1], c='#ff0000', marker='o')
plt.scatter(cov_matrix[generation][:, 0], cov_matrix[generation][:, 1], c='#ff1493', marker='x')
plt.plot()
plt.pause(0.1)
camera.snap()
plt.show()
anim = camera.animate()
#save the animation as a gif file
anim.save('CEM-v2/Rosenbrock' + "/" + 'Rosenbrock' + "-CEMv2-" + str(popsize) + ".gif",writer="pillow")
if __name__=='__main__':
all_fitness = []
num_evaluation = []
test_function = Rosenbrock
seed_number = 19520925
popsize_array = [32, 64, 128, 256, 512, 1024]
for popsize in popsize_array:
print(f"Popsize = {popsize}")
if dimension == 2:
max_evals = 1e5
bounds = [(bound_lower, bound_upper)]*dimension
mean_result = np.mean(all_fitness)
stddev_result = np.std(all_fitness)
mean_evaluation = np.mean(num_evaluation)
# DifferentialEvolution(test_function, dimension, bound_lower, bound_upper, F_scale, cross_prob, popsize, max_evals)
# EvolutionStrategies(test_function, bounds, sigma_init, c_inc, c_dec, popsize, max_evals, dimension)
# CrossEntropyMethod(test_function, dimension, bounds, popsize, num_elite, sigma_init, seed_number, max_evals)
CrossEntropyMethodv2(test_function, dimension, bounds, popsize, num_elite, sigma_init, extra_std, seed_number, max_evals)