4.1. pyopus.optimizer.base — Base classes for optimization algorithms and plugins

Inheritance diagram of pyopus.optimizer.base

Base classes for optimization algorithms and plugins (PyOPUS subsystem name: OPT)

Every optimization algorthm should be used in the following way.

  1. Create the optimizer object.
  2. Call the reset() method of the object to set the initial point.
  3. Optionally call the check() method to check the consistency of settings.
  4. Run the algorithm by calling the run() method.

The same object can be reused by calling the reset() method followed by an optional call to check() and a call to run().

class pyopus.optimizer.base.RandomDelay(obj, delayInterval)

A wrapper class for introducing a random delay into function evaluation

Objects of this class are callable. On call they evaluate the callable object given by obj with the given args. A random delay with uniform distribution specified by the delayInterval list is generated and applied before the return value from obj is returned. The two members of delayInterval list specify the lower and the upper bound on the delay.

Example:

from pyopus.optimizer.mgh import RandomDelay

def f(x):
      return 2*x

# Delayed evaluator with a random delay between 1 and 5 seconds
fprime=RandomDelay(f, [1.0, 5.0])

# Return f(10) without delay
print f(x)

# Return f(10) with a random delay between 1 and 5 seconds
print fprime(10)
class pyopus.optimizer.base.Plugin(quiet=False)

Base class for optimization algorithm plugins.

A plugin is a callable object with the following calling convention

plugin_object(x, f, opt, annotation)

where x is an array representing a point in the search space and f is its corresponding cost function value. opt is a reference to the optimization algorithm where the plugin is installed.

The plugin’s job is to produce output or update some internal structures with the data collected from x, f, and opt. For transferring auxiliary data that is not included in x or f from remote workers the annotations mechanism can be used.

A plugin collects this auxiliary data from local structures returns it when it is called with annotation set to None. We say that the annotation is produced.

If the annotation argument that is not None is passed to the plugin at call time the plugin must use this data for updating the local structures. We say that the annotation is consumed.

Usually the annotation is produced on remote workers when the cost function is evaluated. It is the job of the optimization algorithm to send the value of f along with the corresponding annotations from worker (where the evaluation took place) to the master where the annotation is consumed. This way the master can access all the auxiliary data which is normally produced only on the machine where the evaluation of the cost function takes place.

If quiet is True the plugin supresses its output. Useful on remote workers where the output of a plugin is often uneccessary.

reset()
Resets the plugin to its initial state.
setQuiet(quiet)
If quiet is True all further output is supressed by the plugin. To reenable it, call this method with quiet set to False.
class pyopus.optimizer.base.Reporter(onImprovement=True, onIterStep=1)

A base class for plugins used for reporting the cost function value and its details.

If onImprovement is True the cost function value is reported only when it improves on the best-yet (lowest) cost function value.

onIterStep specifies the number of calls to the reporter after which the cost function value is reported regardless of the onImprovement setting. Setting onIterStep to 0 disables this feature.

The value of the cost function at the first call to the reporter (after the last call to the reset() method) is always reported.

This basic reporter class produces and consumes no annotations. Advanced reporters which report the details of the cost function must implement the annotations mechanism in order for them to work correctly in parallel computing environments.

The iteration number is obtained from the niter member of the opt object passed when the reporter is called. The best-yet value of the cost function is obtained from the f member of the opt object. This member is None at the first iteration. Note that the f member is updated after all plugins are called.

The bestIter member of obj lists the iteration in which the best-yet function value was obtained.

class pyopus.optimizer.base.Stopper(quiet=False)

Stopper plugins are used for stopping the optimization algorithm when a particular condition is satisfied. These plugins produce an annotation. The annotation is usually a flag. If this flag is True the optimization algorithm should be stopped. Annotations enable the stopper to stop the optimization algorithm if the stopping condition is satisfied on some remote worker.

The actuall stopping (consumption of the annotation) is achieved by setting the stop member of the opt object to True.

This base class merely propagates the stopping condition in the opt object at the worker to the opt object of the master.

class pyopus.optimizer.base.Optimizer(function, debug=0, fstop=None, maxiter=None)

Base class for unconstrained optimization algorithms.

function is the cost function (Python function or any other callable object) which should be minimzied.

If debug is greater than 0, debug messages are printed at standard output.

fstop specifies the cost function value at which the algorithm is stopped. If it is None the algorithm is never stopped regardless of how low the cost function’s value gets.

maxiter specifies the number of cost function evaluations after which the algorithm is stopped. If it is None the number of cost function evaluations is unlimited.

