Skip to content

Commit

Permalink
Adjust tests to new constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
AVHopp committed Sep 27, 2024
1 parent 9b7d3f4 commit d5758c5
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 72 deletions.
39 changes: 29 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import time
import warnings
from collections.abc import Sequence
from itertools import chain
from unittest.mock import Mock

Expand Down Expand Up @@ -44,6 +45,7 @@
SubSelectionCondition,
ThresholdCondition,
)
from baybe.constraints.continuous import ContinuousInterPointLinearConstraint
from baybe.kernels import MaternKernel
from baybe.objectives.desirability import DesirabilityObjective
from baybe.objectives.single import SingleTargetObjective
Expand Down Expand Up @@ -546,33 +548,42 @@ def custom_function(df: pd.DataFrame) -> pd.Series:
min_cardinality=1,
max_cardinality=2,
),
# Interpoint constraints using a single parameter
}
return [
c_item
for c_name, c_item in valid_constraints.items()
if c_name in constraint_names
]


@pytest.fixture(name="interpoint_constraints")
def fixture_interpoint_constraints(interpoint_constraints_names: list[str]):
valid_interpoint_constraints = {
"InterConstraint_1": ContinuousInterPointLinearEqualityConstraint(
parameters=["Conti_finite1"],
coefficients=[("Conti_finite1", 0, 1.0), ("Conti_finite1", 1, 3.0)],
coefficients=[1],
rhs=0.3,
),
"InterConstraint_2": ContinuousInterPointLinearInequalityConstraint(
parameters=["Conti_finite1"],
coefficients=[("Conti_finite1", 0, 1.0), ("Conti_finite1", 1, 3.0)],
coefficients=[2],
rhs=0.3,
),
# Interpoint constraints using multiple parameters
"InterConstraint_3": ContinuousInterPointLinearEqualityConstraint(
parameters=["Conti_finite1", "Conti_finite2"],
coefficients=[("Conti_finite1", 0, 1.0), ("Conti_finite2", 1, 3.0)],
coefficients=[1, 1],
rhs=0.3,
),
"InterConstraint_4": ContinuousInterPointLinearInequalityConstraint(
parameters=["Conti_finite1", "Conti_finite2"],
coefficients=[("Conti_finite1", 0, 1.0), ("Conti_finite2", 1, 3.0)],
coefficients=[2, -1],
rhs=0.3,
),
}
return [
c_item
for c_name, c_item in valid_constraints.items()
if c_name in constraint_names
for c_name, c_item in valid_interpoint_constraints.items()
if c_name in interpoint_constraints_names
]


Expand Down Expand Up @@ -910,7 +921,11 @@ def fixture_default_onnx_surrogate(onnx_str) -> CustomONNXSurrogate:
),
)
def run_iterations(
campaign: Campaign, n_iterations: int, batch_size: int, add_noise: bool = True
campaign: Campaign,
n_iterations: int,
batch_size: int,
add_noise: bool = True,
interpoint_constraints: Sequence[ContinuousInterPointLinearConstraint] = None,
) -> None:
"""Run a campaign for some fake iterations.
Expand All @@ -922,10 +937,14 @@ def run_iterations(
n_iterations: Number of iterations run.
batch_size: Number of recommended points per iteration.
add_noise: Flag whether measurement noise should be added every 2nd iteration.
interpoint_constraints: Sequence of inter-point constraints that need to be
respected during the recommendation.
"""
with temporary_seed(int(time.time())):
for k in range(n_iterations):
rec = campaign.recommend(batch_size=batch_size)
rec = campaign.recommend(
batch_size=batch_size, interpoint_constraints=interpoint_constraints
)
# dont use parameter noise for these tests

