Skip to content

Commit

Permalink
Add tests for fallback subsolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
timmens committed Jan 16, 2024
1 parent 268c979 commit cc3ddb7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/tranquilo/subsolvers/fallback_subsolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def robust_cube_solver(model, x_candidate, radius=1.0):
}


def robust_cube_solver_multistart(model, x_candidate, lower_bounds, upper_bounds):
def robust_cube_solver_multistart(model, x_candidate):
np.random.seed(12345)
start_values = draw_exploration_sample(
x=x_candidate,
lower=lower_bounds,
upper=upper_bounds,
lower=-np.ones(len(x_candidate)),
upper=np.ones(len(x_candidate)),
n_samples=100,
sampling_distribution="uniform",
sampling_method="sobol",
Expand Down Expand Up @@ -142,15 +142,19 @@ def robust_sphere_solver_norm_constraint(model, x_candidate):
"""
crit, grad = _get_crit_and_grad(model)
constraints = _get_constraints()
constraint = _get_constraint()

lower_bounds = -np.ones(len(x_candidate))
upper_bounds = np.ones(len(x_candidate))

res = minimize(
crit,
x_candidate,
method="SLSQP",
bounds=Bounds(lower_bounds, upper_bounds),
jac=grad,
constraints=constraints,
options={"maxiter": len(x_candidate)},
constraints=constraint,
options={"maxiter": 3 * len(x_candidate)},
)

return {
Expand Down Expand Up @@ -179,18 +183,17 @@ def _grad(x, g, h):
return crit, grad


def _get_constraints():
def _get_constraint():
def _constr_fun(x):
return x @ x

def _constr_jac(x):
return 2 * x

constr = NonlinearConstraint(
return NonlinearConstraint(
fun=_constr_fun,
lb=-np.inf,
ub=1,
jac=_constr_jac,
keep_feasible=True,
)

return (constr,)
58 changes: 58 additions & 0 deletions tests/subsolvers/test_fallback_solvers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
import numpy as np
from numpy.testing import assert_array_almost_equal as aaae
from tranquilo.models import ScalarModel

from tranquilo.subsolvers.fallback_subsolvers import (
robust_cube_solver,
robust_cube_solver_multistart,
robust_sphere_solver_inscribed_cube,
robust_sphere_solver_norm_constraint,
robust_sphere_solver_reparametrized,
)

FALLBACK_SPHERE_SOLVERS = [
robust_sphere_solver_inscribed_cube,
robust_sphere_solver_reparametrized,
robust_sphere_solver_norm_constraint,
]

FALLBACK_CUBE_SOLVERS = [
robust_cube_solver,
robust_cube_solver_multistart,
]


@pytest.fixture
def model():
"""Simple quadratic scalar model.
- Minimum in cube is at (1, 1)
- Minimum in sphere is at (1/sqrt(2), 1/sqrt(2))
"""
return ScalarModel(
intercept=0.0,
linear_terms=-np.ones(2),
square_terms=-np.eye(2),
)


@pytest.mark.parametrize("solver", FALLBACK_SPHERE_SOLVERS)
def test_fallback_sphere_solver(solver, model):
x_candidate = np.zeros(2)

calculated = solver(model, x_candidate)
expected = np.ones(2) / np.sqrt(2)

aaae(calculated["x"], expected)


@pytest.mark.parametrize("solver", FALLBACK_CUBE_SOLVERS)
def test_fallback_cube_solver(solver, model):
x_candidate = np.zeros(2)

calculated = solver(model, x_candidate)
expected = np.ones(2)

aaae(calculated["x"], expected)

0 comments on commit cc3ddb7

Please sign in to comment.