-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_elites.py
executable file
·416 lines (298 loc) · 12.5 KB
/
map_elites.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
import cPickle
import random
from time import time
from copy import deepcopy
class Individual(object):
def __init__(self, o):
self.o = o
self.fitness = None
self.characteristics = None
def clone(self):
c = Individual(deepcopy(self.o))
return c
def __str__(self):
return str(self.o)
class MAPElites(object):
def __init__(self, dims, solution_generator, mutate, initial_population=10, evaluator=None, batch_evaluator=None):
self._dims = dims
self._solution_generator = solution_generator
self._mutate = mutate
self._evaluator = evaluator
self._batch_evaluator = batch_evaluator
self._initial_population = initial_population
self._log_folder = "map_elites_checkpoints"
if not os.path.exists(self._log_folder):
print "Creating logfolder"
os.makedirs(self._log_folder)
self._pop = None
def __getstate__(self):
state = self.__dict__.copy()
# Remove the unpicklable entries.
del state['_solution_generator']
del state['_evaluator']
del state['_batch_evaluator']
del state['_mutate']
return state
def _recursive_matrix_helper(self, base, dims):
dim = dims[0]
for i in range(dim):
base.append([])
if len(dims) > 1:
return [self._recursive_matrix_helper(cell, dims[1:]) for cell in base]
else:
return base
def _create_pop(self):
pop = self._recursive_matrix_helper([], self._dims)
return pop
def init(self):
self._pop = self._create_pop()
individual_batch = [Individual(self._solution_generator()) for i in range(self._initial_population)]
if self._batch_evaluator is not None:
solutions = map(lambda i: i.o, individual_batch)
results = self._batch_evaluator(0, solutions)
for individual, (fitness, characteristics) in zip(individual_batch, results):
individual.characteristics = characteristics
individual.fitness = fitness
else:
for individual in individual_batch:
fitness, characteristics = self._evaluator(0, individual.o)
individual.characteristics = characteristics
individual.fitness = fitness
map(self._place_solution, individual_batch)
return self
def __exit__(self, type, value, traceback):
pass
def _random_indexes(self):
return [random.randint(0, dim-1) for dim in self._dims]
def _get_individual(self, indexes):
copy_indexes = indexes[:]
r = self._pop
while len(copy_indexes) > 0:
r = r[copy_indexes.pop(0)]
if len(r) == 0:
return None
else:
return r[0]
def save_checkpoint(self, filename=None):
if filename is None:
filename = "mapelites_default.chkpt"
f = open(filename, "w")
cPickle.dump(self, f)
f.close()
def _place_solution(self, solution):
characteristic_values = list(solution.characteristics.values())
assert len(characteristic_values) == len(self._dims), "Wrong number of characteristics valued returned by fitness (%s)" % str( solution.characteristics )
indexes = []
for i, v in enumerate(characteristic_values):
steps = 1./(self._dims[i]-1)
# Fix for this issue, add 0.00001
# Python 2.7.12 (default, Nov 19 2016, 06:48:10)
# [GCC 5.4.0 20160609] on linux2
# >>> int(0.3/0.1)
# 2
# >>> 0.1/0.1
# 1.0
# >>> 0.2/0.1
# 2.0
# >>> 0.3/0.1
# 2.9999999999999996
# >>> 0.4/0.1
# 4.0
# >>> 0.5/0.1
# 5.0
# >>> 0.6/0.1
# 5.999999999999999
bin_index = max(0, min(self._dims[i]-1, int(characteristic_values[i]/steps+0.00001)))
indexes.append(bin_index)
r = self._pop
while len(indexes) > 0:
r = r[indexes.pop(0)]
if len(r) == 0:
r.append(solution)
else:
if solution.fitness > r[0].fitness:
r.pop()
r.append(solution)
def _get_random_individual(self, on_boarder=False):
indexes = self._random_indexes()
ind = self._get_individual(indexes)
retry_count = 10
connections = 1
while ind is None or (on_boarder and self._neighbour_count(indexes) > connections):
if retry_count == 0:
connections += 1
retry_count = 10
retry_count -= 1
indexes = self._random_indexes()
ind = self._get_individual(indexes)
return ind
#Guaranteed to return indexes to a valid individual (if the MAP contains one)
def _random_valid_indexes(self):
indexes = [random.randint(0, dim-1) for dim in self._dims]
while self._get_individual(indexes) is None:
indexes = [random.randint(0, dim-1) for dim in self._dims]
return indexes
def _border_tournament_indexes(self, n=5):
list_of_indexes = [self._random_valid_indexes() for _ in range(n)]
return min(list_of_indexes, key=self._neighbour_count)
def _neighbour_count(self, indexes):
count = 0
for i in range(len(indexes)):
copy_indexes = indexes[:]
copy_indexes[i] = copy_indexes[i] + 1
if self._dims[i] == copy_indexes[i]:
count += 1
elif self._get_individual(copy_indexes) is not None :
count += 1
copy_indexes[i] = copy_indexes[i] - 2
if copy_indexes[i] == 0:
count += 1
elif self._get_individual(copy_indexes) is not None:
count += 1
return count
def run_batch(self, n_gen = 1000, batch_size = 10, prefer_border=False):
start_time = time()
for gen in range(1,n_gen):
checkpoint_name = os.path.join(self._log_folder,"mapelites_gen_%s.chkpt" % gen)
self.save_checkpoint(checkpoint_name)
individual_batch = []
for n in range(batch_size):
if prefer_border and False:
indexes = self._border_tournament_indexes()
offspring = self._get_individual(indexes)
else:
offspring = self._get_random_individual(prefer_border).clone()
offspring.o = self._mutate(offspring.o)
individual_batch.append(offspring)
if self._batch_evaluator is not None:
solutions = map(lambda i: i.o, individual_batch)
results = self._batch_evaluator(gen, solutions)
for individual, (fitness, characteristics) in zip(individual_batch, results):
individual.characteristics = characteristics
individual.fitness = fitness
else:
for individual in individual_batch:
fitness, characteristics = self._evaluator(gen, individual.o)
individual.characteristics = characteristics
individual.fitness = fitness
for individual in individual_batch:
self._place_solution(individual)
current_time = time()
time_diff = current_time-start_time
time_per_generation = time_diff/gen
to_completion = time_per_generation*n_gen - time_diff
print "Elapsed", time_diff, " Time per generation", time_per_generation, " Time to completion", to_completion
def _get_sub_matrix(self, i ):
return self._pop[i]
def _extract_fitness_2dmatrix(self, sub_matrix):
r = []
for i in range(len(sub_matrix)):
row = []
for j in range(len(sub_matrix[0])):
if len(sub_matrix[i][j]) == 1:
row.append(sub_matrix[i][j][0].fitness)
else:
row.append(-1.0)
r.append(row)
return r
def get_plottable_fitness(self, i=None):
evaluated_matrix = self._extract_fitness_2dmatrix(self._pop[i])
return evaluated_matrix
def get_individuals(self, indexes, depth=0, root=None):
assert depth + len(indexes) == len(self._dims)
if root is None:
root = self._pop
i = indexes.pop(0)
if i is None:
if len(indexes) == 0:
return root
else:
r = []
for c in root:
t = self.get_individuals(indexes[:], depth+1, root=c)
r.append(t)
return r
else:
if len(indexes) == 0:
return root[i]
else:
return self.get_individuals(indexes[:], depth+1, root=root[i])
def _recursive_get(self, base, current_indicies=[]):
if len(base) == 0:
return []
elif len(base) == 1:
return [tuple(current_indicies + [base[0]])]
else:
r = []
[r.extend(self._recursive_get(sub_base, current_indicies + [i])) for i,sub_base in enumerate(base)]
return r
def get_all_solutions(self):
solutions = self._recursive_get(self._pop)
return solutions
def __len__(self):
return len(self.get_all_solutions())
if __name__=="__main__":
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
from mpl_toolkits.mplot3d import Axes3D
dims = [10,10,10]
def _characterize(solution):
x, y, z = solution
return {"x": x*x/(100.**2), "y": y*y/(100.*100.), "z": z*z/(100.*100.)}
def mutate(solution):
i = random.randint(0, 2)
solution[i] += random.gauss(0, 5)
solution[i] = max(0., min(100., solution[i]))
return solution
def generate():
return [random.random()*100.,random.random()*100.,random.random()*100.]
def _fitness(solution):
s = np.array(solution)
distance = np.linalg.norm(s- np.array([20.,20., 20.]))
return 10./ (1. + distance)
def evaluate(solution):
return _characterize(solution), _fitness(solution)
def batch_evaluator(batch_number, solutions):
return [ ( _fitness(solution), _characterize(solution)) for solution in solutions]
m = MAPElites(dims, generate, mutate, initial_population=1500, batch_evaluator=batch_evaluator)
m.init()
#m.run_batch(500, prefer_border=False)
print "Bare tilfeldige losninger map elites:", len(m.get_all_solutions())
m = MAPElites(dims, generate, mutate, initial_population=1000, batch_evaluator=batch_evaluator)
m.init()
m.run_batch(100, prefer_border=False)
print "Normal map elites:", len(m.get_all_solutions())
# for i in range(dims[0]):
# plt.matshow(np.matrix(m.get_plottable_fitness(i)), fignum=100, vmin=-1., vmax=1., cmap=plt.get_cmap("bwr"))#
#
# plt.show()
m.init()
m.run_batch(100, prefer_border=True)
print "Explorative search:", len(m.get_all_solutions())
# Display a random matrix with a specified figure number and a grayscale
# colormap
# for i in range(dims[0]):
# plt.matshow(np.matrix(m.get_plottable_fitness(i)), fignum=100, vmin=-1., vmax=1., cmap=plt.get_cmap("bwr"))
#
# plt.show()
cmap = plt.get_cmap("Reds")
my_cmap = cmap(np.arange(cmap.N))
# Set alpha
my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
# Create new colormap
my_cmap = ListedColormap(my_cmap)
solutions = m.get_all_solutions()
xs = map(lambda s: s[0], solutions)
ys = map(lambda s: s[1], solutions)
zs = map(lambda s: s[2], solutions)
fs = map(lambda s: evaluate(s[3].o), solutions)
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
#n = 100
#ax.scatter(xs, ys, zs, c=fs, marker='o', s = 70, depthshade=False, vmin=0., vmax=1., cmap=my_cmap)
#ax.set_xlabel('X Label')
#ax.set_ylabel('Y Label')
#ax.set_zlabel('Z Label')
#plt.show()