def test_cvx_sdp(): """ Test that the CVX SDP optimization works. """ x1, x2, x3 = cp.Variable(), cp.Variable(), cp.Variable(), A1 = np.matrix([[-7, -11], [-11, 3]]) A2 = np.matrix([[7, -18], [-18, 8]]) A3 = np.matrix([[-2, -8], [-8, 1]]) A0 = np.matrix([[33, -9], [-9, 26]]) B1 = np.matrix([ [-21, -11, 0], [-11, 10, 8], [0, 8, 5], ]) B2 = np.matrix([ [0, 10, 16], [10, -10, -10], [16, -10, 3], ]) B3 = np.matrix([ [-5, 2, -17], [2, -6, -7], [-17, 8, 6], ]) B0 = np.matrix([ [14, 9, 40], [9, 91, 10], [40, 10, 15], ]) prob = cp.Problem(cp.Minimize(x1 - x2 + x3), [ A0 - x1 * A1 - x2 * A2 - x3 * A3 == cp.semidefinite(2), B0 - x1 * B1 - x2 * B2 - x3 * B3 == cp.semidefinite(3), ]) prob.solve() print prob.status x = np.matrix([ x1.value, x2.value, x3.value, ]).T x_ = np.matrix([ [-3.68e-01], [1.90e+00], [-8.88e-01], ]) print x.T, x_.T assert np.allclose(x, x_, atol=1e-2)
def test_cvx_sdp(): """ Test that the CVX SDP optimization works. """ x1, x2, x3 = cp.Variable(), cp.Variable(), cp.Variable(), A1 = np.matrix([[-7, -11], [-11, 3]]) A2 = np.matrix([[7, -18], [-18, 8]]) A3 = np.matrix([[-2, -8], [-8, 1]]) A0 = np.matrix([[33, -9], [-9, 26]]) B1 = np.matrix([[-21, -11, 0], [-11, 10, 8], [0, 8, 5], ]) B2 = np.matrix([[ 0, 10, 16], [10, -10, -10], [16, -10, 3], ]) B3 = np.matrix([[-5, 2, -17], [2, -6, -7], [-17, 8, 6], ]) B0 = np.matrix([[14, 9, 40], [9, 91, 10], [40, 10, 15], ]) prob = cp.Problem( cp.Minimize( x1 - x2 + x3 ), [ A0 - x1 * A1 - x2 * A2 - x3 * A3 == cp.semidefinite(2), B0 - x1 * B1 - x2 * B2 - x3 * B3 == cp.semidefinite(3), ]) prob.solve() print prob.status x = np.matrix([ x1.value, x2.value, x3.value, ]).T x_ = np.matrix([ [-3.68e-01], [ 1.90e+00], [-8.88e-01], ]) print x.T, x_.T assert np.allclose(x, x_, atol=1e-2)
def test_key_error(self): """Test examples that caused key error. """ import cvxpy as cvx x = cvx.Variable() u = -cvx.exp(x) prob = cvx.Problem(cvx.Maximize(u), [x == 1]) prob.solve(verbose=True, solver=cvx.CVXOPT) prob.solve(verbose=True, solver=cvx.CVXOPT) ########################################### import numpy as np import cvxopt import cvxpy as cp kD=2 Sk=cp.semidefinite(kD) Rsk=cp.Parameter(kD,kD) mk=cp.Variable(kD,1) musk=cp.Parameter(kD,1) logpart=-0.5*cp.log_det(Sk)+0.5*cp.matrix_frac(mk,Sk)+(kD/2.)*np.log(2*np.pi) linpart=mk.T*musk-0.5*cp.trace(Sk*Rsk) obj=logpart-linpart prob=cp.Problem(cp.Minimize(obj)) musk.value=np.ones((2,1)) covsk=np.diag([0.3,0.5]) Rsk.value=covsk+(musk.value*musk.value.T) prob.solve(verbose=True,solver=cp.CVXOPT) print "second solve" prob.solve(verbose=False, solver=cp.CVXOPT)
def omega_solve(self, snk, alpha): # Create a variable that is constrained to the positive semidefinite cone. n = len(snk[0]) S = cvx.semidefinite(n) # Form the logdet(S) - tr(SY) objective. Note the use of a set # comprehension to form a set of the diagonal elements of S*Y, and the # native sum function, which is compatible with cvxpy, to compute the trace. # TODO: If a cvxpy trace operator becomes available, use it! obj = cvx.Minimize(-cvx.log_det(S) + sum([(S * np.array(snk))[i, i] for i in range(n)])) # Set constraint. constraints = [cvx.sum_entries(cvx.abs(S)) <= alpha] # Form and solve optimization problem prob = cvx.Problem(obj, constraints) prob.solve() if prob.status != cvx.OPTIMAL: raise Exception('CVXPY Error') # If the covariance matrix R is desired, here is how it to create it. # Threshold S element values to enforce exact zeros: S = S.value S[abs(S) <= 1e-4] = 0 return np.array(S).tolist()
def test_cvx_polynomial(): """ Test that the SDP relaxation for polynomial optimization works on simple examples. In particular, consider min (x-2)^2. We get this to be x^2 - 2x + 1, which is min [1 -2 1]' y, subj to yy' >= 0 """ a = 4.0 A = cp.Variable(2,2) # No equality constraint prob = cp.Problem( cp.Minimize( a**2 * A[0,0] - 2 * a * A[0,1] + A[1,1] ), [ A == cp.semidefinite(2), A[0,0] == 1. ]) prob.solve() print prob.status print A.value assert False
def cvx_solve(self, obj, verbose=False): """Solve an objective function with cvx. obj: vector representing the linear objective to be minimized verbose: whether cvx should print verbose output to stdout Returns a pair representing the optimum of the psd and nsd components (None if that component is either infeasible or unbounded along the optimization direction). Also sets psd_spec, nsd_spec, and fully_(un)bounded_directions. """ import cvxpy as cvx x = cvx.Variable(name="x") y = cvx.Variable(name="y") z = cvx.Variable(name="z") # dummy variable to code semidefinite constraint T = cvx.semidefinite(5, name="T") spec = self.A * x + self.B * y + self.C * z + self.D objective = cvx.Minimize(obj[0] * x + obj[1] * y + obj[2] * z) out_psd = out_nsd = None # check PSD component if self.psd_spec: psd = cvx.Problem(objective, [T == spec]) psd.solve(verbose=verbose) if psd.status == cvx.OPTIMAL: out_psd = [x.value, y.value, z.value] elif psd.status == cvx.INFEASIBLE: self.psd_spec = False # check NSD component if self.nsd_spec: nsd = cvx.Problem(objective, [T == -spec]) nsd.solve(verbose=verbose) if nsd.status == cvx.OPTIMAL: out_nsd = [x.value, y.value, z.value] if self.psd_spec and psd.status == cvx.OPTIMAL: self.fully_bounded_directions += 1 elif nsd.status == cvx.UNBOUNDED and self.psd_spec and psd.status == cvx.UNBOUNDED: self.fully_unbounded_directions += 1 elif nsd.status == cvx.INFEASIBLE: self.nsd_spec = False return out_psd, out_nsd
def __setup(self): M = self.__M Rreal = cp.semidefinite(2*M, 'Rreal') # Rreal = [B1 B3 # B2 B4 ] # = [ReR -ImR # ImR ReR] B1 = Rreal[0:M, 0:M] B2 = Rreal[M:(2*M), 0:M] B3 = Rreal[0:M, M:(2*M)] B4 = Rreal[M:(2*M), M:(2*M)] rho = cp.Variable(1+M**2, 1, 'rho') constraints = [] # the list of the constraint equations and inequations constraints.append(0 <= rho[0]) k = 1 for q in range(M): for p in range(M): if p == q: constraints.append(rho[k] == B1[p, q]) k += 1 elif p > q: constraints.append(rho[k] == B1[p, q]) k += 1 constraints.append(rho[k] == B2[p, q]) k += 1 constraints.append(B1 == B4) constraints.append(B1 == B1.T) constraints.append(B3 == -B2) constraints.append(B3 == -B3.T) constraints.extend([B1[k,k]==1 for k in range(M)]) objective = cp.Minimize(cp.quad_form(rho, self.__Gamma)) problem = cp.Problem(objective, constraints) self.__problem = problem self.__ReR = B1 self.__ImR = B2
def __setup(self): M = self.__M Rreal = cp.semidefinite(2 * M, 'Rreal') # Rreal = [B1 B3 # B2 B4 ] # = [ReR -ImR # ImR ReR] B1 = Rreal[0:M, 0:M] B2 = Rreal[M:(2 * M), 0:M] B3 = Rreal[0:M, M:(2 * M)] B4 = Rreal[M:(2 * M), M:(2 * M)] rho = cp.Variable(1 + M**2, 1, 'rho') constraints = [ ] # the list of the constraint equations and inequations constraints.append(0 <= rho[0]) k = 1 for q in range(M): for p in range(M): if p == q: constraints.append(rho[k] == B1[p, q]) k += 1 elif p > q: constraints.append(rho[k] == B1[p, q]) k += 1 constraints.append(rho[k] == B2[p, q]) k += 1 constraints.append(B1 == B4) constraints.append(B1 == B1.T) constraints.append(B3 == -B2) constraints.append(B3 == -B3.T) constraints.extend([B1[k, k] == 1 for k in range(M)]) objective = cp.Minimize(cp.quad_form(rho, self.__Gamma)) problem = cp.Problem(objective, constraints) self.__problem = problem self.__ReR = B1 self.__ImR = B2
def graphlasso(X, tauk, muk, lambd): # tauk nxk matrix, muk: pxk Ss = [] for k in range(len(muk)): Xc = X - muk[k] cluster_cov = np.array([ np.array(np.mat(vect).T * np.mat(vect)) for vect in Xc ]) #check perf with np.dot(X.T, X) and take diag Snk = np.dot(cluster_cov.T, tauk[:, k]) S = cvx.semidefinite(len(muk[0])) obj = cvx.Maximize( cvx.log_det(S) - sum([(S * Snk)[i, i] for i in range(len(muk))])) constraints = [cvx.sum_entries(cvx.abs(S)) <= lambd[k]] # Form and solve optimization problem prob = cvx.Problem(obj, constraints) prob.solve() if prob.status != cvx.OPTIMAL: print "error" raise Exception('CVXPY Error') S = S.value # S[abs(S) <= 1e-4] = 0 #force exact zeros Ss += [S] return Ss
def test_cvx_polynomial(): """ Test that the SDP relaxation for polynomial optimization works on simple examples. In particular, consider min (x-2)^2. We get this to be x^2 - 2x + 1, which is min [1 -2 1]' y, subj to yy' >= 0 """ a = 4.0 A = cp.Variable(2, 2) # No equality constraint prob = cp.Problem(cp.Minimize(a**2 * A[0, 0] - 2 * a * A[0, 1] + A[1, 1]), [A == cp.semidefinite(2), A[0, 0] == 1.]) prob.solve() print prob.status print A.value assert False
# -*- coding: utf-8 -*- """ Created on Mon Jul 14 13:45:05 2014 @author: cyrusl """ import cvxpy as cvx S = cvx.semidefinite(4) x = cvx.vstack(0.1, 0.2, -0.05, 0.1) obj = cvx.Maximize(x.T*S*x) constraints = [S[0,0] == 0.2] constraints += [S[1,1] == 0.1] constraints += [ S[2,2] == 0.3] constraints += [ S[3,3] == 0.1 ] constraints += [ S[0,1] >= 0 ] constraints += [ S[0,2] >= 0 ] constraints += [ S[1,2] <= 0 ] constraints += [ S[1,3] <= 0 ] constraints += [ S[2,3] >= 0 ] #constraints += [ S[0,3] == 0 ] prob = cvx.Problem(obj, constraints) prob.solve() print "status:", prob.status print "optimal value:", sqrt(prob.value) print "optimal covariance matrix:", S.value
# # create data P # P = cvxopt.matrix(np.matrix('4 1 3; 1 3.5 0.8; 3 0.8 1')) # Z = cvx.Variable(3,3) # objective = cvx.Minimize( sum(cvx.square(P - Z)) ) # constr = [cvx.constraints.semi_definite.SDP(P)] # prob = cvx.Problem(objective, constr) # prob.solve() import cvxpy as cp import numpy as np import cvxopt # create data P P = cp.Parameter(3,3) Z = cp.semidefinite(3) objective = cp.Minimize( cp.lambda_max(P) - cp.lambda_min(P - Z) ) prob = cp.Problem(objective, [Z >= 0]) P.value = cvxopt.matrix(np.matrix('4 1 3; 1 3.5 0.8; 3 0.8 1')) prob.solve() print "optimal value =", prob.value # [ 4, 1+2*j, 3-j ; ... # 1-2*j, 3.5, 0.8+2.3*j ; ... # 3+j, 0.8-2.3*j, 4 ]; # # % Construct and solve the model # n = size( P, 1 ); # cvx_begin sdp
def TVGL(data, lengthOfSlice, lamb, beta, indexOfPenalty, verbose=False, eps=3e-3, epsAbs=1e-3, epsRel=1e-3): if indexOfPenalty == 1: print('Use l-1 penalty function') penalty = inferGraphL1 elif indexOfPenalty == 2: print('Use l-2 penalty function') penalty = inferGraphL2 elif indexOfPenalty == 3: print('Use laplacian penalty function') penalty = inferGraphLaplacian elif indexOfPenalty == 4: print('Use l-inf penalty function') penalty = inferGraphLinf else: print('Use perturbation node penalty function') penalty = inferGraphPN numberOfTotalSamples = data.shape[0] timestamps = int(numberOfTotalSamples / lengthOfSlice) size = data.shape[1] # Generate empirical covariance matrices sampleSet = [] # list of array k = 0 empCovSet = [] # list of array for i in range(timestamps): # Generate the slice of samples for each timestamp from data k_next = min(k + lengthOfSlice, numberOfTotalSamples) samples = data[k:k_next, :] k = k_next sampleSet.append(samples) empCov = GenEmpCov(sampleSet[i].T) empCovSet.append(empCov) # delete: for checking print((sampleSet.__len__())) # # print empCovSet print(('lambda = %s, beta = %s' % (lamb, beta))) # Define a graph representation to solve gvx = penalty.TGraphVX() for i in range(timestamps): n_id = i S = cvxpy.semidefinite(size, name='S') obj = -cvxpy.log_det(S) + cvxpy.trace( empCovSet[i] * S) #+ alpha*norm(S,1) gvx.AddNode(n_id, obj) if (i > 0): #Add edge to previous timestamp prev_Nid = n_id - 1 currVar = gvx.GetNodeVariables(n_id) prevVar = gvx.GetNodeVariables(prev_Nid) edge_obj = beta * cvxpy.norm(currVar['S'] - prevVar['S'], indexOfPenalty) gvx.AddEdge(n_id, prev_Nid, Objective=edge_obj) #Add rake nodes, edges gvx.AddNode(n_id + timestamps) gvx.AddEdge(n_id, n_id + timestamps, Objective=lamb * cvxpy.norm(S, 1)) # need to write the parameters of ADMM # gvx.Solve() gvx.Solve(EpsAbs=epsAbs, EpsRel=epsRel, Verbose=verbose) # gvx.Solve(MaxIters = 700, Verbose = True, EpsAbs=eps_abs, EpsRel=eps_rel) # gvx.Solve( NumProcessors = 1, MaxIters = 3) # Extract the set of estimated theta thetaSet = [] for nodeID in range(timestamps): val = gvx.GetNodeValue(nodeID, 'S') thetaEst = upper2FullTVGL(val, eps) thetaSet.append(thetaEst) return thetaSet, empCovSet, gvx.status, gvx
def _generate_matrix_variables(self): self._true_cov_matrix = cv.semidefinite(self._n, name=u"true_cov_matrix") self._error_cov_matrix = cv.Variable(self._n, self._n, name=u"error_cov_matrix")
# # create data P # P = cvxopt.matrix(np.matrix('4 1 3; 1 3.5 0.8; 3 0.8 1')) # Z = cvx.Variable(3,3) # objective = cvx.Minimize( sum(cvx.square(P - Z)) ) # constr = [cvx.constraints.semi_definite.SDP(P)] # prob = cvx.Problem(objective, constr) # prob.solve() import cvxpy as cp import numpy as np import cvxopt # create data P P = cp.Parameter(3,3) Z = cp.semidefinite(3) objective = cp.Minimize( cp.lambda_max(P) - cp.lambda_min(P - Z) ) prob = cp.Problem(objective, 10*[Z >= 0]) P.value = cvxopt.matrix(np.matrix('4 1 3; 1 3.5 0.8; 3 0.8 1')) prob.solve() # [ 4, 1+2*j, 3-j ; ... # 1-2*j, 3.5, 0.8+2.3*j ; ... # 3+j, 0.8-2.3*j, 4 ]; # # % Construct and solve the model # n = size( P, 1 ); # cvx_begin sdp