Test the constants method.
(self)
| 132 | problem.solve(solver=cp.SCS) |
| 133 | |
| 134 | def test_constants(self) -> None: |
| 135 | """Test the constants method. |
| 136 | """ |
| 137 | c1 = numpy.random.randn(1, 2) |
| 138 | c2 = numpy.random.randn(2) |
| 139 | p = Problem(cp.Minimize(c1 @ self.x), [self.x >= c2]) |
| 140 | constants_ = p.constants() |
| 141 | ref = [c1, c2] |
| 142 | self.assertEqual(len(ref), len(constants_)) |
| 143 | for c, r in zip(constants_, ref): |
| 144 | self.assertTupleEqual(c.shape, r.shape) |
| 145 | self.assertTrue((c.value == r).all()) |
| 146 | # Allows comparison between numpy matrices and numpy arrays |
| 147 | # Necessary because of the way cvxpy handles numpy arrays and constants |
| 148 | |
| 149 | # Single scalar constants |
| 150 | p = Problem(cp.Minimize(self.a), [self.x >= 1]) |
| 151 | constants_ = p.constants() |
| 152 | ref = [numpy.array(1)] |
| 153 | self.assertEqual(len(ref), len(constants_)) |
| 154 | for c, r in zip(constants_, ref): |
| 155 | self.assertEqual(c.shape, r.shape) and \ |
| 156 | self.assertTrue((c.value == r).all()) |
| 157 | # Allows comparison between numpy matrices and numpy arrays |
| 158 | # Necessary because of the way cvxpy handles numpy arrays and constants |
| 159 | |
| 160 | def test_size_metrics(self) -> None: |
| 161 | """Test the size_metrics method. |