-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcommon.py
225 lines (196 loc) · 7.13 KB
/
common.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
#! /usr/bin/env python
#| This file is a part of the pymap_elites framework.
#| Copyright 2019, INRIA
#| Main contributor(s):
#| Jean-Baptiste Mouret, [email protected]
#| Eloise Dalin , [email protected]
#| Pierre Desreumaux , [email protected]
#|
#|
#| **Main paper**: Mouret JB, Clune J. Illuminating search spaces by
#| mapping elites. arXiv preprint arXiv:1504.04909. 2015 Apr 20.
#|
#| This software is governed by the CeCILL license under French law
#| and abiding by the rules of distribution of free software. You
#| can use, modify and/ or redistribute the software under the terms
#| of the CeCILL license as circulated by CEA, CNRS and INRIA at the
#| following URL "http://www.cecill.info".
#|
#| As a counterpart to the access to the source code and rights to
#| copy, modify and redistribute granted by the license, users are
#| provided only with a limited warranty and the software's author,
#| the holder of the economic rights, and the successive licensors
#| have only limited liability.
#|
#| In this respect, the user's attention is drawn to the risks
#| associated with loading, using, modifying and/or developing or
#| reproducing the software by the user in light of its specific
#| status of free software, that may mean that it is complicated to
#| manipulate, and that also therefore means that it is reserved for
#| developers and experienced professionals having in-depth computer
#| knowledge. Users are therefore encouraged to load and test the
#| software's suitability as regards their requirements in conditions
#| enabling the security of their systems and/or data to be ensured
#| and, more generally, to use and operate it in the same conditions
#| as regards security.
#|
#| The fact that you are presently reading this means that you have
#| had knowledge of the CeCILL license and that you accept its terms.
#
import math
import numpy as np
import multiprocessing
from pathlib import Path
import sys
import random
from collections import defaultdict
from sklearn.cluster import KMeans
default_params = \
{
# more of this -> higher-quality CVT
"cvt_samples": 25000,
# we evaluate in batches to paralleliez
"batch_size": 100,
# proportion of niches to be filled before starting
"random_init": 0.1,
# batch for random initialization
"random_init_batch": 100,
# when to write results (one generation = one batch)
"dump_period": 10000,
# do we use several cores?
"parallel": True,
# do we cache the result of CVT and reuse?
"cvt_use_cache": True,
# min/max of parameters
"min": 0,
"max": 1,
# only useful if you use the 'iso_dd' variation operator
"iso_sigma": 0.01,
"line_sigma": 0.2
}
class Species:
def __init__(self, x, desc, fitness, centroid=None):
self.x = x
self.desc = desc
self.fitness = fitness
self.centroid = centroid
def polynomial_mutation(x):
'''
Cf Deb 2001, p 124 ; param: eta_m
'''
y = x.copy()
eta_m = 5.0;
r = np.random.random(size=len(x))
for i in range(0, len(x)):
if r[i] < 0.5:
delta_i = math.pow(2.0 * r[i], 1.0 / (eta_m + 1.0)) - 1.0
else:
delta_i = 1 - math.pow(2.0 * (1.0 - r[i]), 1.0 / (eta_m + 1.0))
y[i] += delta_i
return y
def sbx(x, y, params):
'''
SBX (cf Deb 2001, p 113) Simulated Binary Crossover
A large value ef eta gives a higher probablitity for
creating a `near-parent' solutions and a small value allows
distant solutions to be selected as offspring.
'''
eta = 10.0
xl = params['min']
xu = params['max']
z = x.copy()
r1 = np.random.random(size=len(x))
r2 = np.random.random(size=len(x))
for i in range(0, len(x)):
if abs(x[i] - y[i]) > 1e-15:
x1 = min(x[i], y[i])
x2 = max(x[i], y[i])
beta = 1.0 + (2.0 * (x1 - xl) / (x2 - x1))
alpha = 2.0 - beta ** -(eta + 1)
rand = r1[i]
if rand <= 1.0 / alpha:
beta_q = (rand * alpha) ** (1.0 / (eta + 1))
else:
beta_q = (1.0 / (2.0 - rand * alpha)) ** (1.0 / (eta + 1))
c1 = 0.5 * (x1 + x2 - beta_q * (x2 - x1))
beta = 1.0 + (2.0 * (xu - x2) / (x2 - x1))
alpha = 2.0 - beta ** -(eta + 1)
if rand <= 1.0 / alpha:
beta_q = (rand * alpha) ** (1.0 / (eta + 1))
else:
beta_q = (1.0 / (2.0 - rand * alpha)) ** (1.0 / (eta + 1))
c2 = 0.5 * (x1 + x2 + beta_q * (x2 - x1))
c1 = min(max(c1, xl), xu)
c2 = min(max(c2, xl), xu)
if r2[i] <= 0.5:
z[i] = c2
else:
z[i] = c1
return z
def iso_dd(x, y, params):
'''
Iso+Line
Ref:
Vassiliades V, Mouret JB. Discovering the elite hypervolume by leveraging interspecies correlation.
GECCO 2018
'''
assert(x.shape == y.shape)
p_max = np.array(params["max"])
p_min = np.array(params["min"])
a = np.random.normal(0, params['iso_sigma'], size=len(x))
b = np.random.normal(0, params['line_sigma'])
norm = np.linalg.norm(x - y)
z = x.copy() + a + b * (x - y)
return np.clip(z, p_min, p_max)
def variation(x, z, params):
assert(x.shape == z.shape)
y = sbx(x, z, params)
return y
def __centroids_filename(k, dim):
return 'centroids_' + str(k) + '_' + str(dim) + '.dat'
def __write_centroids(centroids):
k = centroids.shape[0]
dim = centroids.shape[1]
filename = __centroids_filename(k, dim)
with open(filename, 'w') as f:
for p in centroids:
for item in p:
f.write(str(item) + ' ')
f.write('\n')
def cvt(k, dim, samples, cvt_use_cache=True):
# check if we have cached values
fname = __centroids_filename(k, dim)
if cvt_use_cache:
if Path(fname).is_file():
print("WARNING: using cached CVT:", fname)
return np.loadtxt(fname)
# otherwise, compute cvt
print("Computing CVT (this can take a while...):", fname)
x = np.random.rand(samples, dim)
k_means = KMeans(init='k-means++', n_clusters=k,
n_init=1, n_jobs=-1, verbose=1)#,algorithm="full")
k_means.fit(x)
__write_centroids(k_means.cluster_centers_)
return k_means.cluster_centers_
def make_hashable(array):
return tuple(map(float, array))
def parallel_eval(evaluate_function, to_evaluate, pool, params):
if params['parallel'] == True:
s_list = pool.map(evaluate_function, to_evaluate)
else:
s_list = map(evaluate_function, to_evaluate)
return list(s_list)
# format: fitness, centroid, desc, genome \n
# fitness, centroid, desc and x are vectors
def __save_archive(archive, gen):
def write_array(a, f):
for i in a:
f.write(str(i) + ' ')
filename = 'archive_' + str(gen) + '.dat'
with open(filename, 'w') as f:
for k in archive.values():
f.write(str(k.fitness) + ' ')
write_array(k.centroid, f)
write_array(k.desc, f)
write_array(k.x, f)
f.write("\n")