add_fake_results(rec, campaign.targets)
Expand Down
62 changes: 0 additions & 62 deletions tests/constraints/test_constraints_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,68 +58,6 @@ def test_inequality2(campaign, n_iterations, batch_size):
assert (1.0 * res["Conti_finite1"] + 3.0 * res["Conti_finite2"]).ge(0.299).all()


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("constraint_names", [["InterConstraint_1"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
def test_interpoint_equality_single_parameter(campaign, n_iterations, batch_size):
"""Test single parameter inter-point equality constraint."""
run_iterations(campaign, n_iterations, batch_size, add_noise=False)
res = campaign.measurements
print(res)

assert np.isclose(
res.at[0, "Conti_finite1"] + 3.0 * res.at[1, "Conti_finite1"], 0.3
)


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("constraint_names", [["InterConstraint_2"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
def test_interpoint_inequality_single_parameter(campaign, n_iterations, batch_size):
"""Test single parameter inter-point inequality constraint."""
run_iterations(campaign, n_iterations, batch_size, add_noise=False)
res = campaign.measurements
print(res)

assert res.at[0, "Conti_finite1"] + 3.0 * res.at[1, "Conti_finite1"] >= 0.299


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("constraint_names", [["InterConstraint_3"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
def test_interpoint_equality_multiple_parameters(campaign, n_iterations, batch_size):
"""Test inter-point equality constraint involving multiple parameters."""
run_iterations(campaign, n_iterations, batch_size, add_noise=False)
res = campaign.measurements
print(res)

assert np.isclose(
res.at[0, "Conti_finite1"] + 3.0 * res.at[1, "Conti_finite2"], 0.3
)


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("constraint_names", [["InterConstraint_4"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
def test_interpoint_inequality_multiple_parameters(campaign, n_iterations, batch_size):
"""Test inter-point inequality constraint involving multiple parameters."""
run_iterations(campaign, n_iterations, batch_size, add_noise=False)
res = campaign.measurements
print(res)

assert res.at[0, "Conti_finite1"] + 3.0 * res.at[1, "Conti_finite2"] >= 0.299


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("constraint_names", [["InterConstraint_2"]])
@pytest.mark.parametrize("batch_size", [1], ids=["b1"])
def test_interpoint_small_batch_size(campaign, n_iterations, batch_size):
"""Fail if requesting too small batch."""
print(f"{batch_size=}")
with pytest.raises(ValueError):
run_iterations(campaign, n_iterations, batch_size, add_noise=False)


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize(
"constraint_names", [["ContiConstraint_4", "InterConstraint_2"]]
Expand Down
91 changes: 91 additions & 0 deletions tests/constraints/test_interpoint_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Test for imposing interpoint constraints."""

import numpy as np
import pytest

from tests.conftest import run_iterations


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
@pytest.mark.parametrize("interpoint_constraints_names", [["InterConstraint_1"]])
def test_interpoint_equality_single_parameter(
campaign, n_iterations, batch_size, interpoint_constraints
):
"""Test single parameter inter-point equality constraint."""
run_iterations(
campaign,
n_iterations,
batch_size,
add_noise=False,
interpoint_constraints=interpoint_constraints,
)
res = campaign.measurements
for batch in range(n_iterations):
res_batch = res[res["BatchNr"] == batch + 1]
assert np.isclose(res_batch["Conti_finite1"].sum(), 0.3)


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
@pytest.mark.parametrize("interpoint_constraints_names", [["InterConstraint_2"]])
def test_interpoint_inequality_single_parameter(
campaign, n_iterations, batch_size, interpoint_constraints
):
"""Test single parameter inter-point inequality constraint."""
run_iterations(
campaign,
n_iterations,
batch_size,
add_noise=False,
interpoint_constraints=interpoint_constraints,
)
res = campaign.measurements
for batch in range(n_iterations):
res_batch = res[res["BatchNr"] == batch + 1]
assert 2 * res_batch["Conti_finite1"].sum() >= 0.299


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
@pytest.mark.parametrize("interpoint_constraints_names", [["InterConstraint_3"]])
def test_interpoint_equality_multiple_parameters(
campaign, n_iterations, batch_size, interpoint_constraints
):
"""Test single parameter inter-point equality constraint."""
run_iterations(
campaign,
n_iterations,
batch_size,
add_noise=False,
interpoint_constraints=interpoint_constraints,
)
res = campaign.measurements
for batch in range(n_iterations):
res_batch = res[res["BatchNr"] == batch + 1]
assert np.isclose(
res_batch["Conti_finite1"].sum() + res_batch["Conti_finite2"].sum(), 0.3
)


@pytest.mark.parametrize("parameter_names", [["Conti_finite1", "Conti_finite2"]])
@pytest.mark.parametrize("batch_size", [5], ids=["b5"])
@pytest.mark.parametrize("interpoint_constraints_names", [["InterConstraint_4"]])
def test_interpoint_inequality_multiple_parameters(
campaign, n_iterations, batch_size, interpoint_constraints
):
"""Test single parameter inter-point inequality constraint."""
run_iterations(
campaign,
n_iterations,
batch_size,
add_noise=False,
interpoint_constraints=interpoint_constraints,
)
res = campaign.measurements
for batch in range(n_iterations):
res_batch = res[res["BatchNr"] == batch + 1]
assert (
2 * res_batch["Conti_finite1"].sum() - res_batch["Conti_finite2"].sum()
>= 0.299
)

0 comments on commit d5758c5

Please sign in to comment.