From 5faa56a74c52c3ac17e8cc77b8c2e0a678d2a418 Mon Sep 17 00:00:00 2001 From: Janos Gabler Date: Sun, 25 Oct 2026 10:30:50 +0100 Subject: [PATCH 1/3] Fix problem types and scaled noise-free criterion in benchmarks 22 Cartis-Roberts problems and the More-Wild kowalik_osborne problem wrapped their least-squares criterion in functools.partial without the mark.least_squares decorator (kowalik_osborne marked partial itself), so minimize rejected them as scalar functions returning arrays and run_benchmark silently counted them as failures. get_benchmark_problems returned the unscaled criterion as noise_free_fun while the start params were scaled, so run_benchmark computed wrong start values for failed runs and wrong criterion histories for noisy problems with scaling. Co-Authored-By: Claude Opus 5.5 --- src/optimagic/benchmarking/cartis_roberts.py | 106 ++++++++++-------- .../benchmarking/get_benchmark_problems.py | 9 +- src/optimagic/benchmarking/more_wild.py | 10 +- .../benchmarking/test_cartis_roberts.py | 8 ++ .../test_get_benchmark_problems.py | 16 +++ .../optimagic/benchmarking/test_more_wild.py | 8 ++ .../benchmarking/test_run_benchmark.py | 18 +++ 7 files changed, 123 insertions(+), 52 deletions(-) diff --git a/src/optimagic/benchmarking/cartis_roberts.py b/src/optimagic/benchmarking/cartis_roberts.py index e5371d43c..dba4af4a9 100644 --- a/src/optimagic/benchmarking/cartis_roberts.py +++ b/src/optimagic/benchmarking/cartis_roberts.py @@ -4895,7 +4895,7 @@ def get_start_points_methanl8(): CARTIS_ROBERTS_PROBLEMS = { "arglale": { # arglale is the same as linear_full_rank with specific settings - "fun": partial(linear_full_rank, dim_out=400), + "fun": mark.least_squares(partial(linear_full_rank, dim_out=400)), "start_x": [1] * 100, "solution_x": [-0.99999952] * 100, "start_criterion": 700, @@ -4903,7 +4903,7 @@ def get_start_points_methanl8(): }, "arglble": { # arglble is the same as linear_rank_one with specific settings - "fun": partial(linear_rank_one, dim_out=400), + "fun": mark.least_squares(partial(linear_rank_one, dim_out=400)), "start_x": [1] * 100, "solution_x": solution_x_arglble, "start_criterion": 5.460944e14, @@ -4938,21 +4938,21 @@ def get_start_points_methanl8(): "solution_criterion": 0, }, "bratu_2d": { - "fun": partial(bratu_2d, alpha=4), + "fun": mark.least_squares(partial(bratu_2d, alpha=4)), "start_x": [0] * 64, "solution_x": solution_x_bratu_2d, "start_criterion": 0.1560738, "solution_criterion": 0, }, "bratu_2d_t": { - "fun": partial(bratu_2d, alpha=6.80812), + "fun": mark.least_squares(partial(bratu_2d, alpha=6.80812)), "start_x": [0] * 64, "solution_x": solution_x_bratu_2d_t, "start_criterion": 0.4521311, "solution_criterion": 1.8534736e-05, }, "bratu_3d": { - "fun": partial(bratu_3d, alpha=6.80812), + "fun": mark.least_squares(partial(bratu_3d, alpha=6.80812)), "start_x": [0] * 27, "solution_x": solution_x_bratu_3d, "start_criterion": 4.888529, @@ -5017,28 +5017,28 @@ def get_start_points_methanl8(): "solution_criterion": 0, }, "drcavty1": { - "fun": partial(drcavty, r=500), + "fun": mark.least_squares(partial(drcavty, r=500)), "start_x": [0] * 100, "solution_x": None, "start_criterion": 0.4513889, "solution_criterion": 0, }, "drcavty2": { - "fun": partial(drcavty, r=1000), + "fun": mark.least_squares(partial(drcavty, r=1000)), "start_x": [0] * 100, "solution_x": solution_x_drcavty2, "start_criterion": 0.4513889, "solution_criterion": 3.988378e-4, }, "drcavty3": { - "fun": partial(drcavty, r=4500), + "fun": mark.least_squares(partial(drcavty, r=4500)), "start_x": [0] * 100, "solution_x": solution_x_drcavty3, "start_criterion": 0.4513889, "solution_criterion": 0, }, "eigena": { - "fun": partial(eigen, param=np.diag(np.arange(1, 11))), + "fun": mark.least_squares(partial(eigen, param=np.diag(np.arange(1, 11)))), "start_x": [1] * 10 + np.eye(10).flatten().tolist(), "solution_x": [*np.arange(1, 11).tolist(), 1] + ([0] * 10 + [1]) * 9, "start_criterion": 285, @@ -5046,8 +5046,8 @@ def get_start_points_methanl8(): "bounds": Bounds(lower=np.zeros(110)), }, "eigenb": { - "fun": partial( - eigen, param=np.diag(2 * np.ones(10)) + np.diag(-np.ones(9), k=1) + "fun": mark.least_squares( + partial(eigen, param=np.diag(2 * np.ones(10)) + np.diag(-np.ones(9), k=1)) ), "start_x": [1] * 10 + np.eye(10).flatten().tolist(), "solution_x": solution_x_eigenb, @@ -5057,11 +5057,13 @@ def get_start_points_methanl8(): # according to table 3 in their paper, the minimum is at 0. }, "flosp2hh": { - "fun": partial( - flosp2, - a=np.array([1, 0, -1], dtype=np.int64), - b=np.array([1, 0, -1], dtype=np.int64), - ra=1e7, + "fun": mark.least_squares( + partial( + flosp2, + a=np.array([1, 0, -1], dtype=np.int64), + b=np.array([1, 0, -1], dtype=np.int64), + ra=1e7, + ) ), "start_x": [0] * 59, "solution_x": None, # multiple argmins @@ -5069,11 +5071,13 @@ def get_start_points_methanl8(): "solution_criterion": 1 / 3, }, "flosp2hl": { - "fun": partial( - flosp2, - a=np.array([1, 0, -1], dtype=np.float64), - b=np.array([1, 0, -1], dtype=np.float64), - ra=1e3, + "fun": mark.least_squares( + partial( + flosp2, + a=np.array([1, 0, -1], dtype=np.float64), + b=np.array([1, 0, -1], dtype=np.float64), + ra=1e3, + ) ), "start_x": [0] * 59, "solution_x": None, # multiple argmins @@ -5081,11 +5085,13 @@ def get_start_points_methanl8(): "solution_criterion": 1 / 3, }, "flosp2hm": { - "fun": partial( - flosp2, - a=np.array([1, 0, -1], dtype=np.float64), - b=np.array([1, 0, -1], dtype=np.float64), - ra=1e5, + "fun": mark.least_squares( + partial( + flosp2, + a=np.array([1, 0, -1], dtype=np.float64), + b=np.array([1, 0, -1], dtype=np.float64), + ra=1e5, + ) ), "start_x": [0] * 59, "solution_x": None, # multiple argmins @@ -5093,11 +5099,13 @@ def get_start_points_methanl8(): "solution_criterion": 1 / 3, }, "flosp2th": { - "fun": partial( - flosp2, - a=np.array([0, 1, 0], dtype=np.float64), - b=np.array([0, 1, 1], dtype=np.float64), - ra=1e7, + "fun": mark.least_squares( + partial( + flosp2, + a=np.array([0, 1, 0], dtype=np.float64), + b=np.array([0, 1, 1], dtype=np.float64), + ra=1e7, + ) ), "start_x": [0] * 59, "solution_x": None, # multiple argmins @@ -5105,11 +5113,13 @@ def get_start_points_methanl8(): "solution_criterion": 0, }, "flosp2tl": { - "fun": partial( - flosp2, - a=np.array([0, 1, 0], dtype=np.float64), - b=np.array([0, 1, 1], dtype=np.float64), - ra=1e3, + "fun": mark.least_squares( + partial( + flosp2, + a=np.array([0, 1, 0], dtype=np.float64), + b=np.array([0, 1, 1], dtype=np.float64), + ra=1e3, + ) ), "start_x": [0] * 59, "solution_x": None, # multiple argmins @@ -5117,11 +5127,13 @@ def get_start_points_methanl8(): "solution_criterion": 0, }, "flosp2tm": { - "fun": partial( - flosp2, - a=np.array([0, 1, 0], dtype=np.float64), - b=np.array([0, 1, 1], dtype=np.float64), - ra=1e5, + "fun": mark.least_squares( + partial( + flosp2, + a=np.array([0, 1, 0], dtype=np.float64), + b=np.array([0, 1, 1], dtype=np.float64), + ra=1e5, + ) ), "start_x": [0] * 59, "solution_x": None, # multiple argmins @@ -5143,14 +5155,14 @@ def get_start_points_methanl8(): "solution_criterion": 0, }, "hydcar20": { - "fun": partial(hydcar, n=20, m=3, k=9), + "fun": mark.least_squares(partial(hydcar, n=20, m=3, k=9)), "start_x": get_start_points_hydcar20(), "solution_x": solution_x_hydcar20, "start_criterion": 1341.663, "solution_criterion": 0, }, "hydcar6": { - "fun": partial(hydcar, n=6, m=3, k=2), + "fun": mark.least_squares(partial(hydcar, n=6, m=3, k=2)), "start_x": get_start_points_hydcar6(), "solution_x": solution_x_hydcar6, "start_criterion": 704.1073, @@ -5296,7 +5308,7 @@ def get_start_points_methanl8(): "solution_criterion": 0, }, "qr3d": { - "fun": partial(qr3d, m=5), + "fun": mark.least_squares(partial(qr3d, m=5)), "start_x": get_start_points_qr3d(5), "solution_x": solution_x_qr3d, "start_criterion": 1.2, @@ -5307,7 +5319,7 @@ def get_start_points_methanl8(): ), }, "qr3dbd": { - "fun": partial(qr3dbd, m=5), + "fun": mark.least_squares(partial(qr3dbd, m=5)), "start_x": get_start_points_qr3dbd(5), "solution_x": solution_x_qr3dbd, "start_criterion": 1.2, @@ -5354,14 +5366,14 @@ def get_start_points_methanl8(): "solution_criterion": 0, }, "yatpsq_1": { - "fun": partial(yatpsq_1, dim_in=10), + "fun": mark.least_squares(partial(yatpsq_1, dim_in=10)), "start_x": [6] * 100 + [0] * 20, "solution_x": solution_x_yatpsq_1, "start_criterion": 2.073643e6, "solution_criterion": 0, }, "yatpsq_2": { - "fun": partial(yatpsq_2, dim_in=10), + "fun": mark.least_squares(partial(yatpsq_2, dim_in=10)), "start_x": [10] * 100 + [0] * 20, "solution_x": solution_x_yatpsq_2, "start_criterion": 1.831687e5, diff --git a/src/optimagic/benchmarking/get_benchmark_problems.py b/src/optimagic/benchmarking/get_benchmark_problems.py index 2986891ed..91fabe771 100644 --- a/src/optimagic/benchmarking/get_benchmark_problems.py +++ b/src/optimagic/benchmarking/get_benchmark_problems.py @@ -117,10 +117,17 @@ def get_benchmark_problems( scaling_options=scaling_options, rng=rng, ) + noise_free_inputs = _create_problem_inputs( + specification, + additive_options=None, + multiplicative_options=None, + scaling_options=scaling_options, + rng=rng, + ) problems[prob_name] = { "inputs": inputs, - "noise_free_fun": specification["fun"], + "noise_free_fun": noise_free_inputs["fun"], "solution": _create_problem_solution( specification, scaling_options=scaling_options ), diff --git a/src/optimagic/benchmarking/more_wild.py b/src/optimagic/benchmarking/more_wild.py index 684e99947..d2fd8e18c 100644 --- a/src/optimagic/benchmarking/more_wild.py +++ b/src/optimagic/benchmarking/more_wild.py @@ -907,10 +907,12 @@ def get_start_points_mancino(n, a=1): "solution_criterion": 0.00821487730657897, }, "kowalik_osborne": { - "fun": mark.least_squares(partial)( - kowalik_osborne, - y1=v_vec, - y2=y2_vec, + "fun": mark.least_squares( + partial( + kowalik_osborne, + y1=v_vec, + y2=y2_vec, + ) ), "start_x": [0.25, 0.39, 0.415, 0.39], "solution_x": kowalik_osborne_solution_x, diff --git a/tests/optimagic/benchmarking/test_cartis_roberts.py b/tests/optimagic/benchmarking/test_cartis_roberts.py index c399d89d7..717b19c0c 100644 --- a/tests/optimagic/benchmarking/test_cartis_roberts.py +++ b/tests/optimagic/benchmarking/test_cartis_roberts.py @@ -7,6 +7,8 @@ get_start_points_bdvalues, get_start_points_msqrta, ) +from optimagic.shared.process_user_function import infer_aggregation_level +from optimagic.typing import AggregationLevel @pytest.mark.parametrize("name, specification", list(CARTIS_ROBERTS_PROBLEMS.items())) @@ -53,3 +55,9 @@ def test_get_start_points_msqrta(): expected = 0.2 * matlab_mat.flatten() result = get_start_points_msqrta(5) assert_array_almost_equal(result, expected, decimal=4) + + +@pytest.mark.parametrize("name, specification", list(CARTIS_ROBERTS_PROBLEMS.items())) +def test_cartis_roberts_function_is_marked_as_least_squares(name, specification): # noqa: ARG001 + problem_type = infer_aggregation_level(specification["fun"]) + assert problem_type == AggregationLevel.LEAST_SQUARES diff --git a/tests/optimagic/benchmarking/test_get_benchmark_problems.py b/tests/optimagic/benchmarking/test_get_benchmark_problems.py index 134ae4f41..dc155522d 100644 --- a/tests/optimagic/benchmarking/test_get_benchmark_problems.py +++ b/tests/optimagic/benchmarking/test_get_benchmark_problems.py @@ -3,6 +3,7 @@ import numpy as np import pytest +from optimagic import minimize from optimagic.benchmarking.get_benchmark_problems import ( _step_func, get_benchmark_problems, @@ -48,3 +49,18 @@ def test_step_func(): got = _step_func(p, lambda x: x @ x) assert np.allclose(got, 0) assert not np.allclose(p @ p, 0) + + +@pytest.mark.parametrize("name", ["more_wild", "cartis_roberts"]) +def test_noise_free_fun_is_scaled_like_inputs(name): + problems = get_benchmark_problems(name, additive_noise=True, scaling=True) + for problem in problems.values(): + start_x = problem["inputs"]["params"] + residuals = problem["noise_free_fun"](start_x) + assert np.allclose(residuals @ residuals, problem["start_criterion"]) + + +def test_minimize_problem_with_partialled_criterion(): + problem = get_benchmark_problems("cartis_roberts")["bratu_2d"] + res = minimize(**problem["inputs"], algorithm="scipy_ls_trf") + assert np.allclose(res.fun, problem["solution"]["value"], atol=1e-8) diff --git a/tests/optimagic/benchmarking/test_more_wild.py b/tests/optimagic/benchmarking/test_more_wild.py index bb3515574..3018100f5 100644 --- a/tests/optimagic/benchmarking/test_more_wild.py +++ b/tests/optimagic/benchmarking/test_more_wild.py @@ -5,6 +5,8 @@ MORE_WILD_PROBLEMS, get_start_points_mancino, ) +from optimagic.shared.process_user_function import infer_aggregation_level +from optimagic.typing import AggregationLevel @pytest.mark.parametrize("name, specification", list(MORE_WILD_PROBLEMS.items())) @@ -30,3 +32,9 @@ def test_get_start_points_mancino(): expected = (np.array([102.4824, 96.3335, 90.4363, 84.7852, 79.3747]),) result = get_start_points_mancino(5) assert np.allclose(expected, result) + + +@pytest.mark.parametrize("name, specification", list(MORE_WILD_PROBLEMS.items())) +def test_more_wild_function_is_marked_as_least_squares(name, specification): # noqa: ARG001 + problem_type = infer_aggregation_level(specification["fun"]) + assert problem_type == AggregationLevel.LEAST_SQUARES diff --git a/tests/optimagic/benchmarking/test_run_benchmark.py b/tests/optimagic/benchmarking/test_run_benchmark.py index d501dd17e..487ba902b 100644 --- a/tests/optimagic/benchmarking/test_run_benchmark.py +++ b/tests/optimagic/benchmarking/test_run_benchmark.py @@ -1,3 +1,4 @@ +import numpy as np import pytest from optimagic import get_benchmark_problems @@ -64,3 +65,20 @@ def test_run_benchmark_failing(): key = (failing_name, "scipy_lbfgsb") assert isinstance(result[key]["solution"], str) + + +def test_run_benchmark_noisy_scaled_problem(): + all_problems = get_benchmark_problems( + "example", additive_noise=True, scaling=True, seed=0 + ) + name = "rosenbrock_good_start" + problems = {name: all_problems[name]} + + result = run_benchmark( + problems=problems, + optimize_options=["scipy_neldermead"], + max_criterion_evaluations=20, + ) + + history = result[(name, "scipy_neldermead")]["criterion_history"] + assert np.allclose(history[0], problems[name]["start_criterion"]) From 8a2eeee2f1ecbf8aa5dcd83af6ae3d639ff7b28e Mon Sep 17 00:00:00 2001 From: Janos Gabler Date: Sun, 25 Oct 2026 10:33:50 +0100 Subject: [PATCH 2/3] Add changelog entry for #723 Co-Authored-By: Claude Opus 5.5 --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 4b1484e25..0758139bb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,9 @@ releases are available on [Anaconda.org](https://anaconda.org/optimagic-dev/opti This release adds experimental support for **SciPy-style callbacks**, replaces pybaum by **optree** for pytree handling and uses **pydantic** to validate algorithm options. +- {gh}`723` Fixes 23 More-Wild and Cartis-Roberts benchmark problems that were not marked as + least-squares problems, and the noise-free criterion of scaled benchmark problems + ({ghuser}`janosg`). - {gh}`697` Adds experimental support for SciPy-style `callback(xk)` in `minimize` and `maximize`. The callback is called after each objective evaluation; its behavior might change in upcoming releases ({ghuser}`Pragati5-DEBUG`). From 9e0a92230c3fd8d04b003e415b124a76c23cdce3 Mon Sep 17 00:00:00 2001 From: Janos Gabler Date: Sun, 25 Oct 2026 10:34:11 +0100 Subject: [PATCH 3/3] Wrap changelog entry for #723 Co-Authored-By: Claude Opus 5.5 --- CHANGES.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 0758139bb..e74f87ff2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,9 +10,9 @@ releases are available on [Anaconda.org](https://anaconda.org/optimagic-dev/opti This release adds experimental support for **SciPy-style callbacks**, replaces pybaum by **optree** for pytree handling and uses **pydantic** to validate algorithm options. -- {gh}`723` Fixes 23 More-Wild and Cartis-Roberts benchmark problems that were not marked as - least-squares problems, and the noise-free criterion of scaled benchmark problems - ({ghuser}`janosg`). +- {gh}`723` Fixes 23 More-Wild and Cartis-Roberts benchmark problems that were not + marked as least-squares problems, and the noise-free criterion of scaled benchmark + problems ({ghuser}`janosg`). - {gh}`697` Adds experimental support for SciPy-style `callback(xk)` in `minimize` and `maximize`. The callback is called after each objective evaluation; its behavior might change in upcoming releases ({ghuser}`Pragati5-DEBUG`).