-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalgorithm.py
251 lines (203 loc) · 7.46 KB
/
algorithm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import numpy as np
def DE(test_function, dimension, bounds, F_scale, cross_prob, popsize, max_evals):
"""
Differential Evolution algorithm
Args:
test_function -- function to conduct
bound_lower -- lower bound of the test function
bound_upper -- upper bound of the test function
F_scale -- scale factor on mutation
cross_prob -- the probability of 2 individuals to do crossover
popsize -- the population size
max_evals -- the maximum fitness evaluation for the algorithm
seed_number -- value of seed we want to run
Returns:
results -- best results after finishing the algorithm
all_pops -- all the population
"""
eps = 0.00001
bound_lower, bound_upper = np.asarray(bounds).T
diff = np.fabs(bound_lower - bound_upper)
pop = bound_lower + diff * np.random.rand(popsize, dimension)
fitness = np.asarray([test_function(ind) for ind in pop])
num_eval = 1
best_idx = np.argmin(fitness)
best = pop[best_idx]
results = []
all_pops = []
results.append((np.copy(best), fitness[best_idx], num_eval))
all_pops.append(np.copy(pop))
generation_count = 0
while True:
# max_evals = 10000 if popsize >= 512 else 5000
if num_eval > max_evals:
break
for i in range(popsize):
# Mutation step
idxes = [idx for idx in range(popsize) if idx != i]
a, b, c = pop[np.random.choice(idxes, 3, replace=False)]
mutant = np.clip(F_scale*(b - c) + a, bound_lower, bound_upper)
# Create cross point
cross_points = np.random.rand(dimension) < cross_prob
if not np.any(cross_points):
cross_points[np.random.randint(0, dimension)] = True
# Offspring
trial = np.where(cross_points, mutant, pop[i])
# Evaluate fitness
f = test_function(trial)
num_eval += 1
if f < fitness[i]:
pop[i] = trial
fitness[i] = f
if f < fitness[best_idx]:
best = trial
best_idx = i
results.append((np.copy(best), fitness[best_idx], num_eval))
all_pops.append(np.copy(pop))
if test_function(best) < eps:
num_eval += 1
break
generation_count += 1
return results, all_pops, generation_count
def ES(test_function, bounds, sigma_init, c_inc, c_dec, popsize, max_evals, dimension):
eps = 0.00001
bound_lower, bound_upper = np.asarray(bounds).T
diff = np.fabs(bound_lower - bound_upper)
mu = bound_lower + diff * np.random.rand(dimension)
mu_fitness = test_function(mu)
num_eval = 0
results = []
all_pops = []
results.append((np.copy(mu), mu_fitness, num_eval))
generation_count = 0
sigma = sigma_init
while True:
max_evals = 10000 if popsize >= 512 else 5000
if num_eval > max_evals:
break
epsilon = np.random.randn(popsize, dimension)
offspring = mu + sigma * epsilon
offspring = np.clip(offspring, bound_lower, bound_upper)
offspring_fitness = np.asarray([test_function(offspring[i]) for i in range(popsize)])
num_eval += popsize
best_idx = offspring_fitness.argmin()
best_fitness = offspring_fitness[best_idx]
best_offspring = offspring[best_idx]
if best_fitness <= mu_fitness:
mu = best_offspring.copy()
mu_fitness = best_fitness
sigma *= c_inc
else:
sigma *= c_dec
results.append((np.copy(mu), mu_fitness, num_eval))
all_pops.append(np.copy(offspring))
if abs(mu_fitness) < eps:
break
generation_count += 1
return results, all_pops, generation_count
def CEM(test_function, dimensions, bounds, popsize, num_elite, sigma_init, seed_number, max_evals):
np.random.seed(seed_number)
eps = 1e-4
bound_lower, bound_upper = np.asarray(bounds).T
sigma = sigma_init * np.eye(dimensions)
diff = np.fabs(bound_lower - bound_upper)
n_evals = 0
num_evals = []
# mu = np.random.rand(dimensions) - (bound_upper + 1)
mu = bound_lower + diff * np.random.rand(dimensions)
generation_count = 0
all_mu = []
all_sigma = []
all_offspring = []
all_pops = []
all_sigma = []
all_elite = []
all_fitness = []
while True:
# for i in range(10000):
if n_evals > max_evals:
break
all_mu.append(mu)
all_sigma.append(sigma)
x = np.random.multivariate_normal(mu, sigma, popsize)
all_pops.append(np.copy(x))
# print(np.sum(x))
all_offspring.append(x)
fitness = np.array([test_function(x[i]) for i in range(popsize)])
n_evals += popsize
best_fitness = max(fitness)
all_fitness.append(best_fitness)
# print(x)
if best_fitness < eps or np.sum(x) > 1e150 or np.sum(x) < -1e150:
break
elite_idx = fitness.argsort()[:num_elite]
all_elite.append(elite_idx)
mu = np.mean(x[elite_idx], axis=0)
sigma = np.zeros_like(sigma)
for i in range(num_elite):
z = x[elite_idx[i]] - mu
z = z.reshape(-1, 1)
# print(num_evals)
# sigma += tf.matmul(z.T, z)
# sigma += (z.T * z)
sigma += (z.T @ z)
all_sigma.append(sigma)
sigma *= (1/num_elite)
generation_count += 1
num_evals.append(n_evals)
all_mu.append(mu)
best_results = mu.copy()
best_fitness = test_function(mu)
return all_pops, all_sigma, all_mu, all_fitness, num_evals, generation_count
def CEMv2(test_function, dimensions, bounds, popsize, num_elite, sigma_init, extra_std, seed_number, max_evals):
np.random.seed(seed_number)
eps = 1e-4
bound_lower, bound_upper = np.asarray(bounds).T
sigma = sigma_init * np.eye(dimensions)
diff = np.fabs(bound_lower - bound_upper)
n_evals = 0
num_evals = []
mu = np.random.rand(dimensions) - (bound_upper + 1)
generation_count = 0
all_mu = []
all_sigma = []
all_offspring = []
all_pops = []
all_sigma = []
all_elite = []
all_fitness = []
while True:
# for i in range(10000):
if n_evals > max_evals:
break
all_mu.append(mu)
all_sigma.append(sigma)
x = np.random.multivariate_normal(mu, sigma, popsize)
all_pops.append(np.copy(x))
if np.sum(x) > 1e150 or np.sum(x) < -1e150:
break
all_offspring.append(x)
fitness = np.array([test_function(x[i]) for i in range(popsize)])
n_evals += popsize
best_fitness = max(fitness)
all_fitness.append(best_fitness)
if best_fitness < eps:
break
elite_idx = fitness.argsort()[:num_elite]
all_elite.append(elite_idx)
sigma = np.zeros_like(sigma)
for i in range(num_elite):
z = x[elite_idx[i]] - mu
z = z.reshape(-1, 1)
# sigma += tf.matmul(z.T, z)
sigma += (z.T @ z)
sigma += np.eye(dimensions)*extra_std
all_sigma.append(sigma)
sigma *= (1/num_elite)
mu = np.mean(x[elite_idx], axis=0)
generation_count += 1
num_evals.append(n_evals)
all_mu.append(mu)
best_results = mu.copy()
best_fitness = test_function(mu)
return all_pops, all_sigma, all_mu, all_fitness, num_evals, generation_count