AMPL tutorial
This tutorial shows how to solve a model written in AMPL with Penelopt.jl, using AmplNLReader.jl.
Penelopt.jl solves problems of the form minimize f(x) s.t. c(x) = 0. If your AMPL model has inequality constraints, the solver will fail.
1. The AMPL model
In this example, we use the Hock–Schittkowski problem HS6.
var x1 := -1.2;
var x2 := 1;
minimize obj: (1 - x1)^2;
subject to c1: 10 * (x2 - x1^2) = 0;We save this in a hs6.mod file.
2. Generate the .nl file
AMPL compiles a model into a .nl file that solvers read directly, without needing AMPL itself at solve time:
$ ampl -oghs6 assets/hs6.modThis produces file hs6.nl.
3. Read the model into Julia
using AmplNLReader
nlp = AmplModel(joinpath(@__DIR__, "assets", "hs6.nl"))4. Solve with Penelopt
using Penelopt
stats = L2Penalty(nlp; print_level = 1)┌ Info:
│ This is Penelopt.jl v1.0.0.
│ Running with linear solver MUMPS v5.8.2.
│
│ ScaledModel{Float64, Vector{Float64}, AmplNLReader.AmplModel, NLPModels.NLPModelMeta{Float64, Vector{Float64}}}
│ Problem name: hs6 (scaled)
│ All variables: ████████████████████ 2 All constraints: ████████████████████ 1
│ free: ████████████████████ 2 free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ low/upp: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 low/upp: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ fixed: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 fixed: ████████████████████ 1
│ infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ nnzh: ( 66.67% sparsity) 1 linear: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ nonlinear: ████████████████████ 1
│ nnzj: ( 0.00% sparsity) 2
│ lin_nnzj: (------% sparsity)
│ nln_nnzj: ( 0.00% sparsity) 2
│
│ Counters:
│ obj: ████████████████████ 1 grad: ████████████████████ 1 cons: ████████████████████ 1
│ cons_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 cons_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 jcon: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ jgrad: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 jac: ████████████████████ 1 jac_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ jac_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 jprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 jprod_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ jprod_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 jtprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 jtprod_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ jtprod_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 hess: ████████████████████ 1 hprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
│ jhess: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 jhprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
└
[ Info: ------------------------------------------------------------------------------------------------------
[ Info: Iter sIter Objective pfeas dfeas τ ptol dtol ‖x‖
[ Info: ------------------------------------------------------------------------------------------------------
[ Info: 0 0 +4.8400000e+00 8.15e+00 4.40e+00 1.00e+00 1.00e+00 8.15e-02 1.56e+00
[ Info: 1 7 +2.2503618e-03 1.03e-01 3.27e-02 1.00e+00 1.03e-03 3.27e-04 1.31e+00
[ Info: 2 2 +1.8092083e-07 5.04e-04 3.18e-04 1.00e+00 5.04e-06 3.18e-06 1.41e+00
[ Info: 3 2 +1.8196284e-15 5.61e-10 3.42e-08 1.00e+00 8.05e-08 1.36e-07 1.41e+00
┌ Info:
│ Number of Iterations: 3
│
│
│ Objective...........: +1.819628395140804e-15
│ Primal Feasibility..: 5.611457964960209e-10
│ Dual Feasibility....: 3.416864829239490e-08
│
│
└ EXIT: first_order.println("status : ", stats.status)
println("objective : ", stats.objective)
println("solution : ", stats.solution)status : first_order
objective : 1.819628395140804e-15
solution : [0.9999999573428975, 0.9999999146296821]See the options reference for the full list of keyword arguments accepted by the solver.