Skip to content

Commit 65befec

Browse files
committed
Remove optimagic dependency from test-suite
1 parent fcc6f31 commit 65befec

2 files changed

Lines changed: 44 additions & 44 deletions

File tree

tests/test_tranquilo.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,18 @@ def test_external_tranquilo_ls_sphere_defaults():
192192
# Noisy case
193193
# ======================================================================================
194194

195-
196-
@pytest.mark.skipif(not IS_OPTIMAGIC_INSTALLED, reason="optimagic is not installed.")
197-
@pytest.mark.parametrize(
198-
"algorithm, criterion",
199-
[
195+
if IS_OPTIMAGIC_INSTALLED:
196+
# Has to be defined here to avoid import errors when optimagic is not installed
197+
ALGORITHM_AND_CRITERION = [
200198
("tranquilo", mark.scalar(lambda x: x @ x)),
201199
("tranquilo_ls", mark.least_squares(lambda x: x)),
202-
],
203-
)
200+
]
201+
else:
202+
ALGORITHM_AND_CRITERION = []
203+
204+
205+
@pytest.mark.skipif(not IS_OPTIMAGIC_INSTALLED, reason="optimagic is not installed.")
206+
@pytest.mark.parametrize("algorithm, criterion", ALGORITHM_AND_CRITERION)
204207
def test_tranquilo_with_noise_handling_and_deterministic_function(algorithm, criterion):
205208
res = minimize(
206209
fun=criterion,
@@ -238,13 +241,7 @@ def _f(x):
238241

239242

240243
@pytest.mark.skipif(not IS_OPTIMAGIC_INSTALLED, reason="optimagic is not installed.")
241-
@pytest.mark.parametrize(
242-
"algorithm, criterion",
243-
[
244-
("tranquilo", mark.scalar(lambda x: x @ x)),
245-
("tranquilo_ls", mark.least_squares(lambda x: x)),
246-
],
247-
)
244+
@pytest.mark.parametrize("algorithm, criterion", ALGORITHM_AND_CRITERION)
248245
def test_tranquilo_with_binding_bounds(algorithm, criterion):
249246
res = minimize(
250247
fun=criterion,

tests/test_visualize.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,41 @@
77
from optimagic.benchmarking.get_benchmark_problems import get_benchmark_problems
88
from optimagic.optimization.optimize import minimize
99

10-
cases = []
11-
algo_options = {
12-
"random_hull": {
13-
"sampler": "random_hull",
14-
"sphere_subsolver": "gqtpar_fast",
15-
"sample_filter": "keep_all",
16-
"stopping_maxiter": 10,
17-
},
18-
"optimal_hull": {
19-
"sampler": "optimal_hull",
20-
"sphere_subsolver": "gqtpar_fast",
21-
"sample_filter": "keep_all",
22-
"stopping_maxiter": 10,
23-
},
24-
}
25-
for problem in ["rosenbrock_good_start", "watson_6_good_start"]:
26-
inputs = get_benchmark_problems("more_wild")[problem]["inputs"]
27-
fun = inputs["fun"]
28-
start_params = inputs["params"]
29-
for algorithm in ["tranquilo", "tranquilo_ls"]:
30-
results = {}
31-
for s, options in algo_options.items():
32-
results[s] = minimize(
33-
fun=fun,
34-
params=start_params,
35-
algo_options=options,
36-
algorithm=algorithm,
37-
)
38-
cases.append(results)
3910

11+
def benchmark_cases():
12+
cases = []
13+
algo_options = {
14+
"random_hull": {
15+
"sampler": "random_hull",
16+
"sphere_subsolver": "gqtpar_fast",
17+
"sample_filter": "keep_all",
18+
"stopping_maxiter": 10,
19+
},
20+
"optimal_hull": {
21+
"sampler": "optimal_hull",
22+
"sphere_subsolver": "gqtpar_fast",
23+
"sample_filter": "keep_all",
24+
"stopping_maxiter": 10,
25+
},
26+
}
27+
for problem in ["rosenbrock_good_start", "watson_6_good_start"]:
28+
inputs = get_benchmark_problems("more_wild")[problem]["inputs"]
29+
fun = inputs["fun"]
30+
start_params = inputs["params"]
31+
for algorithm in ["tranquilo", "tranquilo_ls"]:
32+
results = {}
33+
for s, options in algo_options.items():
34+
results[s] = minimize(
35+
fun=fun,
36+
params=start_params,
37+
algo_options=options,
38+
algorithm=algorithm,
39+
)
40+
cases.append(results)
41+
return cases
4042

41-
@pytest.mark.parametrize("results", cases)
43+
44+
@pytest.mark.parametrize("results", benchmark_cases() if IS_OPTIMAGIC_INSTALLED else [])
4245
def test_visualize_tranquilo(results):
4346
visualize_tranquilo(results, 5)
4447
for res in results.values():

0 commit comments

Comments
 (0)