Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the suggest method #1316

Merged
merged 22 commits into from
Mar 22, 2022
Merged
Changes from 19 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
79 changes: 79 additions & 0 deletions nevergrad/optimization/test_optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,85 @@ def test_infnan(name: str) -> None:
)


def suggestable(name: str) -> bool:
# Some methods are not good with suggestions.
keywords = ["TBPSA", "BO", "EMNA", "EDA", "BO", "Stupid", "Pymoo"]
return not any(x in name for x in keywords)


def suggestion_testing(
name: str,
instrumentation: ng.p.Array,
suggestion: np.ndarray,
budget: int,
objective_function: tp.Callable[..., tp.Any],
optimum: tp.Optional[np.ndarray] = None,
threshold: tp.Optional[float] = None,
):
optimizer_cls = registry[name]
optim = optimizer_cls(instrumentation, budget)
if optimum is None:
optimum = suggestion
optim.suggest(suggestion)
optim.minimize(objective_function)
if threshold is not None:
assert (
objective_function(optim.recommend().value) < threshold
), "{name} proposes {optim.recommend().value} instead of {optimum} (threshold={threshold})"
return
assert np.all(
optim.recommend().value == optimum
), "{name} proposes {optim.recommend().value} instead of {optimum}"


@skip_win_perf # type: ignore
@pytest.mark.parametrize("name", [r for r in registry if suggestable(r)]) # type: ignore
def test_suggest_optimizers(name: str) -> None:
"""Checks that each optimizer is able to converge when optimum is given"""

instrum = ng.p.Array(shape=(100,)).set_bounds(0.0, 1.0)
instrum.set_integer_casting()
suggestion = np.asarray([0] * 17 + [1] * 17 + [0] * 66) # The optimum is the suggestion.
target = lambda x: 0 if np.all(np.asarray(x, dtype=int) == suggestion) else 1
suggestion_testing(name, instrum, suggestion, 7, target)


def good_at_suggest(name: str) -> bool:
keywords = [
"Noisy",
"Optimistic",
"Multi",
"Anisotropic",
"BSO",
"Sparse",
"Recombining",
"PortfolioDiscreteOne",
]
return not any(k in name for k in keywords)


@skip_win_perf # type: ignore
@pytest.mark.parametrize("name", [r for r in registry if "iscre" in r and good_at_suggest(r)]) # type: ignore
def test_harder_suggest_optimizers(name: str) -> None:
"""Checks that discrete optimizers are good when a suggestion is nearby."""
instrum = ng.p.Array(shape=(100,)).set_bounds(0.0, 1.0)
instrum.set_integer_casting()
optimum = np.asarray([0] * 17 + [1] * 17 + [0] * 66)
target = lambda x: min(3, np.sum((np.asarray(x, dtype=int) - optimum) ** 2))
suggestion = np.asarray([0] * 17 + [1] * 16 + [0] * 67)
suggestion_testing(name, instrum, suggestion, 1500, target, optimum)


@skip_win_perf # type: ignore
def test_harder_continuous_suggest_optimizers() -> None:
"""Checks that somes optimizer can converge when provided with a good suggestion."""
instrum = ng.p.Array(shape=(100,)).set_bounds(0.0, 1.0)
optimum = np.asarray([0] * 17 + [1] * 17 + [0] * 66)
target = lambda x: min(2.0, np.sum((x - optimum) ** 2))
suggestion = np.asarray([0] * 17 + [1] * 16 + [0] * 67)
suggestion_testing("NGOpt", instrum, suggestion, 1500, target, optimum, threshold=0.9)


@skip_win_perf # type: ignore
@pytest.mark.parametrize("name", registry) # type: ignore
def test_optimizers(name: str) -> None:
Expand Down