Basic Usage

First we load Convex itself, LinearAlgebra to access the identity matrix I, and two solvers: SCS and GLPK.

using Convex
using LinearAlgebra
using SCS, GLPK

Linear program

\[\begin{array}{ll} \text{maximize} & c^T x \\ \text{subject to} & A x \leq b\\ & x \geq 1 \\ & x \leq 10 \\ & x_2 \leq 5 \\ & x_1 + x_4 - x_2 \leq 10 \\ \end{array}\]

x = Variable(4)
c = [1; 2; 3; 4]
A = I(4)
b = [10; 10; 10; 10]
constraints = [A * x <= b, x >= 1, x <= 10, x[2] <= 5, x[1] + x[4] - x[2] <= 10]
p = minimize(dot(c, x), constraints) # or c' * x
solve!(p, SCS.Optimizer; silent = true)
Problem statistics
  problem is DCP         : true
  number of variables    : 1 (4 scalar elements)
  number of constraints  : 5 (14 scalar elements)
  number of coefficients : 36
  number of atoms        : 17

Solution summary
  termination status : OPTIMAL
  primal status      : FEASIBLE_POINT
  dual status        : FEASIBLE_POINT
  objective value    : 9.9998

Expression graph
  minimize
   └─ sum (affine; real)
      └─ .* (affine; real)
         ├─ 4×1 Matrix{Int64}
         └─ 4-element real variable (id: 152…922)
  subject to
   ├─ ≤ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ * (affine; real)
   │     │  ├─ …
   │     │  └─ …
   │     └─ 4×1 Matrix{Int64}
   ├─ ≥ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ 4-element real variable (id: 152…922)
   │     └─ Convex.NegateAtom (constant; negative)
   │        └─ …
   ├─ ≤ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ 4-element real variable (id: 152…922)
   │     └─ Convex.NegateAtom (constant; negative)
   │        └─ …
   ⋮

We can also inspect the objective value and the values of the variables at the solution:

println(round(p.optval, digits = 2))
println(round.(evaluate(x), digits = 2))
println(evaluate(x[1] + x[4] - x[2]))
10.0
[1.0, 1.0, 1.0, 1.0]
0.9992362732533817

Matrix Variables and promotions

