def test_sigma_max(self) -> None: """Test sigma_max. """ expr = cp.sigma_max(self.A) self.A.value = [[1, 0], [0, 2]] self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), [0, 0, 0, 1]) self.A.value = [[1, 0], [0, 1]] self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), [1, 0, 0, 0])
def test_largest_singvalue(self) -> None: np.random.seed(3) rows, cols = 3, 4 A = np.random.randn(rows, cols) A_sv = np.linalg.svd(A, compute_uv=False) X = cp.Variable(shape=(rows, cols)) sigma = cp.suppfunc(X, [cp.sigma_max(X) <= 1]) Y = cp.Variable(shape=(rows, cols)) cons = [Y == A] prob = cp.Problem(cp.Minimize(sigma(Y)), cons) prob.solve(solver='SCS', eps=1e-8) actual = prob.value expect = np.sum(A_sv) self.assertLessEqual(abs(actual - expect), 1e-6)
def addObjectiveValue(self, sls, objective_value): C1 = sls._system_model._C1 D12 = sls._system_model._D12 Phi_x = sls._Phi_x Phi_u = sls._Phi_u horizon = len(Phi_x) M = cp.hstack( [C1 @ Phi_x[k] + D12 @ Phi_u[k] for k in range(1, horizon)]) self._objective_expression = cp.sigma_max(M) return objective_value + self._objective_expression