def test_valid_derivative_versions(direction, algorithm, derivative, criterion_and_derivative): start_params = pd.DataFrame() start_params["value"] = [1, 2, 3] if direction == "minimize": res = minimize( criterion=sos_dict_criterion, params=start_params, algorithm=algorithm, derivative=derivative, criterion_and_derivative=criterion_and_derivative, error_handling="raise", ) else: deriv = derivative if derivative is None else switch_sign(derivative) crit_and_deriv = (criterion_and_derivative if criterion_and_derivative is None else switch_sign(criterion_and_derivative)) res = maximize( criterion=switch_sign(sos_dict_criterion), params=start_params, algorithm=algorithm, derivative=deriv, criterion_and_derivative=crit_and_deriv, error_handling="raise", ) aaae(res["solution_params"]["value"].to_numpy(), np.zeros(3), decimal=4)
def test_invalid_derivative_versions(direction, algorithm, derivative, criterion_and_derivative): start_params = pd.DataFrame() start_params["value"] = [1, 2, 3] if direction == "minimize": with pytest.raises(ValueError): minimize( criterion=sos_dict_criterion, params=start_params, algorithm=algorithm, derivative=derivative, criterion_and_derivative=criterion_and_derivative, ) else: deriv = derivative if derivative is None else switch_sign(derivative) crit_and_deriv = (criterion_and_derivative if criterion_and_derivative is None else switch_sign(criterion_and_derivative)) with pytest.raises(ValueError): maximize( criterion=switch_sign(sos_dict_criterion), params=start_params, algorithm=algorithm, derivative=deriv, criterion_and_derivative=crit_and_deriv, )
def test_multistart_minimize_with_sum_of_squares_at_defaults( criterion, direction, params): if direction == "minimize": res = minimize( criterion=criterion, params=params, algorithm="scipy_lbfgsb", multistart=True, ) else: res = maximize( criterion=switch_sign(sos_dict_criterion), params=params, algorithm="scipy_lbfgsb", multistart=True, ) assert hasattr(res, "multistart_info") ms_info = res.multistart_info assert len(ms_info["exploration_sample"]) == 40 assert len(ms_info["exploration_results"]) == 40 assert all( isinstance(entry, float) for entry in ms_info["exploration_results"]) assert all( isinstance(entry, OptimizeResult) for entry in ms_info["local_optima"]) assert all( isinstance(entry, pd.DataFrame) for entry in ms_info["start_parameters"]) assert np.allclose(res.criterion, 0) aaae(res.params["value"], np.zeros(4))
def test_convergence_via_max_discoveries_works(params): options = { "convergence_relative_params_tolerance": np.inf, "convergence_max_discoveries": 2, } res = maximize( criterion=switch_sign(sos_dict_criterion), params=params, algorithm="scipy_lbfgsb", multistart=True, multistart_options=options, ) assert len(res.multistart_info["local_optima"]) == 2