Source code for optimagic.optimization.optimize

"""Public functions for optimization.

This module defines the public functions `maximize` and `minimize` that will be called
by users.

Internally, `maximize` and `minimize` just call `create_optimization_problem` with
all arguments and add the `direction`. In `create_optimization_problem`, the user input
is consolidated and converted to stricter types.  The resulting `OptimizationProblem`
is then passed to `_optimize` which handles the optimization logic.

`_optimize` processes the optimization problem and performs the actual optimization.

"""

from __future__ import annotations

from pathlib import Path
from typing import Any, Callable, Literal, Sequence, Type, cast

import numpy as np
import pandas as pd
from numpy.typing import NDArray
from scipy.optimize import Bounds as ScipyBounds

from optimagic.batch_evaluators import process_batch_evaluator
from optimagic.constraints import Constraint, NonlinearConstraint
from optimagic.differentiation.numdiff_options import NumdiffOptions, NumdiffOptionsDict
from optimagic.exceptions import (
    IncompleteBoundsError,
    InvalidFunctionError,
)
from optimagic.logging.logger import LogReader, LogStore
from optimagic.logging.types import ProblemInitialization
from optimagic.optimization.algorithm import Algorithm
from optimagic.optimization.create_optimization_problem import (
    OptimizationProblem,
    create_optimization_problem,
)
from optimagic.optimization.error_penalty import get_error_penalty_function
from optimagic.optimization.fun_value import FunctionValue, SpecificFunctionValue
from optimagic.optimization.internal_optimization_problem import (
    InternalBounds,
    InternalOptimizationProblem,
)
from optimagic.optimization.multistart import (
    run_multistart_optimization,
)
from optimagic.optimization.multistart_options import (
    MultistartOptions,
    MultistartOptionsDict,
    get_internal_multistart_options_from_public,
)
from optimagic.optimization.optimization_logging import log_scheduled_steps_and_get_ids
from optimagic.optimization.optimize_result import OptimizeResult
from optimagic.optimization.process_results import (
    ExtraResultFields,
    process_multistart_result,
    process_single_result,
)
from optimagic.parameters.bounds import Bounds
from optimagic.parameters.conversion import (
    Converter,
    get_converter,
)
from optimagic.parameters.nonlinear_constraints import process_nonlinear_constraints
from optimagic.parameters.scaling import ScalingOptions, ScalingOptionsDict
from optimagic.pytree import leaf_names, tree_leaves
from optimagic.typing import (
    AggregationLevel,
    Direction,
    ErrorHandling,
    ErrorHandlingLiteral,
    NonNegativeFloat,
    PyTree,
    PyTreeNamespace,
)

FunType = Callable[..., float | PyTree | FunctionValue]
AlgorithmType = str | Algorithm | Type[Algorithm]
ConstraintsType = Constraint | dict[str, Any] | Sequence[Constraint | dict[str, Any]]
JacType = Callable[..., PyTree]
FunAndJacType = Callable[..., tuple[float | PyTree | FunctionValue, PyTree]]
HessType = Callable[..., PyTree]
CallbackType = Callable[[PyTree], None]

CriterionType = Callable[..., float | dict[str, Any]]
CriterionAndDerivativeType = Callable[..., tuple[float | dict[str, Any], PyTree]]

MAX_PATHS_IN_ERROR_MESSAGE = 20
"""Maximal number of paths of non-finite entries that are listed in an error."""

MAX_ENTRIES_SHOWN_IN_FULL = 50
"""Numpy arrays and pandas objects with more entries are abbreviated in errors."""


from optimagic.logging.logger import LogOptions