The following members are available in every object of the Optimizer class

  • ndim - dimension of the problem. Updated when the reset() method is called.
  • niter - the consecutive number of cost function evaluation.
  • x - the argument to the cost function resulting in the best-yet (lowest) value.
  • f - the best-yet value of the cost function.
  • bestIter - the iteration in which the best-yet value of the cost function was found.
  • bestAnnotations - a list of annotations produced by the installed plugins for the best-yet value of the cost function.
  • stop - boolean flag indicating that the algorithm should stop.
  • annotations - a list of annotations produced by the installed plugins for the last evaluated cost function value
  • plugins - a list of installed plugin objects

Plugin objects are called at every cost function evaluation or whenever a remotely evaluated cost function value is registered by the newResult() method.

Values of x and related members are arrays.

check()
Checks the optimization algorithm’s settings and raises an exception if something is wrong.
fun(x, count=True)

Evaluates the cost function at x (array). If count is True the newResult() method is invoked with x, the obtained cost function value, and annotations argument set to None. This means that the reuslt is registered (best-yet point information are updated) and the plugins are called to produce annotations.

Use False for count if you need to evaluate the cost function for degugging purposes.

Returns the value of the cost function at x.

installPlugin(plugin, index=-1)

Installs a plugin object at index-th position in the plugins list. The old plugin at that position is discarded.

If index is negative, the plugin is installed at the end of the plugins list.

Returns the index of the installed plugin in the plugins list.

newResult(x, f, annotations=None)

Registers the cost function value f obtained at point x with annotations list given by annotations.

Increases the niter member to reflect the iteration number of the point being registered and updates the f, x, and bestIter members.

If the annotations argument is not given, produces annotations by calling the plugin objects from the plugins member. The plugins produce annotations that get stored in the annotations member. If f improves on the best-yet value, the obtained annotations are also stored in the bestAnnotations member.

If the annotations argument is given, it must be a list with as many members as there are plugin objects installed in the optimizer. The annotations list is stored in the annotations member. If f improves the best-yet value annotations are also stored in the bestAnnotations member. Plugins are invoked with annotations as their last agrument thus applying the annotations to the objects in the local Python interpreter.

Finally it is checked if the best-yet value of cost function is below fstop or the number of iterations exceeded maxiter. If any of these two conditions is satisfied, the algorithm is stopped by setting the stop member to True.

reset(x0)
Puts the optimizer in its initial state and sets the initial point to be the 1-dimensional array or list x0. The length of the array becomes the dimension of the optimization problem (ndim member).
run()
Runs the optimization algorithm.
class pyopus.optimizer.base.BoxConstrainedOptimizer(function, xlo=None, xhi=None, debug=0, fstop=None, maxiter=None)

Box-constrained optimizer class

xlo and xhi are 1-dimensional arrays or lists holding the lower and upper bounds on the components of x. Some algorithms allow the components of xlo to be - \infty and the components of xhi to be + \infty.

See the optimizer class for more information.

bound(x)
Fixes components of x so that teh bounds are enforced. If a component is below lower bound it is set to the lower bound. If a component is above upper bound it is set to the upper bound.
check()
Checks the optimization algorithm’s settings and raises an exception if something is wrong.
denormalize(y)

Returns a denormalized point x corresponding to y. Components of x are

x^i = y^i n_s^i + n_o^i

normDist(x, y)

Calculates normalized distance between x and y.

Normalized distance is calculated as

\sqrt{\sum_{i=1}^{n} (\frac{x^i - y^i}{n_s^i})^2}

normalize(x)

Returnes a normalized point y corresponding to x. Components of y are

y^i = \frac{x^i - n_o^i}{n_s^i}

If both bounds are finite, the result is within the [0,1] interval.

reset(x0)

Puts the optimizer in its initial state and sets the initial point to be the 1-dimensional array or list x0. The length of the array becomes the dimension of the optimization problem (ndim member). The shape of x must match that of xlo and xhi.

Normalization origin n_o and scaling n_s are calculated from the values of xlo, xhi, and intial point x0:

  • If a lower bound x_{lo}^i is - \infty

    n_s^i &= 2 (x_{hi}^i - x_0^i) \\
n_o^i &= x_0^i - n_s^i / 2

  • If an upper bound x_{hi}^i is + \infty

    n_s^i &= 2 (x_0^i - x_{lo}^i) \\
n_o^i &= x_0^i - n_s^i / 2

  • If both lower and upper bound are infinite

    n_s^i &= 2 \\
n_o^i &= x_0^i

  • If bouth bounds are finite

    n_s^i &= x_{hi}^i - x_{lo}^i \\
n_o^i &= x_{lo}

Previous topic

4. pyopus.optimizer — Optimization algorithms and test function suites

Next topic

4.2. pyopus.optimizer.coordinate — Box constrained coordinate search optimizer

This Page