In [1]:
using Convex
using SCS

# passing in verbose=0 to hide output from SCS
solver = SCSSolver(verbose=0)
set_default_solver(solver);

Linear program

$$ \begin{array}{ll} \mbox{maximize} & c^T x \\ \mbox{subject to} & A x <= b\\ & x \geq 1 \\ & x \leq 10 \\ & x_2 \leq 5 \\ & x_1 + x_4 - x_2 \leq 10 \\ \end{array} $$

In [2]:
x = Variable(4)
c = [1; 2; 3; 4]
A = eye(4)
b = [10; 10; 10; 10]
p = minimize(dot(c, x)) # or c' * x
p.constraints += A * x <= b
p.constraints += [x >= 1; x <= 10; x[2] <= 5; x[1] + x[4] - x[2] <= 10]
solve!(p)

println(round(p.optval, 2))
println(round(x.value, 2))
println(evaluate(x[1] + x[4] - x[2]))


10.0
[1.0
 1.0
 1.0
 1.0]
[0.9999794207077799]

Matrix Variables and promotions

$$ \begin{array}{ll} \mbox{minimize} & \| X \|_F + y \\ \mbox{subject to} & 2 X <= 1\\ & X' + y >= 1 \\ & X >= 0 \\ & y >= 0 \\ \end{array} $$

In [3]:
X = Variable(2, 2)
y = Variable()
# X is a 2 x 2 variable, and y is scalar. X' + y promotes y to a 2 x 2 variable before adding them
p = minimize(sum(X) + y, 2 * X <= 1, X' + y >= 1, X >= 0, y >= 0)
solve!(p)
println(round(X.value, 2))
println(y.value)
p.optval


[0.0 0.0
 0.0 0.0]
1.0000000807021356
Out[3]:
1.000001111507024

Norm, exponential and geometric mean

$$ \begin{array}{ll} \mbox{satisfy} & \| x \|_2 \leq 100 \\ & e^{x_1} \leq 5 \\ & x_2 \geq 7 \\ & \sqrt{x_3 x_4} \geq x_2 \end{array} $$

In [4]:
x = Variable(4)
p = satisfy(norm(x) <= 100, exp(x[1]) <= 5, x[2] >= 7, geo_mean(x[3], x[4]) >= x[2])
solve!(p, SCSSolver(verbose=0))
println(p.status)
x.value


Optimal
Out[4]:
4x1 Array{Float64,2}:
  0.0    
  8.65837
 14.6655 
 14.6655 

SDP cone and Eigenvalues


In [5]:
y = Semidefinite(2)
p = maximize(lambda_min(y), trace(y)<=6)
solve!(p, SCSSolver(verbose=0))
p.optval


Out[5]:
3.0000029948079856

In [6]:
x = Variable()
y = Variable((2, 2))
# SDP constraints
p = minimize(x + y[1, 1], isposdef(y), x >= 1, y[2, 1] == 1)
solve!(p)
y.value


Out[6]:
2x2 Array{Float64,2}:
 0.00352332    1.00018
 1.00018     286.415  

Mixed integer program

$$ \begin{array}{ll} \mbox{minimize} & sum(x) \\ \mbox{subject to} & x \in \mathbb{Z} \\ & x >= 0.5 \\ \end{array} $$

In [7]:
using GLPKMathProgInterface
x = Variable(4, :Int)
p = minimize(sum(x), x >= 0.5)
solve!(p, GLPKSolverMIP())
x.value


Out[7]:
4x1 Array{Float64,2}:
 1.0
 1.0
 1.0
 1.0