\[\begin{array}{ll} \text{minimize} & \| X \|_F + y \\ \text{subject to} & 2 X \leq 1\\ & X' + y \geq 1 \\ & X \geq 0 \\ & y \geq 0 \\ \end{array}\]

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(norm(X) + y, 2 * X <= 1, X' + y >= 1, X >= 0, y >= 0)
solve!(p, SCS.Optimizer; silent = true)
Problem statistics
  problem is DCP         : true
  number of variables    : 2 (5 scalar elements)
  number of constraints  : 4 (13 scalar elements)
  number of coefficients : 25
  number of atoms        : 18

Solution summary
  termination status : OPTIMAL
  primal status      : FEASIBLE_POINT
  dual status        : FEASIBLE_POINT
  objective value    : 1.0002

Expression graph
  minimize
   └─ + (convex; real)
      ├─ norm2 (convex; positive)
      │  └─ reshape (affine; real)
      │     └─ …
      └─ real variable (id: 144…397)
  subject to
   ├─ ≤ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ * (affine; real)
   │     │  ├─ …
   │     │  └─ …
   │     └─ Convex.NegateAtom (constant; negative)
   │        └─ …
   ├─ ≥ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ reshape (affine; real)
   │     │  └─ …
   │     ├─ * (affine; real)
   │     │  ├─ …
   │     │  └─ …
   │     └─ Convex.NegateAtom (constant; negative)
   │        └─ …
   ├─ ≥ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ 2×2 real variable (id: 121…750)
   │     └─ Convex.NegateAtom (constant; negative)
   │        └─ …
   ⋮

We can also inspect the values of the variables at the solution:

println(round.(evaluate(X), digits = 2))
println(evaluate(y))
p.optval
1.0002185951700004

Norm, exponential and geometric mean

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

x = Variable(4)
p = satisfy(
    norm(x) <= 100,
    exp(x[1]) <= 5,
    x[2] >= 7,
    geomean(x[3], x[4]) >= x[2],
)
solve!(p, SCS.Optimizer; silent = true)
Problem statistics
  problem is DCP         : true
  number of variables    : 1 (4 scalar elements)
  number of constraints  : 4 (4 scalar elements)
  number of coefficients : 3
  number of atoms        : 13

Solution summary
  termination status : OPTIMAL
  primal status      : FEASIBLE_POINT
  dual status        : FEASIBLE_POINT

Expression graph
  satisfy
   └─ nothing
  subject to
   ├─ ≤ constraint (convex)
   │  └─ + (convex; real)
   │     ├─ norm2 (convex; positive)
   │     │  └─ …
   │     └─ [-100;;]
   ├─ ≤ constraint (convex)
   │  └─ + (convex; real)
   │     ├─ exp (convex; positive)
   │     │  └─ …
   │     └─ [-5;;]
   ├─ ≥ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ index (affine; real)
   │     │  └─ …
   │     └─ [-7;;]
   ⋮

PSD cone and Eigenvalues

y = Semidefinite(2)
p = maximize(eigmin(y), tr(y) <= 6)
solve!(p, SCS.Optimizer; silent = true)
Problem statistics
  problem is DCP         : true
  number of variables    : 1 (4 scalar elements)
  number of constraints  : 2 (5 scalar elements)
  number of coefficients : 1
  number of atoms        : 4

Solution summary
  termination status : OPTIMAL
  primal status      : FEASIBLE_POINT
  dual status        : FEASIBLE_POINT
  objective value    : 3.0

Expression graph
  maximize
   └─ eigmin (concave; real)
      └─ 2×2 real variable (id: 301…916)
  subject to
   ├─ ≤ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ sum (affine; real)
   │     │  └─ …
   │     └─ [-6;;]
   ├─ PSD constraint (convex)
   │  └─ 2×2 real variable (id: 301…916)
   └─ PSD constraint (convex)
      └─ 2×2 real variable (id: 301…916)
x = Variable()
y = Variable((2, 2))

# PSD constraints
p = minimize(x + y[1, 1], y ⪰ 0, x >= 1, y[2, 1] == 1)
solve!(p, SCS.Optimizer; silent = true)
Problem statistics
  problem is DCP         : true
  number of variables    : 2 (5 scalar elements)
  number of constraints  : 3 (6 scalar elements)
  number of coefficients : 2
  number of atoms        : 5

Solution summary
  termination status : OPTIMAL
  primal status      : FEASIBLE_POINT
  dual status        : FEASIBLE_POINT
  objective value    : 1.0009

Expression graph
  minimize
   └─ + (affine; real)
      ├─ real variable (id: 166…121)
      └─ index (affine; real)
         └─ 2×2 real variable (id: 965…523)
  subject to
   ├─ PSD constraint (convex)
   │  └─ 2×2 real variable (id: 965…523)
   ├─ ≥ constraint (affine)
   │  └─ + (affine; real)
   │     ├─ real variable (id: 166…121)
   │     └─ [-1;;]
   └─ == constraint (affine)
      └─ + (affine; real)
         ├─ index (affine; real)
         │  └─ …
         └─ [-1;;]

Mixed integer program

\[\begin{array}{ll} \text{minimize} & \sum_{i=1}^n x_i \\ \text{subject to} & x \in \mathbb{Z}^n \\ & x \geq 0.5 \\ \end{array}\]

x = Variable(4, IntVar)
p = minimize(sum(x), x >= 0.5)
solve!(p, GLPK.Optimizer; silent = true)
Problem statistics
  problem is DCP         : true
  number of variables    : 1 (4 scalar elements)
  number of constraints  : 1 (4 scalar elements)
  number of coefficients : 5
  number of atoms        : 4

Solution summary
  termination status : OPTIMAL
  primal status      : FEASIBLE_POINT
  dual status        : NO_SOLUTION
  objective value    : 4.0

Expression graph
  minimize
   └─ sum (affine; real)
      └─ 4-element real variable (id: 177…231)
  subject to
   └─ ≥ constraint (affine)
      └─ + (affine; real)
         ├─ 4-element real variable (id: 177…231)
         └─ Convex.NegateAtom (constant; negative)
            └─ …

And the value of x at the solution:

evaluate(x)
4-element Vector{Float64}:
 1.0
 1.0
 1.0
 1.0

This page was generated using Literate.jl.