QuickStart

Snopt solves optimization problems of the following form:

\[\begin{aligned} \text{minimize} &\quad f(x) \\ \text{subject to} &\quad l_g \le g(x) \le u_g \\ &\quad l_x \le x \le u_x \end{aligned}\]

Equality constraints can be specified by setting ${l_g}_i = {u_g}_i$. The expected function signature is:

f = func!(g, x)

where $g$ is modified in-place. The optimizer returns $x^*, f^*$, an information message regarding how the algorithm terminated, and potentially a struct with additional solver-specific outputs.

We start by loading the package.

using SNOW
Snopt

If you want to use Snopt as the optimizer you need to build Snopt.jl separately and load the package separate from this one using Snopt (either before or after using SNOW). It can't be loaded by default because the code is not freely available. In this example we use Ipopt for the optimization as it is freely available.

Next, we define the function we wish to optimize.

function simple!(g, x)
    # objective
    f = 4*x[1]^2 - x[1] - x[2] - 2.5

    # constraints
    g[1] = -x[2]^2 + 1.5*x[1]^2 - 2*x[1] + 1
    g[2] = x[2]^2 + 2*x[1]^2 - 2*x[1] - 4.25
    g[3] = x[1]^2 + x[1]*x[2] - 10.0

    return f
end

We can now optimize the function as follows:

x0 = [1.0; 2.0]  # starting point
ng = 3  # number of constraints
xopt, fopt, info = minimize(simple!, x0, ng)

While the defaults are suitable in this case, for the purposes of demonstration we will be more explicit about the bounds on x (defaults to -Inf to +Inf) and the bounds on the nonlinear constraints (defaults to g(x) <= 0). We will also explicitly set the IPOPT optimizer (also a default).

x0 = [1.0; 2.0]  # starting point
lx = [-5.0, -5]  # lower bounds on x
ux = [5.0, 5]  # upper bounds on x
ng = 3  # number of constraints
lg = -Inf*ones(ng)  # lower bounds on g
ug = zeros(ng)  # upper bounds on g
options = Options(solver=IPOPT())  # choosing IPOPT solver

xopt, fopt, info = minimize(simple!, x0, ng, lx, ux, lg, ug, options)

println("xstar = ", xopt)
println("fstar = ", fopt)
println("info = ", info)
This is Ipopt version 3.13.2, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

Number of nonzeros in equality constraint Jacobian...:        0
Number of nonzeros in inequality constraint Jacobian.:        6
Number of nonzeros in Lagrangian Hessian.............:        0

Total number of variables............................:        2
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        2
                     variables with only upper bounds:        0
Total number of equality constraints.................:        0
Total number of inequality constraints...............:        3
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        3

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0 -1.5000000e+00 0.00e+00 2.62e+00   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1 -2.7235302e+00 4.21e+00 1.74e+00   0.0 6.05e+00    -  5.62e-01 1.00e+00f  1
   2 -9.4251777e-01 1.99e+00 6.34e+00  -0.6 8.92e+00    -  5.92e-01 5.44e-01H  1
   3 -4.2494716e+00 2.33e+00 4.45e+00  -0.3 7.06e+00    -  1.00e+00 1.00e+00f  1
   4 -4.8239643e+00 6.84e-01 8.74e-01  -0.5 3.10e+00    -  1.00e+00 9.63e-01h  1
   5 -4.6648335e+00 0.00e+00 1.04e+00  -1.8 1.21e+00    -  9.80e-01 1.00e+00h  1
   6 -4.6800544e+00 0.00e+00 6.43e-02  -3.1 1.41e-01    -  1.00e+00 1.00e+00h  1
   7 -4.6833853e+00 0.00e+00 1.42e-03  -4.7 1.87e-02    -  1.00e+00 1.00e+00h  1
   8 -4.6834353e+00 0.00e+00 2.57e-06  -6.6 3.32e-04    -  1.00e+00 1.00e+00h  1
   9 -4.6834354e+00 0.00e+00 3.14e-07 -11.0 2.79e-06    -  1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10 -4.6834354e+00 0.00e+00 1.34e-08 -11.0 4.90e-07    -  1.00e+00 1.00e+00h  1
  11 -4.6834354e+00 0.00e+00 4.71e-09 -11.0 1.50e-08    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 11

                                   (scaled)                 (unscaled)
Objective...............:  -4.6834354345613223e+00   -4.6834354345613223e+00
Dual infeasibility......:   4.7113362378059520e-09    4.7113362378059520e-09
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   1.0000000000000041e-11    1.0000000000000041e-11
Overall NLP error.......:   4.7113362378059520e-09    4.7113362378059520e-09


Number of objective function evaluations             = 13
Number of objective gradient evaluations             = 12
Number of equality constraint evaluations            = 0
Number of inequality constraint evaluations          = 13
Number of equality constraint Jacobian evaluations   = 0
Number of inequality constraint Jacobian evaluations = 12
Number of Lagrangian Hessian evaluations             = 0
Total CPU secs in IPOPT (w/o function evaluations)   =      0.127
Total CPU secs in NLP function evaluations           =      0.003

EXIT: Optimal Solution Found.
xstar = [0.1644378582645418, 2.127156813219299]
fstar = -4.683435434561322
info = Solve_Succeeded