bluemira.optimisation._nlopt.optimiser ====================================== .. py:module:: bluemira.optimisation._nlopt.optimiser Attributes ---------- .. autoapisummary:: bluemira.optimisation._nlopt.optimiser.NLOPT_ALG_MAPPING Classes ------- .. autoapisummary:: bluemira.optimisation._nlopt.optimiser.NloptOptimiser Functions --------- .. autoapisummary:: bluemira.optimisation._nlopt.optimiser._check_algorithm bluemira.optimisation._nlopt.optimiser._process_nlopt_result Module Contents --------------- .. py:data:: NLOPT_ALG_MAPPING .. py:class:: 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: :py:obj:`bluemira.optimisation._optimiser.Optimiser` .. autoapi-inheritance-diagram:: bluemira.optimisation._nlopt.optimiser.NloptOptimiser :parts: 1 :private-bases: Optimiser implementation using NLOpt as the backend. :param algorithm: The optimisation algorithm to use. Available algorithms are: * SLSQP * COBYLA * SBPLX * MMA * BFGS * DIRECT * DIRECT_L * CRS * ISRES :param n_variables: The number of optimisation parameters. :param f_objective: The objective function to minimise. This function must take one argument (a numpy array), and return a numpy array or float. :param df_objective: The derivative of the objective function. This must take the form: :math:`f(x) \rightarrow y` where :math:`x` is a numpy array containing the optimisation parameters, and :math:`y` is a numpy array where each element :math:`i` is the partial derivative :math:`\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. :param opt_conditions: 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 :param opt_parameters: Parameters specific to the algorithm being used. Consult the NLopt documentation for these. :param keep_history: Whether to record the history of each step of the optimisation. (default: ``False``) .. py:attribute:: _keep_history :value: False .. py:attribute:: _opt .. py:attribute:: _eq_constraints :type: list[bluemira.optimisation._nlopt.functions.Constraint] :value: [] .. py:attribute:: _ineq_constraints :type: list[bluemira.optimisation._nlopt.functions.Constraint] :value: [] .. py:property:: algorithm :type: bluemira.optimisation._algorithm.Algorithm returns: the optimiser's algorithm. .. py:property:: opt_conditions :type: dict[str, float] returns: the optimiser's stopping conditions. .. py:property:: opt_parameters :type: collections.abc.Mapping[str, int | float] returns: the optimiser algorithms's parameters. .. py:property:: lower_bounds :type: numpy.ndarray returns: the lower bounds for the optimisation parameters. .. py:property:: upper_bounds :type: numpy.ndarray returns: the upper bounds for the optimisation parameters. .. py:method:: 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 :meth:`~bluemira.optimisation._optimiser.Optimiser.add_eq_constraint`. :raises OptimisationError: Algorithm does not support equality constraints .. py:method:: 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. See :meth:`~bluemira.optimisation._optimiser.Optimiser.add_ineq_constraint`. :raises OptimisationError: Algorithm does not support inequality constraints .. py:method:: optimise(x0: numpy.ndarray | None = None) -> bluemira.optimisation._optimiser.OptimiserResult Run the optimisation. See :meth:`~bluemira.optimisation._optimiser.Optimiser.optimise`. :returns: The result of optimisation :raises KeyboardInterrupt: Optimisation halted by user :raises OptimisationError: low level optimisation error .. py:method:: _get_constraint_history() .. py:method:: set_lower_bounds(bounds: numpy.typing.ArrayLike) -> None Set the lower bound for each optimisation parameter. See :meth:`~bluemira.optimisation._optimiser.Optimiser.set_lower_bounds`. .. py:method:: set_upper_bounds(bounds: numpy.typing.ArrayLike) -> None Set the upper bound for each optimisation parameter. See :meth:`~bluemira.optimisation._optimiser.Optimiser.set_upper_bounds`. .. py:method:: _get_previous_iter_result() -> tuple[numpy.ndarray, float] :returns: the parameterisation and result from the previous iteration. .. py:method:: _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 .. py:method:: _set_algorithm(alg: bluemira.optimisation._algorithm.AlgorithmType) -> None Set the optimiser's algorithm. .. py:method:: _set_objective_function(func: bluemira.optimisation.typed.ObjectiveCallable, df: bluemira.optimisation.typed.OptimiserCallable | None, n_variables: int) -> None Wrap and set the objective function. .. py:method:: _set_termination_conditions(opt_conditions: collections.abc.Mapping[str, int | float]) -> None Validate and set the termination conditions. .. py:method:: _set_algorithm_parameters(opt_parameters: collections.abc.Mapping) -> None .. py:function:: _check_algorithm(algorithm: bluemira.optimisation._algorithm.AlgorithmType) -> bluemira.optimisation._algorithm.Algorithm Validate, and convert, the given algorithm. :returns: validated and converted algorithm .. py:function:: _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. :param opt: The optimiser to check :param algorithm: The optimisation algorithm