Python cvxpy.Problem() Examples
The following are 30
code examples of cvxpy.Problem().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cvxpy
, or try the search function
.
Example #1
Source File: intervalencoder.py From scikit-lego with MIT License | 6 votes |
def _mk_monotonic_average(xs, ys, intervals, method="increasing", **kwargs): """ Creates smoothed averages of `ys` at the intervals given by `intervals`. :param xs: all the datapoints of a feature (represents the x-axis) :param ys: all the datapoints what we'd like to predict (represents the y-axis) :param intervals: the intervals at which we'd like to get a good average value :param method: the method that is used for smoothing, can be either `increasing` or `decreasing`. :return: An array as long as `intervals` that represents the average `y`-values at those intervals, keeping the constraint in mind. """ x_internal = np.array([xs >= i for i in intervals]).T.astype(np.float) betas = cp.Variable(x_internal.shape[1]) objective = cp.Minimize(cp.sum_squares(x_internal * betas - ys)) if method == "increasing": constraints = [betas[i + 1] >= 0 for i in range(betas.shape[0] - 1)] elif method == "decreasing": constraints = [betas[i + 1] <= 0 for i in range(betas.shape[0] - 1)] else: raise ValueError( f"method must be either `increasing` or `decreasing`, got: {method}" ) prob = cp.Problem(objective, constraints) prob.solve() return betas.value.cumsum()
Example #2
Source File: admm_problem.py From ncvx with GNU General Public License v3.0 | 6 votes |
def polish(orig_prob, polish_depth=5, polish_func=None, *args, **kwargs): # Fix noncvx variables and solve. for var in get_noncvx_vars(orig_prob): var.value = var.z.value old_val = None for t in range(polish_depth): fix_constr = [] for var in get_noncvx_vars(orig_prob): fix_constr += var.restrict(var.value) polish_prob = cvx.Problem(orig_prob.objective, orig_prob.constraints + fix_constr) polish_prob.solve(*args, **kwargs) if polish_prob.status in [cvx.OPTIMAL, cvx.OPTIMAL_INACCURATE] and \ (old_val is None or (old_val - polish_prob.value)/(old_val + 1) > 1e-3): old_val = polish_prob.value else: break return polish_prob.value, polish_prob.status # Add admm method to cvx Problem.
Example #3
Source File: nuclear_norm_minimization.py From ME-Net with MIT License | 6 votes |
def solve(self, X, missing_mask): X = check_array(X, force_all_finite=False) m, n = X.shape S, objective = self._create_objective(m, n) constraints = self._constraints( X=X, missing_mask=missing_mask, S=S, error_tolerance=self.error_tolerance) problem = cvxpy.Problem(objective, constraints) problem.solve( verbose=self.verbose, solver=cvxpy.SCS, max_iters=self.max_iters, # use_indirect, see: https://github.com/cvxgrp/cvxpy/issues/547 use_indirect=False) return S.value
Example #4
Source File: nuclear_norm_minimization.py From fancyimpute with Apache License 2.0 | 6 votes |
def solve(self, X, missing_mask): X = check_array(X, force_all_finite=False) m, n = X.shape S, objective = self._create_objective(m, n) constraints = self._constraints( X=X, missing_mask=missing_mask, S=S, error_tolerance=self.error_tolerance) problem = cvxpy.Problem(objective, constraints) problem.solve( verbose=self.verbose, solver=cvxpy.SCS, max_iters=self.max_iters, # use_indirect, see: https://github.com/cvxgrp/cvxpy/issues/547 use_indirect=False) return S.value
Example #5
Source File: models.py From optnet with Apache License 2.0 | 6 votes |
def get_sudoku_matrix(n): X = np.array([[cp.Variable(n**2) for i in range(n**2)] for j in range(n**2)]) cons = ([x >= 0 for row in X for x in row] + [cp.sum(x) == 1 for row in X for x in row] + [sum(row) == np.ones(n**2) for row in X] + [sum([row[i] for row in X]) == np.ones(n**2) for i in range(n**2)] + [sum([sum(row[i:i+n]) for row in X[j:j+n]]) == np.ones(n**2) for i in range(0,n**2,n) for j in range(0, n**2, n)]) f = sum([cp.sum(x) for row in X for x in row]) prob = cp.Problem(cp.Minimize(f), cons) A = np.asarray(prob.get_problem_data(cp.ECOS)[0]["A"].todense()) A0 = [A[0]] rank = 1 for i in range(1,A.shape[0]): if np.linalg.matrix_rank(A0+[A[i]], tol=1e-12) > rank: A0.append(A[i]) rank += 1 return np.array(A0)
Example #6
Source File: measure_utils.py From ambient-gan with MIT License | 6 votes |
def get_inpaint_func_tv(): def inpaint_func(image, mask): """Total variation inpainting""" inpainted = np.zeros_like(image) for c in range(image.shape[2]): image_c = image[:, :, c] mask_c = mask[:, :, c] if np.min(mask_c) > 0: # if mask is all ones, no need to inpaint inpainted[:, :, c] = image_c else: h, w = image_c.shape inpainted_c_var = cvxpy.Variable(h, w) obj = cvxpy.Minimize(cvxpy.tv(inpainted_c_var)) constraints = [cvxpy.mul_elemwise(mask_c, inpainted_c_var) == cvxpy.mul_elemwise(mask_c, image_c)] prob = cvxpy.Problem(obj, constraints) # prob.solve(solver=cvxpy.SCS, max_iters=100, eps=1e-2) # scs solver prob.solve() # default solver inpainted[:, :, c] = inpainted_c_var.value return inpainted return inpaint_func
Example #7
Source File: cvxpy_examples.py From cvxpylayers with Apache License 2.0 | 6 votes |
def ball_con(): # print(f'--- {sys._getframe().f_code.co_name} ---') print('ball con') npr.seed(0) n = 2 A = cp.Parameter((n, n)) z = cp.Parameter(n) p = cp.Parameter(n) x = cp.Variable(n) t = cp.Variable(n) obj = cp.Minimize(0.5 * cp.sum_squares(x - p)) # TODO automate introduction of variables. cons = [0.5 * cp.sum_squares(A * t) <= 1, t == (x - z)] prob = cp.Problem(obj, cons) L = npr.randn(n, n) A.value = L.T z.value = npr.randn(n) p.value = npr.randn(n) prob.solve(solver=cp.SCS) print(x.value)
Example #8
Source File: cvxpy_examples.py From cvxpylayers with Apache License 2.0 | 6 votes |
def relu(): # print(f'--- {sys._getframe().f_code.co_name} ---') print('relu') npr.seed(0) n = 4 _x = cp.Parameter(n) _y = cp.Variable(n) obj = cp.Minimize(cp.sum_squares(_y - _x)) cons = [_y >= 0] prob = cp.Problem(obj, cons) _x.value = npr.randn(n) prob.solve(solver=cp.SCS) print(_y.value)
Example #9
Source File: cvxpy_examples.py From cvxpylayers with Apache License 2.0 | 6 votes |
def running_example(): print("running example") m = 20 n = 10 x = cp.Variable((n, 1)) F = cp.Parameter((m, n)) g = cp.Parameter((m, 1)) lambd = cp.Parameter((1, 1), nonneg=True) objective_fn = cp.norm(F @ x - g) + lambd * cp.norm(x) constraints = [x >= 0] problem = cp.Problem(cp.Minimize(objective_fn), constraints) assert problem.is_dcp() assert problem.is_dpp() print("is_dpp: ", problem.is_dpp()) F_t = torch.randn(m, n, requires_grad=True) g_t = torch.randn(m, 1, requires_grad=True) lambd_t = torch.rand(1, 1, requires_grad=True) layer = CvxpyLayer(problem, parameters=[F, g, lambd], variables=[x]) x_star, = layer(F_t, g_t, lambd_t) x_star.sum().backward() print("F_t grad: ", F_t.grad) print("g_t grad: ", g_t.grad)
Example #10
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 6 votes |
def test_example(self): n, m = 2, 3 x = cp.Variable(n) A = cp.Parameter((m, n)) b = cp.Parameter(m) constraints = [x >= 0] objective = cp.Minimize(0.5 * cp.pnorm(A @ x - b, p=1)) problem = cp.Problem(objective, constraints) assert problem.is_dpp() cvxpylayer = CvxpyLayer(problem, parameters=[A, b], variables=[x]) A_tch = torch.randn(m, n, requires_grad=True) b_tch = torch.randn(m, requires_grad=True) # solve the problem solution, = cvxpylayer(A_tch, b_tch) # compute the gradient of the sum of the solution with respect to A, b solution.sum().backward()
Example #11
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 6 votes |
def test_simple_batch_socp(self): set_seed(243) n = 5 m = 1 batch_size = 4 P_sqrt = cp.Parameter((n, n), name='P_sqrt') q = cp.Parameter((n, 1), name='q') A = cp.Parameter((m, n), name='A') b = cp.Parameter((m, 1), name='b') x = cp.Variable((n, 1), name='x') objective = 0.5 * cp.sum_squares(P_sqrt @ x) + q.T @ x constraints = [A@x == b, cp.norm(x) <= 1] prob = cp.Problem(cp.Minimize(objective), constraints) prob_tch = CvxpyLayer(prob, [P_sqrt, q, A, b], [x]) P_sqrt_tch = torch.randn(batch_size, n, n, requires_grad=True) q_tch = torch.randn(batch_size, n, 1, requires_grad=True) A_tch = torch.randn(batch_size, m, n, requires_grad=True) b_tch = torch.randn(batch_size, m, 1, requires_grad=True) torch.autograd.gradcheck(prob_tch, (P_sqrt_tch, q_tch, A_tch, b_tch))
Example #12
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 6 votes |
def test_entropy_maximization(self): set_seed(243) n, m, p = 5, 3, 2 tmp = np.random.rand(n) A_np = np.random.randn(m, n) b_np = A_np.dot(tmp) F_np = np.random.randn(p, n) g_np = F_np.dot(tmp) + np.random.rand(p) x = cp.Variable(n) A = cp.Parameter((m, n)) b = cp.Parameter(m) F = cp.Parameter((p, n)) g = cp.Parameter(p) obj = cp.Maximize(cp.sum(cp.entr(x)) - .01 * cp.sum_squares(x)) constraints = [A * x == b, F * x <= g] prob = cp.Problem(obj, constraints) layer = CvxpyLayer(prob, [A, b, F, g], [x]) A_tch, b_tch, F_tch, g_tch = map( lambda x: torch.from_numpy(x).requires_grad_(True), [ A_np, b_np, F_np, g_np]) torch.autograd.gradcheck( lambda *x: layer(*x, solver_args={"eps": 1e-12, "max_iters": 10000}), (A_tch, b_tch, F_tch, g_tch), eps=1e-4, atol=1e-3, rtol=1e-3)
Example #13
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 6 votes |
def test_shared_parameter(self): set_seed(243) m, n = 10, 5 A = cp.Parameter((m, n)) x = cp.Variable(n) b1 = np.random.randn(m) b2 = np.random.randn(m) prob1 = cp.Problem(cp.Minimize(cp.sum_squares(A @ x - b1))) layer1 = CvxpyLayer(prob1, parameters=[A], variables=[x]) prob2 = cp.Problem(cp.Minimize(cp.sum_squares(A @ x - b2))) layer2 = CvxpyLayer(prob2, parameters=[A], variables=[x]) A_tch = torch.randn(m, n, requires_grad=True) solver_args = { "eps": 1e-10, "acceleration_lookback": 0, "max_iters": 10000 } torch.autograd.gradcheck(lambda A: torch.cat( [layer1(A, solver_args=solver_args)[0], layer2(A, solver_args=solver_args)[0]]), (A_tch,))
Example #14
Source File: l1lsq.py From doatools.py with MIT License | 6 votes |
def __init__(self, m, k, n, complex=False): if not cvx_available: raise RuntimeError('Cannot initialize when cvxpy is not available.') # Initialize parameters and variables A = cvx.Parameter((m, k), complex=complex) B = cvx.Parameter((m, n), complex=complex) l = cvx.Parameter(nonneg=True) X = cvx.Variable((k, n), complex=complex) # Create the problem # CVXPY issue: # cvx.norm does not work if axis is not 0. # Workaround: # use cvx.norm(X.T, 2, axis=0) instead of cvx.norm(X, 2, axis=1) obj_func = 0.5 * cvx.norm(cvx.matmul(A, X) - B, 'fro')**2 + \ l * cvx.sum(cvx.norm(X.T, 2, axis=0)) self._problem = cvx.Problem(cvx.Minimize(obj_func)) self._A = A self._B = B self._l = l self._X = X
Example #15
Source File: linear_model.py From scikit-lego with MIT License | 6 votes |
def fit(self, X, y): """ Fit the model using X, y as training data. :param X: array-like, shape=(n_columns, n_samples, ) training data. :param y: array-like, shape=(n_samples, ) training data. :return: Returns an instance of self. """ X, y = check_X_y(X, y, estimator=self, dtype=FLOAT_DTYPES) # Construct the problem. betas = cp.Variable(X.shape[1]) objective = cp.Minimize(cp.sum_squares(X * betas - y)) constraints = [sum(betas) == 1] if self.non_negative: constraints.append(0 <= betas) # Solve the problem. prob = cp.Problem(objective, constraints) prob.solve() self.coefs_ = betas.value return self
Example #16
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 6 votes |
def test_lml(self): tf.random.set_seed(0) k = 2 x = cp.Parameter(4) y = cp.Variable(4) obj = -x * y - cp.sum(cp.entr(y)) - cp.sum(cp.entr(1. - y)) cons = [cp.sum(y) == k] problem = cp.Problem(cp.Minimize(obj), cons) lml = CvxpyLayer(problem, [x], [y]) x_tf = tf.Variable([1., -1., -1., -1.], dtype=tf.float64) with tf.GradientTape() as tape: y_opt = lml(x_tf, solver_args={'eps': 1e-10})[0] loss = -tf.math.log(y_opt[1]) def f(): problem.solve(solver=cp.SCS, eps=1e-10) return -np.log(y.value[1]) grad = tape.gradient(loss, [x_tf]) numgrad = numerical_grad(f, [x], [x_tf]) np.testing.assert_almost_equal(grad, numgrad, decimal=3)
Example #17
Source File: test_analysis.py From controlpy with GNU General Public License v3.0 | 6 votes |
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))
Example #18
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_not_enough_parameters(self): x = cp.Variable(1) lam = cp.Parameter(1, nonneg=True) lam2 = cp.Parameter(1, nonneg=True) objective = lam * cp.norm(x, 1) + lam2 * cp.sum_squares(x) prob = cp.Problem(cp.Minimize(objective)) with self.assertRaises(ValueError): layer = CvxpyLayer(prob, [lam], [x]) # noqa: F841
Example #19
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_infeasible(self): x = cp.Variable(1) param = cp.Parameter(1) prob = cp.Problem(cp.Minimize(param), [x >= 1, x <= -1]) layer = CvxpyLayer(prob, [param], [x]) param_tf = tf.ones(1) with self.assertRaises(diffcp.SolverError): layer(param_tf)
Example #20
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_non_dpp(self): x = cp.Variable(1) y = cp.Variable(1) lam = cp.Parameter(1) objective = lam * cp.norm(x, 1) prob = cp.Problem(cp.Minimize(objective)) with self.assertRaisesRegex(ValueError, 'Problem must be DPP.'): CvxpyLayer(prob, [lam], [x, y]) # noqa: F841
Example #21
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_not_enough_parameters_at_call_time(self): x = cp.Variable(1) lam = cp.Parameter(1, nonneg=True) lam2 = cp.Parameter(1, nonneg=True) objective = lam * cp.norm(x, 1) + lam2 * cp.sum_squares(x) prob = cp.Problem(cp.Minimize(objective)) layer = CvxpyLayer(prob, [lam, lam2], [x]) with self.assertRaisesRegex( ValueError, 'A tensor must be provided for each CVXPY parameter.*'): layer(lam)
Example #22
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_not_enough_parameters(self): x = cp.Variable(1) lam = cp.Parameter(1, nonneg=True) lam2 = cp.Parameter(1, nonneg=True) objective = lam * cp.norm(x, 1) + lam2 * cp.sum_squares(x) prob = cp.Problem(cp.Minimize(objective)) with self.assertRaisesRegex(ValueError, "The layer's parameters.*"): CvxpyLayer(prob, [lam], [x]) # noqa: F841
Example #23
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_logistic_regression(self): np.random.seed(243) N, n = 10, 2 def sigmoid(z): return 1 / (1 + np.exp(-z)) X_np = np.random.randn(N, n) a_true = np.random.randn(n, 1) y_np = np.round(sigmoid(X_np @ a_true + np.random.randn(N, 1) * 0.5)) X_tf = tf.Variable(X_np) lam_tf = tf.Variable(1.0 * tf.ones(1)) a = cp.Variable((n, 1)) X = cp.Parameter((N, n)) lam = cp.Parameter(1, nonneg=True) y = y_np log_likelihood = cp.sum( cp.multiply(y, X @ a) - cp.log_sum_exp(cp.hstack([np.zeros((N, 1)), X @ a]).T, axis=0, keepdims=True).T ) prob = cp.Problem( cp.Minimize(-log_likelihood + lam * cp.sum_squares(a))) fit_logreg = CvxpyLayer(prob, [X, lam], [a]) with tf.GradientTape(persistent=True) as tape: weights = fit_logreg(X_tf, lam_tf, solver_args={'eps': 1e-8})[0] summed = tf.math.reduce_sum(weights) grad_X_tf, grad_lam_tf = tape.gradient(summed, [X_tf, lam_tf]) def f_train(): prob.solve(solver=cp.SCS, eps=1e-8) return np.sum(a.value) numgrad_X_tf, numgrad_lam_tf = numerical_grad( f_train, [X, lam], [X_tf, lam_tf], delta=1e-6) np.testing.assert_allclose(grad_X_tf, numgrad_X_tf, atol=1e-2) np.testing.assert_allclose(grad_lam_tf, numgrad_lam_tf, atol=1e-2)
Example #24
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_equality(self): set_seed(243) n = 10 A = np.eye(n) x = cp.Variable(n) b = cp.Parameter(n) prob = cp.Problem(cp.Minimize(cp.sum_squares(x)), [A@x == b]) layer = CvxpyLayer(prob, parameters=[b], variables=[x]) b_tch = torch.randn(n, requires_grad=True) torch.autograd.gradcheck(lambda b: layer( b, solver_args={"eps": 1e-10, "acceleration_lookback": 0})[0].sum(), (b_tch,))
Example #25
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_docstring_example(self): np.random.seed(0) tf.random.set_seed(0) n, m = 2, 3 x = cp.Variable(n) A = cp.Parameter((m, n)) b = cp.Parameter(m) constraints = [x >= 0] objective = cp.Minimize(0.5 * cp.pnorm(A @ x - b, p=1)) problem = cp.Problem(objective, constraints) assert problem.is_dpp() cvxpylayer = CvxpyLayer(problem, parameters=[A, b], variables=[x]) A_tf = tf.Variable(tf.random.normal((m, n))) b_tf = tf.Variable(tf.random.normal((m,))) with tf.GradientTape() as tape: # solve the problem, setting the values of A and b to A_tf and b_tf solution, = cvxpylayer(A_tf, b_tf) summed_solution = tf.math.reduce_sum(solution) gradA, gradb = tape.gradient(summed_solution, [A_tf, b_tf]) def f(): problem.solve(solver=cp.SCS, eps=1e-10) return np.sum(x.value) numgradA, numgradb = numerical_grad(f, [A, b], [A_tf, b_tf]) np.testing.assert_almost_equal(gradA, numgradA, decimal=4) np.testing.assert_almost_equal(gradb, numgradb, decimal=4)
Example #26
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_basic_gp(self): set_seed(243) x = cp.Variable(pos=True) y = cp.Variable(pos=True) z = cp.Variable(pos=True) a = cp.Parameter(pos=True, value=2.0) b = cp.Parameter(pos=True, value=1.0) c = cp.Parameter(value=0.5) objective_fn = 1/(x*y*z) constraints = [a*(x*y + x*z + y*z) <= b, x >= y**c] problem = cp.Problem(cp.Minimize(objective_fn), constraints) problem.solve(cp.SCS, gp=True, eps=1e-12) layer = CvxpyLayer( problem, parameters=[a, b, c], variables=[x, y, z], gp=True) a_tch = torch.tensor(2.0, requires_grad=True) b_tch = torch.tensor(1.0, requires_grad=True) c_tch = torch.tensor(0.5, requires_grad=True) with torch.no_grad(): x_tch, y_tch, z_tch = layer(a_tch, b_tch, c_tch) self.assertAlmostEqual(x.value, x_tch.detach().numpy(), places=5) self.assertAlmostEqual(y.value, y_tch.detach().numpy(), places=5) self.assertAlmostEqual(z.value, z_tch.detach().numpy(), places=5) torch.autograd.gradcheck(lambda a, b, c: layer( a, b, c, solver_args={ "eps": 1e-12, "acceleration_lookback": 0})[0].sum(), (a_tch, b_tch, c_tch), atol=1e-3, rtol=1e-3)
Example #27
Source File: qcqp.py From qcqp with MIT License | 5 votes |
def solve_spectral(prob, *args, **kwargs): """Solve the spectral relaxation with lambda = 1. """ # TODO: do this efficiently without SDP lifting # lifted variables and semidefinite constraint X = cvx.Semidef(prob.n + 1) W = prob.f0.homogeneous_form() rel_obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(W, X))) W1 = sum([f.homogeneous_form() for f in prob.fs if f.relop == '<=']) W2 = sum([f.homogeneous_form() for f in prob.fs if f.relop == '==']) rel_prob = cvx.Problem( rel_obj, [ cvx.sum_entries(cvx.mul_elemwise(W1, X)) <= 0, cvx.sum_entries(cvx.mul_elemwise(W2, X)) == 0, X[-1, -1] == 1 ] ) rel_prob.solve(*args, **kwargs) if rel_prob.status not in [cvx.OPTIMAL, cvx.OPTIMAL_INACCURATE]: raise Exception("Relaxation problem status: %s" % rel_prob.status) (w, v) = LA.eig(X.value) return np.sqrt(np.max(w))*np.asarray(v[:-1, np.argmax(w)]).flatten(), rel_prob.value
Example #28
Source File: cvxpy_examples.py From cvxpylayers with Apache License 2.0 | 5 votes |
def sdp(): print('sdp') npr.seed(0) d = 2 X = cp.Variable((d, d), PSD=True) Y = cp.Parameter((d, d)) obj = cp.Minimize(cp.trace(Y * X)) prob = cp.Problem(obj, [X >= 1]) Y.value = np.abs(npr.randn(d, d)) print(Y.value.sum()) prob.solve(solver=cp.SCS, verbose=True) print(X.value)
Example #29
Source File: cvxpy_examples.py From cvxpylayers with Apache License 2.0 | 5 votes |
def sigmoid(): # print(f'--- {sys._getframe().f_code.co_name} ---') print('sigmoid') npr.seed(0) n = 4 _x = cp.Parameter((n, 1)) _y = cp.Variable(n) obj = cp.Minimize(-_x.T * _y - cp.sum(cp.entr(_y) + cp.entr(1. - _y))) prob = cp.Problem(obj) _x.value = npr.randn(n, 1) prob.solve(solver=cp.SCS) print(_y.value)
Example #30
Source File: test_cvxpylayer.py From cvxpylayers with Apache License 2.0 | 5 votes |
def test_sdp(self): tf.random.set_seed(5) n = 3 p = 3 C = cp.Parameter((n, n)) A = [cp.Parameter((n, n)) for _ in range(p)] b = [cp.Parameter((1, 1)) for _ in range(p)] C_tf = tf.Variable(tf.random.normal((n, n), dtype=tf.float64)) A_tf = [tf.Variable(tf.random.normal((n, n), dtype=tf.float64)) for _ in range(p)] b_tf = [tf.Variable(tf.random.normal((1, 1), dtype=tf.float64)) for _ in range(p)] X = cp.Variable((n, n), symmetric=True) constraints = [X >> 0] constraints += [ cp.trace(A[i]@X) == b[i] for i in range(p) ] problem = cp.Problem(cp.Minimize( cp.trace(C @ X) - cp.log_det(X) + cp.sum_squares(X)), constraints) layer = CvxpyLayer(problem, [C] + A + b, [X]) values = [C_tf] + A_tf + b_tf with tf.GradientTape() as tape: soln = layer(*values, solver_args={'eps': 1e-10, 'max_iters': 10000})[0] summed = tf.math.reduce_sum(soln) grads = tape.gradient(summed, values) def f(): problem.solve(cp.SCS, eps=1e-10, max_iters=10000) return np.sum(X.value) numgrads = numerical_grad(f, [C] + A + b, values, delta=1e-4) for g, ng in zip(grads, numgrads): np.testing.assert_allclose(g, ng, atol=1e-1)