Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/numbbo/coco into fix…
Browse files Browse the repository at this point in the history
…-logger-filenames
  • Loading branch information
ttusar committed Jun 7, 2024
2 parents e255f69 + 647b719 commit 7f9fc37
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 82 deletions.
21 changes: 11 additions & 10 deletions code-experiments/build/python/example/example_experiment2.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,14 @@
from __future__ import division, print_function, unicode_literals
__author__ = "Nikolaus Hansen and ..."
import sys
import time # output some timings per evaluation
from collections import defaultdict
import os, webbrowser # to show post-processed results in the browser
import numpy as np # for median, zeros, random, asarray
import cocoex # experimentation module
try: import cocopp # post-processing module
except: pass

### MKL bug fix
def set_num_threads(nt=1, disp=1):
"""see https://github.com/numbbo/coco/issues/1919
and https://github.com/CMA-ES/pycma/issues/238
and https://twitter.com/jeremyphoward/status/1185044752753815552
"""
import os
try: import mkl
except ImportError: disp and print("mkl is not installed")
else:
Expand All @@ -65,7 +60,14 @@ def set_num_threads(nt=1, disp=1):
disp and print("setting mkl threads num to", nt)

if sys.platform.lower() not in ('darwin', 'windows'):
set_num_threads(1)
set_num_threads(1) # execute before numpy is imported

import time # output some timings per evaluation
from collections import defaultdict
import numpy as np # for median, zeros, random, asarray
import cocoex # experimentation module
try: import cocopp # post-processing module
except: pass

### solver imports (add other imports if necessary)
import scipy.optimize # to define the solver to be benchmarked
Expand Down Expand Up @@ -193,7 +195,6 @@ def random_search(f, lbounds, ubounds, evals):
print(" %3d %.1e" % (dimension, np.median(timings[dimension])))
print(" -------------------------------------")

### post-process data
### post-process data and open browser
if batches == 1 and 'cocopp' in globals() and cocopp not in (None, 'None'):
cocopp.main(observer.result_folder) # re-run folders look like "...-001" etc
webbrowser.open("file://" + os.getcwd() + "/ppdata/index.html")
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
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})
import cma
def fmin(fun, x0, **kwargs):
return cma.fmin2(fun, x0, 2, {'verbose': -9} if not kwargs.get('disp')
else None)[0]
"""
from __future__ import division, print_function
Expand Down
4 changes: 2 additions & 2 deletions code-experiments/build/python/src/cocoex/interface.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,10 @@ cdef class Problem:
return coco_problem_get_evaluations_constraints(self.problem)
@property
def final_target_hit(self):
"""return 1 if the final target is known and has been hit, 0 otherwise
"""return `True` if the final target has been hit, `False` otherwise
"""
assert(self.problem)
return coco_problem_final_target_hit(self.problem)
return bool(coco_problem_final_target_hit(self.problem))
#@property
#def final_target_fvalue1(self):
# assert(self.problem)
Expand Down
58 changes: 52 additions & 6 deletions code-experiments/build/python/src/cocoex/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def write_setting(dict_, filename, ignore_list=None):
See also `dict_to_eval` and `read_setting`.
"""
if isinstance(filename, (tuple, list)):
filename = _os.path.join(*filename)
if not _os.path.exists(filename):
with open(filename, 'wt') as f:
if ignore_list is None:
Expand Down Expand Up @@ -172,6 +174,36 @@ def read_setting(filename, warn=True):
warn and _warnings.warn("Parameter file '{}' (to check setting consistency)"
" does not exist".format(filename))

def forgiving_import(module, warn_level=0):
"""Do nothing if import fails, return the imported module otherwise.
Usage::
cma = forgiving_import('cma')
in place of::
import cma
This is helpful to keep some code smoothly working when `cma` is not
installed and not used in the current use case.
"""
try:
import importlib
except ImportError:
print('Please replace \n\n {}\n\n with \n\n {}\n\n'
'or incomment the respective line.'.format(
"module = forgiving_import('module')",
"import module"))
raise
try:
return importlib.import_module(module)
except ImportError:
if warn_level:
print("importing module `{}` with `importlib` failed".format(module))
if warn_level > 1:
raise

class ObserverOptions(dict):
"""a `dict` with observer options which can be passed to
the (C-based) `Observer` via the `as_string` property.
Expand Down Expand Up @@ -397,8 +429,10 @@ class MiniPrint:
"""
def __init__(self):
self.dimension = None
self.id_function = None
self.last_index = -1
self._calls = 0
self._calls = 0 # formatting aid
self._functions = 0 # not in use
self._sweeps = 0
self._day0 = _time.localtime()[2]
@property
Expand All @@ -410,17 +444,29 @@ def stime(self):
s = s + "+%dd" % (ltime[0] - self._day0)
return s
def __call__(self, problem, final=False, restarted=False, final_message=False):
if self.dimension != problem.dimension or self.last_index > problem.index:
new_dimension = self.dimension != problem.dimension or (
self.last_index > problem.index)
if new_dimension:
_sys.stdout.flush()
if self.dimension is not None:
print('')
print("%dD %s" % (problem.dimension, self.stime))
self.dimension = problem.dimension
self._calls = 0
elif not self._calls % 10:
if self._calls % 50:
print(' ', end='')
else:
self._functions = 0
# elif not self._calls % 10:
# if self._calls % 50:
# print(' ', end='')
# else:
# print()
if self.id_function != problem.id_function or new_dimension:
self.id_function = problem.id_function
if not new_dimension and self._calls >= 50:
self._calls = 0
print()
print('{}f{}'.format(' ' if self.id_function < 10 else '',
self.id_function), end='', flush=True)
self._functions += 1
self.last_index = problem.index
self._calls += 1
print('|' if problem.final_target_hit else ':' if restarted else '.', end='')
Expand Down
11 changes: 5 additions & 6 deletions code-postprocessing/cocopp/compall/pptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,19 @@ def get_table_caption():
(preceded by the target !!DF!!-value in \textit{italics}) in the first row.
\#succ is the number of trials that reached the target value of the last column.
"""

table_caption_rest = (r"""%
The median number of conducted function evaluations is additionally given in
\textit{italics}, if the target in the last column was never reached.
Entries, succeeded by a star, are statistically significantly better (according to
the rank-sum test) when compared to all other algorithms of the table, with
$p = 0.05$ or $p = 10^{-k}$ when the number $k$ following the star is larger
than 1, with Bonferroni correction by the number of functions (!!TOTAL-NUM-OF-FUNCTIONS!!). """ +
The best entry is marked by a star when the largest $p$-value of pairwise
rank-sum tests with the other algorithms either obeys $0.01 < p \le 0.05$
or $10^{-k-1} < p \le 10^{-k}$ when the star is followed by the number $k$,
with Bonferroni correction by the number of functions (!!TOTAL-NUM-OF-FUNCTIONS!!). """ +
(r"""A ${}$ signifies the number of trials that were worse than the ERT of !!THE-REF-ALG!! """
r"""shown only when less than 10 percent were worse and the ERT was better."""
.format(significance_vs_ref_symbol)
if not (testbedsettings.current_testbed.reference_algorithm_filename == '' or
testbedsettings.current_testbed.reference_algorithm_filename is None)
else "") + r"""Best results are printed in bold.
else "") + r""" Best results are printed in bold.
""")

table_caption = None
Expand Down
Loading

0 comments on commit 7f9fc37

Please sign in to comment.