diff --git a/CHANGES.md b/CHANGES.md index 664c74932..abb87794f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -43,6 +43,9 @@ This release adds experimental support for **SciPy-style callbacks**, replaces p derivative if `fun` is derived from `fun_and_jac`, i.e. if only `fun_and_jac` is provided or `jac=True`. It is now enough to provide either `fun_kwargs` or `fun_and_jac_kwargs` ({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`). diff --git a/src/optimagic/benchmarking/cartis_roberts.py b/src/optimagic/benchmarking/cartis_roberts.py index 29e481556..0c98e792f 100644 --- a/src/optimagic/benchmarking/cartis_roberts.py +++ b/src/optimagic/benchmarking/cartis_roberts.py @@ -4896,7 +4896,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, @@ -4904,7 +4904,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, @@ -4939,21 +4939,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, @@ -5018,28 +5018,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, @@ -5047,8 +5047,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, @@ -5058,11 +5058,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 @@ -5070,11 +5072,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 @@ -5082,11 +5086,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 @@ -5094,11 +5100,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 @@ -5106,11 +5114,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 @@ -5118,11 +5128,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 @@ -5144,14 +5156,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, @@ -5297,7 +5309,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, @@ -5308,7 +5320,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, @@ -5355,14 +5367,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 e1f878408..d33df586d 100644 --- a/src/optimagic/benchmarking/get_benchmark_problems.py +++ b/src/optimagic/benchmarking/get_benchmark_problems.py @@ -115,10 +115,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 235c64d49..a260dd570 100644 --- a/src/optimagic/benchmarking/more_wild.py +++ b/src/optimagic/benchmarking/more_wild.py @@ -908,10 +908,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"])