mot.optimize package¶
Submodules¶
mot.optimize.base module¶
-
class
mot.optimize.base.
ConstraintFunction
[source]¶ Bases:
mot.lib.cl_function.CLFunction
These functions are meant to be provided to the optimization routines.
If provided to the optimization routines, they should hold a CL function with the signature:
void <func_name>(local const mot_float_type* const x, void* data, local mot_float_type* constraints);
Although this is not enforced for general usage of this class.
Since the number of constraints in the
constraints
array is variable, this class additionally specifies the number of constraints using the methodget_nmr_constraints()
.
-
class
mot.optimize.base.
OptimizeResults
[source]¶ Bases:
dict
Represents the optimization results.
-
x
¶ the optimized parameter maps, an (d, p) array with for d problems a value for every p parameters
Type: ndarray
-
status
¶ the return codes, an (d,) vector with for d problems the status return code
Type: ndarray
-
-
class
mot.optimize.base.
SimpleConstraintFunction
(*args, nmr_constraints=None, **kwargs)[source]¶ Bases:
mot.lib.cl_function.SimpleCLFunction
,mot.optimize.base.ConstraintFunction
-
classmethod
from_string
(cl_function, dependencies=(), nmr_constraints=None)[source]¶ Parse the given CL function into a SimpleCLFunction object.
Parameters: - cl_function (str) – the function we wish to turn into an object
- dependencies (list or tuple of CLLibrary) – The list of CL libraries this function depends on
Returns: the CL data type for this parameter declaration
Return type:
-
classmethod
Module contents¶
-
mot.optimize.
get_minimizer_options
(method)[source]¶ Return a dictionary with the default options for the given minimization method.
Parameters: method (str) – the name of the method we want the options off Returns: a dictionary with the default options Return type: dict
-
mot.optimize.
maximize
(func, x0, nmr_observations, **kwargs)[source]¶ Maximization of a function.
This wraps the objective function to take the negative of the computed values and passes it then on to one of the minimization routines.
Parameters: - func (mot.lib.cl_function.CLFunction) –
A CL function with the signature:
double <func_name>(local const mot_float_type* const x, void* data, local mot_float_type* objective_list);
The objective list needs to be filled when the provided pointer is not null. It should contain the function values for each observation. This list is used by non-linear least-squares routines, and will be squared by the least-square optimizer. This is only used by the
Levenberg-Marquardt
routine. - x0 (ndarray) – Initial guess. Array of real elements of size (n, p), for ‘n’ problems and ‘p’ independent variables.
- nmr_observations (int) – the number of observations returned by the optimization function.
- **kwargs – see
minimize()
.
- func (mot.lib.cl_function.CLFunction) –
-
mot.optimize.
minimize
(func, x0, data=None, method=None, lower_bounds=None, upper_bounds=None, constraints_func=None, nmr_observations=None, cl_runtime_info=None, options=None)[source]¶ Minimization of one or more variables.
For an easy wrapper of function maximization, see
maximize()
.All boundary conditions are enforced using the penalty method. That is, we optimize the objective function:
\[F(x) = f(x) \mu \sum \max(0, g_i(x))^2\]where \(F(x)\) is the new objective function, \(f(x)\) is the old objective function, \(g_i\) are the boundary functions defined as \(g_i(x) \leq 0\) and \(\mu\) is the penalty weight.
The penalty weight is by default \(\mu = 1e20\) and can be set using the
options
dictionary aspenalty_weight
.Parameters: - func (mot.lib.cl_function.CLFunction) –
A CL function with the signature:
double <func_name>(local const mot_float_type* const x, void* data, local mot_float_type* objective_list);
The objective list needs to be filled when the provided pointer is not null. It should contain the function values for each observation. This list is used by non-linear least-squares routines, and will be squared by the least-square optimizer. This is only used by the
Levenberg-Marquardt
routine. - x0 (ndarray) – Initial guess. Array of real elements of size (n, p), for ‘n’ problems and ‘p’ independent variables.
- data (mot.lib.kernel_data.KernelData) – the kernel data we will load. This is returned to the likelihood function
as the
void* data
pointer. - method (str) –
Type of solver. Should be one of: - ‘Levenberg-Marquardt’ - ‘Nelder-Mead’ - ‘Powell’ - ‘Subplex’
If not given, defaults to ‘Powell’.
- lower_bounds (tuple) – per parameter a lower bound, if given, the optimizer ensures
a <= x
with a the lower bound and x the parameter. If not given, -infinity is assumed for all parameters. Each tuple element can either be a scalar or a vector. If a vector is given the first dimension length should match that of the parameters. - upper_bounds (tuple) – per parameter an upper bound, if given, the optimizer ensures
x >= b
with b the upper bound and x the parameter. If not given, +infinity is assumed for all parameters. Each tuple element can either be a scalar or a vector. If a vector is given the first dimension length should match that of the parameters. - constraints_func (mot.optimize.base.ConstraintFunction) –
function to compute (inequality) constraints. Should hold a CL function with the signature:
void <func_name>(local const mot_float_type* const x, void* data, local mot_float_type* constraints);
Where
constraints_values
is filled as:constraints[i] = g_i(x)
That is, for each constraint function \(g_i\), formulated as \(g_i(x) <= 0\), we should return the function value of \(g_i\).
- nmr_observations (int) – the number of observations returned by the optimization function.
This is only needed for the
Levenberg-Marquardt
method. - cl_runtime_info (mot.configuration.CLRuntimeInfo) – the CL runtime information
- options (dict) – A dictionary of solver options. All methods accept the following generic options: - patience (int): Maximum number of iterations to perform. - penalty_weight (float): the weight of the penalty term for the boundary conditions
Returns: The optimization result represented as a
OptimizeResult
object. Important attributes are:x
the solution array.Return type: - func (mot.lib.cl_function.CLFunction) –