Base classes for optimization algorithms and plugins (PyOPUS subsystem name: OPT)
Every optimization algorthm should be used in the following way.
The same object can be reused by calling the reset() method followed by an optional call to check() and a call to run().
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)
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.
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.
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.
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
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.
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.
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.
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.
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 and the components of xhi to
be
.
See the optimizer class for more information.
Returns a denormalized point x corresponding to y. Components of x are
Calculates normalized distance between x and y.
Normalized distance is calculated as
Returnes a normalized point y corresponding to x. Components of y are
If both bounds are finite, the result is within the
interval.
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 and scaling
are calculated
from the values of xlo, xhi, and intial point x0:
If a lower bound is
If an upper bound is
If both lower and upper bound are infinite
If bouth bounds are finite