-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo_factory.py
404 lines (314 loc) · 16.6 KB
/
algo_factory.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
import functools
import jax.numpy as jnp
from qdax.core.containers.mapelites_repertoire import compute_euclidean_centroids
from qdax.core.emitters.mutation_operators import isoline_variation
from qdax.core.emitters.pga_me_emitter import PGAMEEmitter, PGAMEConfig
from qdax.core.emitters.standard_emitters import MixingEmitter
from qdax.utils.metrics import default_qd_metrics, default_moqd_metrics
from aria.algos.abstract_algo import AbstractAlgo
from aria.algos.map_elites_algo import MAPElitesAlgo
from aria.algos.mome_reproducibility import MOMEReproducibilityAlgo
from aria.algos.naive_sampling import NaiveSampling
from aria.aria_es_init import ARIA_ES_Init
from aria.aria_mapelites_init import ARIAMapElitesInit
from aria.reevaluator_score import ReEvaluator
from aria.reproducibility_improvers.fitness_shaping import FitnessShaping
from aria.reproducibility_improvers.improver_linear_comb import \
ReproducibilityImproverQDRLinearCombination
from aria.reproducibility_improvers.improver_standard import ReproducibilityImproverQDR
from aria.tasks.task_factory import TaskInfo
from aria.utils.distances_utils import automatic_cell_inf_norm_radius_calculator
from aria.utils.normaliser import Normaliser, NormaliserMinMax
class FactoryAlgo:
def __init__(self, config, task_info: TaskInfo):
self.config = config
self.config_algo = config.algo
self.config_task = config.task
self.algo_name = self.config_algo.algo_name
self.scoring_fn = task_info.scoring_fn
self.env = task_info.env
self.policy_network = task_info.policy_network
def _automatic_radius_calculator(self) -> float:
radius_acceptance_bd = automatic_cell_inf_norm_radius_calculator(
grid_shape=self.config_task.grid_shape,
min_bd=self.config_task.min_bd,
max_bd=self.config_task.max_bd,
)
return radius_acceptance_bd
def _get_robustness_improver_aria(self):
config_task_aria = self.config_task.aria
learning_rate = config_task_aria.learning_rate
batch_size = config_task_aria.batch_size
radius_acceptance_bd = self._automatic_radius_calculator()
robustness_improver = ReproducibilityImproverQDR(
perturbation_std=config_task_aria.perturbation_std,
population_size=batch_size,
scoring_fn=self.scoring_fn,
fitness_shaping=FitnessShaping.CENTERED_RANK,
radius_acceptance_bd=radius_acceptance_bd,
learning_rate=learning_rate,
center_fitness=False,
)
return robustness_improver
def _get_fitness_normaliser(self) -> Normaliser:
config_task_normaliser = self.config_task.normaliser
return NormaliserMinMax(
min_val=config_task_normaliser.min_fitness,
max_val=config_task_normaliser.max_fitness,
)
def _get_distance_normaliser(self) -> Normaliser:
config_task_normaliser = self.config_task.normaliser
return NormaliserMinMax(
min_val=config_task_normaliser.min_distance,
max_val=config_task_normaliser.max_distance,
)
def _get_robustness_improver_aria_linearcomb(self):
config_task_aria = self.config_task.aria
learning_rate = config_task_aria.learning_rate
batch_size = config_task_aria.batch_size
weight_fitness_obj = self.config_algo.weight_fitness_obj
robustness_improver = ReproducibilityImproverQDRLinearCombination(
perturbation_std=config_task_aria.perturbation_std,
population_size=batch_size,
scoring_fn=self.scoring_fn,
fitness_shaping=FitnessShaping.CENTERED_RANK,
learning_rate=learning_rate,
center_fitness=False,
fitness_normaliser=self._get_fitness_normaliser(),
distance_normaliser=self._get_distance_normaliser(),
weight_fitness_obj=weight_fitness_obj,
)
return robustness_improver
def get_centroids(self):
grid_shape = tuple(self.config_task.grid_shape)
min_bd = self.config_task.min_bd
max_bd = self.config_task.max_bd
centroids = compute_euclidean_centroids(
grid_shape=grid_shape,
minval=min_bd,
maxval=max_bd,
)
return centroids
def _get_reevaluator_aria(self) -> ReEvaluator:
config_task_aria = self.config_task.aria
reevaluator = ReEvaluator(scoring_fn=self.scoring_fn,
num_reevals=config_task_aria.num_reevals_estimate_mean_initial)
return reevaluator
def _get_aria_es_init_algo(self) -> ARIA_ES_Init:
config_task_aria = self.config_task.aria
reevaluator = self._get_reevaluator_aria()
centroids = self.get_centroids()
robustness_improver = self._get_robustness_improver_aria()
aria_es_init = ARIA_ES_Init(robustness_improver,
reevaluator=reevaluator,
centroids=centroids,
number_robust_iterations_initial=config_task_aria.number_robust_iterations_initial,
number_robust_iterations_loop=config_task_aria.number_robust_iterations_loop,
number_parallel_optimisations=config_task_aria.number_parallel_optimisations,
config=self.config,
scoring_fn=self.scoring_fn,
)
return aria_es_init
def _get_aria_mapelites_init_algo(self) -> ARIAMapElitesInit:
config_task_aria = self.config_task.aria
reevaluator = self._get_reevaluator_aria()
centroids = self.get_centroids()
robustness_improver = self._get_robustness_improver_aria()
total_map_elites_evaluations = self.config_algo.total_map_elites_evaluations
aria_mapelites_init_algo = ARIAMapElitesInit(robustness_improver,
reevaluator=reevaluator,
centroids=centroids,
number_robust_iterations_initial=config_task_aria.number_robust_iterations_initial,
number_robust_iterations_loop=config_task_aria.number_robust_iterations_loop,
number_parallel_optimisations=config_task_aria.number_parallel_optimisations,
config=self.config,
scoring_fn=self.scoring_fn,
total_map_elites_evaluations=total_map_elites_evaluations,
emitter=self._get_uniform_emitter(consider_reevals=False),
metrics_fn_map_elites=self._get_default_qd_metrics(),
)
return aria_mapelites_init_algo
def _get_aria_pga_init_algo(self) -> ARIAMapElitesInit:
config_task_aria = self.config_task.aria
reevaluator = self._get_reevaluator_aria()
centroids = self.get_centroids()
robustness_improver = self._get_robustness_improver_aria()
total_map_elites_evaluations = self.config_algo.total_map_elites_evaluations
pga_emitter = self._get_pga_emitter(consider_reevals=False,
override_env_batch_size=self.config_algo.env_batch_size)
aria_mapelites_init_algo = ARIAMapElitesInit(robustness_improver,
reevaluator=reevaluator,
centroids=centroids,
number_robust_iterations_initial=config_task_aria.number_robust_iterations_initial,
number_robust_iterations_loop=config_task_aria.number_robust_iterations_loop,
number_parallel_optimisations=config_task_aria.number_parallel_optimisations,
config=self.config,
scoring_fn=self.scoring_fn,
total_map_elites_evaluations=total_map_elites_evaluations,
emitter=pga_emitter,
metrics_fn_map_elites=self._get_default_qd_metrics(),
)
return aria_mapelites_init_algo
def _get_aria_linearcomb(self) -> ARIA_ES_Init:
config_task_aria = self.config_task.aria
reevaluator = self._get_reevaluator_aria()
centroids = self.get_centroids()
robustness_improver = self._get_robustness_improver_aria_linearcomb()
total_map_elites_evaluations = self.config_algo.total_map_elites_evaluations
aria_map_elites_init = ARIAMapElitesInit(robustness_improver,
reevaluator=reevaluator,
centroids=centroids,
number_robust_iterations_initial=config_task_aria.number_robust_iterations_initial,
number_robust_iterations_loop=config_task_aria.number_robust_iterations_loop,
number_parallel_optimisations=config_task_aria.number_parallel_optimisations,
config=self.config,
scoring_fn=self.scoring_fn,
total_map_elites_evaluations=total_map_elites_evaluations,
emitter=self._get_uniform_emitter(consider_reevals=False),
metrics_fn_map_elites=self._get_default_qd_metrics(),
)
return aria_map_elites_init
def _get_variation_fn(self):
config_variation_fn = self.config_task.variation_fn
iso_sigma = config_variation_fn.iso_sigma
line_sigma = config_variation_fn.line_sigma
# Define emitter
variation_fn = functools.partial(
isoline_variation,
iso_sigma=iso_sigma,
line_sigma=line_sigma
)
return variation_fn
def _get_uniform_emitter(self, consider_reevals=False) -> MixingEmitter:
if not consider_reevals:
batch_size = self.config_task.budget_per_eval
else:
assert self.config_task.budget_per_eval % self.config_task.reeval.evals_per_gen == 0
batch_size = self.config_task.budget_per_eval // self.config_task.reeval.evals_per_gen
variation_fn = self._get_variation_fn()
mixing_emitter = MixingEmitter(
mutation_fn=None,
variation_fn=variation_fn,
variation_percentage=1.0,
batch_size=batch_size,
)
return mixing_emitter
def _get_default_qd_metrics(self):
qd_offset = 1e6 # Analysis is done in preprocessing anyway...
metrics_fn = functools.partial(
default_qd_metrics,
qd_offset=qd_offset,
)
return metrics_fn
def _get_naive_sampling(self) -> NaiveSampling:
metrics_fn = self._get_default_qd_metrics()
emitter = self._get_uniform_emitter(consider_reevals=True)
naive_sampling_algo = NaiveSampling(
config=self.config,
scoring_fn=self.scoring_fn,
centroids=self.get_centroids(),
fitness_normaliser=self._get_fitness_normaliser(),
dist_normaliser=self._get_distance_normaliser(),
emitter=emitter,
metrics_fn=metrics_fn,
)
return naive_sampling_algo
def _get_mome_reproducibility(self) -> MOMEReproducibilityAlgo:
reference_point = jnp.array([1e6, 1e6]) # Analysis is done in preprocessing anyway...
# how to compute metrics from a repertoire
metrics_fn = functools.partial(
default_moqd_metrics,
reference_point=reference_point
)
uniform_emitter = self._get_uniform_emitter(consider_reevals=True)
mome_reproducibility_algo = MOMEReproducibilityAlgo(
config=self.config,
scoring_fn=self.scoring_fn,
centroids=self.get_centroids(),
emitter=uniform_emitter,
metrics_fn=metrics_fn,
)
return mome_reproducibility_algo
def _get_map_elites(self) -> MAPElitesAlgo:
metrics_fn = self._get_default_qd_metrics()
map_elites_algo = MAPElitesAlgo(
config=self.config,
scoring_fn=self.scoring_fn,
centroids=self.get_centroids(),
emitter=self._get_uniform_emitter(),
metrics_fn=metrics_fn,
)
return map_elites_algo
def _get_pga_emitter(self, override_env_batch_size: int = None, consider_reevals=False) -> PGAMEEmitter:
if consider_reevals:
raise NotImplementedError("Reevals not implemented for PGA")
variation_fn = self._get_variation_fn()
if override_env_batch_size is None:
env_batch_size = self.config_task.budget_per_eval
else:
env_batch_size = override_env_batch_size
pga_emitter_config = PGAMEConfig(
env_batch_size=env_batch_size,
batch_size=self.config_algo.transitions_batch_size,
proportion_mutation_ga=self.config_algo.proportion_mutation_ga,
critic_hidden_layer_size=self.config_algo.critic_hidden_layer_size,
critic_learning_rate=self.config_algo.critic_learning_rate,
greedy_learning_rate=self.config_algo.greedy_learning_rate,
policy_learning_rate=self.config_algo.policy_learning_rate,
noise_clip=self.config_algo.noise_clip,
policy_noise=self.config_algo.policy_noise,
discount=self.config_algo.discount,
reward_scaling=self.config_algo.reward_scaling,
replay_buffer_size=self.config_algo.replay_buffer_size,
soft_tau_update=self.config_algo.soft_tau_update,
num_critic_training_steps=self.config_algo.num_critic_training_steps,
num_pg_training_steps=self.config_algo.num_pg_training_steps,
policy_delay=self.config_algo.policy_delay,
)
assert self.env is not None
assert self.policy_network is not None
pga_me_emitter = PGAMEEmitter(
config=pga_emitter_config,
policy_network=self.policy_network,
env=self.env,
variation_fn=variation_fn,
)
return pga_me_emitter
def _get_pga_map_elites(self) -> MAPElitesAlgo:
metrics_fn = self._get_default_qd_metrics()
emitter = self._get_pga_emitter()
map_elites_algo = MAPElitesAlgo(
config=self.config,
scoring_fn=self.scoring_fn,
centroids=self.get_centroids(),
emitter=emitter,
metrics_fn=metrics_fn,
)
return map_elites_algo
def create(self) -> AbstractAlgo:
if self.algo_name == "aria_es_init":
aria_algo = self._get_aria_es_init_algo()
return aria_algo
elif self.algo_name == "aria_mapelites_init":
aria_mapelites_init_algo = self._get_aria_mapelites_init_algo()
return aria_mapelites_init_algo
elif self.algo_name == "aria_pga_init":
aria_pga_init_algo = self._get_aria_pga_init_algo()
return aria_pga_init_algo
elif self.algo_name == "aria_linearcomb":
aria_linearcomb_algo = self._get_aria_linearcomb()
return aria_linearcomb_algo
elif self.algo_name == "naive_sampling":
naive_sampling_algo = self._get_naive_sampling()
return naive_sampling_algo
elif self.algo_name == "mome_reproducibility":
mome_reproducibility_algo = self._get_mome_reproducibility()
return mome_reproducibility_algo
elif self.algo_name == "map_elites":
map_elites_algo = self._get_map_elites()
return map_elites_algo
elif self.algo_name == "pga_me":
pga_map_elites_algo = self._get_pga_map_elites()
return pga_map_elites_algo
else:
raise ValueError(f"Unknown algo_name: {self.algo_name}")