def as_constraint(self, *args): """ Imagine there are bubbles of a fixed size floating about constraining the discrete space each face is given a coordinate X = 1, Xg is coordinate dist_real <= r X = 1, Xg is (0, 0) dist_fake <= r + 1000 note - 2-norm is SIGNIFICANTLY Faster than 1norm. """ N = len(self.space.faces) # centroids.shape[N, 2] centroids = np.asarray(self.space.faces.centroids) M = 2 * centroids.max() centroids = Parameter(shape=centroids.shape, value=centroids) px, py = self._p.X, self._p.Y C = [] for i, face_set in enumerate(self._actions): X = face_set.vars # selected faces cx = cvx.multiply(centroids[:, 0], X) cy = cvx.multiply(centroids[:, 1], X) Xg = cvx.vstack([cx, cy]).T v = cvx.vstack( [cvx.promote(px[i], (N, )), cvx.promote(py[i], (N, ))]).T C = [cvx.norm(v - Xg, 2, axis=1) <= self._r[i] + M * (1 - X)] return C
def relative_c_sage(s): """ Given a signomial "s", return a list of CVXPY Constraint objects over CVXPY Variables c_vars and nu_vars such that s is SAGE iff c_vars and nu_vars satisfy every constraint in this list. :param s: a Signomial object (likely with the property that s.c is a CVXPY Expression). :return: constraints - a list of CVXPY Constraint objects. """ if s.m <= 2: return [cvxpy.vstack(s.c.tolist()) >= 0] alpha, c = s.alpha_c_arrays() non_constants = [ i for i, c_i in enumerate(c) if not isinstance(c_i, __NUMERIC_TYPES__) ] N_I = [i for i, c_i in enumerate(c) if (i in non_constants) or (c_i < 0)] c_vars = dict() nu_vars = dict() constraints = [] for i in N_I: c_i, nu_i, constrs_i = relative_c_age(s, i) c_vars[i] = c_i nu_vars[i] = nu_i constraints += constrs_i # Now the constraints that the c_vars sum to c. vec_expr = sum(c_vars.values()) c = cvxpy.vstack(c.tolist()) constraints.append(vec_expr == c) return constraints
def test_vstack(self) -> None: atom = cp.vstack([self.x, self.y, self.x]) self.assertEqual(atom.name(), "Vstack(x, y, x)") self.assertEqual(atom.shape, (3, 2)) atom = cp.vstack([self.A, self.C, self.B]) self.assertEqual(atom.name(), "Vstack(A, C, B)") self.assertEqual(atom.shape, (7, 2)) entries = [] for i in range(self.x.shape[0]): entries.append(self.x[i]) atom = cp.vstack(entries) self.assertEqual(atom.shape, (2, 1)) # self.assertEqual(atom[1,0].name(), "vstack(x[0,0], x[1,0])[1,0]") with self.assertRaises(Exception) as cm: cp.vstack([self.C, 1]) self.assertEqual( str(cm.exception), "All the input dimensions except for axis 0 must match exactly.") with self.assertRaises(Exception) as cm: cp.vstack([self.x, Variable(3)]) self.assertEqual( str(cm.exception), "All the input dimensions except for axis 0 must match exactly.") with self.assertRaises(TypeError) as cm: cp.vstack() # Test scalars with variables of shape (1,) expr = cp.vstack([2, Variable((1, ))]) self.assertEqual(expr.shape, (2, 1))
def constraints(self): """ F - Set of Faces E - Set of Edges H - Set of Half Edges V - Set of Vertices """ C, N = [], len(self.placements[0]) face_vars = self.adj_vars(face=True) edge_vars = self.adj_vars(edge=True) # X_i => f(F) # self on face for i, ixs in enumerate(self.placements): # if X_i is 1 -> N <= corresponding faces should sum to = N # if X_i is 0 -> 0 <= corresponding faces should sum to < N C += [ N * self.X[i] <= cvx.sum( cvx.vstack([face_vars[x][self.index] for x in ixs])) ] # X_i => f(X) # self on self - if X_i is placed, X_j cannot be placed for i, v in self._mutex_placements().items(): # if X_i is 1 -> 0 >= sum X_j will == 0 # if X_i is 0 -> N >= sum X_j will >= 0 C += [(1 - self.X[i]) * N >= cvx.sum(cvx.vstack(self.X[list(v)]))] # cannot exceed max times its used # C += self._jentries() C += [cvx.sum(self.X) <= self.template.max_uses] return C
def as_constraint(self, *args): """ Imagine there are bubbles of a fixed size floating about constraining the discrete space each face is given a coordinate X = 1, Xg is coordinate dist_real <= r X = 1, Xg is (0, 0) dist_fake <= r + 1000 note - 2-norm is SIGNIFICANTLY Faster than 1norm. """ N = len(self.space.faces) X = self.stacked # selected faces M = 100 # upper bound # centroids.shape[N, 2] centroids = np.asarray(self.space.faces.centroids) centroids = Parameter(shape=centroids.shape, value=centroids) cx = cvx.multiply(centroids[:, 0], X) cy = cvx.multiply(centroids[:, 1], X) Xg = cvx.vstack([cx, cy]).T v = cvx.vstack( [cvx.promote(self._bx, (N, )), cvx.promote(self._by, (N, ))]).T C = [cvx.norm(v - Xg, 2, axis=1) <= self._r + M * (1 - X)] return C
def test_affine_atoms_canon(self): """Test canonicalization for affine atoms. """ # Scalar. x = Variable() expr = cvx.imag(x + 1j * x) prob = Problem(Minimize(expr), [x >= 0]) result = prob.solve() self.assertAlmostEqual(result, 0) self.assertAlmostEqual(x.value, 0) x = Variable(imag=True) expr = 1j * x prob = Problem(Minimize(expr), [cvx.imag(x) <= 1]) result = prob.solve() self.assertAlmostEqual(result, -1) self.assertAlmostEqual(x.value, 1j) x = Variable(2) expr = x / 1j prob = Problem(Minimize(expr[0] * 1j + expr[1] * 1j), [cvx.real(x + 1j) >= 1]) result = prob.solve() self.assertAlmostEqual(result, -np.inf) prob = Problem(Minimize(expr[0] * 1j + expr[1] * 1j), [cvx.real(x + 1j) <= 1]) result = prob.solve() self.assertAlmostEqual(result, -2) self.assertItemsAlmostEqual(x.value, [1, 1]) prob = Problem( Minimize(expr[0] * 1j + expr[1] * 1j), [cvx.real(x + 1j) >= 1, cvx.conj(x) <= 0]) result = prob.solve() self.assertAlmostEqual(result, np.inf) x = Variable((2, 2)) y = Variable((3, 2), complex=True) expr = cvx.vstack([x, y]) prob = Problem(Minimize(cvx.sum(cvx.imag(cvx.conj(expr)))), [x == 0, cvx.real(y) == 0, cvx.imag(y) <= 1]) result = prob.solve() self.assertAlmostEqual(result, -6) self.assertItemsAlmostEqual(y.value, 1j * np.ones((3, 2))) self.assertItemsAlmostEqual(x.value, np.zeros((2, 2))) x = Variable((2, 2)) y = Variable((3, 2), complex=True) expr = cvx.vstack([x, y]) prob = Problem(Minimize(cvx.sum(cvx.imag(expr.H))), [x == 0, cvx.real(y) == 0, cvx.imag(y) <= 1]) result = prob.solve() self.assertAlmostEqual(result, -6) self.assertItemsAlmostEqual(y.value, 1j * np.ones((3, 2))) self.assertItemsAlmostEqual(x.value, np.zeros((2, 2)))
def tv_cvxpy_regulariser(self, u, isotropic=True, direction = "forward", boundaries = "Neumann"): G = self.sparse_gradient_matrix(u.shape, direction = direction, order = 1, boundaries = boundaries) DX, DY = G[1], G[0] if isotropic: return cvxpy.sum(cvxpy.norm(cvxpy.vstack([DX @ cvxpy.vec(u), DY @ cvxpy.vec(u)]), 2, axis = 0)) else: return cvxpy.sum(cvxpy.norm(cvxpy.vstack([DX @ cvxpy.vec(u), DY @ cvxpy.vec(u)]), 1, axis = 0))
def gen_data_ndim(nb_datapoints, dim, savefile, rand_seed=7): """Sampling according to Table 1 in manuscript: - uniform point positioning (x) in [0,1] for each dimension - uniform eigenvalues in [-1,1] - ortho-normal basis/matrix (eigvecs) of eigenvectors :param nb_datapoints: how many data points to sample :param dim: dimensionality of SDP sub-problem to sample :param savefile: file to save sampled data in :param rand_seed: random seed, pre-set to 7 :return: None """ np.random.seed(rand_seed) X = cvx.Variable(dim, dim) data_points = [] t_init = timer() t0 = timer() for data_pt_nb in range(1, nb_datapoints + 1): # ortho-normal basis/matrix (eigvecs) of eigenvectors eigvecs = ortho_group.rvs(dim) # uniform eigenvalues in [-1,1] eigvals = np.random.uniform(-1, 1, dim).tolist() # construct sampled Q from eigen-decomposition Q = np.matmul(np.matmul(eigvecs, np.diag(eigvals)), np.transpose(eigvecs)) # uniform point positioning (x) in [0,1] for each dimension x = np.random.uniform(0, 1, dim).tolist() # construct cvx SDP sub-problem obj_sdp = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(Q, X))) constraints = [ cvx.lambda_min( cvx.hstack(cvx.vstack(1, np.array(x)), cvx.vstack(cvx.vec(x).T, X))) >= 0, *[X[ids, ids] <= x[ids] for ids in range(0, dim)] ] prob = cvx.Problem(obj_sdp, constraints) # solve it using Mosek SDP solver prob.solve(verbose=False, solver=cvx.MOSEK) # store upper triangular matrix in array (row major) since it is symmetric Q = np.triu(Q, 1) + np.triu(Q, 0) # save eigendecomposition, point positioning, matrix Q and solution of SDP sub-problem data_points.append([ *eigvecs.T.flatten(), *eigvals, *x, *list(Q[np.triu_indices(dim)]), obj_sdp.value ]) # save to file and empty data_points buffer every 1000 entries if not data_pt_nb % 1000: t1 = timer() with open(savefile, "a") as f: for line in data_points: f.write(",".join(str(x) for x in line) + "\n") data_points = [] print( str(data_pt_nb) + " " + str(dim) + "D points, time = " + str(t1 - t0) + "s" + ", total = " + str(t1 - t_init) + "s") t0 = t1
def a(): x = cp.Variable() y = cp.Variable() obj = cp.Minimize(0) constraints = [cp.norm(cp.vstack([x + 2 * y, x - y])) == 0] problem = cp.Problem(obj, constraints) print(f"a before: {problem.is_dcp()}") constraints = [cp.norm(cp.vstack([x + 2 * y, x - y])) <= 0] problem = cp.Problem(obj, constraints) print(f"a after: {problem.is_dcp()}") problem.solve()
def test_rank_one_nmf(self): X = cp.Variable((3, 3), pos=True) x = cp.Variable((3, ), pos=True) y = cp.Variable((3, ), pos=True) xy = cp.vstack([x[0] * y, x[1] * y, x[2] * y]) R = cp.maximum(cp.multiply(X, (xy)**(-1.0)), cp.multiply(X**(-1.0), xy)) objective = cp.sum(R) constraints = [ X[0, 0] == 1.0, X[0, 2] == 1.9, X[1, 1] == 0.8, X[2, 0] == 3.2, X[2, 1] == 5.9, x[0] * x[1] * x[2] == 1.0, ] # smoke test. prob = cp.Problem(cp.Minimize(objective), constraints) prob.solve(SOLVER, gp=True) optimal_value = prob.value param = cp.Parameter(value=-2.0) R = cp.maximum(cp.multiply(X, (xy)**(param)), cp.multiply(X**(param), xy)) objective = cp.sum(R) prob = cp.Problem(cp.Minimize(objective), constraints) prob.solve(SOLVER, gp=True, enforce_dpp=True) # now change param to point to known_value, and check we recover the # correct optimal value param.value = -1.0 prob.solve(SOLVER, gp=True, enforce_dpp=True) self.assertAlmostEqual(prob.value, optimal_value)
def skew_symmetric(V): skew = cp.vstack([ cp.hstack([0, -V[2, 0], V[1, 0]]), cp.hstack([V[2, 0], 0, -V[0, 0]]), cp.hstack([-V[1, 0], V[0, 0], 0]) ]) return skew
def solve_MLE(self, rewards_history, features_history): if self.iteration > 1: if not self.model is None: n_samples = len(self.rewards_history) n_features = self.d X = np.zeros((n_samples, n_features)) X = 1 * np.array(self.features_history) y = (np.array(self.rewards_history).reshape((n_samples, ))) beta = cp.Variable(n_features) lambd = cp.Parameter(nonneg=True) lambd.value = self.reg_factor / 2 if self.model == 'bernoulli': log_likelihood = cp.sum( cp.multiply(y, X @ beta) - cp.log_sum_exp( cp.vstack([np.zeros(n_samples), X @ beta]), axis=0) ) - lambd * cp.norm(beta, 2) problem = cp.Problem(cp.Maximize(log_likelihood)) problem.solve(verbose=False, warm_start=False, max_iters=200) return beta.value else: log_likelihood = cp.sum( cp.multiply(y, X @ beta) - cp.power(X @ beta, 2) / 2) - lambd * cp.norm(beta, 2) problem = cp.Problem(cp.Maximize(log_likelihood)) problem.solve(verbose=False, warm_start=False, max_iters=200) return beta.value else: return np.zeros((self.d, ))
def init_beta_b_convex_QP(X, rankings, mat_Pij): ''' n: number of items p: number of features :param rankings: (c_l, A_l): 1...M :param X: n*p, feature matrix :param mat_Pij = est_Pij(n, rankings) min._{beta,b} {beta,b}^T Q {beta,b}, s.t. Xbeta + b >=0 and sum(Xbeta + b)=1 :return: beta, b, time ''' p = X.shape[1] # Define variables (HT: added second dimension to get vstack to work) beta = cp.Variable((p, 1)) b = cp.Variable((1, 1)) params = cp.vstack([beta, b]) # Define objective Q = est_sum_dij_dijT(X, rankings, mat_Pij) objective = cp.quad_form(params, Q) # Define constraints (HT: use `@` for matrix-vector multiplication; # sum_entries -> sum in cvxpy > 1.0) constraints = [X @ beta + b >= rtol, cp.sum(X @ beta + b) == 1] start_beta_b = time() # Optimize prob = cp.Problem(cp.Minimize(objective), constraints=constraints) prob.solve(solver='SCS') time_beta_b_init = time() - start_beta_b if beta.value is None: constraints = [X @ beta + b >= rtol] # If cannot be solved, reduce the accuracy requirement start_beta_b = time() # Optimize prob = cp.Problem(cp.Minimize(objective), constraints=constraints) prob.solve(solver='SCS', eps=1e-2) time_beta_b_init = time() - start_beta_b return np.squeeze(np.array(beta.value)), b.value, time_beta_b_init
def test_synthesis(A,B): # Problem data. N_u = B.shape[1] N_x = A.shape[0] Cv = np.eye(N_u) Av = cvxpy.Variable(rows=N_u,cols=N_u); Bv = cvxpy.Variable(rows=N_u,cols=N_x); P1 = np.eye(A.shape[0]); P2 = np.eye(Cv.shape[0]); # Construct the problem. objective = cvxpy.Minimize(cvxpy.norm1(Av) + cvxpy.norm1(Bv)) SystemMatrix1 = cvxpy.hstack([P1, np.zeros((N_x,N_u)), A.T, Bv.T]); SystemMatrix2 = cvxpy.hstack([np.zeros( ( Cv.shape[0],N_x ) ),P2,np.matmul(Cv.T,B.T),Av.T]); SystemMatrix3 = cvxpy.hstack([A, np.matmul(B,Cv), P1, np.zeros( (A.shape[0],P2.shape[0]) )]); SystemMatrix4 = cvxpy.hstack([Bv, Av, np.zeros((N_u,N_x) ) ,P2]); SystemMatrix = cvxpy.vstack( [SystemMatrix1,SystemMatrix2,SystemMatrix3,SystemMatrix4]); constraints = [0<SystemMatrix]; prob = cvxpy.Problem(objective, constraints) # The optimal objective is returned by prob.solve(). result = prob.solve(solver='ECOS',verbose=True) return result, Av.value, Bv.value
def ntf_fir_from_digested(Qs, A, C, H_inf, **opts): """ Synthesize FIR NTF from predigested specification Version for the cvxpy modeler. """ verbose = opts['show_progress'] if opts['cvxpy_opts']['solver'] == 'cvxopt': opts['cvxpy_opts']['solver'] = cvxpy.CVXOPT elif opts['cvxpy_opts']['solver'] == 'scs': opts['cvxpy_opts']['solver'] = cvxpy.SCS order = np.size(Qs, 0)-1 br = cvxpy.Variable(order, 1, name='br') b = cvxpy.vstack(1, br) X = cvxpy.Symmetric(order, name='X') target = cvxpy.Minimize(cvxpy.norm2(Qs*b)) B = np.vstack((np.zeros((order-1, 1)), 1.)) C = C+br[::-1].T D = np.matrix(1.) M1 = A.T*X M2 = M1*B M = cvxpy.bmat([[M1*A-X, M2, C.T], [M2.T, B.T*X*B-H_inf**2, D], [C, D, np.matrix(-1.)]]) constraints = [M << 0, X >> 0] p = cvxpy.Problem(target, constraints) p.solve(verbose=verbose, **opts['cvxpy_opts']) return np.hstack((1, np.asarray(br.value.T)[0]))
def cvx_opt(self, cp_p, cp_Tx_real, cp_Tx_imag, cp_Rx_real, cp_Rx_imag, cp_beta_real, cp_beta_imag, cp_mu): K, L, Ml, M, N = self.Ksize cp_Theta_real = cp.Variable(L*Ml) cp_Theta_imag = cp.Variable(L*Ml) cp_Rx_Theta_real = [email protected](cp_Theta_real) - [email protected](cp_Theta_imag) cp_Rx_Theta_imag = [email protected](cp_Theta_imag) + [email protected](cp_Theta_real) cp_h_real = cp_Rx_Theta_real@cp_Tx_real - cp_Rx_Theta_imag@cp_Tx_imag cp_h_imag = cp_Rx_Theta_real@cp_Tx_imag + cp_Rx_Theta_imag@cp_Tx_real cp_numerator_real = cp.multiply( cp.diag(cp_h_real), cp.sqrt(2*cp.multiply(cp_p, 1 + cp_mu)) ) cp_numerator_imag = cp.multiply( cp.diag(cp_h_imag), cp.sqrt(2*cp.multiply(cp_p, 1 + cp_mu)) ) cp_h_square = cp.square(cp_h_real) + cp.square(cp_h_imag) cp_h_gain = cp.multiply(cp_h_square, cp.vstack([cp_p]*K)) cp_denominator = cp.sum(cp_h_gain, axis=1) + self.sigma2 cp_beta_square = cp.square(cp_beta_real) + cp.square(cp_beta_imag) objective = cp.Maximize( cp_beta_real@cp_numerator_real + cp_beta_imag@cp_numerator_imag - cp_beta_square@cp_denominator ) constrains = [cp.square(cp_Theta_real) + cp.square(cp_Theta_imag) <= 1] prob = cp.Problem(objective, constrains) assert prob.is_dcp(dpp=False) result = prob.solve() theta_out = torch.stack((torch.from_numpy(cp_Theta_real.value.astype(np.float32)), torch.from_numpy(cp_Theta_imag.value.astype(np.float32))), dim = 1).view(L, Ml, 2) return theta_out
def as_objective(self, maximize=True): """ 'maximize the number of tile boundary vertices that intersect with the marked vertices' add M @ X term to the problem objectives implementation: create Variable for each vertex for placement X_p,i if placement violates constraint, 0, else 1 if (X_0 or X_1 or ... or X_n ) cause a violation mul operation results in 0 example: """ if maximize is False: raise NotImplemented('not yet implemented') # Variable(shape=len(self._vertices), boolean=True) # M @ X -> { v | v in {0, 1}} X = [] M = np.zeros((len(self._vertices), self.num_actions)) cnt = 0 for p in self._actions: X.append(p.X) for j in range(len(p)): new_verts = p.transformed(j, exterior=True, vertices=True) for k, v in enumerate(self._vertices): if v in new_verts: M[k, cnt] = 1 cnt += 1 objective = cvx.sum(self._weights * (M @ cvx.vstack(X))) return objective
def socp_3(axis): x = cp.Variable(shape=(2,)) c = np.array([-1, 2]) root2 = np.sqrt(2) u = np.array([[1 / root2, -1 / root2], [1 / root2, 1 / root2]]) mat1 = np.diag([root2, 1 / root2]) @ u.T mat2 = np.diag([1, 1]) mat3 = np.diag([0.2, 1.8]) X = cp.vstack([mat1 @ x, mat2 @ x, mat3 @ x]) # stack these as rows t = cp.Constant(np.ones(3, )) objective = cp.Minimize(c @ x) if axis == 0: con = cp.constraints.SOC(t, X.T, axis=0) con_expect = [ np.array([0, 1.16454469e+00, 7.67560451e-01]), np.array([[0, -9.74311819e-01, -1.28440860e-01], [0, 6.37872081e-01, 7.56737724e-01]]) ] else: con = cp.constraints.SOC(t, X, axis=1) con_expect = [ np.array([0, 1.16454469e+00, 7.67560451e-01]), np.array([[0, 0], [-9.74311819e-01, 6.37872081e-01], [-1.28440860e-01, 7.56737724e-01]]) ] obj_pair = (objective, -1.932105) con_pairs = [(con, con_expect)] var_pairs = [(x, np.array([0.83666003, -0.54772256]))] sth = SolverTestHelper(obj_pair, var_pairs, con_pairs) return sth
def ntf_fir_from_digested(Qs, A, C, H_inf, **opts): """ Synthesize FIR NTF from predigested specification Version for the cvxpy modeler. """ verbose = opts['show_progress'] if opts['cvxpy_opts']['solver'] == 'cvxopt': opts['cvxpy_opts']['solver'] = cvxpy.CVXOPT elif opts['cvxpy_opts']['solver'] == 'scs': opts['cvxpy_opts']['solver'] = cvxpy.SCS order = np.size(Qs, 0) - 1 br = cvxpy.Variable(order, 1, name='br') b = cvxpy.vstack(1, br) X = cvxpy.Symmetric(order, name='X') target = cvxpy.Minimize(cvxpy.norm2(Qs * b)) B = np.vstack((np.zeros((order - 1, 1)), 1.)) C = C + br[::-1].T D = np.matrix(1.) M1 = A.T * X M2 = M1 * B M = cvxpy.bmat([[M1 * A - X, M2, C.T], [M2.T, B.T * X * B - H_inf**2, D], [C, D, np.matrix(-1.)]]) constraints = [M << 0, X >> 0] p = cvxpy.Problem(target, constraints) p.solve(verbose=verbose, **opts['cvxpy_opts']) return np.hstack((1, np.asarray(br.value.T)[0]))
def maxSoftMaxEpigraphProblem(problemOptions, solverOptions): k = problemOptions['k'] #class m = problemOptions['m'] #instances n = problemOptions['n'] #dim p = problemOptions['p'] #p-largest X = __normalized_data_matrix(m,n,1) Y = np.random.randint(0, k, m) # Problem construction def one_hot(y, k): m = len(y) return sps.coo_matrix((np.ones(m), (np.arange(m), y)), shape=(m, k)).todense() Theta = cp.Variable(n,k) beta = cp.Variable(1, k) t = cp.Variable(m) texp = cp.Variable(m) f = cp.sum_largest(t+texp, p) + cp.sum_squares(Theta) C = [] C.append(cp.log_sum_exp(X*Theta + np.ones((m, 1))*beta, axis=1) <= texp) Yi = one_hot(Y, k) C.append(t == cp.vstack([-(X[i]*Theta + beta)[Y[i]] for i in range(m)])) prob = cp.Problem(cp.Minimize(f), C) prob.solve(**solverOptions) return {'Problem':prob, 'name':'maxSoftMaxEpigraphProblem'}
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 test_rank_one_nmf(self) -> None: X = cp.Variable((3, 3), pos=True) x = cp.Variable((3, ), pos=True) y = cp.Variable((3, ), pos=True) xy = cp.vstack([x[0] * y, x[1] * y, x[2] * y]) a = cp.Parameter(value=-1.0) b = cp.Parameter(pos=True, shape=(6, ), value=np.array([1.0, 1.9, 0.8, 3.2, 5.9, 1.0])) R = cp.maximum(cp.multiply(X, (xy)**(a)), cp.multiply(X**(a), xy)) objective = cp.sum(R) constraints = [ X[0, 0] == b[0], X[0, 2] == b[1], X[1, 1] == b[2], X[2, 0] == b[3], X[2, 1] == b[4], x[0] * x[1] * x[2] == b[5], ] problem = cp.Problem(cp.Minimize(objective), constraints) # SCS struggles to solves this problem (solved/inaccurate, unless # max_iters is very high like 10000) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-2, max_iters=1000) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-2, max_iters=1000)
def TRCPA_v2(M,n,tf,q,lam): Avars = [] for i in range(tf-1): Avars.append(cvx.Variable(n,n)) Ltop = cvx.Variable(n,q) Lbottom = np.zeros((n*(tf-1),q)) L = cvx.vstack(Ltop,Lbottom) S = cvx.Variable(n*tf,q) print("Check 1") objective = cvx.Minimize(cvx.norm(L,"nuc") + lam*np.ones((1,n*tf))*cvx.abs(S)*np.ones((q,1))) identity = np.eye(n) print("Check 2") constraints = [np.dot(identity,M[0:n,:])==Ltop+S[0:n,:]] for i in range(tf-1): constraints.append(-1*Avars[i]*M[(i*n):(i+1)*n,:] + np.dot(identity,M[((i+1)*n):(i+2)*n,:])==L[((i+1)*n):(i+2)*n,:]+S[((i+1)*n):(i+2)*n,:]) print("Check 3") print(constraints) prob = cvx.Problem(objective,constraints) print("Check 4") result = prob.solve(verbose = True) print("Check 5") Ltop_final = Ltop.value Avars_final = [] for x in Avars: Avars_final.append(x.value) S_final = S.value return (Avars_final,Ltop_final,Lbottom,S_final)
def layout(self): constraints = [] for box in self.boxes: # Enforce that boxes lie in bounding box. constraints += [ box.bottom >= FloorPlan.MARGIN, box.top + FloorPlan.MARGIN <= self.height ] constraints += [ box.left >= FloorPlan.MARGIN, box.right + FloorPlan.MARGIN <= self.width ] # Enforce aspect ratios. constraints += [(1 / box.ASPECT_RATIO) * box.height <= box.width, box.width <= box.ASPECT_RATIO * box.height] # Enforce minimum area constraints += [ geo_mean(vstack([box.width, box.height])) >= math.sqrt( box.min_area) ] # Enforce the relative ordering of the boxes. for ordering in self.horizontal_orderings: constraints += self._order(ordering, True) for ordering in self.vertical_orderings: constraints += self._order(ordering, False) p = Problem(Minimize(2 * (self.height + self.width)), constraints) return p.solve()
def log_cash(self, phi): tmp = [np.log(b) + phi[g] for g, b in izip(self.goods, self.b)] tmp = cvx.log_sum_exp(cvx.vstack(*tmp)) if tmp.is_constant(): return tmp.value else: return tmp
def gram2edm(G, N, mode):#Generating the EDM from the gramian matrix,using the formula if mode: dG = cvx.vstack( cvx.diag(G) ) D = cvx.matmul(dG ,np.ones((1,N)))-2*G + cvx.matmul(np.ones((N,1)),dG.T) else: dG = np.diag(G)[:,None] D = dG-2*G+dG.T return D
def gram2edm(G, N, mode): if mode: dG = cvx.vstack( cvx.diag(G) ) D = cvx.matmul(dG ,np.ones((1,N)))-2*G + cvx.matmul(np.ones((N,1)),dG.T) else: dG = np.diag(G)[:,None] D = dG-2*G+dG.T return D
def vectorize1(A): rows, cols = A.size Avec = A[:, 0] for col in xrange(1, cols): Avec = cvxpy.vstack(Avec, A[:, col]) return Avec
def power_flow_constraints(model, optvar, optpar): """ This function defines the power flow constraints (i.e. Kirchhoff's laws), corresponding to equations (2a) to (2c) along with equation (6) for the relaxed variable change in `Optimal power flow of a distribution system based on increasingly tight cutting planes added to a second order cone relaxation <http://www.sciencedirect.com/science/article/pii/S0142061515000095/>`__ Args : | model : a Model object containing the data necessary for the simulation | optvar : a dictionary containing cvxpy Variable objects | optpar : a dictionary containing cvxpy Parameter objects Returns : A list of Constraint objects, with a length equal to four times the number of lines """ # Initializing the constraints definition by obtaining the node count and the list of downstream and upstream nodes node_cnt = len(model.DERModel.pv_inverter_power) upstream_nodes = numpy.concatenate([numpy.zeros(1, dtype='int'), model.NetworkModel.from_node]) downstream_nodes = [] for i in range(node_cnt): # Obtaining the list of indices for which the current node is in the from vector from_idx = numpy.where(model.NetworkModel.from_node == i) # Subsetting the previous list of indices into the to_nodes vector to obtain the list of downstream nodes downstream_nodes.append(model.NetworkModel.to_node[from_idx]) constraints = [] for i in range(1, node_cnt): # add the constraints corresponding to the first power flow equation i.e. Kirchhoff's current law on active # powers constraints.append(optvar['P'][i - 1] - optvar['I'][i - 1] * model.NetworkModel.resistance[i - 1] - sum(optvar['P'][j - 1] for j in downstream_nodes[i]) + optvar['Pst'][i] + optvar['Ppv'][i] == optpar['active_load'][i]) # add the constraints corresponding to the second power flow equation i.e. Kirchhoff's current law on reactive # powers constraints.append(optvar['Q'][i - 1] - optvar['I'][i - 1] * model.NetworkModel.reactance[i - 1] - sum(optvar['Q'][j - 1] for j in downstream_nodes[i]) + optvar['Qst'][i] + optvar['Qpv'][i] == optpar['reactive_load'][i]) # add the constraints corresponding to the third power flow equation i.e. Kirchhoff's voltage law constraints.append(-optvar['U'][upstream_nodes[i]] + optvar['U'][i] + 2 * optvar['P'][i - 1] * model.NetworkModel.resistance[i - 1] + 2 * optvar['Q'][i - 1] * model.NetworkModel.reactance[i - 1] - optvar['I'][i - 1] * (model.NetworkModel.resistance[i - 1] ** 2 + model.NetworkModel.reactance[i - 1] ** 2) == 0) # add the constraint corresponding to the convexified variable change P² + Q² = I*U. The constraint cannot be # modelled as it is, because of the I*U product. So we transform the rotated cone to a normal one by rotation # and the constraint becomes ||(2*P, 2*Q, U-I)||² - (U+I) <= 0 constraints.append(cvx.norm(cvx.vstack(2 * optvar['P'][i - 1], 2 * optvar['Q'][i - 1], optvar['I'][i - 1] - optvar['U'][upstream_nodes[i]])) - optvar['U'][upstream_nodes[i]] - optvar['I'][i - 1] <= 0) return constraints
def __solve_for_A_and_C_given_X_and_Xdot__(X, Xdot, num_integrations, dt, gammaC=1e-1, gammaA=1e-6, solver='MOSEK', A_known=None, epsilon=1e-6, rows_of_interest='all'): assert type(X) == np.matrix assert type(Xdot) == np.matrix if rows_of_interest == 'all': rows_of_interest = np.arange(0, X.shape[0]) # Set up the variables A = cvxpy.Variable((X.shape[0], X.shape[0])) C = cvxpy.Variable((X.shape[0], num_integrations)) # Integrate the integration constants Csum = 0 t = np.arange(0, X.shape[1]) * dt for n in range(num_integrations): C_subscript = n t_exponent = num_integrations - n - 1 den = math.factorial(t_exponent) Cn = cvxpy.vstack((1 / den * C[i, C_subscript] * t**t_exponent for i in range(X.shape[0]))) Csum = Csum + Cn # Define the objective function error = cvxpy.sum_squares(Xdot[rows_of_interest, :] - (A @ X + Csum)[rows_of_interest, :]) C_regularization = gammaC * cvxpy.sum(cvxpy.abs(C)) A_regularization = gammaA * cvxpy.sum(cvxpy.abs(A)) obj = cvxpy.Minimize(error + C_regularization + A_regularization) # constraints constraints = [] if A_known is not None: for i in range(A_known.shape[0]): for j in range(A_known.shape[1]): if not np.isnan(A_known[i, j]): constraint_lo = A[i, j] >= A_known[i, j] - epsilon constraint_hi = A[i, j] <= A_known[i, j] + epsilon constraints.extend([constraint_lo, constraint_hi]) # Solve the problem prob = cvxpy.Problem(obj, constraints) prob.solve(solver=solver) # MOSEK does not take max_iters A = np.matrix(A.value) return A, np.matrix(C.value)
def own_constraints(self): """ b1, b2 """ rr = cvx.vstack([ self.x == self.b2.x, self.x == self.b2.x, self.x == self.b2.x, self.x == self.b2.x, ]) constraints = [] return constraints
def gram2edm(G, param, mode): N = param.N if mode: dG = cvx.vstack(cvx.diag(G)) D = cvx.matmul(dG, np.ones( (1, N))) - 2 * G + cvx.matmul(np.ones((N, 1)), dG.T) else: dG = np.diag(G)[:, None] D = dG - 2 * G + dG.T D[D <= 0] = 0 return D
def kernel_group_lasso(kDD, kDx, size_groups=None, l=0.01, max_iters=10000, eps=1e-4, gpu=False): """Solve Kernel Group LASSO problem via CVXPY with large scale SCS solver. :param kDD: Dictionary-vs-dictionary Gram matrix, positive semidefinite (d x d) :param kDx: Dictionary-vs-input Gram vector (d x 1) :param size_groups: List of size of groups :param l: Regularization parameter :param max_iters: Iteration count limit :param eps: Convergence tolerance :type kDD: np.ndarray :type kDx: np.ndarray :type num_groups: int :type l: float :type max_iters: int :type eps: float References: [1] `Jeni, László A., et al. "Spatio-temporal event classification using time-series kernel based structured sparsity." <https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5098425/>`_. """ assert (isinstance(kDD, np.ndarray) and kDD.ndim == 2 and kDD.shape[0] == kDD.shape[1]) #, (kDD.shape, kDx.shape)) assert (isinstance(kDx, np.ndarray) and kDx.ndim == 1 and kDx.shape[0] == kDD.shape[0]) #, (kDD.shape, kDx.shape)) if size_groups is None: size_groups = [1] * kDD.shape[0] assert (np.sum(size_groups) == len(kDx)) assert (l >= 0) cumsum = np.cumsum(size_groups) alpha = cvxpy.Variable(kDD.shape[0]) obj = cvxpy.Minimize( 0.5 * cvxpy.quad_form(alpha, kDD) \ - cvxpy.sum_entries(cvxpy.mul_elemwise(kDx, alpha)) \ + l * cvxpy.norm(cvxpy.vstack(*[np.sqrt(e - s) * cvxpy.norm(alpha[s:e]) for (s, e) in zip(np.concatenate([np.array([0]), cumsum[:-1]]), cumsum)]), 1) ) prob = cvxpy.Problem(obj) prob.solve(solver='SCS', max_iters=max_iters, verbose=True, eps=eps, gpu=gpu) a = np.asarray(alpha.value) g = np.asarray([ (1. / np.sqrt(e - s)) * np.linalg.norm(a[s:e]) for (s, e) in zip(np.concatenate([np.array([0]), cumsum[:-1]]), cumsum) ]) return a, g
def s_n_sparsity_constraint(self): sparsity_list = [] for s_id in range(self.N): funclist_len = len(self.sessions_beingserved_dict[s_id]['req_dict']['func_list']) for k in range(funclist_len, self.max_numitfuncs): sparsity_list.append(self.s_n[s_id, k]) # # if len(sparsity_list) == 0: return [] else: return [cp.vstack(*sparsity_list) == 0]
def cbp_polar(y, F, Fprev, Fnext, Delta, penalty=0.1, order=1): """ CBP with polar interpolation """ # compute r and theta a = 0.5 * np.linalg.norm(Fnext-Fprev, axis=0)[int(F.shape[1]/2)] b = np.linalg.norm(F-Fprev, axis=0)[int(F.shape[1]/2)] theta = np.pi - 2 * np.arcsin(a/b) # radians r = a / np.sin(theta) # build the polar transformation matrix P = np.array([[1,r*np.cos(theta),-r*np.sin(theta)], [1,r,0], [1,r*np.cos(theta),r*np.sin(theta)]]) # get C, U, and V pol = np.linalg.inv(P).dot(np.vstack((Fprev.ravel(),F.ravel(),Fnext.ravel()))) C = pol[0,:].reshape(F.shape) U = pol[1,:].reshape(F.shape) V = pol[2,:].reshape(F.shape) ## construct the problem # discretized matrices Cp = cvxopt.matrix(C) Up = cvxopt.matrix(U) Vp = cvxopt.matrix(V) yp = cvxopt.matrix(y) # sparsity penalty gamma = cp.Parameter(sign="positive", name='gamma') gamma.value = penalty # variables dx = cp.Variable(F.shape[1],name='x') dy = cp.Variable(F.shape[1],name='y') dz = cp.Variable(F.shape[1],name='z') # objective and constraints objective = cp.Minimize(sum(cp.square(yp - Cp*dx - Up*dy - Vp*dz)) + gamma*cp.norm(dx, 1)) #constraints = [0 <= x, cp.sqrt(cp.square(y)+cp.square(z)) <= r*x, r*np.cos(theta)*x <= y, y <= r*x] sqcon = [cp.norm(cp.vstack(yi,zi),2) <= xi*r for xi, yi, zi in zip(dx,dy,dz)] constraints = [0 <= dx, dy <= r*dx, r*np.cos(theta)*dx <= dy] constraints.extend(sqcon) p = cp.Problem(objective, constraints) # solve result = p.solve() # reconstruct yhat = C.dot(np.array(dx.value)) + U.dot(np.array(dy.value)) + V.dot(np.array(dz.value)) return np.array(dx.value), yhat, p.value
def test_log_sum_exp(self): """Test log_sum_exp function that failed in Github issue. """ import cvxpy as cp import numpy as np np.random.seed(1) m = 5 n = 2 X = np.matrix(np.ones((m,n))) w = cp.Variable(n) expr2 = [cp.log_sum_exp(cp.vstack(0, X[i,:]*w)) for i in range(m)] expr3 = sum(expr2) obj = cp.Minimize(expr3) p = cp.Problem(obj) p.solve(solver=SCS, max_iters=1) # # Risk return tradeoff curve # def test_risk_return_tradeoff(self): # from math import sqrt # from cvxopt import matrix # from cvxopt.blas import dot # from cvxopt.solvers import qp, options # import scipy # n = 4 # S = matrix( [[ 4e-2, 6e-3, -4e-3, 0.0 ], # [ 6e-3, 1e-2, 0.0, 0.0 ], # [-4e-3, 0.0, 2.5e-3, 0.0 ], # [ 0.0, 0.0, 0.0, 0.0 ]] ) # pbar = matrix([.12, .10, .07, .03]) # N = 100 # # CVXPY # Sroot = numpy.asmatrix(scipy.linalg.sqrtm(S)) # x = cp.Variable(n, name='x') # mu = cp.Parameter(name='mu') # mu.value = 1 # TODO cp.Parameter("positive") # objective = cp.Minimize(-pbar*x + mu*quad_over_lin(Sroot*x,1)) # constraints = [sum_entries(x) == 1, x >= 0] # p = cp.Problem(objective, constraints) # mus = [ 10**(5.0*t/N-1.0) for t in range(N) ] # xs = [] # for mu_val in mus: # mu.value = mu_val # p.solve() # xs.append(x.value) # returns = [ dot(pbar,x) for x in xs ] # risks = [ sqrt(dot(x, S*x)) for x in xs ] # # QP solver
def train(self, level=0, lamb=0.01): """ :param level: 0: 非正则化; 1: 1阶正则化; 2: 2阶正则化 :param lamb: 正则化系数水平 :return: 无 """ L = cvx.Parameter(sign="positive") L.value = lamb # 正则化系数 w = cvx.Variable(self.n + 1) # 参数向量 loss = 0 for i in range(self.m): # 构造成本函数和正则化项 loss += self.y_trans[i] * \ cvx.log_sum_exp(cvx.vstack(0, cvx.exp(self.x_trans[i, :].T * w))) + \ (1 - self.y_trans[i]) * \ cvx.log_sum_exp(cvx.vstack(0, cvx.exp(-1 * self.x_trans[i, :].T * w))) # 为什么一定要用log_sum_exp? cvx.log(1 + cvx.exp(x[i, :].T * w))为什么不行? if level > 0: reg = cvx.norm(w[:self.n], level) prob = cvx.Problem(cvx.Minimize(loss / self.m + L / (2 * self.m) * reg)) else: prob = cvx.Problem(cvx.Minimize(loss / self.m)) prob.solve() self.w = np.array(w.value)
def test_log_sum_exp(self): """Test log_sum_exp function that failed in Github issue. """ import cvxpy as cp import numpy as np np.random.seed(1) m = 5 n = 2 X = np.matrix(np.ones((m, n))) w = cp.Variable(n) expr2 = [cp.log_sum_exp(cp.vstack(0, X[i, :]*w)) for i in range(m)] expr3 = sum(expr2) obj = cp.Minimize(expr3) p = cp.Problem(obj) p.solve(solver=SCS, max_iters=1)
def get_constraints(self, X_v, U_v, X_last_p, U_last_p): """ Get model specific constraints. :param X_v: cvx variable for current states :param U_v: cvx variable for current inputs :param X_last_p: cvx parameter for last states :param U_last_p: cvx parameter for last inputs :return: A list of cvx constraints """ # Boundary conditions: constraints = [ X_v[0, 0] == self.x_init[0], X_v[1:4, 0] == self.x_init[1:4], X_v[4:7, 0] == self.x_init[4:7], # X_v[7:11, 0] == self.x_init[7:11], # initial orientation is free X_v[11:14, 0] == self.x_init[11:14], # X_[0, -1] final mass is free X_v[1:, -1] == self.x_final[1:], U_v[1:3, -1] == 0, ] constraints += [ # State constraints: X_v[0, :] >= self.m_dry, # minimum mass cvxpy.norm(X_v[2: 4, :], axis=0) <= X_v[1, :] / \ self.tan_gamma_gs, # glideslope cvxpy.norm(X_v[9:11, :], axis=0) <= np.sqrt( (1 - self.cos_theta_max) / 2), # maximum angle # maximum angular velocity cvxpy.norm(X_v[11: 14, :], axis=0) <= self.w_B_max, # Control constraints: cvxpy.norm(U_v[1:3, :], axis=0) <= self.tan_delta_max * \ U_v[0, :], # gimbal angle constraint cvxpy.norm(U_v, axis=0) <= self.T_max, # upper thrust constraint ] # linearized lower thrust constraint rhs = [U_last_p[:, k] / cvxpy.norm(U_last_p[:, k]) * U_v[:, k] for k in range(X_v.shape[1])] constraints += [ self.T_min <= cvxpy.vstack(rhs) ] return constraints
def r_bwprocdur_sparsity_constraint(self): sparsity_list = [] for s_id in range(self.N): path_info_dict = self.sid_res_dict[s_id]['path_info'] # for l_id in range(0, self.num_link): # if not (l_id in path_info_dict['linkid_list']): # sparsity_list.append(self.r_bw.get((s_id, l_id)) ) for itr_id in range(self.num_itr): itr_id_ = itr_id + self.ll_index + 1 if not (itr_id_ in path_info_dict['itrid_list']): sparsity_list.append(self.r_proc[s_id, itr_id]) # sparsity_list.append(self.r_dur[s_id, itr_id]) # if not sparsity_list: return []; else: return [cp.vstack(*sparsity_list) == 0]
def run(self, util_rate_generator, load): """ Given a load and a pricing scheme, computes the optimal charge/discharge schedule for the battery which minimizes total cost. """ # renamaing vars for readibility T = const.HORIZON # battery constraint params C = self.max_capacity M = self.max_power_output # energy storage of the battery s = cvx.Variable(T+1) # power output of the battery u = cvx.Variable(T) # state matrix A = np.diag([1]*(T), k=-1) # cost metrics for enery and demand energy_metric = lambda x : cvx.norm(x,1) demand_metric = lambda x : cvx.norm(x, 'inf') cost = cvx.Minimize(self.cost_function(u, load , util_rate_generator, energy_metric, demand_metric)) #cost = cvx.Minimize(.2*load.T*(load + u)) constraints = [ s == A*s + cvx.vstack(0, self.acdc_eff*u), s <= C, s >= 0, u <= M, u >= -M, u + load >=0 ] # sums problem objectives and concatenates constraints. prob = cvx.Problem(cost, constraints) # add final stopping conditions on the battery prob.constraints += [s[T] == 0, s[0] == 0] optval = prob.solve() self.optimal_cost = optval self.optimal_s = s self.optimal_u = u self.problem = prob
def test_problem_penalty(self): """ Compare cvxpy solutions to cvxopt ground truth """ from cvxpy import (matrix,variable,program,minimize, sum,abs,norm2,log,square,zeros,max, hstack,vstack) m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # set tolerance to 5 significant digits tol_exp = 5 # l1 approximation x = variable(n) p = program(minimize(sum(abs(A*x + b)))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x1,tol_exp) # l2 approximation x = variable(n) p = program(minimize(norm2(A*x + b))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x2,tol_exp) # Deadzone approximation - implementation is currently ugly (need max along axis) x = variable(n) Axbm = abs(A*x+b)-0.5 Axbm_deadzone = vstack([max(hstack((Axbm[i,0],0.0))) for i in range(m)]) p = program(minimize(sum(Axbm_deadzone))) p.solve(True) obj_dz_cvxpy = np.sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) np.testing.assert_array_almost_equal(obj_dz_cvxpy,self.obj_dz,tol_exp) # Log barrier x = variable(n) p = program(minimize(-sum(log(1.0-square(A*x + b))))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.cxlb,tol_exp)
def prox(self, x0, phi0): terms = [] for a, x in self._xs.iteritems(): for g, v in x.iteritems(): terms.append(v - x0[a][g]) for g, v in self._phi.iteritems(): terms.append(v - phi0[g]) prob = cvx.Problem(cvx.Minimize(cvx.norm2(cvx.vstack(*terms))), self._constr) prob.solve(solver=cvx.SCS, verbose=False) assert prob.status == 'optimal' xs = {} for a, x in self._xs.iteritems(): # take the positive part of the good allocations. (they should # be positive by the constraints anyway, but do this to polish # solution xs[a] = {g: max(v.value, 0) for g, v in x.iteritems()} phi = {g: v.value for g, v in self._phi.iteritems()} return xs, phi
def addSource(self, regressor, name = None, costFunction='sse',alpha = 1, # Cost function for fit to regressors, alpha is a scalar multiplier regularizeTheta=None, beta = 1, # Cost function for parameter regularization, beta is a scalar multiplier regularizeSource=None, gamma = 1, # Cost function for signal smoothing, gamma is a scalar multiplier lb=None, # Lower bound on source ub=None, # Upper bound on source idxScrReg=None, # indices used to break the source signal into smaller ones to apply regularization numWind=1, # number of time windows (relevant for time-varying regressors) ): ### This is a method to add a new source self.modelcounter += 1 # Increment model counter ## Write model name if it doesn't exist. if name is None: name = str(self.modelcounter) ## Instantiate a dictionary of model terms model = {} model['name'] = name model['alpha'] = alpha model['lb']=lb model['ub']=ub ## Check regressor shape regressor = np.array(regressor) if regressor.ndim == 0: ## If no regressors are included, set them an empty array regressor = np.zeros((self.N,0)) if regressor.ndim == 1: regressor = np.expand_dims(regressor,1) if regressor.ndim > 2: raise NameError('Regressors cannot have more than 2 dimensions') ## Check that regressors have the correct shape (Nobs, Nregressors) if regressor.shape[0] != self.N: if regressor.shape[1] == self.N: regressor = regressor.transpose() else: raise NameError('Lengths of regressors and aggregate signal must match') ## Define model regressors and order model['regressor'] = regressor model['order'] = regressor.shape[1] ## Define decision variables and cost function style model['source'] = cvp.Variable(self.N,1) #model['source'] = cvp.Variable((self.N,1)) # required for cvxpy 1.0.1 model['theta'] = cvp.Variable(model['order'],1) #model['theta'] = cvp.Variable((model['order'],1)) # required for cvxpy 1.0.1 model['costFunction'] = costFunction ## Define objective function to fit model to regressors if costFunction.lower() == 'sse': residuals = (model['source'] - model['regressor'] * model['theta']) modelObj = cvp.sum_squares(residuals) * model['alpha'] elif costFunction.lower() == 'l1': residuals = (model['source'] - model['regressor'] * model['theta']) #residuals = (model['source'] - auxVec - model['regressor'] * model['theta']) modelObj = cvp.norm(residuals,1) * model['alpha'] elif costFunction.lower()=='l2': residuals = (model['source'] - model['regressor'] * model['theta']) modelObj = cvp.norm(residuals,2) * model['alpha'] else: raise ValueError('{} wrong option, use "sse","l2" or "l1"'.format(costFunction)) ## Define cost function to regularize theta **************** # ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** * # Check that beta is scalar or of length of number of parameters. beta = np.array(beta) if beta.size not in [1, model['order']]: raise ValueError('Beta must be scalar or vector with one element for each regressor') if regularizeTheta is not None: if callable(regularizeTheta): ## User can input their own function to regularize theta. # Must input a cvxpy variable vector and output a scalar # or a vector with one element for each parameter. try: regThetaObj = regularizeTheta(model['theta']) * beta except: raise ValueError('Check custom regularizer for model {}'.format(model['name'])) if regThetaObj.size[0]* regThetaObj.size[1] != 1: raise ValueError('Check custom regularizer for model {}, make sure it returns a scalar'.format(model['name'])) elif regularizeTheta.lower() == 'l2': ## Sum square errors. regThetaObj = cvp.norm(model['theta'] * beta) elif regularizeTheta.lower() == 'l1': regThetaObj = cvp.norm(model['theta'] * beta, 1) elif regularizeTheta.lower() == 'diff_l2': if numWind==1: regThetaObj = 0 else: if regressor.shape[1] == numWind: # this actually corresponds to the solar model (no intercept) thetaDiffVec = cvp.diff(model['theta']) else: thetaDiffVec = cvp.vstack(cvp.diff(model['theta'][0:numWind]),cvp.diff(model['theta'][numWind:2*numWind])) regThetaObj = cvp.norm(thetaDiffVec,2) * beta elif regularizeTheta.lower() == 'diff_l1': if numWind==1: regThetaObj = 0 else: if regressor.shape[1] == numWind: # this actually corresponds to the solar model (no intercept) thetaDiffVec = cvp.diff(model['theta']) else: thetaDiffVec = cvp.vstack(cvp.diff(model['theta'][0:numWind]),cvp.diff(model['theta'][numWind:2*numWind])) regThetaObj = cvp.norm(thetaDiffVec,1) * beta else: regThetaObj = 0 ## Define cost function to regularize source signal **************** # ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** * # Check that gamma is scalar gamma = np.array(gamma) if gamma.size != 1: raise NameError('Gamma must be scalar') ## Calculate regularization. if regularizeSource is not None: if idxScrReg is not None: scrVec = model['source'][0:idxScrReg[0]] idxStart = idxScrReg[0]+1 for idxEnd in idxScrReg[1:]: scrCur = cvp.diff(model['source'][idxStart:idxEnd]) scrVec = cvp.vstack(scrVec,scrCur) idxStart = idxEnd+1 scrVec = cvp.vstack(scrVec,cvp.diff(model['source'][idxStart:])) else: scrVec = cvp.diff(model['source']) if callable(regularizeSource): ## User can input their own function to regularize the source signal. # Must input a cvxpy variable vector and output a scalar. regSourceObj = regularizeSource(scrVec) * gamma elif regularizeSource.lower() == 'diff1_ss': regSourceObj = cvp.sum_squares(scrVec) * gamma elif regularizeSource.lower() == 'diff_l1': regSourceObj = cvp.norm(scrVec,1) * gamma elif regularizeSource.lower() == 'diff_l2': regSourceObj = cvp.norm(scrVec,2) * gamma else: regSourceObj = 0 ## Sum total model objective model['obj'] = modelObj + regThetaObj + regSourceObj ## Append model to models list self.models[name]= model
@author: cyrusl EE 364A Convex Optimization Homework 4 4.21 (Python Version) Due 7/21/2014 """ import numpy as np import cvxpy as cvx import matplotlib.pyplot as plt np.random.seed(10) (m, n) = (30, 10) A = np.random.rand(m,n); A = np.asmatrix(A) b = np.random.rand(m,1); b = np.asmatrix(b) c_nom = np.ones((n,1)) + np.random.rand(n,1); c_nom = np.asmatrix(c_nom) F = cvx.vstack(np.matrix(np.identity(n)), -np.matrix(np.identity(n)), 1/n*np.matrix(np.ones((1,n))), -1/n* np.matrix(np.ones((1,n)))) g = cvx.vstack(1.25 *c_nom, -0.75*c_nom, 1.1/n*cvx.sum_entries(c_nom), -0.9/n*cvx.sum_entries(c_nom)) # robust LP x = cvx.Variable(n) lambd = cvx.Variable(g.size[0]) obj = cvx.Minimize(lambd.T*g) constraints = [A*x >= b, lambd >= 0, F.T *lambd == x] prob = cvx.Problem(obj, constraints) #prob.solve(solver=cvx.SCS) prob.solve() print "status:", prob.status print "nominal cost (robust case):", (c_nom.T * x).value print "f(x):", prob.value
# -*- 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
def sys_norm_hinf_LMI(A, Bdisturbance, C, D = None): '''Compute a system's Hinfinity norm, using an LMI approach. Acl, Bdisturbance are system matrices, describing the systems dynamics: dx/dt = Acl*x + Bdisturbance*v where x is the system state and v is the disturbance. The system output is: z = C*x + D*v The matrix Acl must be Hurwitz for the Hinf norm to be finite. Parameters ---------- A : (n, n) Matrix Input Bdisturbance : (n, m) Matrix Input C : (q, n) Matrix Input D : (q,m) Matrix Input (optional) Returns ------- Jinf : Systems Hinf norm. See: Robust Controller Design By Convex Optimization, Alireza Karimi Laboratoire d'Automatique, EPFL ''' if not controlpy.analysis.is_hurwitz(A): return np.Inf n = A.shape[0] ndist = Bdisturbance.shape[1] nout = C.shape[0] X = cvxpy.Semidef(n) g = cvxpy.Variable() if D is None: D = np.matrix(np.zeros([nout, ndist])) r1 = cvxpy.hstack(cvxpy.hstack(A.T*X+X*A, X*Bdisturbance), C.T) r2 = cvxpy.hstack(cvxpy.hstack(Bdisturbance.T*X, -g*np.matrix(np.identity(ndist))), D.T) r3 = cvxpy.hstack(cvxpy.hstack(C, D), -g*np.matrix(np.identity(nout))) tmp = cvxpy.vstack(cvxpy.vstack(r1,r2),r3) constraints = [tmp == -cvxpy.Semidef(n + ndist + nout)] obj = cvxpy.Minimize(g) prob = cvxpy.Problem(obj, constraints) try: prob.solve()#solver='CVXOPT', kktsolver='robust') except cvxpy.error.SolverError: print('Solution not found!') return None if not prob.status == cvxpy.OPTIMAL: return None return g.value
#define variables. x_vals = [cp.Variable() for i in range(N)] y_vals = [cp.Variable() for i in range(N)] objective = cp.Minimize(cp.max_elemwise( *(x_vals + y_vals) ) + 0.5) constraints = [] for i in range(N): constraints += [0.5*r[i] <= x_vals[i], 0.5*r[i] <= y_vals[i]] diff_vars = [] for i in range(N-1): for j in range(i+1,N): t = cp.Variable() diff_vars.append(ncvx.Annulus(2, 0.5 * (r[i]+r[j]), N)) constraints += [ cp.vstack(x_vals[i] - x_vals[j], y_vals[i] - y_vals[j]) == diff_vars[-1]] prob = cp.Problem(objective, constraints) result = prob.solve(method="relax-round-polish", polish_depth=100) #plot the circles circ = np.linspace(0,2 * np.pi) for i in range(N): plt.plot(x_vals[i].value+0.5*r[i]*np.cos(circ),y_vals[i].value+0.5*r[i]*np.sin(circ),'b') plt.xlim([0,objective.value]) plt.ylim([0,objective.value]) plt.axes().set_aspect('equal') plt.show()
def N4SID(u,y,NumRows,NumCols,NSig,require_stable=False): """ A,B,C,D,Cov,Sigma = N4SID(u,y,NumRows,NumCols,n,require_stable=False) Let NumVals be the number of input and output values available In this case: u - NumInputs x NumVals array of inputs y - NumOutputs x NumVals array of outputs NumRows - Number of block rows in the past and future block Hankel matrices NumCols - Number of columns in the past and future block Hankel matrices n - desired state dimension. For the algorithm to work, you must have: NumVals >= 2*NumRows + NumCols - 1 Returns A,B,C,D - the state space realization from inputs to outputs Cov - the joint covariance of the process and measurement noise Sigma - the singular values of the oblique projection of row space of future outputs along row space of future inputs on the row space of past inputs and outputs. Examining Sigma can be used to determine the required state dimension require_stable - An optional boolean parameter. Default is False If False, the standard N4SID algorithm is used If True, the state matrix, A, will have spectral radius < 1. In order to run with require_stable=True, cvxpy must be installed. """ NumInputs = u.shape[0] NumOutputs = y.shape[0] NumDict = {'Inputs': NumInputs, 'Outputs': NumOutputs, 'Dimension':NSig, 'Rows':NumRows, 'Columns':NumCols} GammaDict,S = preProcess(u,y,NumDict) GamData = GammaDict['Data'] GamYData = GammaDict['DataY'] if not require_stable: K = la.lstsq(GamData.T,GamYData.T)[0].T else: Kvar = cvx.Variable(NSig+NumOutputs,NSig+NumInputs*NumRows) Avar = Kvar[:NSig,:NSig] Pvar = cvx.Semidef(NSig) LyapCheck = cvx.vstack(cvx.hstack(Pvar,Avar), cvx.hstack(Avar.T,np.eye(NSig))) Constraints = [LyapCheck>>0,Pvar << np.eye(NSig)] diffVar = GamYData - Kvar*GamData objFun = cvx.norm(diffVar,'fro') Objective = cvx.Minimize(objFun) Prob = cvx.Problem(Objective,Constraints) result = Prob.solve() K = Kvar.value AID,BID,CID,DID,CovID = postProcess(K,GammaDict,NumDict) return AID,BID,CID,DID,CovID,S
import cvxpy as cp k = 2000 t = [-3.0+6.0*(i)/(k-1) for i in range(k) ] #range start from zero y = np.exp(t) T_powers = np.matrix(np.hstack((np.ones((k,1)),np.matrix(t).T,np.power(np.matrix(t).T,2)))) u = np.exp(3) l = 0 bisection_tol = 1e-3 gamma1 = cp.Parameter(sign='positive') a = cp.Variable(3) b = cp.Variable(2) objective = 0 #constraints = [cp.abs(T_powers[i]*a-y[i]*(T_powers[i]*cp.vstack(1,b)))<=gamma1*(T_powers[i]*cp.vstack(1,b)) for i in range(100)] #constraints = [T_powers*a-np.diag(y)*(T_powers*cp.vstack(1,b))<=gamma1*(T_powers*cp.vstack(1,b)), # T_powers*a-np.diag(y)*(T_powers*cp.vstack(1,b))>=-gamma1*(T_powers*cp.vstack(1,b))] constraints = [cp.abs(T_powers*a-np.diag(y)*(T_powers*cp.vstack(1,b)))<=gamma1*(T_powers*cp.vstack(1,b))] objective = cp.Minimize(np.ones((1,3))*a) p = cp.Problem(objective,constraints) gamma1.value = (l+u)/2.0 a_opt = 0 b_opt = 0 gamma1.value = (u+l)/2 while (u-l)>=bisection_tol: print p.is_dcp() # p.solve(solver=cp.CVXOPT) p.solve() # p.solve(verbose=True) if p.status is 'optimal': u = gamma1.value a_opt = a.value b_opt = b.value
import numpy as np import cvxpy as cp img = mpimg.imread('flowgray.png') img_d = np.copy(img) #plt.imshow(np.asarray(img),cmap=cm.binary_r) m,n = img.shape mask = np.random.rand(m,n) known = np.zeros((m,n)) known = mask>0.5 x,y=np.where(mask>0.5) ########### Ul2= cp.Variable(m,n) constraints1=[Ul2[x[i],y[i]]==np.float(img[x[i],y[i]]) for i in range(x.size)] Ux1 = Ul2[1:,1:]-Ul2[1:,0:n-1] Uy1 = Ul2[1:,1:]-Ul2[0:m-1,1:] objective1 = cp.Minimize(cp.norm2(cp.vstack(Ux1,Uy1))) p1 = cp.Problem(objective1,constraints1) ######################################### Utv= cp.Variable(m,n) constraints2=[Utv[x[i],y[i]]==np.float(img[x[i],y[i]]) for i in range(x.size)] Ux2 = Utv[1:,1:]-Utv[1:,0:n-1] Uy2 = Utv[1:,1:]-Utv[0:m-1,1:] objective2 = cp.Minimize(cp.norm(cp.vstack(Ux2,Uy2),1)) p2 = cp.Problem(objective2,constraints2) p1.solve(verbose = True) p2.solve(verbose = True) plt.figure(1) plt.set_cmap(cm.binary_r)
def tt_epigraph_form_constraint(self): # trans_time_i <= tt_i ; i=0,1,2,3,...,N-1 return [cp.vstack(*self.s_transt.get_column(0)) <= self.tt]
np.random.seed(1) (m, n) = (70, 500) k = 10 z_true = np.zeros(n, dtype=complex) z_true[0:k] = np.random.randn(k) + 1j * np.random.randn(k) np.random.shuffle(z_true); z_true = np.asmatrix(z_true).T A = np.random.randn(m, n) + 1j * np.random.randn(m, n) A = np.asmatrix(A) b = A*z_true; b = np.asmatrix(b) zr = cvx.Variable(n) zi = cvx.Variable(n) t = cvx.Variable(m) obj = cvx.Minimize(cvx.sum_entries(t)) #AA = cvx.norm2(cvx.hstack(cvx.vstack(A.real,A.imag),cvx.vstack(-A.imag,A.real))) z = cvx.vstack(zr,zi) constraints = [ cvx.norm2(cvx.hstack(cvx.vstack(A[i,:].real,A[i,:].imag), cvx.vstack(-A[i,:].imag,A[i,:].real))*z -cvx.vstack(b[i].real,b[i].imag)) <= t[i] for i in range(m)] ''' constraints = [ cvx.norm2(cvx.hstack(cvx.vstack(A.real,A.imag),cvx.vstack(-A.imag,A.real))*z - cvx.vstack(b.real,b.imag)) <= t] ''' prob = cvx.Problem(obj, constraints) sol = prob.solve(solver=cvx.ECOS) print 'status: {}'.format(prob.status) print 'norm z: {}'.format(cvx.norm2(z).value) print 'norm z_true: {}'.format((cvx.norm2(z_true.real)+cvx.norm2(z_true.imag)).value) for i in range(n): print '{} + {}j' .format(zr[i].value,zi[i].value)
k = 201 t = np.asmatrix(np.linspace(-3,3,k)).T y = np.exp(t) one = np.asmatrix(np.ones((k,1))) Tpow = cvx.hstack(one, t, cvx.square(t)) u = np.exp(3) l = 0 tol = 1e-3 while u-l >= tol: mid = (l+u)/2 a = cvx.Variable(3) b = cvx.Variable(2) obj = cvx.Minimize(0) constraints = [cvx.abs(Tpow*a - cvx.mul_elemwise(y, Tpow*cvx.vstack(1, b))) <= mid * Tpow*cvx.vstack(1, b)] prob = cvx.Problem(obj, constraints) sol = prob.solve(solver=cvx.CVXOPT) if prob.status == cvx.OPTIMAL: print('gamma = {}', format(mid)) u = mid a_opt = a b_opt = b objval_opt = mid else: l = mid y_fit = cvx.mul_elemwise(Tpow*a_opt.value, cvx.inv_pos(Tpow*cvx.vstack(1, b_opt.value))) plt.figure(0)