optim¶
This module defines the interface for solving minimization problems:
where \(f : \mathbb R^n \to \mathbb R\).
Module Contents¶
- type optim.ObjectiveFn = Callable[[np.ndarray, object], int]¶
Signature of the objective function \(f(, y)\).
!!!!!!!!! The function accepts four arguments: !!!!!!!!! - t: current time, !!!!!!!!! - y: state vector at time \(t\), !!!!!!!!! - ydot: output array to which the result !!!!!!!!! - user_data: additional context (user-defined data) that !!!!!!!!! must be passed to the function (e.g., parameters of the system).
- class optim.Optim(impl: str)¶
Interface for solving optimization (minimization) problems.
This class serves as a gateway to the implementations of the solvers for optimization problems.
- Parameters:
impl (str) – Name of the desired implementation.
Examples
Let’s solve the following convex optimization problem:
\[minimize \sum_{i = 1}^N x_i^2\]where the solution is \([0, ..., 0]\) as the problem is convex.
First, import the necessary modules: >>> import numpy as np >>> from oif.interfaces.optim import Optim
Define the objective function:
>>> def objective_fn(x): ... return np.sum(x**2)
Create an instance of the optim solver using the implementation “scipy_optimize”, which is an adapter to the scipy.optimize Python package:
>>> s = Optim("scipy_optimize")
We set the initial value, the right-hand side function, and the tolerance:
>>> s.set_initial_guess([2.718, 3.142]) >>> s.set_objective_fn(objective_fn)
Now we solve the minimization problem and print the return status and message:
>>> status, message = s.minimize()
We can print the resultant minimizer by retrieving it from the solver:
>>> print(f"Minimizer is {s.x}")
- x0: numpy.ndarray¶
Current value of the state vector.
- x: numpy.ndarray¶
- user_data: object¶
- oif_user_data: openinterfaces.core.OIFUserData¶
- set_initial_guess(x0: numpy.ndarray)¶
Set initial guess for the optimization problem
- set_objective_fn(objective_fn: ObjectiveFn)¶
- set_user_data(user_data: object)¶
Specify additional data that will be used for right-hand side function.
- set_method(method_name: str, method_params: dict = {})¶
Set integrator, if the name is recognizable.
- minimize()¶
Integrate to time t and write solution to y.