from cvxpy import *
# Create two scalar optimization variables.
x = Variable()
y = Variable()
# Create two constraints.
constraints = [x + y == 1,
x - y >= 1]
# Form objective.
obj = Minimize(square(x - y))
# Form and solve problem.
prob = Problem(obj, constraints)
prob.solve()
# The optimal dual variable (Lagrange multiplier) for
# a constraint is stored in constraint.dual_value.
print "optimal (x + y == 1) dual variable", constraints[0].dual_value
print "optimal (x - y >= 1) dual variable", constraints[1].dual_value
print "x - y value:", (x - y).value
optimal (x + y == 1) dual variable 6.47610300459e-18 optimal (x - y >= 1) dual variable 2.00025244976 x - y value: 0.999999986374
# Solving a problem with different solvers.
x = Variable(2)
obj = Minimize(norm(x, 2) + norm(x, 1))
constraints = [x >= 2]
prob = Problem(obj, constraints)
# Solve with ECOS.
prob.solve(solver=ECOS)
print "optimal value with ECOS:", prob.value
# Solve with CVXOPT.
prob.solve(solver=CVXOPT)
print "optimal value with CVXOPT:", prob.value
# Solve with SCS.
prob.solve(solver=SCS)
print "optimal value with SCS:", prob.value
optimal value with ECOS: 6.82842708233 optimal value with CVXOPT: 6.82842708994 optimal value with SCS: 6.82837896978
# Solve with ECOS.
prob.solve(solver=ECOS, verbose=True)
print "optimal value with ECOS:", prob.value
ECOS 1.0.3 - (c) A. Domahidi, Automatic Control Laboratory, ETH Zurich, 2012-2014. It pcost dcost gap pres dres k/t mu step IR 0 +0.000e+00 +4.000e+00 +2e+01 2e+00 1e+00 1e+00 3e+00 N/A 1 1 - 1 +6.451e+00 +8.125e+00 +5e+00 7e-01 5e-01 7e-01 7e-01 0.7857 1 1 1 2 +6.788e+00 +6.839e+00 +9e-02 1e-02 8e-03 3e-02 2e-02 0.9829 1 1 1 3 +6.828e+00 +6.829e+00 +1e-03 1e-04 8e-05 3e-04 2e-04 0.9899 1 1 1 4 +6.828e+00 +6.828e+00 +1e-05 1e-06 8e-07 3e-06 2e-06 0.9899 2 1 1 5 +6.828e+00 +6.828e+00 +1e-07 1e-08 8e-09 4e-08 2e-08 0.9899 2 1 1 OPTIMAL (within feastol=1.3e-08, reltol=1.5e-08, abstol=1.0e-07). Runtime: 0.000121 seconds. optimal value with ECOS: 6.82842708233
# Solve with SCS and display output.
opts = {"USE_INDIRECT": False}
prob.solve(solver=SCS, verbose=True, solver_specific_opts=opts)
print "optimal value with SCS:", prob.value
---------------------------------------------------------------------------- scs v1.0 - Splitting Conic Solver (c) Brendan O'Donoghue, Stanford University, 2012 ---------------------------------------------------------------------------- Method: sparse-direct, nnz in A = 13 EPS = 1.00e-03, ALPHA = 1.80, MAX_ITERS = 2500, NORMALIZE = 1, SCALE = 5.0 Variables n = 5, constraints m = 9 Cones: primal zero / dual free vars: 0 linear vars: 6 soc vars: 3, soc blks: 1 sd vars: 0, sd blks: 0 exp vars: 0, dual exp vars: 0 ---------------------------------------------------------------------------- Iter | pri res | dua res | rel gap | pri obj | dua obj | kappa | time (s) ============================================================================ 0| 4.60e+00 5.78e-01 nan -inf inf 8.32e+00 1.54e-03 60| 3.92e-05 1.12e-04 6.64e-06 6.83e+00 6.83e+00 9.31e-18 1.62e-03 ---------------------------------------------------------------------------- Status: Solved Timing: Solve time: 1.63e-03s, setup time: 1.70e-04s Lin-sys: nnz in L factor: 29, avg solve time: 1.38e-07s Cones: avg projection time: 5.05e-08s ---------------------------------------------------------------------------- Error metrics: |Ax + s - b|_2 / (1 + |b|_2) = 3.9223e-05 |A'y + c|_2 / (1 + |c|_2) = 1.1168e-04 |c'x + b'y| / (1 + |c'x| + |b'y|) = 6.6446e-06 dist(s, K) = 0, dist(y, K*) = 0, s'y = 0 ---------------------------------------------------------------------------- c'x = 6.8284, -b'y = 6.8285 ============================================================================ optimal value with SCS: 6.82837896975
# Get ECOS arguments.
c, G, h, dims, A, b = prob.get_problem_data(ECOS)
# Get CVXOPT arguments.
c, G, h, dims, A, b = prob.get_problem_data(CVXOPT)
# Get CVXOPT arguments.
data, dims = prob.get_problem_data(SCS)