def test_sum_smallest(self): """Test the sum_smallest atom and related atoms. """ with self.assertRaises(Exception) as cm: cp.sum_smallest(self.x, -1) self.assertEqual(str(cm.exception), "Second argument must be a positive integer.") with self.assertRaises(Exception) as cm: cp.lambda_sum_smallest(Variable((2, 2)), 2.4) self.assertEqual(str(cm.exception), "Second argument must be a positive integer.")
def test_eigval_atoms(self): """Test eigenvalue atoms. """ P = np.arange(9) - 2j*np.arange(9) P = np.reshape(P, (3, 3)) P1 = np.conj(P.T).dot(P)/10 + np.eye(3)*.1 P2 = np.array([[10, 1j, 0], [-1j, 10, 0], [0, 0, 1]]) for P in [P1, P2]: value = cvx.lambda_max(P).value X = Variable(P.shape, complex=True) prob = Problem(cvx.Minimize(cvx.lambda_max(X)), [X == P]) result = prob.solve(solver=cvx.SCS, eps=1e-6) self.assertAlmostEqual(result, value, places=2) eigs = np.linalg.eigvals(P).real value = cvx.sum_largest(eigs, 2).value X = Variable(P.shape, complex=True) prob = Problem(cvx.Minimize(cvx.lambda_sum_largest(X, 2)), [X == P]) result = prob.solve(solver=cvx.SCS, eps=1e-8) self.assertAlmostEqual(result, value, places=3) self.assertItemsAlmostEqual(X.value, P, places=3) value = cvx.sum_smallest(eigs, 2).value X = Variable(P.shape, complex=True) prob = Problem(cvx.Maximize(cvx.lambda_sum_smallest(X, 2)), [X == P]) result = prob.solve(solver=cvx.SCS, eps=1e-6) self.assertAlmostEqual(result, value, places=3)
def findPSDlincomb(mats, nullity, **kwdargs): adim = len(mats) (dim, dim2) = np.shape(mats[0]) assert dim == dim2, "Matrices are not square; shape of first matrix in list is: " + str( (dim, dim2)) X = cvx.Variable((dim, dim), PSD=True) a = cvx.Variable((adim)) rhs = sum(a[j] * mats[j] for j in range(adim)) con = [X == rhs, cvx.trace(X) == 1] obj = cvx.Maximize(cvx.lambda_sum_smallest(X, nullity + 1)) prob = cvx.Problem(obj, con) prob.solve(**kwdargs) return X, a, prob
def find_psd_stress(edge_list,P,is_Phat=False,**kwargs): """ Tries to find a PSD stress for dimension d. edge_list is a list of edges [i,j] P is an n x d configuration matrix or an n x (d+1) augmented configuration matrix """ if not is_Phat: P = augmented_config(P) G = nx.Graph() G.add_edges_from(edge_list) Abar = adjacency_matrix(G,True) n,dplusone = P.shape X = cvx.Variable((n,n),PSD=True) obj = cvx.Maximize(cvx.lambda_sum_smallest(X, dplusone+1)) constraints = [cvx.trace(X) == n, X*P == np.zeros((n,dplusone)),cvx.multiply(X,Abar) == np.zeros((n,n))] prob = cvx.Problem(obj, constraints) prob.solve(**kwargs) return X.value, prob.value, prob.status, prob
def getProbability(self): num_subgraphs = len(self.L_matrices) p = cp.Variable(num_subgraphs) L = p[0] * self.L_matrices[0] for i in range(num_subgraphs - 1): L += p[i + 1] * self.L_matrices[i + 1] eig = cp.lambda_sum_smallest(L, 2) sum_p = p[0] for i in range(num_subgraphs - 1): sum_p += p[i + 1] # cvx optimization for activation probabilities obj_fn = eig constraint = [sum_p <= num_subgraphs * self.commBudget, p >= 0, p <= 1] problem = cp.Problem(cp.Maximize(obj_fn), constraint) problem.solve(solver='CVXOPT', kktsolver=cp.ROBUST_KKTSOLVER) # get solution tmp_p = p.value originActivationRatio = np.zeros((num_subgraphs)) for i, pval in enumerate(tmp_p): originActivationRatio[i] = np.real(float(pval)) return np.minimum(originActivationRatio, 1)
-.1]], Constant([4.6, -8.1])), (lambda x: cp.diff(x, 1, axis=0), (1, 2), [np.array([[-5, -3], [2, 1]])], Constant([[7], [4]])), (lambda x: cp.diff(x, 1, axis=1), (2, 1), [np.array([[-5, -3], [2, 1]])], Constant([[2, -1]])), (lambda x: cp.pnorm(x, .5), tuple(), [[1.1, 2, .1]], Constant([7.724231543909264])), (lambda x: cp.pnorm(x, -.4), tuple(), [[1.1, 2, .1]], Constant([0.02713620334])), (lambda x: cp.pnorm(x, -1), tuple(), [[1.1, 2, .1]], Constant([0.0876494023904])), (lambda x: cp.pnorm(x, -2.3), tuple(), [[1.1, 2, .1]], Constant([0.099781528576])), (cp.lambda_min, tuple(), [[[2, 0], [0, 1]]], Constant([1])), (cp.lambda_min, tuple(), [[[5, 7], [7, -3]]], Constant([-7.06225775])), (lambda x: cp.lambda_sum_smallest(x, 2), tuple(), [[[1, 2, 3], [2, 4, 5], [3, 5, 6]]], Constant([-0.34481428])), (cp.log, (2, 2), [[[1, math.e], [math.e**2, 1.0 / math.e]]], Constant([[0, 1], [2, -1]])), (cp.log1p, (2, 2), [[[0, math.e - 1], [math.e**2 - 1, 1.0 / math.e - 1]]], Constant([[0, 1], [2, -1]])), (cp.minimum, (2, ), [[-5, 2], [-3, 1], 0, [1, 2]], Constant([-5, 0])), (cp.minimum, (2, 2), [[[-5, 2], [-3, -1]], 0, [[5, 4], [-1, 2]]], Constant([[-5, 0], [-3, -1]])), (cp.min, tuple(), [[[-5, 2], [-3, 1]]], Constant([-5])), (cp.min, tuple(), [[-5, -10]], Constant([-10])), (lambda x: x**0.25, tuple(), [7.45], Constant([7.45**0.25])), (lambda x: x**0.32, (2, ), [[7.45, 3.9]],
def __init__(self, A, lower_limit): super().__init__() self.constraint_list = [ cvx.lambda_sum_smallest(self.laplacian(A), 2) >= lower_limit ]
(lambda x: cp.diff(x, 2), tuple(), [[1, 2, 3]], Constant([0])), (cp.diff, (3,), [[2.1, 1, 4.5, -.1]], Constant([-1.1, 3.5, -4.6])), (lambda x: cp.diff(x, 2), (2,), [[2.1, 1, 4.5, -.1]], Constant([4.6, -8.1])), (lambda x: cp.diff(x, 1, axis=0), (1, 2), [np.array([[-5, -3], [2, 1]])], Constant([[7], [4]])), (lambda x: cp.diff(x, 1, axis=1), (2, 1), [np.array([[-5, -3], [2, 1]])], Constant([[2, -1]])), (lambda x: cp.pnorm(x, .5), tuple(), [[1.1, 2, .1]], Constant([7.724231543909264])), (lambda x: cp.pnorm(x, -.4), tuple(), [[1.1, 2, .1]], Constant([0.02713620334])), (lambda x: cp.pnorm(x, -1), tuple(), [[1.1, 2, .1]], Constant([0.0876494023904])), (lambda x: cp.pnorm(x, -2.3), tuple(), [[1.1, 2, .1]], Constant([0.099781528576])), (cp.lambda_min, tuple(), [[[2, 0], [0, 1]]], Constant([1])), (cp.lambda_min, tuple(), [[[5, 7], [7, -3]]], Constant([-7.06225775])), (lambda x: cp.lambda_sum_smallest(x, 2), tuple(), [[[1, 2, 3], [2, 4, 5], [3, 5, 6]]], Constant([-0.34481428])), (cp.log, (2, 2), [[[1, math.e], [math.e**2, 1.0 / math.e]]], Constant([[0, 1], [2, -1]])), (cp.log1p, (2, 2), [[[0, math.e - 1], [math.e**2 - 1, 1.0 / math.e - 1]]], Constant([[0, 1], [2, -1]])), (cp.minimum, (2,), [[-5, 2], [-3, 1], 0, [1, 2]], Constant([-5, 0])), (cp.minimum, (2, 2), [[[-5, 2], [-3, -1]], 0, [[5, 4], [-1, 2]]], Constant([[-5, 0], [-3, -1]])), (cp.min, tuple(), [[[-5, 2], [-3, 1]]], Constant([-5])), (cp.min, tuple(), [[-5, -10]], Constant([-10])), (lambda x: x**0.25, tuple(), [7.45], Constant([7.45**0.25])), (lambda x: x**0.32, (2,), [[7.45, 3.9]], Constant(np.power(np.array([7.45, 3.9]), 0.32))), (lambda x: x**0.9, (2, 2), [[[7.45, 2.2], [4, 7]]], Constant(np.power(np.array([[7.45, 2.2], [4, 7]]).T, 0.9))),