Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions src/optimagic/optimizers/scipy_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,18 +1550,24 @@ def _int_if_not_none(value: SupportsInt | None) -> int | None:
def _get_scipy_constraints(constraints):
"""Transform internal nonlinear constraints to scipy readable format.

This format is currently only used by scipy_trust_constr.
This format is used by scipy_trust_constr and scipy_differential_evolution.

"""
scipy_constraints = [_internal_to_scipy_constraint(c) for c in constraints]
return scipy_constraints


def _internal_to_scipy_constraint(c):
# Equality constraints are relaxed to |g(x)| <= tol; exact equality is never met
# by the population of stochastic algorithms such as differential evolution.
if c["type"] == "eq":
lower, upper = -c["tol"], c["tol"]
else:
lower, upper = 0.0, np.inf
new_constr = NonlinearConstraint(
fun=c["fun"],
lb=np.zeros(c["n_constr"]),
ub=np.tile(np.inf, c["n_constr"]),
lb=np.full(c["n_constr"], lower),
ub=np.full(c["n_constr"], upper),
jac=c["jac"],
)
return new_constr
Expand Down Expand Up @@ -1889,16 +1895,12 @@ class ScipyDifferentialEvolution(Algorithm):
comes at the cost of requiring more criterion evaluations than gradient-based
methods. It is a good choice for global optimization of non-smooth or noisy
problems with a moderate number of parameters. Finite bounds are required for
all parameters. Nonlinear inequality constraints are supported.
all parameters. Nonlinear equality and inequality constraints are supported;
equality constraints are enforced up to the constraint's ``tol``.

The start values are not used by the algorithm, but they are still necessary
for optimagic to infer the number and format of the parameters.

.. warning::
optimagic currently passes all nonlinear constraints to SciPy as inequality
constraints. An equality constraint ``g(x) = value`` is therefore only
enforced as ``g(x) >= value``.

.. note::
Due to optimagic's general parameter format, the SciPy arguments
``integrality`` and ``vectorized`` are not supported. The SciPy argument
Expand Down Expand Up @@ -2071,7 +2073,7 @@ def _solve_internal_problem(
is_global=True,
needs_jac=True,
needs_hess=False,
needs_bounds=False,
needs_bounds=True,
supports_parallelism=False,
supports_bounds=True,
supports_infinite_bounds=True,
Expand All @@ -2095,9 +2097,9 @@ class ScipySHGO(Algorithm):
guarantees of convergence to the global minimum in finite time for Lipschitz
smooth functions.

.. warning::
SciPy requires bounds for all parameters; without bounds, the optimization
fails. SciPy replaces infinite bounds by -1e50 and 1e50.
.. note::
Bounds are required for all parameters. SciPy replaces infinite bounds by
-1e50 and 1e50.

.. note::
If nonlinear constraints are used, only the local algorithms "COBYLA",
Expand Down Expand Up @@ -2260,11 +2262,6 @@ class ScipySHGO(Algorithm):
def _solve_internal_problem(
self, problem: InternalOptimizationProblem, x0: NDArray[np.float64]
) -> InternalOptimizeResult:
if self.local_algorithm == "COBYLA":
nonlinear_constraints = equality_as_inequality_constraints(
problem.nonlinear_constraints
)

nonlinear_constraints = vector_as_list_of_scalar_constraints(
problem.nonlinear_constraints
)
Expand Down
27 changes: 27 additions & 0 deletions tests/optimagic/optimizers/test_scipy_optimizers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import numpy as np
import pytest
from numpy.testing import assert_array_almost_equal as aaae

import optimagic as om
from optimagic.exceptions import IncompleteBoundsError


def test_differential_evolution_respects_equality_constraint():
# The constraint is not binding if it is treated as x[0] + x[1] >= -1.
res = om.minimize(
fun=lambda x: x @ x,
params=np.array([1.0, 2.0]),
algorithm=om.algos.scipy_differential_evolution(seed=0),
bounds=om.Bounds(lower=np.full(2, -5.0), upper=np.full(2, 5.0)),
constraints=om.NonlinearConstraint(func=lambda x: x[0] + x[1], value=-1.0),
)
aaae(res.params, np.array([-0.5, -0.5]), decimal=4)


def test_shgo_without_bounds_raises():
with pytest.raises(IncompleteBoundsError):
om.minimize(
fun=lambda x: x @ x,
params=np.array([1.0, 2.0]),
algorithm="scipy_shgo",
)
Loading