|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 |
|
7 | | -def test_x0_as_list(): |
| 7 | +def test_x0_as_list(backend_fixture): |
8 | 8 | x0 = [0.0] * 2 |
9 | | - res = minimize(fun, x0) |
| 9 | + method = 'COBYLA' if backend_fixture == 'Python' else None |
| 10 | + res = minimize(fun, x0, method=method, options={'backend': backend_fixture}) |
10 | 11 | assert fun.result_point_and_value_are_optimal(res) |
11 | 12 |
|
12 | 13 |
|
13 | | -def test_x0_as_array(): |
| 14 | +def test_x0_as_array(backend_fixture): |
14 | 15 | x0 = np.array([0.0] * 2) |
15 | | - res = minimize(fun, x0) |
| 16 | + method = 'COBYLA' if backend_fixture == 'Python' else None |
| 17 | + res = minimize(fun, x0, method=method, options={'backend': backend_fixture}) |
16 | 18 | assert fun.result_point_and_value_are_optimal(res) |
17 | 19 |
|
18 | 20 |
|
19 | | -def test_x0_as_scalar(): |
| 21 | +def test_x0_as_scalar(backend_fixture): |
20 | 22 | x0 = 0.0 |
21 | 23 | # We need a custom function since the default objective function we're |
22 | 24 | # using for tests expects something that can be unpacked into two variables. |
23 | | - res = minimize(lambda x: (x-5)**2, x0) |
| 25 | + method = 'COBYLA' if backend_fixture == 'Python' else None |
| 26 | + res = minimize(lambda x: (x-5)**2, x0, method=method, options={'backend': backend_fixture}) |
24 | 27 | assert np.isclose(res.x, 5.0, rtol=1e-6) |
25 | 28 | assert np.isclose(res.fun, 0.0, rtol=1e-6) |
26 | 29 |
|
27 | 30 |
|
28 | | -def test_constraint_function_returns_numpy_array(): |
| 31 | +def test_constraint_function_returns_numpy_array(backend_fixture): |
29 | 32 | nlc = NLC(lambda x: np.array([x[0], x[1]]), lb=[-np.inf]*2, ub=[10]*2) |
30 | 33 | x0 = [0, 0] |
31 | | - res = minimize(fun, x0, method='COBYLA', constraints=nlc) |
| 34 | + res = minimize(fun, x0, method='COBYLA', constraints=nlc, options={'backend': backend_fixture}) |
32 | 35 | assert fun.result_point_and_value_are_optimal(res) |
33 | 36 |
|
34 | 37 |
|
35 | | -def test_constraint_function_returns_list(): |
| 38 | +def test_constraint_function_returns_list(backend_fixture): |
36 | 39 | nlc = NLC(lambda x: [x[0], x[1]], lb=[-np.inf]*2, ub=[10]*2) |
37 | 40 | x0 = [0, 0] |
38 | | - res = minimize(fun, x0, method='COBYLA', constraints=nlc) |
| 41 | + res = minimize(fun, x0, method='COBYLA', constraints=nlc, options={'backend': backend_fixture}) |
39 | 42 | assert fun.result_point_and_value_are_optimal(res) |
40 | 43 |
|
41 | 44 |
|
42 | | -def test_constraint_function_returns_scalar(): |
| 45 | +def test_constraint_function_returns_scalar(backend_fixture): |
43 | 46 | nlc = NLC(lambda x: float(np.linalg.norm(x) - np.linalg.norm(fun.optimal_x)), lb=[-np.inf], ub=[0]) |
44 | 47 | x0 = [0, 0] |
45 | | - res = minimize(fun, x0, method='COBYLA', constraints=nlc) |
| 48 | + res = minimize(fun, x0, method='COBYLA', constraints=nlc, options={'backend': backend_fixture}) |
46 | 49 | assert fun.result_point_and_value_are_optimal(res) |
47 | 50 |
|
48 | 51 |
|
49 | 52 |
|
50 | 53 | @pytest.mark.parametrize("A", (1, [1], np.array([1]))) |
51 | 54 | @pytest.mark.parametrize("lb", (0, [0], np.array([0]))) |
52 | 55 | @pytest.mark.parametrize("ub", (4, [4], np.array([4]))) |
53 | | -def test_linear_constraint_data_types(A, lb, ub): |
| 56 | +def test_linear_constraint_data_types(A, lb, ub, backend_fixture): |
54 | 57 | myLC = LC(A=A, lb=lb, ub=ub) |
55 | 58 | # If A is scalar, x must have dimension 1, so we need a univariate function for that |
56 | 59 | scalar_fun = lambda x: (x - 5)**2 |
57 | 60 | x0 = 0 |
58 | | - res = minimize(scalar_fun, x0, method='LINCOA', constraints=myLC) |
| 61 | + method = 'COBYLA' if backend_fixture == 'Python' else None |
| 62 | + res = minimize(scalar_fun, x0, constraints=myLC, method=method, options={'backend': backend_fixture}) |
59 | 63 | assert np.isclose(res.x, 4, rtol=1e-2) |
60 | 64 |
|
61 | 65 |
|
62 | | -def test_nonlinear_constraint_lb_scalar_ub_scalar(): |
| 66 | +def test_nonlinear_constraint_lb_scalar_ub_scalar(backend_fixture): |
63 | 67 | nlc = NLC(lambda x: [x[0]**2, x[1]**2], lb=25, ub=100) |
64 | 68 | x0 = [0, 0] |
65 | | - res = minimize(fun, x0, constraints=nlc) |
| 69 | + res = minimize(fun, x0, constraints=nlc, options={'backend': backend_fixture}) |
66 | 70 | assert np.isclose(res.x[0], 5, atol=1e-6, rtol=1e-6) |
67 | 71 | assert np.isclose(res.x[1], 5, atol=1e-6, rtol=1e-6) |
68 | 72 | assert np.isclose(res.fun, 1, atol=1e-6, rtol=1e-6) |
69 | 73 |
|
70 | 74 |
|
71 | | -def test_nonlinear_constraint_lb_scalar_ub_not_scalar(): |
| 75 | +def test_nonlinear_constraint_lb_scalar_ub_not_scalar(backend_fixture): |
72 | 76 | nlc = NLC(lambda x: [x[0]**2, x[1]**2], lb=9, ub=[9, 16]) |
73 | 77 | x0 = [0, 0] |
74 | | - res = minimize(fun, x0, constraints=nlc) |
| 78 | + res = minimize(fun, x0, constraints=nlc, options={'backend': backend_fixture}) |
75 | 79 | assert np.isclose(res.x[0], 3, atol=1e-6, rtol=1e-6) |
76 | 80 | # cp38-manylinux_i686 didn't quite get to 4 with a tol of e-6, |
77 | 81 | # so we lower the tolerance a little bit. |
78 | 82 | assert np.isclose(res.x[1], 4, atol=1e-5, rtol=1e-5) |
79 | 83 | assert np.isclose(res.fun, 4, atol=1e-6, rtol=1e-6) |
80 | 84 |
|
81 | 85 |
|
82 | | -def test_nonlinear_constraint_lb_not_scalar_ub_scalar(): |
| 86 | +def test_nonlinear_constraint_lb_not_scalar_ub_scalar(backend_fixture): |
83 | 87 | nlc = NLC(lambda x: [x[0]**2, x[1]**2], lb=[25, 36], ub=100) |
84 | 88 | x0 = [0, 0] |
85 | | - res = minimize(fun, x0, constraints=nlc) |
| 89 | + res = minimize(fun, x0, constraints=nlc, options={'backend': backend_fixture}) |
86 | 90 | assert np.isclose(res.x[0], 5, atol=1e-6, rtol=1e-6) |
87 | 91 | assert np.isclose(res.x[1], 6, atol=1e-6, rtol=1e-6) |
88 | 92 | assert np.isclose(res.fun, 4, atol=1e-6, rtol=1e-6) |
89 | 93 |
|
90 | 94 |
|
91 | | -def test_nonlinear_constraint_lb_not_scalar_ub_not_scalar(): |
| 95 | +def test_nonlinear_constraint_lb_not_scalar_ub_not_scalar(backend_fixture): |
92 | 96 | nlc = NLC(lambda x: [x[0]**2, x[1]**2], lb=[4, 4], ub=[4, 9]) |
93 | 97 | x0 = [0, 0] |
94 | | - res = minimize(fun, x0, constraints=nlc) |
| 98 | + res = minimize(fun, x0, constraints=nlc, options={'backend': backend_fixture}) |
95 | 99 | assert np.isclose(res.x[0], 2, atol=1e-6, rtol=1e-6) |
96 | 100 | assert np.isclose(res.x[1], 3, atol=1e-6, rtol=1e-6) |
97 | 101 | assert np.isclose(res.fun, 10, atol=1e-6, rtol=1e-6) |
0 commit comments