[docs] def maximize( fun: FunType | CriterionType | None = None, params: PyTree | None = None, algorithm: AlgorithmType | None = None, *, bounds: Bounds | ScipyBounds | Sequence[tuple[float, float]] | None = None, constraints: ConstraintsType | None = None, fun_kwargs: dict[str, Any] | None = None, algo_options: dict[str, Any] | None = None, jac: JacType | Sequence[JacType] | Literal[True] | None = None, jac_kwargs: dict[str, Any] | None = None, fun_and_jac: FunAndJacType | CriterionAndDerivativeType | Sequence[FunAndJacType] | None = None, fun_and_jac_kwargs: dict[str, Any] | None = None, numdiff_options: NumdiffOptions | NumdiffOptionsDict | None = None, # TODO: add typed-dict support? logging: bool | str | Path | LogOptions | dict[str, Any] | None = None, error_handling: ErrorHandling | ErrorHandlingLiteral = ErrorHandling.RAISE, error_penalty: dict[str, float] | None = None, scaling: bool | ScalingOptions | ScalingOptionsDict = False, multistart: bool | MultistartOptions | MultistartOptionsDict = False, collect_history: bool = True, skip_checks: bool = False, # scipy aliases x0: PyTree | None = None, method: str | None = None, args: tuple[Any] | None = None, # scipy arguments that are not yet supported hess: HessType | None = None, hessp: HessType | None = None, callback: CallbackType | None = None, # scipy arguments that will never be supported options: dict[str, Any] | None = None, tol: NonNegativeFloat | None = None, # deprecated arguments criterion: CriterionType | None = None, criterion_kwargs: dict[str, Any] | None = None, derivative: JacType | None = None, derivative_kwargs: dict[str, Any] | None = None, criterion_and_derivative: CriterionAndDerivativeType | None = None, criterion_and_derivative_kwargs: dict[str, Any] | None = None, log_options: dict[str, Any] | None = None, lower_bounds: PyTree | None = None, upper_bounds: PyTree | None = None, soft_lower_bounds: PyTree | None = None, soft_upper_bounds: PyTree | None = None, scaling_options: dict[str, Any] | None = None, multistart_options: dict[str, Any] | None = None, ) -> OptimizeResult: """Maximize fun using algorithm subject to constraints. Args: fun: The objective function of a scalar, least-squares or likelihood optimization problem. Non-scalar objective functions have to be marked with the `mark.likelihood` or `mark.least_squares` decorators. `fun` maps params and fun_kwargs to an objective value. See :ref:`how-to-fun` for details and examples. params: The start parameters for the optimization. Params can be numpy arrays, dictionaries, pandas.Series, pandas.DataFrames, NamedTuples, floats, lists, and any nested combination thereof. See :ref:`params` for details and examples. The objective function has to be finite at the start parameters. This also holds for global optimizers and multistart optimizations. algorithm: The optimization algorithm to use. Can be a string, subclass of :class:`optimagic.Algorithm` or an instance of a subclass of :class:`optimagic.Algorithm`. For guidelines on how to choose an algorithm see :ref:`how-to-select-algorithms`. For examples of specifying and configuring algorithms see :ref:`specify-algorithm`. bounds: Lower and upper bounds on the parameters. The most general and preferred way to specify bounds is an :class:`optimagic.Bounds` object that collects lower, upper, soft_lower and soft_upper bounds. The soft bounds are used for sampling based optimizers but are not enforced during optimization. Each bound type mirrors the structure of params. See :ref:`how-to-bounds` for details and examples. If params is a flat numpy array, you can also provide bounds via any format that is supported by scipy.optimize.minimize. constraints: Constraints for the optimization problem. Constraints can be specified as a single :class:`optimagic.Constraint` object, a list of Constraint objects. For details and examples check :ref:`constraints`. fun_kwargs: Additional keyword arguments for the objective function. algo_options: Additional options for the optimization algorithm. `algo_options` is an alternative to configuring algorithm objects directly. See :ref:`list_of_algorithms` for supported options of each algorithm. jac: The first derivative of `fun`. Providing a closed form derivative can be a great way to speed up your optimization. The easiest way to get a derivative for your objective function are autodiff frameworks like JAX. If you provide a sequence of derivatives, the one that matches the aggregation level of the algorithm is used. For compatibility with scipy, `jac=True` means that `fun` returns a tuple of the function value and its derivative. For details and examples see :ref:`how-to-jac`. jac_kwargs: Additional keyword arguments for `jac`. fun_and_jac: A function that returns both the objective value and the derivative. This can be used do exploit synergies in the calculation of the function value and its derivative. If you provide a sequence, the element that matches the aggregation level of the algorithm is used and `fun` must also be provided. For details and examples see :ref:`how-to-jac`. fun_and_jac_kwargs: Additional keyword arguments for `fun_and_jac`. numdiff_options: Options for numerical differentiation. Can be a dictionary or an instance of :class:`optimagic.NumdiffOptions`. logging: If None, no logging is used. If a str or pathlib.Path is provided, it is interpreted as path to an sqlite3 file (which typically has the file extension ``.db``. If the file does not exist, it will be created. and the optimization history will be stored in that database. For more customization, provide LogOptions. For details and examples see :ref:`how-to-logging`. error_handling: If "raise" or ErrorHandling.RAISE, exceptions that occur during the optimization are raised and the optimization is stopped. If "continue" or ErrorHandling.CONTINUE, exceptions are caught and the function value and its derivative are replaced by penalty values. The penalty values are constructed such that the optimizer is guided back towards the start parameters until a feasible region is reached and then continues the optimization from there. For details see :ref:`how-to-errors`. error_penalty: A dictionary with the keys "slope" and "constant" that influences the magnitude of the penalty values. For maximization problems both should be negative. For details see :ref:`error-handling-explanation`. scaling: If None or False, the parameter space is not rescaled. If True, a heuristic is used to improve the conditioning of the optimization problem. To choose which heuristic is used and to customize the scaling, provide a dictionary or an instance of :class:`optimagic.ScalingOptions`. For details and examples see :ref:`scaling`. multistart: If False, no multistart approach is used. If True, the optimization is restarted from multiple starting points. Note that this requires finite bounds or soft bounds for all parameters. To customize the multistart approach, provide a dictionary or an instance of :class:`optimagic.MultistartOptions`. For details and examples see :ref:`how-to-multistart`. collect_history: If True, the optimization history is collected and returned in the OptimizeResult. This is required to create `criterion_plot` or `params_plot` from an OptimizeResult. skip_checks: If True, some checks are skipped to speed up the optimization. This is only relevant if your objective function is very fast, i.e. runs in a few microseconds. x0: Alias for params for scipy compatibility. method: Alternative to algorithm for scipy compatibility. With `method` you can select scipy optimizers via their original scipy name. args: Alternative to fun_kwargs for scipy compatibility. hess: Not yet supported. hessp: Not yet supported. callback: Experimental; its behavior might change in upcoming releases. Optional callable called after each objective evaluation with signature ``callback(xk)``, where ``xk`` holds the current parameters (a PyTree with the same structure as ``params``). ``xk`` is not copied, so the callback must not modify it in place. The callback is not called during the exploration phase of a multistart optimization. Raising ``StopIteration`` to abort optimization is not yet supported. The ``callback(intermediate_result)`` interface is not yet supported. options: Not yet supported. tol: Not yet supported. criterion: Deprecated. Use fun instead. criterion_kwargs: Deprecated. Use fun_kwargs instead. derivative: Deprecated. Use jac instead. derivative_kwargs: Deprecated. Use jac_kwargs instead. criterion_and_derivative: Deprecated. Use fun_and_jac instead. criterion_and_derivative_kwargs: Deprecated. Use fun_and_jac_kwargs instead. lower_bounds: Deprecated. Use bounds instead. upper_bounds: Deprecated. Use bounds instead. soft_lower_bounds: Deprecated. Use bounds instead. soft_upper_bounds: Deprecated. Use bounds instead. scaling_options: Deprecated. Use scaling instead. multistart_options: Deprecated. Use multistart instead. """ problem = create_optimization_problem( direction=Direction.MAXIMIZE, fun=fun, params=params, bounds=bounds, algorithm=algorithm, fun_kwargs=fun_kwargs, constraints=constraints, algo_options=algo_options, jac=jac, jac_kwargs=jac_kwargs, fun_and_jac=fun_and_jac, fun_and_jac_kwargs=fun_and_jac_kwargs, numdiff_options=numdiff_options, logging=logging, log_options=log_options, error_handling=error_handling, error_penalty=error_penalty, scaling=scaling, multistart=multistart, collect_history=collect_history, skip_checks=skip_checks, # scipy aliases x0=x0, method=method, args=args, # scipy arguments that are not yet supported hess=hess, hessp=hessp, callback=callback, # scipy arguments that will never be supported options=options, tol=tol, # deprecated arguments criterion=criterion, criterion_kwargs=criterion_kwargs, derivative=derivative, derivative_kwargs=derivative_kwargs, criterion_and_derivative=criterion_and_derivative, criterion_and_derivative_kwargs=criterion_and_derivative_kwargs, lower_bounds=lower_bounds, upper_bounds=upper_bounds, soft_lower_bounds=soft_lower_bounds, soft_upper_bounds=soft_upper_bounds, scaling_options=scaling_options, multistart_options=multistart_options, ) return _optimize(problem)
[docs] def minimize( fun: FunType | CriterionType | None = None, params: PyTree | None = None, algorithm: AlgorithmType | None = None, *, bounds: Bounds | ScipyBounds | Sequence[tuple[float, float]] | None = None, constraints: ConstraintsType | None = None, fun_kwargs: dict[str, Any] | None = None, algo_options: dict[str, Any] | None = None, jac: JacType | Sequence[JacType] | Literal[True] | None = None, jac_kwargs: dict[str, Any] | None = None, fun_and_jac: FunAndJacType | CriterionAndDerivativeType | Sequence[FunAndJacType] | None = None, fun_and_jac_kwargs: dict[str, Any] | None = None, numdiff_options: NumdiffOptions | NumdiffOptionsDict | None = None, # TODO: add typed-dict support? logging: bool | str | Path | LogOptions | dict[str, Any] | None = None, error_handling: ErrorHandling | ErrorHandlingLiteral = ErrorHandling.RAISE, error_penalty: dict[str, float] | None = None, scaling: bool | ScalingOptions | ScalingOptionsDict = False, multistart: bool | MultistartOptions | MultistartOptionsDict = False, collect_history: bool = True, skip_checks: bool = False, # scipy aliases x0: PyTree | None = None, method: str | None = None, args: tuple[Any] | None = None, # scipy arguments that are not yet supported hess: HessType | None = None, hessp: HessType | None = None, callback: CallbackType | None = None, # scipy arguments that will never be supported options: dict[str, Any] | None = None, tol: NonNegativeFloat | None = None, # deprecated arguments criterion: CriterionType | None = None, criterion_kwargs: dict[str, Any] | None = None, derivative: JacType | None = None, derivative_kwargs: dict[str, Any] | None = None, criterion_and_derivative: CriterionAndDerivativeType | None = None, criterion_and_derivative_kwargs: dict[str, Any] | None = None, log_options: dict[str, Any] | None = None, lower_bounds: PyTree | None = None, upper_bounds: PyTree | None = None, soft_lower_bounds: PyTree | None = None, soft_upper_bounds: PyTree | None = None, scaling_options: dict[str, Any] | None = None, multistart_options: dict[str, Any] | None = None, ) -> OptimizeResult: """Minimize criterion using algorithm subject to constraints. Args: fun: The objective function of a scalar, least-squares or likelihood optimization problem. Non-scalar objective functions have to be marked with the `mark.likelihood` or `mark.least_squares` decorators. `fun` maps params and fun_kwargs to an objective value. See :ref:`how-to-fun` for details and examples. params: The start parameters for the optimization. Params can be numpy arrays, dictionaries, pandas.Series, pandas.DataFrames, NamedTuples, floats, lists, and any nested combination thereof. See :ref:`params` for details and examples. The objective function has to be finite at the start parameters. This also holds for global optimizers and multistart optimizations. algorithm: The optimization algorithm to use. Can be a string, subclass of :class:`optimagic.Algorithm` or an instance of a subclass of :class:`optimagic.Algorithm`. For guidelines on how to choose an algorithm see :ref:`how-to-select-algorithms`. For examples of specifying and configuring algorithms see :ref:`specify-algorithm`. bounds: Lower and upper bounds on the parameters. The most general and preferred way to specify bounds is an :class:`optimagic.Bounds` object that collects lower, upper, soft_lower and soft_upper bounds. The soft bounds are used for sampling based optimizers but are not enforced during optimization. Each bound type mirrors the structure of params. See :ref:`how-to-bounds` for details and examples. If params is a flat numpy array, you can also provide bounds via any format that is supported by scipy.optimize.minimize. constraints: Constraints for the optimization problem. Constraints can be specified as a single :class:`optimagic.Constraint` object, a list of Constraint objects. For details and examples check :ref:`constraints`. fun_kwargs: Additional keyword arguments for the objective function. algo_options: Additional options for the optimization algorithm. `algo_options` is an alternative to configuring algorithm objects directly. See :ref:`list_of_algorithms` for supported options of each algorithm. jac: The first derivative of `fun`. Providing a closed form derivative can be a great way to speed up your optimization. The easiest way to get a derivative for your objective function are autodiff frameworks like JAX. If you provide a sequence of derivatives, the one that matches the aggregation level of the algorithm is used. For compatibility with scipy, `jac=True` means that `fun` returns a tuple of the function value and its derivative. For details and examples see :ref:`how-to-jac`. jac_kwargs: Additional keyword arguments for `jac`. fun_and_jac: A function that returns both the objective value and the derivative. This can be used do exploit synergies in the calculation of the function value and its derivative. If you provide a sequence, the element that matches the aggregation level of the algorithm is used and `fun` must also be provided. For details and examples see :ref:`how-to-jac`. fun_and_jac_kwargs: Additional keyword arguments for `fun_and_jac`. numdiff_options: Options for numerical differentiation. Can be a dictionary or an instance of :class:`optimagic.NumdiffOptions`. logging: If None, no logging is used. If a str or pathlib.Path is provided, it is interpreted as path to an sqlite3 file (which typically has the file extension ``.db``. If the file does not exist, it will be created. and the optimization history will be stored in that database. For more customization, provide LogOptions. For details and examples see :ref:`how-to-logging`. error_handling: If "raise" or ErrorHandling.RAISE, exceptions that occur during the optimization are raised and the optimization is stopped. If "continue" or ErrorHandling.CONTINUE, exceptions are caught and the function value and its derivative are replaced by penalty values. The penalty values are constructed such that the optimizer is guided back towards the start parameters until a feasible region is reached and then continues the optimization from there. For details see :ref:`how-to-errors`. error_penalty: A dictionary with the keys "slope" and "constant" that influences the magnitude of the penalty values. For minimization problems both should be positive. For details see :ref:`error-handling-explanation`. scaling: If None or False, the parameter space is not rescaled. If True, a heuristic is used to improve the conditioning of the optimization problem. To choose which heuristic is used and to customize the scaling, provide a dictionary or an instance of :class:`optimagic.ScalingOptions`. For details and examples see :ref:`scaling`. multistart: If False, no multistart approach is used. If True, the optimization is restarted from multiple starting points. Note that this requires finite bounds or soft bounds for all parameters. To customize the multistart approach, provide a dictionary or an instance of :class:`optimagic.MultistartOptions`. For details and examples see :ref:`how-to-multistart`. collect_history: If True, the optimization history is collected and returned in the OptimizeResult. This is required to create `criterion_plot` or `params_plot` from an OptimizeResult. skip_checks: If True, some checks are skipped to speed up the optimization. This is only relevant if your objective function is very fast, i.e. runs in a few microseconds. x0: Alias for params for scipy compatibility. method: Alternative to algorithm for scipy compatibility. With `method` you can select scipy optimizers via their original scipy name. args: Alternative to fun_kwargs for scipy compatibility. hess: Not yet supported. hessp: Not yet supported. callback: Experimental; its behavior might change in upcoming releases. Optional callable called after each objective evaluation with signature ``callback(xk)``, where ``xk`` holds the current parameters (a PyTree with the same structure as ``params``). ``xk`` is not copied, so the callback must not modify it in place. The callback is not called during the exploration phase of a multistart optimization. Raising ``StopIteration`` to abort optimization is not yet supported. The ``callback(intermediate_result)`` interface is not yet supported. options: Not yet supported. tol: Not yet supported. criterion: Deprecated. Use fun instead. criterion_kwargs: Deprecated. Use fun_kwargs instead. derivative: Deprecated. Use jac instead. derivative_kwargs: Deprecated. Use jac_kwargs instead. criterion_and_derivative: Deprecated. Use fun_and_jac instead. criterion_and_derivative_kwargs: Deprecated. Use fun_and_jac_kwargs instead. lower_bounds: Deprecated. Use bounds instead. upper_bounds: Deprecated. Use bounds instead. soft_lower_bounds: Deprecated. Use bounds instead. soft_upper_bounds: Deprecated. Use bounds instead. scaling_options: Deprecated. Use scaling instead. multistart_options: Deprecated. Use multistart instead. """ problem = create_optimization_problem( direction=Direction.MINIMIZE, fun=fun, params=params, algorithm=algorithm, bounds=bounds, fun_kwargs=fun_kwargs, constraints=constraints, algo_options=algo_options, jac=jac, jac_kwargs=jac_kwargs, fun_and_jac=fun_and_jac, fun_and_jac_kwargs=fun_and_jac_kwargs, numdiff_options=numdiff_options, logging=logging, error_handling=error_handling, error_penalty=error_penalty, scaling=scaling, multistart=multistart, collect_history=collect_history, skip_checks=skip_checks, # scipy aliases x0=x0, method=method, args=args, # scipy arguments that are not yet supported hess=hess, hessp=hessp, callback=callback, # scipy arguments that will never be supported options=options, tol=tol, # deprecated arguments criterion=criterion, criterion_kwargs=criterion_kwargs, derivative=derivative, derivative_kwargs=derivative_kwargs, criterion_and_derivative=criterion_and_derivative, criterion_and_derivative_kwargs=criterion_and_derivative_kwargs, lower_bounds=lower_bounds, log_options=log_options, upper_bounds=upper_bounds, soft_lower_bounds=soft_lower_bounds, soft_upper_bounds=soft_upper_bounds, scaling_options=scaling_options, multistart_options=multistart_options, ) return _optimize(problem)
def _optimize(problem: OptimizationProblem) -> OptimizeResult: """Solve an optimization problem.""" # ================================================================================== # Split constraints into nonlinear and reparametrization parts # ================================================================================== constraints = problem.constraints nonlinear_constraints = [ c for c in constraints if isinstance(c, NonlinearConstraint) ] if nonlinear_constraints: if not problem.algorithm.algo_info.supports_nonlinear_constraints: raise ValueError( f"Algorithm {problem.algorithm.name} does not support " "nonlinear constraints." ) # the following constraints will be handled via reparametrization constraints = [c for c in constraints if not isinstance(c, NonlinearConstraint)] # ================================================================================== # Do first evaluation of user provided functions # ================================================================================== first_crit_eval = problem.fun_eval _check_fun_is_finite_at_start(first_crit_eval) # do first derivative evaluation (if given and used by the algorithm) derivative_evals = {} if problem.algorithm.algo_info.needs_jac: if problem.jac is not None: try: derivative_evals["jac"] = problem.jac(problem.params) except (KeyboardInterrupt, SystemExit): raise except Exception as e: msg = "Error while evaluating derivative at start params." raise InvalidFunctionError(msg) from e if problem.fun_and_jac is not None: if problem.fun_and_jac_eval is not None: # fun_and_jac was already evaluated to get the first objective value derivative_evals["fun_and_jac"] = problem.fun_and_jac_eval else: try: derivative_evals["fun_and_jac"] = problem.fun_and_jac( problem.params )[1] except (KeyboardInterrupt, SystemExit): raise except Exception as e: msg = ( "Error while evaluating criterion_and_derivative at start " "params." ) raise InvalidFunctionError(msg) from e used_deriv = derivative_evals.get("jac", derivative_evals.get("fun_and_jac")) # ================================================================================== # Get the converter (for tree flattening, constraints and scaling) # ================================================================================== converter, internal_params = get_converter( params=problem.params, constraints=constraints, bounds=problem.bounds, func_eval=first_crit_eval.value, solver_type=problem.algorithm.algo_info.solver_type, scaling=problem.scaling, derivative_eval=used_deriv, add_soft_bounds=problem.multistart is not None, ) _check_derivatives_are_finite_at_start( derivative_evals, converter=converter, x=internal_params.values ) # ================================================================================== # initialize the log database # ================================================================================== logger: LogStore[Any, Any] | None if problem.logging: logger = LogStore.from_options(problem.logging) problem_data = ProblemInitialization(problem.direction, problem.params) logger.problem_store.insert(problem_data) else: logger = None # ================================================================================== # Strict checking if bounds are required and infinite values in bounds # ================================================================================== if problem.algorithm.algo_info.supports_bounds: bounds_missing = ( internal_params.lower_bounds is None or internal_params.upper_bounds is None ) # Check for infinite values in bounds arrays (only possible in mixed cases now) infinite_values_in_bounds = False if internal_params.lower_bounds is not None: infinite_values_in_bounds |= np.isinf(internal_params.lower_bounds).any() if internal_params.upper_bounds is not None: infinite_values_in_bounds |= np.isinf(internal_params.upper_bounds).any() # Case 1: Algorithm needs bounds but none provided if problem.algorithm.algo_info.needs_bounds and bounds_missing: raise IncompleteBoundsError( f"Algorithm {problem.algorithm.name} requires bounds for all " "parameters. Please provide finite lower and upper bounds." ) # Case 2: Algorithm doesn't support infinite bounds but they are present if ( not problem.algorithm.algo_info.supports_infinite_bounds and infinite_values_in_bounds ): raise IncompleteBoundsError( f"Algorithm {problem.algorithm.name} does not support infinite bounds. " "Please provide finite bounds for all parameters." ) # ================================================================================== # Do some things that require internal parameters or bounds # ================================================================================== if converter.has_transforming_constraints and problem.multistart is not None: raise NotImplementedError( "multistart optimizations are not yet compatible with transforming " "constraints." ) # get error penalty function error_penalty_func = get_error_penalty_function( start_x=internal_params.values, start_criterion=first_crit_eval, error_penalty=problem.error_penalty, solver_type=problem.algorithm.algo_info.solver_type, direction=problem.direction, ) # process nonlinear constraints; converting to dicts is a temporary seam during # the constraints refactoring, as the processing is still dict-based internal_nonlinear_constraints = process_nonlinear_constraints( nonlinear_constraints=[c._to_dict() for c in nonlinear_constraints], params=problem.params, bounds=problem.bounds, converter=converter, numdiff_options=problem.numdiff_options, skip_checks=problem.skip_checks, ) x = internal_params.values internal_bounds = InternalBounds( lower=internal_params.lower_bounds, upper=internal_params.upper_bounds, ) # ================================================================================== # Create a batch evaluator # ================================================================================== # TODO: Make batch evaluator an argument of maximize and minimize and move this # to create_optimization_problem batch_evaluator = process_batch_evaluator("joblib") # ================================================================================== # Create the InternalOptimizationProblem # ================================================================================== internal_problem = InternalOptimizationProblem( fun=problem.fun, jac=problem.jac, fun_and_jac=problem.fun_and_jac, converter=converter, solver_type=problem.algorithm.algo_info.solver_type, direction=problem.direction, bounds=internal_bounds, numdiff_options=problem.numdiff_options, error_handling=problem.error_handling, error_penalty_func=error_penalty_func, batch_evaluator=batch_evaluator, # TODO: Actually pass through linear constraints if possible linear_constraints=None, nonlinear_constraints=internal_nonlinear_constraints, logger=logger, callback=problem.callback, ) # ================================================================================== # Do actual optimization # ================================================================================== if problem.multistart is None: steps = [{"type": "optimization", "name": "optimization"}] # TODO: Actually use the step ids step_id = log_scheduled_steps_and_get_ids( # noqa: F841 steps=steps, logger=logger, )[0] raw_res = problem.algorithm.solve_internal_problem(internal_problem, x, step_id) else: multistart_options = get_internal_multistart_options_from_public( options=problem.multistart, params=problem.params, params_to_internal=converter.params_to_internal, ) sampling_bounds = InternalBounds( lower=internal_params.soft_lower_bounds, upper=internal_params.soft_upper_bounds, ) raw_res = run_multistart_optimization( local_algorithm=problem.algorithm, internal_problem=internal_problem, x=x, sampling_bounds=sampling_bounds, options=multistart_options, logger=logger, error_handling=problem.error_handling, ) # ================================================================================== # Process the result # ================================================================================== _scalar_start_criterion = cast( float, first_crit_eval.internal_value(AggregationLevel.SCALAR) ) log_reader: LogReader[Any] | None extra_fields = ExtraResultFields( start_fun=_scalar_start_criterion, start_params=problem.params, algorithm=problem.algorithm.algo_info.name, direction=problem.direction, n_free=internal_params.free_mask.sum(), ) if problem.multistart is None: res = process_single_result( raw_res=raw_res, converter=converter, solver_type=problem.algorithm.algo_info.solver_type, extra_fields=extra_fields, ) else: res = process_multistart_result( raw_res=raw_res, converter=converter, solver_type=problem.algorithm.algo_info.solver_type, extra_fields=extra_fields, ) if logger is not None: assert problem.logging is not None logger.close() log_reader = LogReader.from_options(problem.logging) else: log_reader = None res.logger = log_reader return res def _check_fun_is_finite_at_start(fun_eval: SpecificFunctionValue) -> None: """Raise an error if the objective is not finite at the start parameters. No optimizer can make progress from such a point, and the penalty used with ``error_handling="continue"`` is derived from the start value. """ if not np.isfinite(fun_eval.internal_value(AggregationLevel.SCALAR)): details = _describe_non_finite_entries( fun_eval.value, label="returned by the objective function", reason_if_all_finite=( "the sum of the contributions or squared residuals is not" ), ) msg = ( "The objective function is not finite (inf or nan) at the start params. " "Choose start params at which the objective function is finite. This is " "required for all algorithms, including global optimizers and multistart " f"optimizations.{details}\n\nThe objective function returned:" f"\n\n{_format_abbreviated(fun_eval.value)}" ) raise InvalidFunctionError(msg) def _check_derivatives_are_finite_at_start( derivative_evals: dict[str, PyTree], converter: Converter, x: NDArray[np.float64], ) -> None: """Raise an error if a user provided derivative is not finite at the start params. The check is done on the derivative with respect to the internal parameters, because this is what the optimizer sees. Args: derivative_evals: Derivatives at the start params by the name of the argument through which they were provided. converter: Converter between external and internal parameters. x: Internal start parameters. Has shape (n_internal,). """ for name, derivative_eval in derivative_evals.items(): internal = converter.derivative_to_internal(derivative_eval, x) if not np.all(np.isfinite(internal)): details = _describe_non_finite_entries( derivative_eval, label="of the derivative", reason_if_all_finite=( "the derivative is not finite after the constraints and the " "scaling are applied" ), ) msg = ( f"The derivative provided via {name} is not finite (inf or nan) at the " "start params. Check the derivative function or choose start params " f"at which it is finite.{details}\n\n{name} returned the following " f"derivative:\n\n{_format_abbreviated(derivative_eval)}" ) raise InvalidFunctionError(msg) def _describe_non_finite_entries( tree: PyTree, label: str, reason_if_all_finite: str ) -> str: """Describe which entries of a pytree are not finite. The entries are identified by their paths, i.e. the names that optimagic uses for the entries of a pytree. For a one-dimensional numpy array, the paths are the positions of the entries. Args: tree: A pytree with numbers, e.g. the output of the objective function. label: Describes where the entries come from. It is placed after "entries" in the description. reason_if_all_finite: Describes what is not finite if all entries of the pytree are finite. Returns: A paragraph for an error message that starts with two line breaks. Empty if the pytree has just one entry. """ names = leaf_names(tree, namespace=PyTreeNamespace.VALUE) leaves = np.array( tree_leaves(tree, namespace=PyTreeNamespace.VALUE), dtype=np.float64 ) paths = [ name for name, is_finite in zip(names, np.isfinite(leaves), strict=True) if not is_finite ] n_entries = len(names) if n_entries == 1: return "" if not paths: out = f"All {n_entries} entries {label} are finite, but {reason_if_all_finite}." elif len(paths) == 1: out = ( f"1 of the {n_entries} entries {label} is not finite. Its path is: " f"{paths[0]}" ) else: shown = ", ".join(paths[:MAX_PATHS_IN_ERROR_MESSAGE]) n_omitted = len(paths) - MAX_PATHS_IN_ERROR_MESSAGE if n_omitted > 0: shown += f" (and {n_omitted} more)" out = ( f"{len(paths)} of the {n_entries} entries {label} are not finite. Their " f"paths are: {shown}" ) return f"\n\n{out}" def _format_abbreviated(value: PyTree) -> str: """Represent a pytree and abbreviate large numpy arrays and pandas objects.""" with ( np.printoptions(threshold=MAX_ENTRIES_SHOWN_IN_FULL), pd.option_context( "display.max_rows", MAX_ENTRIES_SHOWN_IN_FULL, "display.min_rows", 10, ), ): return repr(value)