|
| 1 | + |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | +from scipy import sparse |
| 5 | + |
| 6 | +import cvxpy as cp |
| 7 | +from cvxpy.reductions.solvers.defines import INSTALLED_SOLVERS |
| 8 | +from cvxpy.reductions.solvers.nlp_solvers.nlp_solver import DerivativeChecker |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.skipif('IPOPT' not in INSTALLED_SOLVERS, reason='IPOPT is not installed.') |
| 12 | +class TestParametersDiffEngine: |
| 13 | + |
| 14 | + def test_scalar_param_multiply(self): |
| 15 | + np.random.seed(0) |
| 16 | + n = 5 |
| 17 | + a = cp.Parameter(nonneg=True) |
| 18 | + a.value = 2.0 |
| 19 | + x = cp.Variable(n, bounds=[0.5, 2]) |
| 20 | + prob = cp.Problem(cp.Minimize(cp.sum(cp.exp(a * x)))) |
| 21 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 22 | + DerivativeChecker(prob).run_and_assert() |
| 23 | + |
| 24 | + a.value = 3.5 |
| 25 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 26 | + DerivativeChecker(prob).run_and_assert() |
| 27 | + |
| 28 | + def test_vector_param_multiply(self): |
| 29 | + np.random.seed(0) |
| 30 | + n = 5 |
| 31 | + a = cp.Parameter(n, nonneg=True) |
| 32 | + a.value = np.random.rand(n) + 0.1 |
| 33 | + x = cp.Variable(n, bounds=[0.5, 2]) |
| 34 | + prob = cp.Problem(cp.Minimize(cp.sum(cp.exp(cp.multiply(a, x))))) |
| 35 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 36 | + DerivativeChecker(prob).run_and_assert() |
| 37 | + |
| 38 | + a.value = np.random.rand(n) + 0.5 |
| 39 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 40 | + DerivativeChecker(prob).run_and_assert() |
| 41 | + |
| 42 | + def test_dense_param_matmul(self): |
| 43 | + np.random.seed(0) |
| 44 | + n = 5 |
| 45 | + P = cp.Parameter((n, n)) |
| 46 | + P.value = np.random.rand(n, n) |
| 47 | + x = cp.Variable(n, bounds=[0.5, 2]) |
| 48 | + prob = cp.Problem(cp.Minimize(cp.sum(cp.exp(P @ x)))) |
| 49 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 50 | + DerivativeChecker(prob).run_and_assert() |
| 51 | + |
| 52 | + P.value = np.random.rand(n, n) * 2 |
| 53 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 54 | + DerivativeChecker(prob).run_and_assert() |
| 55 | + |
| 56 | + def test_sparse_param_matmul(self): |
| 57 | + np.random.seed(0) |
| 58 | + n = 6 |
| 59 | + mask = np.random.rand(n, n) > 0.6 |
| 60 | + rows, cols = np.where(mask) |
| 61 | + P = cp.Parameter((n, n), sparsity=(rows, cols)) |
| 62 | + P.value_sparse = sparse.coo_array( |
| 63 | + (np.random.rand(len(rows)), (rows, cols)), shape=(n, n)) |
| 64 | + x = cp.Variable(n, bounds=[0.5, 2]) |
| 65 | + prob = cp.Problem(cp.Minimize(cp.sum(cp.exp(P @ x)))) |
| 66 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 67 | + DerivativeChecker(prob).run_and_assert() |
| 68 | + |
| 69 | + P.value_sparse = sparse.coo_array( |
| 70 | + (np.random.rand(len(rows)) * 2, (rows, cols)), shape=(n, n)) |
| 71 | + prob.solve(solver=cp.IPOPT, nlp=True, verbose=False) |
| 72 | + DerivativeChecker(prob).run_and_assert() |
0 commit comments