forked from numbbo/coco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_experiment_for_beginners.py
executable file
·49 lines (40 loc) · 2.06 KB
/
example_experiment_for_beginners.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
#!/usr/bin/env python
"""A short and simple example experiment with restarts.
The code is fully functional but mainly emphasises on readability.
Hence produces only rudimentary progress messages and does not provide
batch distribution or timing prints, as `example_experiment2.py` does.
To apply the code to a different solver, `fmin` must be re-assigned or
re-defined accordingly. For example, using `cma.fmin` instead of
`scipy.optimize.fmin` can be done like::
>>> import cma # doctest:+SKIP
>>> def fmin(fun, x0):
... return cma.fmin(fun, x0, 2, {'verbose':-9})
"""
from __future__ import division, print_function
import cocoex, cocopp # experimentation and post-processing modules
import scipy.optimize # to define the solver to be benchmarked
from numpy.random import rand # for randomised restarts
import os, webbrowser # to show post-processed results in the browser
### input
suite_name = "bbob"
output_folder = "scipy-optimize-fmin"
fmin = scipy.optimize.fmin
budget_multiplier = 1 # increase to 10, 100, ...
### prepare
suite = cocoex.Suite(suite_name, "", "")
observer = cocoex.Observer(suite_name, "result_folder: " + output_folder)
minimal_print = cocoex.utilities.MiniPrint()
### go
for problem in suite: # this loop will take several minutes or longer
problem.observe_with(observer) # generates the data for cocopp post-processing
x0 = problem.initial_solution
# apply restarts while neither the problem is solved nor the budget is exhausted
while (problem.evaluations < problem.dimension * budget_multiplier
and not problem.final_target_hit):
fmin(problem, x0, disp=False) # here we assume that `fmin` evaluates the final/returned solution
x0 = problem.lower_bounds + ((rand(problem.dimension) + rand(problem.dimension)) *
(problem.upper_bounds - problem.lower_bounds) / 2)
minimal_print(problem, final=problem.index == len(suite) - 1)
### post-process data
cocopp.main(observer.result_folder) # re-run folders look like "...-001" etc
webbrowser.open("file://" + os.getcwd() + "/ppdata/index.html")