-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment_template.py
315 lines (259 loc) · 11.5 KB
/
experiment_template.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
import warnings
# Remove warning from cocoex
warnings.filterwarnings('ignore', message='"is" with a literal. Did you mean "=="?')
from tqdm import tqdm
from datetime import datetime
import os
import time
import numpy as np
import cocoex
import platform
import cma
from brutelogger import BruteLogger
import ray
from dibb import DiBB
import traceback
"""
To run different experiments, make a copy of this file and adapt the configuration parameters.
You can either run one experiment per file or multiple experiments if enough machines are
available. The Experiment class below is meant to run only for one specific dimension and number
of blocks, so if you want to run problems of multiple dimensions at the same time, add them to the
list exp_config[experiments].
"""
class Experiment:
def __init__(self, name, dimension, nblocks, head_node_ip, progress_bar_color='#FFFFFF'):
self.name = name
self.dimension = dimension
self.head_node_ip = head_node_ip
self.progress_bar_color = progress_bar_color
self.nblocks = nblocks
exp_config = {
# Where to store the logs
'log_dir': '/home/glf/logs',
# If true, notify will send notifications on experiment start and end
'use_notify': False,
'use_cma_ipop': True,
'use_dibb_ipop': False,
# List of experiments (one dimension per experiment)
'experiments': [
Experiment(
name='ls_40d_2b',
dimension=40,
head_node_ip='134.21.220.201',
nblocks=2,
)
],
# Function that generates a new initial individual given a coco problem
'init_ind_generator': lambda problem: np.random.uniform(-4, 4, problem.dimension)
}
coco_config = {
# Which suite to use in the experiment
'suite': 'bbob-largescale',
# Evaluation budget = budget_multiplier * dimension
'budget_multiplier': 1e4,
# Either 'error', 'warning', 'info' or 'debug'
'log_level': 'warning',
# Which problem indices should be loaded
'problem_indices': [*range(0, 75), *range(135, 210)], # Separable (f1-f5) and ill-conditioned (f10-f14) problems
}
dibb_config = {
'verbose': False,
'optimizer': 'cma-ipop' if exp_config['use_dibb_ipop'] else'cma',
'optimizer_options': {
'init_sigma': 2,
'verbose': -9,
},
'nfitness_evaluators': 0,
'print_fn': None,
}
ray_config = {
'configure_logging': False,
'logging_level': 50,
'address': 'auto',
}
script_name = os.path.basename(__file__).strip('.py')
fname = f'log_{script_name}_{datetime.now().strftime("%y%m%d_%H%M")}.txt'
BruteLogger.save_stdout_to_file(path=exp_config['log_dir'], fname=fname, encoding='utf8', also_stderr=True)
assert not (exp_config['use_cma_ipop'] and exp_config['use_dibb_ipop']), 'It is not recommended to use both CMA-IPOP and DIBB-IPOP at the same time'
if exp_config['use_notify']:
from notify_run import Notify
notify = Notify()
ray.init(**ray_config)
def format_time(seconds):
hh = int(seconds // 3600)
mm = int(seconds % 3600 // 60)
ss = int(seconds % 3600 % 60)
return f'{hh:02}h {mm:02}m {ss:02}s'
def RemCocoWrapper(rem_coco):
"""Filters the relevant problems and uses tqdm to track progress.."""
return tqdm(rem_coco.problems,
leave=None,
desc=f'<exp: {rem_coco.experiment_name}> Evaluating problems ({rem_coco.dim}D) on {rem_coco.host}',
unit='problem',
colour=rem_coco.bar_color)
class FitnessWrapper:
"""Wrapper to make a coco problem work with DiBB FitnessEvaluators."""
def __init__(self, problem_id, observer_opts, dibb_id, run_number=1):
self.problem_id = problem_id
self.observer_opts = observer_opts
self.dibb_id = dibb_id
self.observer = None
self.problem = None
self.run_number = run_number
cocoex.log_level(coco_config['log_level'])
def _init_problem(self):
hostname = platform.node()
opts = self.observer_opts.format(prob=self.problem_id,
host=hostname,
block_id=self.block_id,
run_number=self.run_number)
self.observer = cocoex.Observer(coco_config['suite'], opts)
self.problem = cocoex.Suite(coco_config['suite'], '', '').get_problem(self.problem_id, self.observer)
def target_hit(self):
if self.problem is None:
return False
return self.problem.final_target_hit
def __call__(self, ind):
if not self.problem:
self._init_problem()
return self.problem(ind)
def __reduce__(self):
"""
If problem is initialized, serialization will break the current observation
and initialize the problem and observer again from scratch. Don't fetch
the FitnessWrapper from the remote machine unless you are finished and you
want to know the number of evaluations.
"""
if self.problem:
self.problem.free()
return self.__class__, (self.problem_id, self.observer_opts, self.dibb_id, self.run_number)
def __del__(self):
if self.problem:
self.problem.free()
# DiBB Hooks
def add_block_id(block_worker):
if not hasattr(block_worker.fit_fn, 'block_id'):
block_worker.fit_fn.block_id = block_worker.block_id
def check_target(block_worker):
if block_worker.fit_fn.target_hit():
block_worker.comm.set.remote('target_hit', val=True)
block_worker.comm.set.remote('terminate', val=True)
@ray.remote
class RemCoco:
"""Ray actor for COCO"""
def __init__(self, dibb_conf_init, dim, budget_multiplier, experiment_name, observer_opts, problem_indices=range(360), bar_color='#FFFFFF'):
self.dibb = DiBB(**dibb_conf_init)
self.dim = dim
self.default_popsize = cma.CMAEvolutionStrategy([1] * self.dim, 1, {'verbose': -9}).popsize
self.eval_budget = dim * budget_multiplier + 1
self.evals_left = self.eval_budget
self.experiment_name = experiment_name
self.observer_opts = observer_opts
self.bar_color = bar_color
self.suite = cocoex.Suite(coco_config['suite'], '', f'dimensions: {self.dim}')
self.problems = [self.suite[i] for i in problem_indices]
self.host = platform.node()
self.reset_config = {
'optimizer_options': {
'init_sigma': 2,
'popsize': self.default_popsize,
'verbose': -9,
},
'hooks': {
'BlockWorker': [add_block_id, check_target]
},
}
print('Initialized RemCoco instance on machine', self.host)
def start(self):
try:
# Preparing run
parent_dir = f'exdata/{self.experiment_name}'
os.makedirs(parent_dir, exist_ok=True)
print(f'Starting experiment {self.experiment_name}!')
t0 = time.time()
self.evals_left = self.eval_budget
for i, problem in enumerate(RemCocoWrapper(self)):
self.evals_left = self.eval_budget
run_number = 0
irestart = -1
while self.evals_left > 0:
run_number += 1
irestart += 1
self.reset_config['fit_fn'] = FitnessWrapper(problem.id, self.observer_opts, self.dibb.dibb_id, run_number=run_number)
self.reset_config['init_ind'] = exp_config['init_ind_generator'](problem)
if exp_config['use_cma_ipop']:
# Will keep going at maximum popsize after 9 restarts in order to reach evaluation budget
popsize = 2 ** min(irestart, 9) * self.default_popsize
self.reset_config['optimizer_options']['popsize'] = popsize
self.dibb.reset(**self.reset_config)
self.reset_target_hit() # Add flag to communication dict again
self.dibb.optimize(nevals=self.evals_left)
self.evals_left -= sum(self.dibb.comm_dict['nevals'])
if self.target_hit():
break
# Statistics and logging
if self.evals_left < self.reset_config['optimizer_options']['popsize']:
reason = 'Evaluation budget reached'
else:
assert self.target_hit(), 'Exited loop without proper termination condition!'
reason = 'Target hit'
print(f'<exp: {self.experiment_name}> Finished evaluating {problem.id}',
f'<exp: {self.experiment_name}> Reason for termination: {reason}', sep='\n')
nevals = self.dibb.comm_dict['nevals']
print(f'> Eval stats for experiment {self.experiment_name}:', problem.id)
print('>> evals done:', nevals, f'(Total: {sum(nevals)})')
print('>> evals left:', self.evals_left)
print('>> eval_budget:', self.eval_budget)
print('>> nblocks:', self.dibb.nblocks)
print('>> popsize:', self.dibb.optimizer_options['popsize'])
print('>> ntrials_per_ind:', self.dibb.ntrials_per_ind)
# Reset one last time to make observer write the last data
self.dibb.reset(**self.reset_config)
seconds_elapsed = time.time() - t0
termination_msg = f'Experiment {self.experiment_name} finished after {format_time(seconds_elapsed)} ({int(seconds_elapsed)} seconds total)!'
print(termination_msg)
if exp_config['use_notify']:
notify.send(termination_msg)
except:
print(traceback.format_exc())
def target_hit(self):
return ray.get(self.dibb.workers_comm.get.remote('target_hit'))
def reset_target_hit(self):
self.dibb.workers_comm.set.remote('target_hit', val=False)
# Format: {experiment_name: ObjRef}
experiments = {}
for i, exp in enumerate(exp_config['experiments']):
exdata_folder = '_'.join(script_name.split('_')[:-1])
observer_opts = 'algorithm_info: Distributed CMA-ES using DiBB '
observer_opts += 'algorithm_name: CMA-ES '
output_folder = f'{exdata_folder}/' + exp.name + '/{prob}_{host}_b{block_id:02}_r{run_number:02} '
observer_opts += 'result_folder: ' + output_folder
dibb_conf_init = {
'nblocks': exp.nblocks,
'ndims': exp.dimension,
**dibb_config
}
experiments[exp.name] = RemCoco.options(resources={f'node:{exp.head_node_ip}': 1}).remote(
dibb_conf_init=dibb_conf_init,
dim=exp.dimension,
budget_multiplier=coco_config['budget_multiplier'],
experiment_name=exp.name,
observer_opts=observer_opts,
bar_color=exp.progress_bar_color,
problem_indices=coco_config['problem_indices']
)
experiments_obj_refs = {}
for experiment_name in experiments:
if exp_config['use_notify']:
notify.send(f'Starting experiment {experiment_name}!')
experiments_obj_refs[experiment_name] = experiments[experiment_name].start.remote()
# Wait for all experiments, free up resources once an experiment terminates
while len(experiments_obj_refs) > 0:
(ref, *_), _ = ray.wait(list(experiments_obj_refs.values()), num_returns=1)
experiment_name = None
for key, value in experiments_obj_refs.items():
if value == ref:
experiment_name = key
del experiments_obj_refs[experiment_name]
del experiments[experiment_name]
print(f'All experiments finished!')