module documentation

Undocumented

Class MiniPrint print dimension when changed and a single symbol for each call.
Class ObserverOptions a dict with observer options which can be passed to the (C-based) Observer via the as_string property.
Class ProblemNonAnytime The non-anytime problem class.
Class SameFunction Count the number of consecutive instances of the same function.
Class ShortInfo print minimal info during benchmarking.
Function about_equal Return True if the floating point number ${a} and ${b} are about equal.
Function args_to_dict return a dict from a list of "name=value" strings.
Function ascetime return elapsed time as str.
Function dict_to_eval return a pruned dict so that ast.literal_eval(repr(dict_)) works.
Function forgiving_import Do nothing if import fails, return the imported module otherwise.
Function print_flush print without newline but with flush
Function read_setting return file content evaluated as Python literal (e.g. a dict),
Function write_setting write a simplified parameters dictionary to a file,
def about_equal(a, b, precision=1e-06):

Return True if the floating point number ${a} and ${b} are about equal.

def args_to_dict(args, known_names, specials=None, split='=', print=(lambda *args, **kwargs: None)):

return a dict from a list of "name=value" strings.

args come in the form of a list of "name=value" strings without spaces, like ["budget_multiplier=100"].

Return dict(arg.split(split) for arg in args) in the most basic case, but additionally (i) checks that the keys of this dict are known names, (ii) evaluates the values in some cases and (iii) handles specials.

known_names is an iterable (dict or list or tuple) of strings. If known_names is None, all args are processed, otherwise a ValueError is raised for unknown names. This is useful if we want to re-assign variables (overwrite default values) and avoid spelling mistakes pass silently.

The value is processed as a Python literal with ast.literal_eval or remains a str when this is unsuccessful.

specials is a dict and can currently only contain 'batch', followed by "name1/name2" as value. name1 and name2 are then assigned from the values in arg, for example to 2 and 4 with batch=2/4.

A main usecase is to process sys.argv[1:] into a dict in a python script, like:

command_line_dict = args_to_dict(sys.argv[1:], globals())
globals().update(command_line_dict)
>>> import cocoex
>>> d = cocoex.utilities.args_to_dict(["budget=2.3", "bed=bed-name", "number=4"],
...                                   ["budget", "bed", "number", "whatever"])
>>> len(d)
3
>>> assert d['bed'] == 'bed-name'
>>> assert isinstance(d["budget"], float)
def ascetime(sec, decimals=0):

return elapsed time as str.

Example: return "0h33:21" if sec == 33*60 + 21.

def dict_to_eval(dict_, ignore_list=('_', 'self')):

return a pruned dict so that ast.literal_eval(repr(dict_)) works.

Keys that start with entries from ignore_list when interpreted as str are removed, by default those starting with _ or self, where ignore_list must be a str or a tuple of str.

See also write_setting.

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.

def print_flush(*args):

print without newline but with flush

def read_setting(filename, warn=True):

return file content evaluated as Python literal (e.g. a dict),

return None if filename is not a valid path.

If warn, throw a warning when the file filename does not exist.

A typical usecase could be old_multiplier = read_setting('parameters.pydat')['budget_multiplier'].

See also write_setting.

def write_setting(dict_, filename, ignore_list=None):

write a simplified parameters dictionary to a file,

for keeping a record or, e.g., for checking the budget_multiplier later.

A typical usecase is write_setting(locals(), 'parameters.pydat').

When ignore_list is not None it is passed to dict_to_eval which determines which parameters are written or omitted. By default, keys starting with _ or self are omitted and items that bail on literal_eval.

See also dict_to_eval and read_setting.