Skip to content

Commit e335ef3

Browse files
authored
Adds sparsity for powerflow and fix hessian indexing bug (#107)
* adds change for powerflow example * adds hessian tests for idx * reformat imports in problem * remove remove_later.py * remove pdf that was committed accidently * fix hess index test * adds back original formulation
1 parent 29a3bb4 commit e335ef3

File tree

4 files changed

+267
-34
lines changed

4 files changed

+267
-34
lines changed

cvxpy/atoms/affine/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def _verify_hess_vec_args(self):
253253

254254
def _hess_vec(self, vec):
255255
""" See the docstring of the hess_vec method of the atom class. """
256-
idx = self.key
256+
idx = np.reshape(self._select_mat, self._select_mat.size, order='F')
257257
e = np.zeros(self.args[0].size)
258258
e[idx] = vec
259259
return self.args[0].hess_vec(e)

cvxpy/problems/problem.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from cvxpy.problems.objective import Maximize, Minimize
3838
from cvxpy.reductions import InverseData
3939
from cvxpy.reductions.chain import Chain
40+
from cvxpy.reductions.cvx_attr2constr import CvxAttr2Constr
4041
from cvxpy.reductions.dgp2dcp.dgp2dcp import Dgp2Dcp
4142
from cvxpy.reductions.dnlp2smooth.dnlp2smooth import Dnlp2Smooth
4243
from cvxpy.reductions.dqcp2dcp import dqcp2dcp
@@ -1220,7 +1221,7 @@ def _solve(self,
12201221
reductions = [FlipObjective()]
12211222
else:
12221223
reductions = []
1223-
reductions = reductions + [Dnlp2Smooth()]
1224+
reductions = reductions + [CvxAttr2Constr(reduce_bounds=False), Dnlp2Smooth()]
12241225
# instantiate based on user provided solver
12251226
# (default to Ipopt)
12261227
if solver is s.IPOPT or solver is None:

cvxpy/tests/NLP_tests/hess_tests/test_hess_idx.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import numpy as np
2+
import pytest
23

34
import cvxpy as cp
45

56

6-
class TestHessAdd():
7-
7+
class TestHessIndex():
88

99
def test_single_idx(self):
1010
n = 3
@@ -34,7 +34,6 @@ def test_slice_two_idx(self):
3434
computed_hess[rows, cols] = vals
3535
assert(np.allclose(computed_hess, correct_matrix))
3636

37-
3837
def test_slice_two_other_idx(self):
3938
n = 3
4039
x = cp.Variable((n, ), name='x')
@@ -49,3 +48,39 @@ def test_slice_two_other_idx(self):
4948
rows, cols, vals = result_dict[(x, x)]
5049
computed_hess[rows, cols] = vals
5150
assert(np.allclose(computed_hess, correct_matrix))
51+
52+
def test_special_index_matrix(self):
53+
"""
54+
This test was failing because hess_vec was not properly handling
55+
matrix indexing.
56+
"""
57+
x = cp.Variable((2, 2), name='x')
58+
x.value = np.array([[1.0, 2.0], [3.0, 4.0]])
59+
vec = np.array([5, 6])
60+
rows, cols = np.array([0, 0]), np.array([0, 1])
61+
logx = cp.log(x)[rows, cols]
62+
result_dict = logx.hess_vec(vec)
63+
correct_matrix = 5 * (-np.diag(np.array([1.0, 0, 0, 0]))) + \
64+
6 * (-np.diag(np.array([0, 0, 1/4, 0])))
65+
computed_hess = np.zeros((4, 4))
66+
rows, cols, vals = result_dict[(x, x)]
67+
computed_hess[rows, cols] = vals
68+
assert(np.allclose(computed_hess, correct_matrix))
69+
70+
@pytest.mark.skip(reason="TODO fix this test for duplicate indices")
71+
def test_special_index_duplicate_matrix(self):
72+
"""
73+
TODO fix this test
74+
"""
75+
x = cp.Variable((2, 2), name='x')
76+
x.value = np.array([[1.0, 2.0], [3.0, 4.0]])
77+
vec = np.array([5, 6])
78+
rows, cols = np.array([0, 0]), np.array([0, 0])
79+
logx = cp.log(x)[rows, cols]
80+
result_dict = logx.hess_vec(vec)
81+
correct_matrix = 5 * (-np.diag(np.array([1.0, 0, 0, 0]))) + \
82+
6 * (-np.diag(np.array([1.0, 0, 0, 0])))
83+
computed_hess = np.zeros((4, 4))
84+
rows, cols, vals = result_dict[(x, x)]
85+
computed_hess[rows, cols] = vals
86+
assert(np.allclose(computed_hess, correct_matrix))

examples/nlp_examples/power_flow.ipynb

Lines changed: 226 additions & 29 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)