forked from numbbo/coco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_experiment.py
executable file
·304 lines (271 loc) · 12.5 KB
/
example_experiment.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
#!/usr/bin/env python
"""Use case for the COCO experimentation module `cocoex` which can be used as
template.
Usage from a system shell::
python example_experiment.py 3 1 20
runs the first of 20 batches with maximal budget
of 3 * dimension f-evaluations.
Usage from a python shell::
>>> import example_experiment as ee
>>> ee.main(3, 1, 1) # doctest: +ELLIPSIS
Benchmarking solver...
does the same but runs the "first" of one single batch.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
try: range = xrange
except NameError: pass
import os, sys
import time
import numpy as np # "pip install numpy" installs numpy
import cocoex
from cocoex import Suite, Observer, log_level
verbose = 1
try: import cma # cma.fmin is a solver option, "pip install cma" installs cma
except: pass
try: from scipy.optimize import fmin_slsqp # "pip install scipy" installs scipy
except: pass
try: range = xrange # let range always be an iterator
except NameError: pass
def print_flush(*args):
"""print without newline and flush"""
print(*args, end="")
sys.stdout.flush()
def ascetime(sec):
"""return elapsed time as str.
Example: return `"0h33:21"` if `sec == 33*60 + 21`.
"""
h = sec / 60**2
m = 60 * (h - h // 1)
s = 60 * (m - m // 1)
return "%dh%02d:%02d" % (h, m, s)
class ShortInfo(object):
"""print minimal info during benchmarking.
After initialization, to be called right before the solver is called with
the respective problem. Prints nothing if only the instance id changed.
Example output:
Jan20 18h27:56, d=2, running: f01f02f03f04f05f06f07f08f09f10f11f12f13f14f15f16f17f18f19f20f21f22f23f24f25f26f27f28f29f30f31f32f33f34f35f36f37f38f39f40f41f42f43f44f45f46f47f48f49f50f51f52f53f54f55 done
Jan20 18h27:56, d=3, running: f01f02f03f04f05f06f07f08f09f10f11f12f13f14f15f16f17f18f19f20f21f22f23f24f25f26f27f28f29f30f31f32f33f34f35f36f37f38f39f40f41f42f43f44f45f46f47f48f49f50f51f52f53f54f55 done
Jan20 18h27:57, d=5, running: f01f02f03f04f05f06f07f08f09f10f11f12f13f14f15f16f17f18f19f20f21f22f23f24f25f26f27f28f29f30f31f32f33f34f35f36f37f38f39f40f41f42f43f44f45f46f47f48f49f50f51f52f53f54f55 done
"""
def __init__(self):
self.f_current = None # function id (not problem id)
self.d_current = 0 # dimension
self.t0_dimension = time.time()
self.evals_dimension = 0
self.runs_function = 0
def print(self, problem, end="", **kwargs):
print(self(problem), end=end, **kwargs)
sys.stdout.flush()
def add_evals(self, evals, runs):
self.evals_dimension += evals
self.runs_function += runs
def dimension_done(self):
s = '\n done in %.1e seconds/evaluation' % ((time.time() - self.t0_dimension) / self.evals_dimension)
# print(self.evals_dimension)
self.t0_dimension = time.time()
self.evals_dimension = 0
return s
def function_done(self):
s = "(%d)" % self.runs_function + (2 - int(np.log10(self.runs_function))) * ' '
self.runs_function = 0
return s
def __call__(self, problem):
"""uses `problem.id` and `problem.dimension` to decide what to print.
"""
f = "f" + problem.id.lower().split('_f')[1].split('_')[0]
res = ""
if self.f_current and f != self.f_current:
res += self.function_done() + ' '
if problem.dimension != self.d_current:
res += '%s%s, d=%d, running: ' % (self.dimension_done() + "\n\n" if self.d_current else '',
ShortInfo.short_time_stap(), problem.dimension)
self.d_current = problem.dimension
if f != self.f_current:
res += '%s' % f
self.f_current = f
# print_flush(res)
return res
@staticmethod
def short_time_stap():
l = time.asctime().split()
d = l[0]
d = l[1] + l[2]
h, m, s = l[3].split(':')
return d + ' ' + h + 'h' + m + ':' + s
# ===============================================
# prepare (the most basic example solver)
# ===============================================
def random_search(fun, lbounds, ubounds, budget):
"""Efficient implementation of uniform random search between `lbounds` and `ubounds`."""
lbounds, ubounds = np.array(lbounds), np.array(ubounds)
dim, x_min, f_min = len(lbounds), (lbounds + ubounds) / 2, None
max_chunk_size = 1 + 4e4 / dim
while budget > 0:
chunk = int(min([budget, max_chunk_size]))
# about five times faster than "for k in range(budget):..."
X = lbounds + (ubounds - lbounds) * np.random.rand(chunk, dim)
F = [fun(x) for x in X]
if fun.number_of_objectives == 1:
index = np.argmin(F)
if f_min is None or F[index] < f_min:
x_min, f_min = X[index], F[index]
budget -= chunk
return x_min
# ===============================================
# loops over a benchmark problem suite
# ===============================================
def batch_loop(solver, suite, observer, budget,
max_runs, current_batch, number_of_batches):
"""loop over all problems in `suite` calling
`coco_optimize(solver, problem, budget * problem.dimension, max_runs)`
for each eligible problem.
A problem is eligible if
`problem_index + current_batch - 1` modulo `number_of_batches`
equals to zero.
"""
addressed_problems = []
short_info = ShortInfo()
for problem_index, problem in enumerate(suite):
if (problem_index + current_batch - 1) % number_of_batches:
continue
observer.observe(problem)
short_info.print(problem) if verbose else None
runs = coco_optimize(solver, problem, budget * problem.dimension, max_runs)
if verbose:
print_flush("!" if runs > 2 else ":" if runs > 1 else ".")
short_info.add_evals(problem.evaluations, runs)
problem.free()
addressed_problems += [problem.id]
print(short_info.function_done() + short_info.dimension_done())
print(" %s done (%d of %d problems benchmarked%s)" %
(suite_name, len(addressed_problems), len(suite),
((" in batch %d of %d" % (current_batch, number_of_batches))
if number_of_batches > 1 else "")), end="")
if number_of_batches > 1:
print("\n MAKE SURE TO RUN ALL BATCHES", end="")
return addressed_problems
#===============================================
# interface: ADD AN OPTIMIZER BELOW
#===============================================
def coco_optimize(solver, fun, max_evals, max_runs=1e9):
"""`fun` is a callable, to be optimized by `solver`.
The `solver` is called repeatedly with different initial solutions
until either the `max_evals` are exhausted or `max_run` solver calls
have been made or the `solver` has not called `fun` even once
in the last run.
Return number of (almost) independent runs.
"""
range_ = fun.upper_bounds - fun.lower_bounds
center = fun.lower_bounds + range_ / 2
if fun.evaluations:
print('WARNING: %d evaluations were done before the first solver call' %
fun.evaluations)
for restarts in range(int(max_runs)):
remaining_evals = max_evals - fun.evaluations
x0 = center + (restarts > 0) * 0.8 * range_ * (
np.random.rand(fun.dimension) - 0.5)
fun(x0) # can be incommented, if this is done by the solver
if solver.__name__ in ("random_search", ):
solver(fun, fun.lower_bounds, fun.upper_bounds,
remaining_evals)
elif solver.__name__ == 'fmin' and solver.__globals__['__name__'] == 'cma':
if x0[0] == center[0]:
sigma0 = 0.02
restarts_ = 0
else:
x0 = "%f + %f * np.random.rand(%d)" % (
center[0], 0.8 * range_[0], fun.dimension)
sigma0 = 0.2
restarts_ = 6 * (observer_options.find('IPOP') >= 0)
solver(fun, x0, sigma0 * range_[0], restarts=restarts_,
options=dict(scaling=range_/range_[0], maxfevals=remaining_evals,
termination_callback=lambda es: fun.final_target_hit,
verb_log=0, verb_disp=0, verbose=-9))
elif solver.__name__ == 'fmin_slsqp':
solver(fun, x0, iter=1 + remaining_evals / fun.dimension,
iprint=-1)
############################ ADD HERE ########################################
# ### IMPLEMENT HERE THE CALL TO ANOTHER SOLVER/OPTIMIZER ###
# elif True:
# CALL MY SOLVER, interfaces vary
##############################################################################
else:
raise ValueError("no entry for solver %s" % str(solver.__name__))
if fun.evaluations >= max_evals or fun.final_target_hit:
break
# quit if fun.evaluations did not increase
if fun.evaluations <= max_evals - remaining_evals:
if max_evals - fun.evaluations > fun.dimension + 1:
print("WARNING: %d evaluations remaining" %
remaining_evals)
if fun.evaluations < max_evals - remaining_evals:
raise RuntimeError("function evaluations decreased")
break
return restarts + 1
# ===============================================
# set up: CHANGE HERE SOLVER AND FURTHER SETTINGS AS DESIRED
# ===============================================
######################### CHANGE HERE ########################################
# CAVEAT: this might be modified from input args
budget = 2 # maxfevals = budget x dimension ### INCREASE budget WHEN THE DATA CHAIN IS STABLE ###
max_runs = 1e9 # number of (almost) independent trials per problem instance
number_of_batches = 1 # allows to run everything in several batches
current_batch = 1 # 1..number_of_batches
##############################################################################
SOLVER = random_search
#SOLVER = my_solver # fmin_slsqp # SOLVER = cma.fmin
suite_name = "bbob-biobj"
#suite_name = "bbob"
suite_instance = "year:2016"
suite_options = "" # "dimensions: 2,3,5,10,20 " # if 40 is not desired
observer_name = suite_name
observer_options = (
' result_folder: %s_on_%s_budget%04dxD '
% (SOLVER.__name__, suite_name, budget) +
' algorithm_name: %s ' % SOLVER.__name__ +
' algorithm_info: "A SIMPLE RANDOM SEARCH ALGORITHM" ') # CHANGE THIS
######################### END CHANGE HERE ####################################
# ===============================================
# run (main)
# ===============================================
def main(budget=budget,
max_runs=max_runs,
current_batch=current_batch,
number_of_batches=number_of_batches):
"""Initialize suite and observer, then benchmark solver by calling
`batch_loop(SOLVER, suite, observer, budget,...`.
"""
observer = Observer(observer_name, observer_options)
suite = Suite(suite_name, suite_instance, suite_options)
print("Benchmarking solver '%s' with budget=%d*dimension on %s suite, %s"
% (' '.join(str(SOLVER).split()[:2]), budget,
suite.name, time.asctime()))
if number_of_batches > 1:
print('Batch usecase, make sure you run *all* %d batches.\n' %
number_of_batches)
t0 = time.clock()
batch_loop(SOLVER, suite, observer, budget, max_runs,
current_batch, number_of_batches)
print(", %s (%s)." % (time.asctime(), ascetime(time.clock() - t0)))
# ===============================================
if __name__ == '__main__':
"""read input parameters and call `main()`"""
if len(sys.argv) > 1:
if sys.argv[1] in ["--help", "-h"]:
print(__doc__)
exit(0)
budget = float(sys.argv[1])
if observer_options.find('budget') > 0: # reflect budget in folder name
idx = observer_options.find('budget')
observer_options = observer_options[:idx+6] + \
"%04d" % int(budget + 0.5) + observer_options[idx+10:]
if len(sys.argv) > 2:
current_batch = int(sys.argv[2])
if len(sys.argv) > 3:
number_of_batches = int(sys.argv[3])
if len(sys.argv) > 4:
messages = ['Argument "%s" disregarded (only 3 arguments are recognized).' % sys.argv[i]
for i in range(4, len(sys.argv))]
messages.append('See "python example_experiment.py -h" for help.')
raise ValueError('\n'.join(messages))
main(budget, max_runs, current_batch, number_of_batches)