-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_loop.py
executable file
·268 lines (242 loc) · 9.94 KB
/
main_loop.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
import os
import pickle
import time
from typing import Any, Callable, Dict, Tuple
import jax.numpy as jnp
from qdax.core.containers.mapelites_repertoire import MapElitesRepertoire
from qdax.types import RNGKey
def main_loop(
logger: Any,
init_time: float,
centroid_time: float,
behavior_descriptor_length: int,
num_iterations: int,
update_fn: Callable,
repertoire: MapElitesRepertoire,
second_repertoire: MapElitesRepertoire,
emitter_state: Any,
count_evals_fn: Callable,
reevaluation_fn: Callable,
metrics_fn: Callable,
reeval_metrics_fn: Callable,
full_metrics: Dict,
full_reeval_metrics: Dict,
timings: Dict,
additional_metrics_fn: Callable,
log_period: int,
num_reevals: int,
log_period_reevals: int,
store_repertoire: bool,
store_repertoire_log_period: int,
random_key: RNGKey,
) -> Tuple[Dict, Dict, Dict, RNGKey]:
output_dir = "./"
# Setup metrics checkpoint save
_last_metrics_dir = os.path.join(output_dir, "checkpoints", "last_metrics")
os.makedirs(_last_metrics_dir, exist_ok=True)
_grid_img_dir = os.path.join(output_dir, "images", "me_grids")
os.makedirs(_grid_img_dir, exist_ok=True)
_metrics_img_dir = os.path.join(output_dir, "images", "me_metrics")
os.makedirs(_metrics_img_dir, exist_ok=True)
_timings_dir = os.path.join(output_dir, "timings")
os.makedirs(_timings_dir, exist_ok=True)
# Setup repertoire checkpoint save
_last_grid_dir = os.path.join(output_dir, "checkpoints", "last_grid")
os.makedirs(_last_grid_dir, exist_ok=True)
_last_reeval_grid_dir = os.path.join(output_dir, "checkpoints", "last_reeval_grid")
os.makedirs(_last_reeval_grid_dir, exist_ok=True)
_last_fit_reeval_grid_dir = os.path.join(
output_dir, "checkpoints", "last_fit_reeval_grid"
)
os.makedirs(_last_fit_reeval_grid_dir, exist_ok=True)
_last_desc_reeval_grid_dir = os.path.join(
output_dir, "checkpoints", "last_desc_reeval_grid"
)
os.makedirs(_last_desc_reeval_grid_dir, exist_ok=True)
_last_fit_var_grid_dir = os.path.join(
output_dir, "checkpoints", "last_fit_var_grid"
)
os.makedirs(_last_fit_var_grid_dir, exist_ok=True)
_last_reeval_fit_var_grid_dir = os.path.join(
output_dir, "checkpoints", "last_reeval_fit_var_grid"
)
os.makedirs(_last_reeval_fit_var_grid_dir, exist_ok=True)
_last_desc_var_grid_dir = os.path.join(
output_dir, "checkpoints", "last_desc_var_grid"
)
os.makedirs(_last_desc_var_grid_dir, exist_ok=True)
_last_reeval_desc_var_grid_dir = os.path.join(
output_dir, "checkpoints", "last_reeval_desc_var_grid"
)
os.makedirs(_last_reeval_desc_var_grid_dir, exist_ok=True)
# Main QD Loop
total_start_time = time.time()
algorithm_time = 0.0
total_evals = 0
for iteration in range(num_iterations):
logger.warning(
f"--- Iteration indice : {iteration} out of {num_iterations} ---"
)
start_time = time.time()
(
repertoire,
second_repertoire,
emitter_state,
metrics,
random_key,
) = update_fn(
repertoire,
second_repertoire,
emitter_state,
random_key,
)
iteration_time = time.time() - start_time
algorithm_time += iteration_time
logger.warning(f"--- Current QD Score: {metrics['qd_score'][-1]:.2f}")
logger.warning(f"--- Current Coverage: {metrics['coverage'][-1]:.2f}%")
logger.warning(f"--- Current Max Fitness: {metrics['max_fitness'][-1]}")
logger.warning(f"--- Iteration time: {iteration_time}")
# Add epoch and evals
total_evals += count_evals_fn(iteration)
metrics["epoch"] = jnp.array([iteration])
metrics["evals"] = jnp.array([total_evals])
metrics["time"] = jnp.array([algorithm_time])
# Add emitter_state metrics to metrics
metrics = additional_metrics_fn(metrics, repertoire, emitter_state)
# Save metrics
full_metrics = {
key: jnp.concatenate((full_metrics[key], metrics[key]))
for key in full_metrics
}
if iteration % log_period == 0:
with open(
os.path.join(_last_metrics_dir, "metrics.pkl"), "wb"
) as file_to_save:
pickle.dump(full_metrics, file_to_save)
# Compute reeval metrics
if num_reevals > 0 and iteration % log_period_reevals == 0:
(
reeval_repertoire,
fit_reeval_repertoire,
desc_reeval_repertoire,
fit_var_repertoire,
reeval_fit_var_repertoire,
desc_var_repertoire,
reeval_desc_var_repertoire,
random_key,
) = reevaluation_fn(
repertoire=repertoire,
random_key=random_key,
)
reeval_metrics = reeval_metrics_fn(
reeval_repertoire,
fit_reeval_repertoire,
desc_reeval_repertoire,
fit_var_repertoire,
reeval_fit_var_repertoire,
desc_var_repertoire,
reeval_desc_var_repertoire,
)
logger.warning(
f"--- Current Reeval QD Score: "
+ f"{reeval_metrics['reeval_qd_score'][-1]:.2f}"
)
logger.warning(
f"--- Current Reeval Coverage: "
+ f"{reeval_metrics['reeval_coverage'][-1]:.2f}%"
)
logger.warning(
f"--- Current Reeval Max Fitness: "
+ f"{reeval_metrics['reeval_max_fitness'][-1]}"
)
# Add epoch and evals
reeval_metrics["epoch"] = jnp.array([iteration])
reeval_metrics["evals"] = jnp.array([total_evals])
# Save reeval metrics
full_reeval_metrics = {
key: jnp.concatenate((full_reeval_metrics[key], reeval_metrics[key]))
for key in full_reeval_metrics
}
with open(
os.path.join(_last_metrics_dir, "reeval_metrics.pkl"), "wb"
) as file_to_save:
pickle.dump(full_reeval_metrics, file_to_save)
# Store the latest controllers of the reeval repertoires
if store_repertoire and iteration % store_repertoire_log_period == 0:
reeval_repertoire.save(path=_last_reeval_grid_dir + "/")
fit_reeval_repertoire.save(path=_last_fit_reeval_grid_dir + "/")
desc_reeval_repertoire.save(path=_last_desc_reeval_grid_dir + "/")
fit_var_repertoire.save(path=_last_fit_var_grid_dir + "/")
desc_var_repertoire.save(path=_last_desc_var_grid_dir + "/")
reeval_fit_var_repertoire.save(path=_last_reeval_fit_var_grid_dir + "/")
reeval_desc_var_repertoire.save(
path=_last_reeval_desc_var_grid_dir + "/"
)
# Store the latest controllers of the repertoire
if store_repertoire and iteration % store_repertoire_log_period == 0:
repertoire.save(path=_last_grid_dir + "/")
total_time = time.time() - total_start_time
logger.warning("--- Final metrics ---")
logger.warning(f"Total time: {total_time:.2f}s")
logger.warning(f"Algorithm time: {algorithm_time:.2f}s")
logger.warning(f"QD Score: {metrics['qd_score'][-1]:.2f}")
logger.warning(f"Coverage: {metrics['coverage'][-1]:.2f}%")
# Save final metrics
with open(os.path.join(_last_metrics_dir, "metrics.pkl"), "wb") as file_to_save:
pickle.dump(full_metrics, file_to_save)
# Save final repertoire
repertoire.save(path=_last_grid_dir + "/")
# Reeval final repertoire
if num_reevals > 0:
(
reeval_repertoire,
fit_reeval_repertoire,
desc_reeval_repertoire,
fit_var_repertoire,
reeval_fit_var_repertoire,
desc_var_repertoire,
reeval_desc_var_repertoire,
random_key,
) = reevaluation_fn(
repertoire=repertoire,
random_key=random_key,
)
reeval_metrics = reeval_metrics_fn(
reeval_repertoire,
fit_reeval_repertoire,
desc_reeval_repertoire,
fit_var_repertoire,
reeval_fit_var_repertoire,
desc_var_repertoire,
reeval_desc_var_repertoire,
)
logger.warning(
f"--- Reeval QD Score: {reeval_metrics['reeval_qd_score'][-1]:.2f}"
)
logger.warning(
f"--- Reeval Coverage: {reeval_metrics['reeval_coverage'][-1]:.2f}%"
)
logger.warning(
f"--- Reeval Max Fitness: {reeval_metrics['reeval_max_fitness'][-1]}"
)
# Add epoch and evals
reeval_metrics["epoch"] = jnp.array([iteration])
reeval_metrics["evals"] = jnp.array([total_evals])
# Save reeval metrics
full_reeval_metrics = {
key: jnp.concatenate((full_reeval_metrics[key], reeval_metrics[key]))
for key in full_reeval_metrics
}
with open(
os.path.join(_last_metrics_dir, "reeval_metrics.pkl"), "wb"
) as file_to_save:
pickle.dump(full_reeval_metrics, file_to_save)
# Store the latest controllers of the reeval repertoires
reeval_repertoire.save(path=_last_reeval_grid_dir + "/")
fit_reeval_repertoire.save(path=_last_fit_reeval_grid_dir + "/")
desc_reeval_repertoire.save(path=_last_desc_reeval_grid_dir + "/")
fit_var_repertoire.save(path=_last_fit_var_grid_dir + "/")
desc_var_repertoire.save(path=_last_desc_var_grid_dir + "/")
reeval_fit_var_repertoire.save(path=_last_reeval_fit_var_grid_dir + "/")
reeval_desc_var_repertoire.save(path=_last_reeval_desc_var_grid_dir + "/")
return full_metrics, full_reeval_metrics, algorithm_time, random_key