Solvers
Convex.jl depends on third-party solvers to solve optimization problems. Therefore, you will need to install one before you can solve problems with Convex.jl.
Install a solver using the Julia package manager, replacing "SCS"
by Julia package name as appropriate:
import Pkg
Pkg.add("SCS")
The JuMP documentation has a list of support solvers and a list of problem classes they support.
To use a specific solver, you can use the following syntax:
julia> using Convex, SCS
julia> x = Variable();
julia> p = minimize(x, [x >= 1]);
julia> solve!(p, SCS.Optimizer)
[ Info: [Convex.jl] Compilation finished: 0.0 seconds, 53.344 KiB of memory allocated ------------------------------------------------------------------ SCS v3.2.3 - Splitting Conic Solver (c) Brendan O'Donoghue, Stanford University, 2012 ------------------------------------------------------------------ problem: variables n: 1, constraints m: 1 cones: l: linear vars: 1 settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07 alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1 max_iters: 100000, normalize: 1, rho_x: 1.00e-06 acceleration_lookback: 10, acceleration_interval: 10 lin-sys: sparse-direct-amd-qdldl nnz(A): 1, nnz(P): 0 ------------------------------------------------------------------ iter | pri res | dua res | gap | obj | scale | time (s) ------------------------------------------------------------------ 0| 1.90e+01 1.00e+00 2.00e+01 -8.00e+00 1.00e-01 4.21e-05 25| 9.46e-06 2.35e-08 9.49e-06 1.00e+00 1.00e-01 5.37e-05 ------------------------------------------------------------------ status: solved timings: total: 5.44e-05s = setup: 3.42e-05s + solve: 2.02e-05s lin-sys: 2.52e-06s, cones: 1.47e-06s, accel: 1.04e-06s ------------------------------------------------------------------ objective = 1.000005 ------------------------------------------------------------------ Problem statistics problem is DCP : true number of variables : 1 (1 scalar elements) number of constraints : 1 (1 scalar elements) number of coefficients : 1 number of atoms : 1 Solution summary termination status : OPTIMAL primal status : FEASIBLE_POINT dual status : FEASIBLE_POINT objective value : 1.0 Expression graph minimize └─ real variable (id: 881…673) subject to └─ ≥ constraint (affine) └─ + (affine; real) ├─ real variable (id: 881…673) └─ [-1;;]
A different solver can be used by replacing SCS
as appropriate. For example, GLPK is a mixed-inter linear solver:
julia> using GLPK
julia> solve!(p, GLPK.Optimizer)
[ Info: [Convex.jl] Compilation finished: 0.0 seconds, 104.516 KiB of memory allocated Problem statistics problem is DCP : true number of variables : 1 (1 scalar elements) number of constraints : 1 (1 scalar elements) number of coefficients : 1 number of atoms : 1 Solution summary termination status : OPTIMAL primal status : FEASIBLE_POINT dual status : FEASIBLE_POINT objective value : 1.0 Expression graph minimize └─ real variable (id: 881…673) subject to └─ ≥ constraint (affine) └─ + (affine; real) ├─ real variable (id: 881…673) └─ [-1;;]
Many of the solvers also allow options to be passed using MOI.OptimizerWithAttributes
. For example, to set a time limit (in milliseconds) with GLPK, use:
julia> import Convex: MOI
julia> solver = MOI.OptimizerWithAttributes(GLPK.Optimizer, "tm_lim" => 60_000.0)
MathOptInterface.OptimizerWithAttributes(GLPK.Optimizer, Pair{MathOptInterface.AbstractOptimizerAttribute, Any}[MathOptInterface.RawOptimizerAttribute("tm_lim") => 60000.0])
julia> solve!(p, solver)
[ Info: [Convex.jl] Compilation finished: 0.05 seconds, 1.098 MiB of memory allocated Problem statistics problem is DCP : true number of variables : 1 (1 scalar elements) number of constraints : 1 (1 scalar elements) number of coefficients : 1 number of atoms : 1 Solution summary termination status : OPTIMAL primal status : FEASIBLE_POINT dual status : FEASIBLE_POINT objective value : 1.0 Expression graph minimize └─ real variable (id: 881…673) subject to └─ ≥ constraint (affine) └─ + (affine; real) ├─ real variable (id: 881…673) └─ [-1;;]
As another example, if we wish to turn off printing for the SCS solver (that is, run in quiet mode), we can do so as follows:
julia> silent_scs = MOI.OptimizerWithAttributes(SCS.Optimizer, MOI.Silent() => true)
MathOptInterface.OptimizerWithAttributes(SCS.Optimizer, Pair{MathOptInterface.AbstractOptimizerAttribute, Any}[MathOptInterface.Silent() => true])
julia> solve!(p, silent_scs)
[ Info: [Convex.jl] Compilation finished: 0.0 seconds, 54.453 KiB of memory allocated Problem statistics problem is DCP : true number of variables : 1 (1 scalar elements) number of constraints : 1 (1 scalar elements) number of coefficients : 1 number of atoms : 1 Solution summary termination status : OPTIMAL primal status : FEASIBLE_POINT dual status : FEASIBLE_POINT objective value : 1.0 Expression graph minimize └─ real variable (id: 881…673) subject to └─ ≥ constraint (affine) └─ + (affine; real) ├─ real variable (id: 881…673) └─ [-1;;]
Another option is to use the solver-independent silent
keyword argument to solve!
:
julia> solve!(p, SCS.Optimizer; silent=true)
Problem statistics problem is DCP : true number of variables : 1 (1 scalar elements) number of constraints : 1 (1 scalar elements) number of coefficients : 1 number of atoms : 1 Solution summary termination status : OPTIMAL primal status : FEASIBLE_POINT dual status : FEASIBLE_POINT objective value : 1.0 Expression graph minimize └─ real variable (id: 881…673) subject to └─ ≥ constraint (affine) └─ + (affine; real) ├─ real variable (id: 881…673) └─ [-1;;]
See each solver's documentation for more information on solver-dependent options.