本文整理汇总了Python中cvxpy.Problem.solve方法的典型用法代码示例。如果您正苦于以下问题:Python Problem.solve方法的具体用法?Python Problem.solve怎么用?Python Problem.solve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cvxpy.Problem
的用法示例。
在下文中一共展示了Problem.solve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_matrix_frac
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_matrix_frac(self):
"""Test domain for matrix_frac.
"""
dom = matrix_frac(self.x, self.A + np.eye(2)).domain
prob = Problem(Minimize(sum_entries(diag(self.A))), dom)
prob.solve(solver=cvxpy.SCS)
self.assertAlmostEquals(prob.value, -2, places=3)
示例2: test_partial_optimize_numeric_fn
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_partial_optimize_numeric_fn(self):
x, y = Variable(1), Variable(1)
xval = 4
# Solve the (simple) two-stage problem by "combining" the two stages (i.e., by solving a single linear program)
p1 = Problem(Minimize(y), [xval + y >= 3])
p1.solve()
# Solve the two-stage problem via partial_optimize
constr = [y >= -100]
p2 = Problem(Minimize(y), [x + y >= 3] + constr)
g = cvxpy.partial_optimize(p2, [y], [x])
x.value = xval
y.value = 42
constr[0].dual_variable.value = 42
result = g.value
self.assertAlmostEqual(result, p1.value)
self.assertAlmostEqual(y.value, 42)
self.assertAlmostEqual(constr[0].dual_value, 42)
# No variables optimized over.
p2 = Problem(Minimize(y), [x + y >= 3])
g = cvxpy.partial_optimize(p2, [], [x, y])
x.value = xval
y.value = 42
p2.constraints[0].dual_variable.value = 42
result = g.value
self.assertAlmostEqual(result, y.value)
self.assertAlmostEqual(y.value, 42)
self.assertAlmostEqual(p2.constraints[0].dual_value, 42)
示例3: test_pnorm
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_pnorm(self):
""" Test domain for pnorm.
"""
dom = pnorm(self.a, -0.5).domain
prob = Problem(Minimize(self.a), dom)
prob.solve()
self.assertAlmostEqual(prob.value, 0)
示例4: test_value_at_risk
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_value_at_risk(self):
# Create problem data.
n = numpy.random.randint(1,10)
pbar = numpy.random.randn(n)
Sigma = numpy.eye(n)
p = NormalRandomVariable(pbar,Sigma)
o = numpy.ones((n,1))
beta = 0.05
num_samples = 50
# Create and solve optimization problem.
x = Variable(n)
p1 = Problem(Minimize(-x.T*pbar), [prob(-x.T*p >= 0, num_samples) <= beta, x.T*o == 1, x >= -0.1])
p1.solve()
# Create and solve analytic form of optimization problem (as a check).
p2 = Problem(Minimize(-x.T*pbar),
[x.T*pbar >= scipy.stats.norm.ppf(1-beta) * norm2(sqrtm(Sigma) * x), x.T*o == 1, x >= -0.1])
p2.solve()
tol = 0.1
if numpy.abs(p1.value - p2.value) < tol:
self.assertAlmostEqual(1,1)
else:
self.assertAlmostEqual(1,0)
示例5: l1_solution
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def l1_solution(A, b, lam=0.5):
N = A.shape[0]
x = Variable(N)
objective = Minimize(sum_entries(square(A * x - b)) + lam * norm(x, 1))
constraints = []
prob = Problem(objective, constraints)
prob.solve()
xhat = x.value
return xhat
示例6: cvxpy_solve_qp
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def cvxpy_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None,
solver=None):
"""
Solve a Quadratic Program defined as:
minimize
(1/2) * x.T * P * x + q.T * x
subject to
G * x <= h
A * x == b
calling a given solver using the CVXPY <http://www.cvxpy.org/> modelling
language.
Parameters
----------
P : array, shape=(n, n)
Primal quadratic cost matrix.
q : array, shape=(n,)
Primal quadratic cost vector.
G : array, shape=(m, n)
Linear inequality constraint matrix.
h : array, shape=(m,)
Linear inequality constraint vector.
A : array, shape=(meq, n), optional
Linear equality constraint matrix.
b : array, shape=(meq,), optional
Linear equality constraint vector.
initvals : array, shape=(n,), optional
Warm-start guess vector (not used).
solver : string, optional
Solver name in ``cvxpy.installed_solvers()``.
Returns
-------
x : array, shape=(n,)
Solution to the QP, if found, otherwise ``None``.
"""
if initvals is not None:
print("CVXPY: note that warm-start values are ignored by wrapper")
n = q.shape[0]
x = Variable(n)
P = Constant(P) # see http://www.cvxpy.org/en/latest/faq/
objective = Minimize(0.5 * quad_form(x, P) + q * x)
constraints = []
if G is not None:
constraints.append(G * x <= h)
if A is not None:
constraints.append(A * x == b)
prob = Problem(objective, constraints)
prob.solve(solver=solver)
x_opt = array(x.value).reshape((n,))
return x_opt
示例7: test_partial_optimize_min_1norm
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_partial_optimize_min_1norm(self):
# Minimize the 1-norm in the usual way
dims = 3
x, t = Variable(dims), Variable(dims)
p1 = Problem(Minimize(sum_entries(t)), [-t<=x, x<=t])
# Minimize the 1-norm via partial_optimize
g = partial_optimize(p1, [t], [x])
p2 = Problem(Minimize(g))
p2.solve()
p1.solve()
self.assertAlmostEqual(p1.value, p2.value)
示例8: test_partial_optimize_numeric_fn
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_partial_optimize_numeric_fn(self):
x,y = Variable(1), Variable(1)
xval = 4
# Solve the (simple) two-stage problem by "combining" the two stages (i.e., by solving a single linear program)
p1 = Problem(Minimize(y), [xval+y>=3])
p1.solve()
# Solve the two-stage problem via partial_optimize
p2 = Problem(Minimize(y), [x+y>=3])
g = partial_optimize(p2, [y], [x])
result = g(x).numeric([xval])
self.assertAlmostEqual(result, p1.value)
示例9: test_partial_optimize_simple_problem
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_partial_optimize_simple_problem(self):
x, y = Variable(1), Variable(1)
# Solve the (simple) two-stage problem by "combining" the two stages (i.e., by solving a single linear program)
p1 = Problem(Minimize(x+y), [x+y>=3, y>=4, x>=5])
p1.solve()
# Solve the two-stage problem via partial_optimize
p2 = Problem(Minimize(y), [x+y>=3, y>=4])
g = partial_optimize(p2, [y], [x])
p3 = Problem(Minimize(x+g), [x>=5])
p3.solve()
self.assertAlmostEqual(p1.value, p3.value)
示例10: test_partial_optimize_eval_1norm
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_partial_optimize_eval_1norm(self):
# Evaluate the 1-norm in the usual way (i.e., in epigraph form).
dims = 3
x, t = Variable(dims), Variable(dims)
xval = [-5]*dims
p1 = Problem(Minimize(sum_entries(t)), [-t<=xval, xval<=t])
p1.solve()
# Minimize the 1-norm via partial_optimize.
p2 = Problem(Minimize(sum_entries(t)), [-t<=x, x<=t])
g = partial_optimize(p2, [t], [x])
p3 = Problem(Minimize(g(xval)), [])
p3.solve()
self.assertAlmostEqual(p1.value, p3.value)
示例11: test_robust_svm
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_robust_svm(self):
# Create problem data.
m = 100 # num train points
m_pos = math.floor(m/2)
m_neg = m - m_pos
n = 2 # num dimensions
mu_pos = 2*numpy.ones(n)
mu_neg = -2*numpy.ones(n)
sigma = 1
X = numpy.matrix(numpy.vstack((mu_pos + sigma*numpy.random.randn(m_pos,n),
mu_neg + sigma*numpy.random.randn(m_neg,n))))
y = numpy.hstack((numpy.ones(m_pos), -1*numpy.ones(m_neg)))
C = 1 # regularization trade-off parameter
ns = 50
eta = 0.1
# Create and solve optimization problem.
w, b, xi = Variable(n), Variable(), NonNegative(m)
constr = []
Sigma = 0.1*numpy.eye(n)
for i in range(m):
mu = numpy.array(X[i])[0]
x = NormalRandomVariable(mu, Sigma)
chance = prob(-y[i]*(w.T*x+b) >= (xi[i]-1), ns)
constr += [chance <= eta]
p = Problem(Minimize(norm(w,2) + C*sum_entries(xi)),
constr)
p.solve(verbose=True)
w_new = w.value
b_new = b.value
# Create and solve the canonical SVM problem.
constr = []
for i in range(m):
constr += [y[i]*(X[i]*w+b) >= (1-xi[i])]
p2 = Problem(Minimize(norm(w,2) + C*sum_entries(xi)), constr)
p2.solve()
w_old = w.value
b_old = b.value
self.assert_feas(p)
示例12: test_partial_optimize_eval_1norm
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_partial_optimize_eval_1norm(self):
# Evaluate the 1-norm in the usual way (i.e., in epigraph form).
dims = 3
x, t = Variable(dims), Variable(dims)
xval = [-5]*dims
p1 = Problem(cvxpy.Minimize(sum_entries(t)), [-t <= xval, xval <= t])
p1.solve()
# Minimize the 1-norm via partial_optimize.
p2 = Problem(cvxpy.Minimize(sum_entries(t)), [-t <= x, x <= t])
g = cvxpy.partial_optimize(p2, [t], [x])
p3 = Problem(cvxpy.Minimize(g), [x == xval])
p3.solve()
self.assertAlmostEqual(p1.value, p3.value)
# Minimize the 1-norm using maximize.
p2 = Problem(cvxpy.Maximize(sum_entries(-t)), [-t <= x, x <= t])
g = cvxpy.partial_optimize(p2, opt_vars=[t])
p3 = Problem(cvxpy.Maximize(g), [x == xval])
p3.solve()
self.assertAlmostEqual(p1.value, -p3.value)
# Try leaving out args.
# Minimize the 1-norm via partial_optimize.
p2 = Problem(cvxpy.Minimize(sum_entries(t)), [-t <= x, x <= t])
g = cvxpy.partial_optimize(p2, opt_vars=[t])
p3 = Problem(cvxpy.Minimize(g), [x == xval])
p3.solve()
self.assertAlmostEqual(p1.value, p3.value)
# Minimize the 1-norm via partial_optimize.
g = cvxpy.partial_optimize(p2, dont_opt_vars=[x])
p3 = Problem(cvxpy.Minimize(g), [x == xval])
p3.solve()
self.assertAlmostEqual(p1.value, p3.value)
with self.assertRaises(Exception) as cm:
g = cvxpy.partial_optimize(p2)
self.assertEqual(str(cm.exception),
"partial_optimize called with neither opt_vars nor dont_opt_vars.")
with self.assertRaises(Exception) as cm:
g = cvxpy.partial_optimize(p2, [], [x])
self.assertEqual(str(cm.exception),
("If opt_vars and new_opt_vars are both specified, "
"they must contain all variables in the problem.")
)
示例13: test_partial_optimize_params
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_partial_optimize_params(self):
"""Test partial optimize with parameters.
"""
x, y = Variable(1), Variable(1)
gamma = Parameter()
# Solve the (simple) two-stage problem by "combining" the two stages (i.e., by solving a single linear program)
p1 = Problem(Minimize(x+y), [x+y>=gamma, y>=4, x>=5])
gamma.value = 3
p1.solve()
# Solve the two-stage problem via partial_optimize
p2 = Problem(Minimize(y), [x+y>=gamma, y>=4])
g = partial_optimize(p2, [y], [x])
p3 = Problem(Minimize(x+g), [x>=5])
p3.solve()
self.assertAlmostEqual(p1.value, p3.value)
示例14: GetMinimalSpeedToReachEpsilonNeighbordhoodVector
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def GetMinimalSpeedToReachEpsilonNeighbordhoodVector(dt, epsilon, W, dW, dF):
Ndim = W.shape[0]
Nsamples = W.shape[1]
dist_w = np.zeros((Nsamples-1))
for i in range(0,Nsamples-1):
dist_w[i] = np.linalg.norm(W[:,i]-W[:,i+1])
p = Variable(Nsamples-1)
sM = Variable(Nsamples-1)
constraints = []
objfunc = 0.0
for i in range(0,Nsamples-1):
#constraints.append( norm(p*dt*dW0[0:2] + dF +np.dot(dw,np.array((1,0))) ) < epsilon )
constraints.append( norm(p[i]*dt*dW[:,i] + dt*dt/2*dF[:,i] + np.dot(dist_w[i],np.array((1,0,0,0))) ) < epsilon )
constraints.append( sM[i] >= p[i] )
constraints.append( sM[i] >= 0.0)
constraints.append( p[i] >= 0.0 )
objfunc += norm(sM[i])
objective = Minimize(objfunc)
prob = Problem(objective, constraints)
print "solve minimal speed"
result = prob.solve(solver=SCS)
print "done.(",prob.value,"|",np.min(sM.value),")"
if prob.value < inf:
return np.array(sM.value).flatten()
else:
return np.array(sM.value).flatten()
示例15: test_yield_constr_cost_min
# 需要导入模块: from cvxpy import Problem [as 别名]
# 或者: from cvxpy.Problem import solve [as 别名]
def test_yield_constr_cost_min(self):
# Create problem data.
n = 10
c = numpy.random.randn(n)
P, q, r = numpy.eye(n), numpy.random.randn(n), numpy.random.randn()
mu, Sigma = numpy.zeros(n), 0.1*numpy.eye(n)
omega = NormalRandomVariable(mu, Sigma)
m, eta = 100, 0.95
# Create and solve optimization problem.
x = Variable(n)
yield_constr = prob(quad_form(x+omega,P)
+ (x+omega).T*q + r >= 0, m) <= 1-eta
p = Problem(Minimize(x.T*c), [yield_constr])
p.solve()
self.assert_feas(p)