def GroupGraphicLasso(X, rho, alpha ,groups): """ S is the empirical covariance matrix. """ S = np.cov(X.T) assert S.shape[0] == S.shape[1], "Matrix must be square" n = S.shape[0] #Phi = cvx.Variable(n, n) Phi = cvx.Semidef(n) rest = cvx.Semidef(n) group_pennal=[] non_one_pennal=[] A=set() for group in groups: group_pennal.append(cvx.norm(Phi[group,group],"fro")) non_index=nonGroupIndex(group, n) non_one_pennal.append(cvx.norm(Phi[group][:,non_index],1)) A.update(set(group)) non_block = [i for i in range(n) if i not in A] if len(non_block) > 0: block_onenorm = cvx.norm(Phi[:,non_block],1) obj = cvx.Minimize(-(cvx.log_det(Phi) - cvx.trace(S*Phi) - rho*sum(group_pennal) - alpha * sum(non_one_pennal)- alpha * block_onenorm)) else: obj = cvx.Minimize(-(cvx.log_det(Phi) - cvx.trace(S*Phi) - rho*sum(group_pennal) - alpha * sum(non_one_pennal))) constraints = [] prob = cvx.Problem(obj,constraints) prob.solve(solver=cvx.SCS, eps=1e-5) return Phi.value
def test_problem_ubsdp(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, sum, abs, norm2, log, square, zeros, max, hstack, vstack, eye, eq, trace, semidefinite_cone, belongs, reshape) (m, n) = (self.m, self.n) c = matrix(self.c) A = matrix(self.A) B = matrix(self.B) Xubsdp = matrix(self.Xubsdp) tol_exp = 6 # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # X + S = I # X >= 0, S >= 0. # # c is an n-vector. # A is an m^2 x n-matrix. # B is an m x m-matrix. X = variable(m, n) S = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X) + c[i,0], 0.0) for i in range(n)] constr += [eq(X + S, eye(m))] constr += [belongs(X, semidefinite_cone), belongs(S, semidefinite_cone)] p = program(minimize(trace(B * X)), constr) p.solve(True) np.testing.assert_array_almost_equal( X.value, self.Xubsdp, tol_exp) # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # 0 <= X <= I X2 = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X2) + c[i,0], 0.0) for i in range(n)] constr += [belongs(X2, semidefinite_cone), belongs(eye(m) - X2, semidefinite_cone)] p = program(minimize(trace(B * X2)), constr) p.solve(True) np.testing.assert_array_almost_equal( X2.value, X.value, tol_exp)
def sys_norm_h2_LMI(Acl, Bdisturbance, C): #doesn't work very well, if problem poorly scaled Riccati works better. #Dullerud p 210 n = Acl.shape[0] X = cvxpy.Semidef(n) Y = cvxpy.Semidef(n) constraints = [ Acl*X + X*Acl.T + Bdisturbance*Bdisturbance.T == -Y, ] obj = cvxpy.Minimize(cvxpy.trace(Y)) prob = cvxpy.Problem(obj, constraints) prob.solve() eps = 1e-16 if np.max(np.linalg.eigvals((-Acl*X - X*Acl.T - Bdisturbance*Bdisturbance.T).value)) > -eps: print('Acl*X + X*Acl.T +Bdisturbance*Bdisturbance.T is not neg def.') return np.Inf if np.min(np.linalg.eigvals(X.value)) < eps: print('X is not pos def.') return np.Inf return np.sqrt(np.trace(C*X.value*C.T))
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 synth_h2_state_feedback_LMI(A, Binput, Bdist, C1, D12): #Dullerud p 217 (?) n = A.shape[0] #num states m = Binput.shape[1] #num control inputs q = C1.shape[0] #num outputs to "be kept small" X = cvxpy.Variable(n,n) Y = cvxpy.Variable(m,n) Z = cvxpy.Variable(q,q) tmp1 = cvxpy.hstack(X, (C1*X+D12*Y).T) tmp2 = cvxpy.hstack((C1*X+D12*Y), Z) tmp = cvxpy.vstack(tmp1, tmp2) constraints = [A*X + Binput*Y + X*A.T + Y.T*Binput.T + Bdist*Bdist.T == -cvxpy.Semidef(n), tmp == cvxpy.Semidef(n+q), ] obj = cvxpy.Minimize(cvxpy.trace(Z)) prob = cvxpy.Problem(obj, constraints) prob.solve(solver='CVXOPT', kktsolver='robust') K = -Y.value*np.linalg.inv(X.value) return K
def basic_glasso(target_dim, cov_mat, reg=0.1, norm=1, return_inv=False, verbose=False): theta = cvx.Semidef(target_dim) objective = cvx.Minimize(-cvx.log_det(theta) + cvx.trace(cov_mat*theta) + reg*cvx.norm(theta, norm)) problem = cvx.Problem(objective) problem.solve(verbose=verbose) out = theta.value if return_inv: out = np.linalg.inv(out) return out
def test_sdp(self): np.random.seed(0) n = 3 p = 3 C = cp.Parameter((n, n)) As = [cp.Parameter((n, n)) for _ in range(p)] bs = [cp.Parameter((1, 1)) for _ in range(p)] C.value = np.random.randn(n, n) for A, b in zip(As, bs): A.value = np.random.randn(n, n) b.value = np.random.randn(1, 1) X = cp.Variable((n, n), PSD=True) constraints = [cp.trace(As[i] @ X) == bs[i] for i in range(p)] problem = cp.Problem(cp.Minimize(cp.trace(C @ X) + cp.sum_squares(X)), constraints) gradcheck(problem, atol=1e-3) perturbcheck(problem)
def unentangled_value(self) -> float: r""" Calculate the unentangled value of an extended nonlocal game. The *unentangled value* of an extended nonlocal game is the supremum value for Alice and Bob's winning probability in the game over all unentangled strategies. Due to convexity and compactness, it is possible to calculate the unentangled extended nonlocal game by: .. math:: \omega(G) = \max_{f, g} \lVert \sum_{(x,y) \in \Sigma_A \times \Sigma_B} \pi(x,y) V(f(x), g(y)|x, y) \rVert where the maximum is over all functions :math:`f : \Sigma_A \rightarrow \Gamma_A` and :math:`g : \Sigma_B \rightarrow \Gamma_B`. :return: The unentangled value of the extended nonlocal game. """ dim_x, dim_y, alice_out, bob_out, alice_in, bob_in = self.pred_mat.shape max_unent_val = float("-inf") for a_out in range(alice_out): for b_out in range(bob_out): p_win = np.zeros([dim_x, dim_y], dtype=complex) for x_in in range(alice_in): for y_in in range(bob_in): p_win += ( self.prob_mat[x_in, y_in] * self.pred_mat[:, :, a_out, b_out, x_in, y_in]) rho = cvxpy.Variable((dim_x, dim_y), hermitian=True) objective = cvxpy.Maximize( cvxpy.real(cvxpy.trace(p_win.conj().T @ rho))) constraints = [cvxpy.trace(rho) == 1, rho >> 0] problem = cvxpy.Problem(objective, constraints) unent_val = problem.solve() max_unent_val = max(max_unent_val, unent_val) return max_unent_val
def dnorm_problem(dim): X, constraints = initialize_constraints_on_dnorm_problem(dim) Jr = cvxpy.Parameter((dim**2, dim**2)) Ji = cvxpy.Parameter((dim**2, dim**2)) # The objective, however, depends on J. objective = cvxpy.Maximize(cvxpy.trace( Jr.T @ X.re + Ji.T @ X.im )) problem = cvxpy.Problem(objective, constraints) return problem, Jr, Ji
def optimize_L_primal(Y): L = cp.Variable((n, n), symmetric=True) obj = cp.Minimize(alpha * cp.trace(Y.T @ L @ Y) + beta * (cp.norm(L, p='fro')**2)) constraints = [cp.trace(L) == n] constraints += [L >> 0] constraints += [L @ np.ones((n)) == np.zeros((n))] for i in range(n): for j in range(i + 1, n): constraints += [L[i, j] <= 0] prob = cp.Problem(obj, constraints) p_star = prob.solve() print(f"primal value is {p_star}") L_opt = L.value return L_opt
def sdr_problem(objective, constraints, minimize=True, unit_ball=False): """Creates a semidefinite relaxation (SDR) problem for the quadratically constrained quadratic program (QCQP) given in the objective and constraints. Parameters ---------- objective : QuadraticForm A quadratic expression for the objective function. constraints: set or list of QuadraticForm The constraints of the type x'Qx + 2x'b + c <= 0. minimize: bool, optional Whether the problem is of minimization (true, default) or of maximization. unit_ball : bool, optional Add the SDR of the constraint x'T @ x == 1, restraining the problem to the unit ball. The default is false. Returns ------- cvxpy.Problem The SDR problem See Also -------- QuadraticForm.sdr_expression : SDR relaxation of a quadratic expression. """ if objective: n = objective.n else: for con in constraints: break n = con.n objective = QuadraticForm(np.zeros((n, n))) # Create variables and build SDR problem X = cvx.Variable((n, n), PSD=True) if objective.b is None and all(Q.b is None for Q in constraints): obj = objective.sdr_expression(X) con = [Q.sdr_expression(X) <= 0 for Q in constraints] else: x = cvx.Variable((n, 1)) obj = objective.sdr_expression(X, x) con = [Q.sdr_expression(X, x) <= 0 for Q in constraints] # Below: [X x; x' 1] >= 0 con.append(cvx.bmat([[X, x], [x.T, np.array([[1]])]]) >> 0) if unit_ball: con.append(cvx.trace(X) == 1) if minimize: return cvx.Problem(cvx.Minimize(obj), con) else: return cvx.Problem(cvx.Maximize(obj), con)
def test_family(A, M, constraints): objective = cvx.Maximize(cvx.trace(A * M)) problem = cvx.Problem(objective, constraints) problem.solve(solver=cvx.MOSEK) if problem.status == 'optimal': return problem.value else: return np.nan
def variable_UNF_state(da, db, hermitian=True): """ SDP2 FROM THE PAPER - D=2 CASE Creates a bipartite state as a variable, with the constraints for it to be unfaithful da, db: Dimensions of the two subsystems hermitian If True, rho and constraints are complex, if false, only real OUTPUT: constraints: list of SDP constraints for cvxpy rho: cvxpy variable matrix that, when optimised over the constraints, is the state This is equivalent to variable_D_UNF_state(da, db, D, hermitian), but faster. """ rho = _make_cp_matrix((da * db, da * db), hermitian) Na = _make_cp_matrix((da, da), hermitian) Nb = _make_cp_matrix((db, db), hermitian) constraints = [rho >> 0, cp.trace(rho) == 1, Na >> 0, Nb >> 0] constraints += [tensor(Na, np.eye(db)) + tensor(np.eye(da), Nb) >> rho] constraints += [cp.trace(Na) + cp.trace(Nb) == 1] return constraints, rho
def admm(X, Lt, gradient, Htp1, taut, dt, beta, verbose): S = len(taut) # variable L = cp.Variable(Lt.shape) N = Lt.shape[0] # constraints constraints = [ cp.trace(L) == N, L.T == L, L @ np.ones((N, 1)) == np.zeros((N, 1)) ] for i in range(N - 1): constraints += [L[i][i + 1:] <= 0] # objective obj = cp.Minimize(cp.trace(gradient.T@(L-Lt)) \ + dt/2*(cp.norm(L-Lt, 'fro')**2) + beta*(cp.norm(L, 'fro')**2)) # solve problem prob = cp.Problem(obj, constraints) prob.solve(verbose=verbose, solver=cp.SCS, scale=1000, use_indirect=False) if L.value is None: prob.solve(verbose=verbose, solver=cp.MOSEK) return L.value
def main(n=3, p=3): # Generate problem data C = randn_psd(n) As = [randn_symm(n) for _ in range(p)] Bs = np.random.randn(p) # Extract problem data using cvxpy X = cp.Variable((n, n), PSD=True) objective = cp.trace(C @ X) constraints = [cp.trace(As[i] @ X) == Bs[i] for i in range(p)] prob = cp.Problem(cp.Minimize(objective), constraints) A, b, c, cone_dims = scs_data_from_cvxpy_problem(prob) # Print problem size mn_plus_m_plus_n = A.size + b.size + c.size n_plus_2n = c.size + 2 * b.size entries_in_derivative = mn_plus_m_plus_n * n_plus_2n print( f"""n={n}, p={p}, A.shape={A.shape}, nnz in A={A.nnz}, derivative={mn_plus_m_plus_n}x{n_plus_2n} ({entries_in_derivative} entries)""" ) # Compute solution and derivative maps start = time.perf_counter() x, y, s, derivative, adjoint_derivative = diffcp.solve_and_derivative( A, b, c, cone_dims, eps=1e-5) end = time.perf_counter() print("Compute solution and set up derivative: %.2f s." % (end - start)) # Derivative lsqr_args = dict(atol=1e-5, btol=1e-5) start = time.perf_counter() dA, db, dc = adjoint_derivative(diffcp.cones.vec_symm(C), np.zeros(y.size), np.zeros(s.size), **lsqr_args) end = time.perf_counter() print("Evaluate derivative: %.2f s." % (end - start)) # Adjoint of derivative start = time.perf_counter() dx, dy, ds = derivative(A, b, c, **lsqr_args) end = time.perf_counter() print("Evaluate adjoint of derivative: %.2f s." % (end - start))
def norm_trace_dist(rho, sigma, sdp=False, dual=False, display=False): ''' Calculates the normalized trace distance (1/2)*||rho-sigma||_1 using an SDP, where rho and sigma are quantum states. More generally, they can be Hermitian operators. ''' if sdp: if not dual: dim = rho.shape[0] L1 = cvx.Variable((dim, dim), hermitian=True) L2 = cvx.Variable((dim, dim), hermitian=True) c = [L1 >> 0, L2 >> 0, eye(dim) - L1 >> 0, eye(dim) - L2 >> 0] obj = cvx.Maximize(cvx.real(cvx.trace((L1 - L2) @ (rho - sigma)))) prob = cvx.Problem(obj, constraints=c) prob.solve(verbose=display, eps=1e-7) return (1 / 2) * prob.value elif dual: dim = rho.shape[0] Y1 = cvx.Variable((dim, dim), hermitian=True) Y2 = cvx.Variable((dim, dim), hermitian=True) c = [Y1 >> 0, Y2 >> 0, Y1 >> rho - sigma, Y2 >> -(rho - sigma)] obj = cvx.Minimize(cvx.real(cvx.trace(Y1 + Y2))) prob = cvx.Problem(obj, c) prob.solve(verbose=display, eps=1e-7) return (1 / 2) * prob.value else: return (1 / 2) * trace_norm(rho - sigma)
def bregman_map_cvx(s_mat, Wk_plus_value, Wk_minus_value, gamma, l1_pen, dagness_pen, dagness_exp): """ Solves argmin g(W) + <grad f (Wk), W-Wk> + 1/gamma * Dh(W, Wk) with CVX this is only implemented for a specific penalty and kernel Args: s_mat (np.array): data matrix Wk_plus_value (np.array): current iterate value for W+ Wk_minus_value (np.array): current iterate value for W- gamma (float): Bregman iteration map param l1_pen (float): lambda in paper dagness_pen (float): mu in paper dagness_exp (float): alpha in paper """ n = s_mat.shape[1] W_plus = cp.Variable((n, n), nonneg=True) W_plus.value = Wk_plus_value W_minus = cp.Variable((n, n), nonneg=True) W_minus.value = Wk_minus_value sum_W = W_plus + W_minus # sum variable obj_ll = cp.norm(s_mat @ (np.eye(n) - W_plus + W_minus), "fro") ** 2 obj_spars = l1_pen * cp.sum(W_plus + W_minus) # Compute C sum_Wk = Wk_plus_value + Wk_minus_value C = compute_C(n, sum_Wk, dagness_pen, dagness_exp, 1/gamma) obj_trace = cp.trace(C @ sum_W) obj_kernel = 1 / gamma * dagness_pen * (n - 1) * (1 + dagness_exp * cp.norm(sum_W, "fro"))**n obj = obj_ll + obj_spars + obj_trace + obj_kernel prob = cp.Problem(cp.Minimize(obj), [cp.sum(W_plus) + cp.sum(W_minus) >= n/((n-2)*dagness_exp)]) prob.solve() if prob.status != "optimal": prob.solve(verbose=True) next_W_plus, next_W_minus = W_plus.value, W_minus.value tilde_W_plus = np.maximum(next_W_plus - next_W_minus, 0.0) tilde_W_minus = np.maximum(next_W_minus - next_W_plus, 0.0) tilde_sum = tilde_W_plus + tilde_W_minus # if np.sum(tilde_sum) >= n / ((n - 2) * dagness_exp): return tilde_W_plus, tilde_W_minus else: return next_W_plus, next_W_minus
def costL1(K, X, S, lam): # print(type(K)) loss = 0. for q in S: i, j, k = q # should this be Mt^T * K?? Mt = (2. * np.outer(X[i], X[j]) - 2. * np.outer(X[i], X[k]) - np.outer(X[j], X[j]) + np.outer(X[k], X[k])) score = cvx.trace(Mt.T * K) loss = loss + cvx.logistic(-score) loss = loss / len(S) + lam * cvx.norm(K, 1) # add L1 penalty return loss
def variable_max_rank_D_state(da, db, D, hermitian=True): """ SDP1 FROM THE PAPER Creates a bipartite state as a variable, with the constraints for it to have Schmidt Rank <= D da, db: Dimensions of the two subsystems D: Maximum Schmidt rank imposed by the constrints hermitian If True, rho and constraints are complex, if false, only real OUTPUT: constraints: list of SDP constraints for cvxpy rho: cvxpy variable matrix that, when optimised over the constraints, is the state """ dT = da * D * D * db proj = tensor(np.eye(da), np.sqrt(D) * max_entangled_ket(D), np.eye(db)) rho = _make_cp_matrix((da * db, da * db), hermitian) sigma = _make_cp_matrix((dT, dT), hermitian) constraints = [rho >> 0, sigma >> 0] constraints += [cp.trace(rho) == 1, cp.trace(sigma) == D] constraints += [proj @ sigma @ proj.T == rho] constraints += [partial_transpose(sigma, [0], [da * D, D * db]) >> 0] return constraints, rho
def main(n=3, p=3): # Generate problem data C = randn_psd(n) As = [randn_symm(n) for _ in range(p)] Bs = np.random.randn(p) # Extract problem data using cvxpy X = cp.Variable((n, n), PSD=True) objective = cp.trace(C @ X) constraints = [cp.trace(As[i] @ X) == Bs[i] for i in range(p)] prob = cp.Problem(cp.Minimize(objective), constraints) A, b, c, cone_dims = scs_data_from_cvxpy_problem(prob) cone_dims = {'f': 25, 's': [50]} print(cone_dims) # Compute solution and derivative maps print(A.shape, b.shape, c.shape, cone_dims) x, y, s, derivative, adjoint_derivative = diffcp.solve_and_derivative( A, b, c, cone_dims, eps=1e-5) return dict(A=A, b=b, c=c), cone_dims
def state_constraint(self): # a) The trace of the sum of variables is 1 self.constraints.append( sum([ cp.trace(self.rho_variable[i]) for i in map(self.StI, self.indices_A1Q1A2Q2_ext) ]) - 1 == 0) # b) Each variable is a positive semidefinite matrix for i in map(self.StI, self.indices_A1Q1A2Q2_ext): self.constraints.append(self.rho_variable[i] >> 0)
def min_dop_same(filename, eps_guess, fig_num): """ Function to maximize DOP within an error tolerance Each rho has its own optimal epsilon in this function -- maybe a problem? :param filename: the name of the file to maximize dop on :param eps: the initial error bound :param fig_num: the number of the figure to be plotted on """ #Read in calculated coherency matrix elements and reconstruct matrices data = pd.read_excel(filename, sheet_name='calculated') size = data.values.shape[0] J_elems = [data.values[i][1:5] for i in range(size)] dop = [] epsilons = [] for i in range(size): J = np.array([[J_elems[i][0], J_elems[i][2] + 1j * J_elems[i][3]], [J_elems[i][2] - 1j * J_elems[i][3], J_elems[i][1]]]) epsilons.append(adaptive_step(J, eps_guess)[1]) #opt_eps = max(epsilons) opt_eps = 3.2 print(epsilons) print(opt_eps) for i in range(size): print(i) J = np.array([[J_elems[i][0], J_elems[i][2] + 1j * J_elems[i][3]], [J_elems[i][2] - 1j * J_elems[i][3], J_elems[i][1]]]) rho = cp.Variable((2, 2), PSD=True) cost = cp.norm(J - rho)**2 constraints = [cp.trace(rho) == 1, cp.norm(J - rho) <= opt_eps] prob = cp.Problem(cp.Minimize(cost), constraints) prob.solve(solver='CVXOPT') temp = rho.value s0 = temp[0][0] + temp[1][1] s1 = temp[0][0] - temp[1][1] s2 = 2 * np.real(temp[0][1]) s3 = 2 * np.imag(temp[0][1]) dop.append(np.sqrt(s1**2 + s2**2 + s3**2) / s0) theta = [data.values[i][0] for i in range(size)] fig = plt.figure(fig_num) ax = fig.add_subplot(111) ax.set_title("Max DOP for " + filename) ax.set_xlabel("theta") ax.set_ylabel("DOP") ax.plot(theta, dop, label='Worst Same')
def compute_rho(filename): """ Least squares minimize the calculated coherency matrix :param filename: the name of the data file (should be an excel file) """ #Construct the dataframe from the file wb = pd.read_excel(filename, sheet_name=None) data = wb['calculated'] size = data.values.shape[0] columns = ['theta', 'Jxx', 'Jyy', 'beta', 'gamma', 'trace_sq'] #Slice coherency matrix elements from the data J_elems = [data.values[i][1:5] for i in range(size)] #Set up list for storing rhos rho_list = [] temp = [] for i in range(size): #Use J_elems to construct matrix J = np.array([[J_elems[i][0], J_elems[i][2] + 1j * J_elems[i][3]], [J_elems[i][2] - 1j * J_elems[i][3], J_elems[i][1]]]) #Construct variable to be minimized rho = cp.Variable((2, 2), PSD=True) #Construct constraint(s) constraints = [cp.trace(rho) == 1] #Construct cost function cost = cp.norm(J - rho)**2 #Construct problem prob = cp.Problem(cp.Minimize(cost), constraints) prob.solve(solver='CVXOPT') rho_list.append(rho.value[0]) #Construct a temporary list to be added to the excel file temp.append([ data.values[i][0], rho.value[0][0], rho.value[1][1], np.real(rho.value[0][1]), np.imag(rho.value[0][1]), np.sum(rho.value.T * rho.value) ]) #Construct the new dataframe and write to the file results = pd.DataFrame(np.array(temp), columns=columns) wb['rho'] = results with pd.ExcelWriter(filename) as writer: for key in wb: wb[key].to_excel(writer, sheet_name=key, index=False)
def generate_single(self, s): n = 3 p = 3 np.random.seed(self.seed[s]) C = np.random.randn(n, n) A = [] b = [] for i in range(p): A.append(np.random.randn(n, n)) b.append(np.random.randn()) # Define and solve the CVXPY problem. # Create a symmetric matrix variable. self.X[s] = cp.Variable((n,n), symmetric=True) # The operator >> denotes matrix inequality. constraints = [self.X[s] >> 0] constraints += [ cp.trace(A[i]@self.X[s]) == b[i] for i in range(p) ] self.prob[s] = cp.Problem(cp.Minimize(cp.trace([email protected][s])), constraints)
def cvxpy_density_matrix_feasibility_sdp_routine(D_matrix,E_matrix,R_matrices,F_matrices,gammas,sdp_tolerance_bound, verbose = True,additionalbetaconstraints=None,betainitialpoint=None):#additional beta constraints comes in list of list , [[matrix,value],[matrix,value]] numstate = len(D_matrix) D_matrix_np = np.array(D_matrix) #D_matrix_np = 0.5*(D_matrix_np + np.conjugate(np.transpose(D_matrix_np))) E_matrix_np = np.array(E_matrix) #E_matrix_np = 0.5*(E_matrix_np + np.conjugate(np.transpose(E_matrix_np))) R_matrices_np = [] F_matrices_np = [] for r in R_matrices: R_matrices_np.append(np.array(r)) for f in F_matrices: F_matrices_np.append(np.array(f)) beta = cp.Variable((numstate,numstate),complex=True) constraints = [beta >> 0] #constraints += [cp.trace(E_matrix_np @ beta) == 1] constraints += [beta.H == beta] if additionalbetaconstraints != None: for con,value in additionalbetaconstraints: constraints += [cp.trace(con@beta)==value] a = -1j*(D_matrix_np@beta@E_matrix_np-E_matrix_np@beta@D_matrix_np) #finalconstraints = [] for i in range(len(gammas)): a = a + gammas[i]*(R_matrices_np[i]@[email protected](np.conjugate(R_matrices_np[i]))-0.5*F_matrices_np[i]@beta@E_matrix_np-0.5*E_matrix_np@beta@F_matrices_np[i]) #a = -1j*(D_matrix_np@beta@E_matrix_np-E_matrix_np@beta@D_matrix_np)+gammas[0]*(R_matrices_np[0]@[email protected](np.conjugate(R_matrices_np[0]))-0.5*F_matrices_np[0]@beta@E_matrix_np-0.5*E_matrix_np@beta@F_matrices_np[0]) #constraints += [cp.trace(E_matrix_np @ beta) == 1] #constraints += [cp.trace(a@(a.H))<=sdp_tolerance_bound] #constraints += [cp.trace(a@(a.H))>=-sdp_tolerance_bound] if sdp_tolerance_bound == 0: if verbose: print('Feasibility SDP is set up with hard equality constraints') #constraints += [a == 0] constraints += [a == 0] # constraints += [cp.trace(E_matrix_np @ beta) == 1] #try not enforcing the trace condition else: print('Feasibility SDP is set up with interval constraint <'+str(sdp_tolerance_bound)+ ' & >-'+str(sdp_tolerance_bound)) raise(RuntimeError("Not implemented yet")) #constraints += [cp.abs(cp.trace(a@(a.H)))<=sdp_tolerance_bound] #constraints += [cp.abs(cp.trace(a@(a.H)))>=-sdp_tolerance_bound] # constraints += [cp.real(cp.trace(E_matrix_np @ beta)) <= 1+sdp_tolerance_bound] # constraints += [cp.real(cp.trace(E_matrix_np @ beta)) >= 1-sdp_tolerance_bound] prob = cp.Problem(cp.Minimize(0),constraints) if type(betainitialpoint)!=type(None): beta.value = betainitialpoint prob.solve(solver=cp.MOSEK,verbose=False) denmat = beta.value denmat = denmat / np.trace(E_matrix_np @ denmat) #print(denmat) if type(denmat) == type(None): return None,None else: minval = np.trace(denmat@D_matrix_np) return denmat,minval
def main(X): ################# # Initialise ################# create_ifne() ################# # Variables ################# # x = cp.Variable((4)) idx = np.where(X > 0) m, n = X.shape r = cp.Variable() X_hat = cp.Variable((m, n)) Y = cp.Variable((m, m), PSD=True) Z = cp.Variable((n, n), PSD=True) A = cp.bmat([[Y, X_hat], [X_hat.T, Z]]) print(A.shape) ################# # Problem Setup ################# obj = cp.Minimize(r) constraints = [ X_hat[idx] == X[idx], cp.trace(Y) + cp.trace(Z) <= 2 * r, X_hat >= 0, A >> 0 ] prob = cp.Problem(obj, constraints) prob.solve() ################# # Inference ################# _, s, _ = np.linalg.svd(X_hat.value) print(f"Status: {prob.status}") print(f"Value of r: {prob.value}") print( f"Actual rank (via np.linalg): {np.linalg.matrix_rank(X_hat.value, tol=1e-6)}" ) print(f"Singular values {s}")
def sdp(rho0, rho1): n = len(rho0) # A and B are positive semidefinite matrices. B is an invertible matrix. A = rho0 B = rho0 + rho1 # Define and solve the CVXPY problem. # Create a symmetric matrix variable. P = cp.Variable((n, n), symmetric=True) x = fractional_matrix_power(B, -0.5) y = fractional_matrix_power(B, 0.5) gamma = y @ P @ y # The operator << denotes matrix inequality. # constraints = [cp.trace(B @ P) <= 1] # constraints += [P >> 0] # constraints += [P << np.eye(2)] constraints = [gamma << B] constraints += [P >> 0] constraints += [cp.trace(gamma) <= 1] prob = cp.Problem(cp.Maximize(cp.trace(x @ A @ x @ gamma)), constraints) prob.solve() # Print result. # print("Input rho_0 is:") # print(rho0) # print("Input rho_1 is:") # print(rho1) ''' Change desired decimal places for solution here ''' value = round(prob.value, ndigits=5) # solution = np.around(P.value, decimals=3) # gammaResult = np.around(y @ P.value @ y, decimals=3) # print("The optimal value is", value) # print("A solution P is") # print(solution) # print("Gamma result is") # print(gammaResult) return value
def solve(W): print("problem size:", W.shape[0]) #dual of original: print("dual of original:") dim = W.shape[0] v = cp.Variable((dim, 1)) constraints = [W + cp.diag(v) >> 0] prob = cp.Problem(cp.Maximize(-cp.sum(v)), constraints) prob.solve() # print("prob.status:", prob.status) lower_bound = 0 if prob.status not in ["infeasible", "unbounded"]: print("Optimal value: %s" % prob.value) lower_bound = prob.value print("lower_bound: ", lower_bound) #dual of relaxed: #restrict to PSD for randomized sampling #on proper covariance matrix later X = cp.Variable((dim, dim)) constraints = [X >> 0, cp.diag(X) == np.ones((dim, ))] prob = cp.Problem(cp.Minimize(cp.trace(cp.matmul(W, X))), constraints) prob.solve(solver=cp.SCS, max_iters=4000, eps=1e-11, warm_start=True) # print("prob.status:", prob.status) if prob.status not in ["infeasible", "unbounded"]: print("Optimal value: %s" % prob.value) ret = prob.variables()[0].value K = 100 #number of samples xs_approx = np.random.multivariate_normal(np.zeros((dim, )), ret, size=(K)) xs_approx = np.sign(xs_approx) #shape: (K,dim) p_best = math.inf for i in range(0, K): p = (xs_approx[i, :].dot(W)).dot(xs_approx[i, :].T) if p < p_best: p_best = p print("best objective (randomized): ", p_best, "size: ", K) print("-----") return p_best, xs_approx
def information_transfer(phiphiT, dqn_feat, target_dqn_feat, replay_buffer, batch_size, num_actions, feat_dim): d = [[] for i in range(num_actions)] phi = [[] for i in range(num_actions)] n = [0 for i in range(num_actions)] print("transforming information") from datetime import datetime fmt = '%Y-%m-%d %H:%M:%S' d1 = datetime.strptime('2010-01-01 17:31:22', fmt) for j in range(batch_size): obs_t, action, reward, obs_tp1, done = replay_buffer.sample(1) # confidence scores for old data c = target_dqn_feat(obs_tp1).reshape((feat_dim, 1)) d[int(action)].append(np.dot(np.dot(c.T, phiphiT[int(action)]), c)) # new data correlations c = dqn_feat(obs_tp1).reshape((feat_dim, 1)) phi[int(action)].append(np.outer(c, c)) n[int(action)] += 1 print(n, sum(n)) precisions_return = [] cov = [] prior = (0.001) * np.eye(feat_dim) print("solving optimization") for a in range(num_actions): print("for action {}".format(a)) if d[a] != []: X = cvx.Variable((feat_dim, feat_dim), PSD=True) # Form objective. obj = cvx.Minimize( sum([(cvx.trace(X * phi[a][i]) - np.squeeze(d[a][i]))**2 for i in range(len(d[a]))])) prob = cvx.Problem(obj) prob.solve() if X.value is None: print("failed - cvxpy couldn't solve for action {}".format(a)) precisions_return.append(np.linalg.inv(prior)) cov.append(prior) else: precisions_return.append(np.linalg.inv(X.value + prior)) cov.append(X.value + prior) else: print("failed - no samples for this action - {}".format(a)) precisions_return.append(np.linalg.inv(prior)) cov.append(prior) d2 = datetime.strptime('2010-01-03 17:31:22', fmt) print("total time for information transfer:") print(d2.minute - d1.minute + (d2.hour - d1.hour) * 60) print(d2.minute - d1.minute + (d2.hour - d1.hour) * 60) return precisions_return, cov
def dilat_lvggm_ccg_cvx_sub(S, alpha, beta, covariance_h, precision_h, max_iter_in=1000, threshold_in=1e-3, verbose=False): ''' A cvx implementation of the Decayed-influence Latent variable Gaussian Graphial Model The subproblem in Convex-Concave Procedure min_{R} -log det(R) + trace(R*S_t) + alpha*||[1,0]*R*[1;0]||_1 + gamma*||[0, Theta]*R*[1;0]||_{1,2} s.t. [0,1]*R*[0;1] = Theta^{-1} R >= 0 S_t = [Cov(X), -Cov*B*Theta; -Theta*B'*Cov, beta I] ''' if np.linalg.norm(S-S.T) > 1e-3: raise ValueError("Covariance matrix should be symmetric.") n = S.shape[0] #n1 = covariance.shape[0] n2 = covariance_h.shape[0] n1 = n - n2 #if n != (n1+n2): if n1 < 0: raise ValueError("dimension mismatch n=%d, n1=%d, n2=%d" % (n,n1,n2)) mask = np.zeros((n,n)) mask[np.ix_(np.arange(n1), np.arange(n1))] J1 = np.zeros((n, n1)) J1[np.arange(n1),:] = np.eye(n1) J2 = np.zeros((n, n2)) J2[np.arange(n1,n), :] = np.eye(n2) Q = np.zeros((n,n2)) Q[np.arange(n1,n),:] = precision_h #Q = np.zeros((n, n1)) #Q[np.arange(n1),:] = -covariance J1 = np.asmatrix(J1) J2 = np.asmatrix(J2) Q = np.asmatrix(Q) S - np.asmatrix(S) R = cvx.Semidef(n) # define the SDP problem objective = cvx.Minimize(-cvx.log_det(R) + cvx.trace(S*R) + alpha*(cvx.norm((J1.T*R*J1), 1) + beta*cvx.mixed_norm((J1.T*R*Q).T, 2, 1) ))#beta*cvx.norm( (J1.T*R*Q), 1)) ) constraints = [J2.T*R*J2 == covariance_h] # solve the problem problem = cvx.Problem(objective, constraints) problem.solve(verbose = verbose) return np.asarray(R.value)
def costL12(K, X, S, lam): # compute log loss loss = 0. for q in S: i, j, k = q # should this be Mt^T * K?? Mt = (2. * np.outer(X[i], X[j]) - 2. * np.outer(X[i], X[k]) - np.outer(X[j], X[j]) + np.outer(X[k], X[k])) score = cvx.trace(Mt.T * K) loss = loss + cvx.logistic(-score) # regularize with the 1,2 norm loss = loss / len(S) + lam * cvx.mixed_norm(K, 2, 1) return loss
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 cvx_solve(x, y, z): ''' Using mosek to update t,r. :param x: The centroids. [n,m] array :param y: The grids. [t,m] array :param z: The samples. [k,m] array :return: !) t: The new t. [t,n] array 2) r: The new r. [k,n] array ''' num_centers = np.size(x, 0) num_grids = np.size(y, 0) size = int(sqrt(num_grids)) num_samples = np.size(z, 0) yyt = y.dot(y.T) xxt = x.dot(x.T) xyt = x.dot(y.T) m = np.kron(np.ones(num_centers), np.sum(np.mat(np.power(z, 2)), 1)) + \ np.kron(np.ones([num_samples, 1]), np.sum(np.mat(np.power(x, 2)), 1).T) - 2 * z.dot(x.T) t = cvx.Variable((num_grids, num_centers)) r = cvx.Variable((num_samples, num_centers)) constraints = [ t * np.ones([num_centers, 1]) == np.ones([num_grids, 1]), r * np.ones([num_centers, 1]) == np.ones([num_samples, 1]), t.T * np.ones([num_grids, 1]) / num_grids == r.T * np.ones([num_samples, 1]) / num_samples, t >= 0, r >= 0 ] err = np.trace(yyt) - 2 * cvx.trace(t * xyt) for i in range(num_grids): err += cvx.quad_form(t[i], xxt) # for i in range(num_centers): # err += cvx.tv(t[:,i].reshape([size,size])) err += cvx.trace(r * m.T) prob = cvx.Problem(cvx.Minimize(err), constraints) prob.solve(solver=cvx.MOSEK) return t.value, r.value
def solve_relaxed_SDP(self): """Solve the Semi Deifnite Program and save the optimal solution.""" density = 2 * np.sum(self.X) / (self.n * (self.n - 1)) #alpha = min(1, (self.K**3/self.n)*np.exp(2*self.n*density)) alpha = 1 / min(list(filter(lambda x: x > 0, self.effectifs))) B = cp.Variable((self.n, self.n)) constraints = [B >> 0] constraints += [B == B.T] ones = np.ones(self.n) constraints += [B @ ones == ones] constraints += [cp.trace(B) == self.K] constraints += [ B[i // self.n, i % self.n] >= 0 for i in range(self.n * self.n) ] constraints += [ alpha - B[i // self.n, i % self.n] >= 0 for i in range(self.n * self.n) ] prob = cp.Problem(cp.Minimize(-cp.trace(self.X @ (self.X).T @ B)), constraints) prob.solve() self.B_relaxed = B.value
def variable_D_UNF_state(da, db, D, hermitian=True): """ SDP2 FROM THE PAPER - GENERAL D CASE creates a bipartite state as a variable, with the constraints for it to be D-unfaithful da, db: Dimensions of the two subsystems D: Dimension of unfaithfulness to satisfy hermitian If True, rho and constraints are complex, if false, only real OUTPUT: constraints: list of SDP constraints for cvxpy rho: cvxpy variable matrix that, when optimised over the constraints, is the state """ rho = _make_cp_matrix((da * db, da * db), hermitian) Na = _make_cp_matrix((da, da), hermitian) Nb = _make_cp_matrix((db, db), hermitian) constraints = [rho >> 0, cp.trace(rho) == 1, Na >> 0, Nb >> 0] constraints += [tensor(Na, np.eye(db)) + tensor(np.eye(da), Nb) >> rho] constraints += [cp.trace(Na) * np.eye(da) >> (D - 1) * Na] constraints += [cp.trace(Nb) * np.eye(db) >> (D - 1) * Nb] constraints += [cp.trace(Na) + cp.trace(Nb) == D - 1] return constraints, rho
def test_simpler_eye_minus_inv(self): X = cvxpy.Variable((2, 2), pos=True) obj = cvxpy.Minimize(cvxpy.trace(cvxpy.eye_minus_inv(X))) constr = [ cvxpy.diag(X) == 0.1, cvxpy.hstack([X[0, 1], X[1, 0]]) == 0.1 ] problem = cvxpy.Problem(obj, constr) problem.solve(gp=True, solver="ECOS") np.testing.assert_almost_equal(X.value, 0.1 * np.ones((2, 2)), decimal=3) self.assertAlmostEqual(problem.value, 2.25)
def dnorm_problem(dim): # Start assembling constraints and variables. constraints = [] # Make a complex variable for X. X = complex_var(dim ** 2, dim ** 2, "X") # Make complex variables for rho0 and rho1. rho0 = complex_var(dim, dim, "rho0") rho1 = complex_var(dim, dim, "rho1") constraints += dens(rho0, rho1) # Finally, add the tricky positive semidefinite constraint. # Since we're using column-stacking, but Watrous used row-stacking, # we need to swap the order in Rho0 and Rho1. This is not straightforward, # as CVXPY requires that the constant be the first argument. To solve this, # We conjugate by SWAP. W = qudit_swap(dim).data.todense() W = Complex(re=W.real, im=W.imag) Rho0 = conj(W, kron(np.eye(dim), rho0)) Rho1 = conj(W, kron(np.eye(dim), rho1)) Y = cvxpy.bmat([ [Rho0.re, X.re, -Rho0.im, -X.im], [X.re.T, Rho1.re, X.im.T, -Rho1.im], [Rho0.im, X.im, Rho0.re, X.re], [-X.im.T, Rho1.im, X.re.T, Rho1.re], ]) constraints += [Y >> 0] logger.debug("Using {} constraints.".format(len(constraints))) Jr = cvxpy.Parameter(dim ** 2, dim ** 2) Ji = cvxpy.Parameter(dim ** 2, dim ** 2) # The objective, however, depends on J. objective = cvxpy.Maximize(cvxpy.trace( Jr.T * X.re + Ji.T * X.im )) problem = cvxpy.Problem(objective, constraints) return problem, Jr, Ji, X, rho0, rho1
n = 20 N = 1000 C = np.random.random_sample((n, n)) C = - C.dot(C.T) + 1000 * np.eye(n) # print(np.linalg.eigvals(C)) import cvxpy as cp mX = cp.Symmetric(n, n) constraints = [cp.lambda_min(mX) >= 0, cp.diag(mX) == 1] obj = cp.Maximize(cp.trace(C * mX)) maxcut = cp.Problem(obj, constraints).solve() print(maxcut) # print(mX.value) data = np.zeros(N) for i in range(N): X = np.matrix(np.random.randint(0, 2, n) * 2 - 1).T data[i] = X.T.dot(C).dot(X)[0][0] fig = plt.figure(figsize=[10, 5]) plt.plot(data, 'bo', markersize=1) plt.plot([maxcut], 'ro') plt.plot([maxcut for i in range(N)], 'ro', markersize=0.2) plt.show()
def diamonddist(A, B, mxBasis='gm', dimOrStateSpaceDims=None): """ Returns the approximate diamond norm describing the difference between gate matrices A and B given by : D = ||A - B ||_diamond = sup_rho || AxI(rho) - BxI(rho) ||_1 Parameters ---------- A, B : numpy array The *gate* matrices to use when computing the diamond norm. mxBasis : {"std","gm","pp"}, optional the basis of the gate matrices A and B : standard (matrix units), Gell-Mann, or Pauli-product, respectively. dimOrStateSpaceDims : int or list of ints, optional Structure of the density-matrix space, which further specifies the basis of gateMx (see BasisTools). Returns ------- float Diamond norm """ #currently cvxpy is only needed for this function, so don't import until here import cvxpy as _cvxpy # This SDP implementation is a modified version of Kevin's code #Compute the diamond norm #Uses the primal SDP from arXiv:1207.5726v2, Sec 3.2 #Maximize 1/2 ( < J(phi), X > + < J(phi).dag, X.dag > ) #Subject to [[ I otimes rho0, X], # [X.dag, I otimes rho1]] >> 0 # rho0, rho1 are density matrices # X is linear operator #Jamiolkowski representation of the process # J(phi) = sum_ij Phi(Eij) otimes Eij #< A, B > = Tr(A.dag B) #def vec(matrix_in): # # Stack the columns of a matrix to return a vector # return _np.transpose(matrix_in).flatten() # #def unvec(vector_in): # # Slice a vector into columns of a matrix # d = int(_np.sqrt(vector_in.size)) # return _np.transpose(vector_in.reshape( (d,d) )) dim = A.shape[0] smallDim = int(_np.sqrt(dim)) assert(dim == A.shape[1] == B.shape[0] == B.shape[1]) #Code below assumes *un-normalized* Jamiol-isomorphism, so multiply by density mx dimension JAstd = smallDim * _jam.jamiolkowski_iso(A, mxBasis, "std", dimOrStateSpaceDims) JBstd = smallDim * _jam.jamiolkowski_iso(B, mxBasis, "std", dimOrStateSpaceDims) #CHECK: Kevin's jamiolowski, which implements the un-normalized isomorphism: # smallDim * _jam.jamiolkowski_iso(M, "std", "std") #def kevins_jamiolkowski(process, representation = 'superoperator'): # # Return the Choi-Jamiolkowski representation of a quantum process # # Add methods as necessary to accept different representations # process = _np.array(process) # if representation == 'superoperator': # # Superoperator is the linear operator acting on vec(rho) # dimension = int(_np.sqrt(process.shape[0])) # print "dim = ",dimension # jamiolkowski_matrix = _np.zeros([dimension**2, dimension**2], dtype='complex') # for i in range(dimension**2): # Ei_vec= _np.zeros(dimension**2) # Ei_vec[i] = 1 # output = unvec(_np.dot(process,Ei_vec)) # tmp = _np.kron(output, unvec(Ei_vec)) # print "E%d = \n" % i,unvec(Ei_vec) # #print "contrib =",_np.kron(output, unvec(Ei_vec)) # jamiolkowski_matrix += tmp # return jamiolkowski_matrix #JAstd_kev = jamiolkowski(A) #JBstd_kev = jamiolkowski(B) #print "diff A = ",_np.linalg.norm(JAstd_kev/2.0-JAstd) #print "diff B = ",_np.linalg.norm(JBstd_kev/2.0-JBstd) #Kevin's function: def diamondnorm( jamiolkowski_matrix ): jamiolkowski_matrix = JBstd-JAstd # Here we define a bunch of auxiliary matrices because CVXPY doesn't use complex numbers K = jamiolkowski_matrix.real # J.real L = jamiolkowski_matrix.imag # J.imag Y = _cvxpy.Variable(dim, dim) # X.real Z = _cvxpy.Variable(dim, dim) # X.imag sig0 = _cvxpy.Variable(smallDim,smallDim) # rho0.real sig1 = _cvxpy.Variable(smallDim,smallDim) # rho1.real tau0 = _cvxpy.Variable(smallDim,smallDim) # rho1.imag tau1 = _cvxpy.Variable(smallDim,smallDim) # rho1.imag ident = _np.identity(smallDim, 'd') objective = _cvxpy.Maximize( _cvxpy.trace( K.T * Y + L.T * Z) ) constraints = [ _cvxpy.bmat( [ [ _cvxpy.kron(ident, sig0), Y, -_cvxpy.kron(ident, tau0), -Z], [ Y.T, _cvxpy.kron(ident, sig1), Z.T, -_cvxpy.kron(ident, tau1)], [ _cvxpy.kron(ident, tau0), Z, _cvxpy.kron(ident, sig0), Y], [ -Z.T, _cvxpy.kron(ident, tau1), Y.T, _cvxpy.kron(ident, sig1)]] ) >> 0, _cvxpy.bmat( [[sig0, -tau0], [tau0, sig0]] ) >> 0, _cvxpy.bmat( [[sig1, -tau1], [tau1, sig1]] ) >> 0, sig0 == sig0.T, sig1 == sig1.T, tau0 == -tau0.T, tau1 == -tau1.T, _cvxpy.trace(sig0) == 1., _cvxpy.trace(sig1) == 1. ] prob = _cvxpy.Problem(objective, constraints) # try: prob.solve(solver="CVXOPT") # prob.solve(solver="ECOS") # prob.solve(solver="SCS")#This always fails # except: # return -1 return prob.value
def dens(*rhos): return pos(*rhos) + [ cvxpy.trace(rho.re) == 1 for rho in rhos ]
def test_admm(self): """Test ADMM algorithm. """ X = Variable((10,5)) B = np.reshape(np.arange(50), (10,5))*1. prox_fns = [sum_squares(X, b=B)] sltn = admm.solve(prox_fns, [], 1.0, eps_abs=1e-4, eps_rel=1e-4) self.assertItemsAlmostEqual(X.value, B, places=2) self.assertAlmostEqual(sltn, 0) prox_fns = [norm1(X, b=B, beta=2)] sltn = admm.solve(prox_fns, [], 1.0) self.assertItemsAlmostEqual(X.value, B/2., places=2) self.assertAlmostEqual(sltn, 0) prox_fns = [norm1(X), sum_squares(X, b=B)] sltn = admm.solve(prox_fns, [], 1.0, eps_rel=1e-5, eps_abs=1e-5) cvx_X = cvx.Variable(10, 5) cost = cvx.sum_squares(cvx_X - B) + cvx.norm(cvx_X, 1) prob = cvx.Problem(cvx.Minimize(cost)) prob.solve() self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2) self.assertAlmostEqual(sltn, prob.value) psi_fns, omega_fns = admm.partition(prox_fns) sltn = admm.solve(psi_fns, omega_fns, 1.0, eps_rel=1e-5, eps_abs=1e-5) self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2) self.assertAlmostEqual(sltn, prob.value) prox_fns = [norm1(X)] quad_funcs = [sum_squares(X, b=B)] sltn = admm.solve(prox_fns, quad_funcs, 1.0, eps_rel=1e-5, eps_abs=1e-5) self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2) self.assertAlmostEqual(sltn, prob.value) # With parameters for sum_squares prox_fns = [norm1(X)] quad_funcs = [sum_squares(X, b=B, alpha=0.1, beta=2., gamma=1, c=B)] sltn = admm.solve(prox_fns, quad_funcs, 1.0, eps_rel=1e-5, eps_abs=1e-5) cvx_X = cvx.Variable(10, 5) cost = 0.1*cvx.sum_squares(2*cvx_X - B) + cvx.sum_squares(cvx_X) + \ cvx.norm(cvx_X, 1) + cvx.trace(B.T*cvx_X) prob = cvx.Problem(cvx.Minimize(cost)) prob.solve() self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2) self.assertAlmostEqual(sltn, prob.value, places=3) prox_fns = [norm1(X)] quad_funcs = [sum_squares(X - B, alpha=0.1, beta=2., gamma=1, c=B)] quad_funcs[0] = absorb_offset(quad_funcs[0]) sltn = admm.solve(prox_fns, quad_funcs, 1.0, eps_rel=1e-5, eps_abs=1e-5) self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2) self.assertAlmostEqual(sltn, prob.value, places=3) prox_fns = [norm1(X)] cvx_X = cvx.Variable(10, 5) # With linear operators. kernel = np.array([1,2,3]) x = Variable(3) b = np.array([-41,413,2]) prox_fns = [nonneg(x), sum_squares(conv(kernel, x), b=b)] sltn = admm.solve(prox_fns, [], 1.0, eps_abs=1e-5, eps_rel=1e-5) kernel_mat = np.matrix("2 1 3; 3 2 1; 1 3 2") cvx_X = cvx.Variable(3) cost = cvx.norm(kernel_mat*cvx_X - b) prob = cvx.Problem(cvx.Minimize(cost), [cvx_X >= 0]) prob.solve() self.assertItemsAlmostEqual(x.value, cvx_X.value, places=2) self.assertAlmostEqual(np.sqrt(sltn), prob.value, places=2) prox_fns = [nonneg(x)] quad_funcs = [sum_squares(conv(kernel, x), b=b)] sltn = admm.solve(prox_fns, quad_funcs, 1.0, eps_abs=1e-5, eps_rel=1e-5) self.assertItemsAlmostEqual(x.value, cvx_X.value, places=2) self.assertAlmostEqual(np.sqrt(sltn), prob.value, places=2)