bluemira.optimisation._nlopt.optimiser
Attributes
Classes
Optimiser implementation using NLOpt as the backend. |
Functions
|
Validate, and convert, the given algorithm. |
|
Communicate to the user the NLopt optimisation result. |
Module Contents
- bluemira.optimisation._nlopt.optimiser.NLOPT_ALG_MAPPING
- class bluemira.optimisation._nlopt.optimiser.NloptOptimiser(algorithm: bluemira.optimisation._algorithm.AlgorithmType, n_variables: int, f_objective: bluemira.optimisation.typed.ObjectiveCallable, df_objective: bluemira.optimisation.typed.OptimiserCallable | None = None, opt_conditions: collections.abc.Mapping[str, int | float] | None = None, opt_parameters: collections.abc.Mapping[str, Any] | None = None, *, keep_history: bool = False)
Bases:
bluemira.optimisation._optimiser.OptimiserOptimiser implementation using NLOpt as the backend.
- Parameters:
algorithm (bluemira.optimisation._algorithm.AlgorithmType) –
The optimisation algorithm to use. Available algorithms are:
SLSQP
COBYLA
SBPLX
MMA
BFGS
DIRECT
DIRECT_L
CRS
ISRES
n_variables (int) – The number of optimisation parameters.
f_objective (bluemira.optimisation.typed.ObjectiveCallable) – The objective function to minimise. This function must take one argument (a numpy array), and return a numpy array or float.
df_objective (bluemira.optimisation.typed.OptimiserCallable | None) – The derivative of the objective function. This must take the form: \(f(x) \rightarrow y\) where \(x\) is a numpy array containing the optimisation parameters, and \(y\) is a numpy array where each element \(i\) is the partial derivative \(\frac{\partial f(x)}{\partial x_{i}}\). If not given, and a gradient based algorithm is used, a numerical approximation of the gradient will be made.
opt_conditions (collections.abc.Mapping[str, int | float] | None) –
The stopping conditions for the optimiser. At least one stopping condition is required. Supported conditions are:
ftol_abs: float
ftol_rel: float
xtol_abs: float
xtol_rel: float
max_eval: int
max_time: float
stop_val: float
opt_parameters (collections.abc.Mapping[str, Any] | None) – Parameters specific to the algorithm being used. Consult the NLopt documentation for these.
keep_history (bool) – Whether to record the history of each step of the optimisation. (default:
False)
- _keep_history = False
- _opt
- _eq_constraints: list[bluemira.optimisation._nlopt.functions.Constraint] = []
- _ineq_constraints: list[bluemira.optimisation._nlopt.functions.Constraint] = []
- property algorithm: bluemira.optimisation._algorithm.Algorithm
returns: the optimiser’s algorithm.
- Return type:
- property opt_conditions: dict[str, float]
returns: the optimiser’s stopping conditions.
- Return type:
dict[str, float]
- property opt_parameters: collections.abc.Mapping[str, int | float]
returns: the optimiser algorithms’s parameters.
- Return type:
collections.abc.Mapping[str, int | float]
- property lower_bounds: numpy.ndarray
returns: the lower bounds for the optimisation parameters.
- Return type:
numpy.ndarray
- property upper_bounds: numpy.ndarray
returns: the upper bounds for the optimisation parameters.
- Return type:
numpy.ndarray
- add_eq_constraint(f_constraint: bluemira.optimisation.typed.OptimiserCallable, tolerance: numpy.ndarray, df_constraint: bluemira.optimisation.typed.OptimiserCallable | None = None) None
Add an equality constraint.
See
add_eq_constraint().- Raises:
OptimisationError – Algorithm does not support equality constraints
- Parameters:
f_constraint (bluemira.optimisation.typed.OptimiserCallable)
tolerance (numpy.ndarray)
df_constraint (bluemira.optimisation.typed.OptimiserCallable | None)
- Return type:
None
- add_ineq_constraint(f_constraint: bluemira.optimisation.typed.OptimiserCallable, tolerance: numpy.ndarray, df_constraint: bluemira.optimisation.typed.OptimiserCallable | None = None) None
Add an inequality constraint.
- Raises:
OptimisationError – Algorithm does not support inequality constraints
- Parameters:
f_constraint (bluemira.optimisation.typed.OptimiserCallable)
tolerance (numpy.ndarray)
df_constraint (bluemira.optimisation.typed.OptimiserCallable | None)
- Return type:
None
- optimise(x0: numpy.ndarray | None = None) bluemira.optimisation._optimiser.OptimiserResult
Run the optimisation.
See
optimise().- Returns:
The result of optimisation
- Raises:
KeyboardInterrupt – Optimisation halted by user
OptimisationError – low level optimisation error
- Parameters:
x0 (numpy.ndarray | None)
- Return type:
- _get_constraint_history()
- set_lower_bounds(bounds: numpy.typing.ArrayLike) None
Set the lower bound for each optimisation parameter.
See
set_lower_bounds().- Parameters:
bounds (numpy.typing.ArrayLike)
- Return type:
None
- set_upper_bounds(bounds: numpy.typing.ArrayLike) None
Set the upper bound for each optimisation parameter.
See
set_upper_bounds().- Parameters:
bounds (numpy.typing.ArrayLike)
- Return type:
None
- _get_previous_iter_result() tuple[numpy.ndarray, float]
- Returns:
the parameterisation and result from the previous iteration.
- Return type:
tuple[numpy.ndarray, float]
- _handle_round_off_error() tuple[numpy.ndarray, float]
Handle a round-off error occurring in an optimisation.
It’s likely the last call was a decent solution, so return that (with a warning).
- Returns:
the parameterisation and result from the previous iteration with a warning
- Return type:
tuple[numpy.ndarray, float]
- _set_algorithm(alg: bluemira.optimisation._algorithm.AlgorithmType) None
Set the optimiser’s algorithm.
- Parameters:
alg (bluemira.optimisation._algorithm.AlgorithmType)
- Return type:
None
- _set_objective_function(func: bluemira.optimisation.typed.ObjectiveCallable, df: bluemira.optimisation.typed.OptimiserCallable | None, n_variables: int) None
Wrap and set the objective function.
- Parameters:
df (bluemira.optimisation.typed.OptimiserCallable | None)
n_variables (int)
- Return type:
None
- _set_termination_conditions(opt_conditions: collections.abc.Mapping[str, int | float]) None
Validate and set the termination conditions.
- Parameters:
opt_conditions (collections.abc.Mapping[str, int | float])
- Return type:
None
- _set_algorithm_parameters(opt_parameters: collections.abc.Mapping) None
- Parameters:
opt_parameters (collections.abc.Mapping)
- Return type:
None
- bluemira.optimisation._nlopt.optimiser._check_algorithm(algorithm: bluemira.optimisation._algorithm.AlgorithmType) bluemira.optimisation._algorithm.Algorithm
Validate, and convert, the given algorithm.
- Returns:
validated and converted algorithm
- Parameters:
algorithm (bluemira.optimisation._algorithm.AlgorithmType)
- Return type:
- bluemira.optimisation._nlopt.optimiser._process_nlopt_result(opt: nlopt.opt, algorithm: bluemira.optimisation._algorithm.Algorithm) None
Communicate to the user the NLopt optimisation result.
Usually this would be called when dealing with an error in an optimisation loop.
- Parameters:
opt (nlopt.opt) – The optimiser to check
algorithm (bluemira.optimisation._algorithm.Algorithm) – The optimisation algorithm
- Return type:
None