Solve a problem

We consider the Hanging Chain problem from COPS package as an example. The problem is to find the shape of a chain hanging between two points a and b. The chain is assumed to be a uniform cable with a given length L. The aim is to find the shape of the chain that minimizes the potential energy.

Solving from JuMP model

We need first to import the needed packages and the problem.

using OptimalControlProblems
using JuMP

model = chain(JuMPBackend())
A JuMP Model
├ solver: none
├ objective_sense: MIN_SENSE
│ └ objective_function_type: JuMP.VariableRef
├ num_variables: 404
├ num_constraints: 305
│ ├ JuMP.NonlinearExpr in MOI.EqualTo{Float64}: 200
│ └ JuMP.AffExpr in MOI.EqualTo{Float64}: 105
└ Names registered in the model
  └ :con_x1, :con_x2, :con_x3, :u, :x1, :x2, :x3

Then we can solve the problem using the optimize! function of Ipopt solver.

using Ipopt

# Set the optimizer
set_optimizer(model, Ipopt.Optimizer)

# Set the optimizer attributes
set_optimizer_attribute(model, "tol", 1e-8)
set_optimizer_attribute(model, "constr_viol_tol", 1e-6)
set_optimizer_attribute(model, "mu_strategy", "adaptive")
set_optimizer_attribute(model, "linear_solver", "mumps")
set_optimizer_attribute(model, "sb", "yes")

# Solve the model
optimize!(model)
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.7.3.

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

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

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  1.8607232e+01 2.08e+00 3.06e-04   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  5.3977582e+00 2.13e-02 8.41e-03 -11.0 1.32e+01    -  1.00e+00 1.00e+00f  1
   2  5.0927541e+00 4.00e-04 4.60e-03 -11.0 4.61e-01  -2.0 1.00e+00 1.00e+00h  1
   3  5.0695401e+00 3.01e-03 3.73e-04 -11.0 3.61e+00    -  1.00e+00 1.00e+00h  1
   4  5.0687930e+00 1.81e-03 4.12e-04 -11.0 3.01e+00    -  1.00e+00 1.00e+00h  1
   5  5.0697325e+00 3.51e-05 3.56e-05 -11.0 3.59e-01    -  1.00e+00 1.00e+00h  1
   6  5.0697842e+00 4.43e-07 3.13e-07 -11.0 4.08e-02    -  1.00e+00 1.00e+00h  1
   7  5.0697846e+00 3.87e-11 3.99e-11 -11.0 2.31e-04    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 7

                                   (scaled)                 (unscaled)
Objective...............:   5.0697846107010411e+00    5.0697846107010411e+00
Dual infeasibility......:   3.9922221778398992e-11    3.9922221778398992e-11
Constraint violation....:   3.8657833878463777e-11    3.8657833878463777e-11
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   3.9922221778398992e-11    3.9922221778398992e-11


Number of objective function evaluations             = 8
Number of objective gradient evaluations             = 8
Number of equality constraint evaluations            = 8
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 8
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 7
Total seconds in IPOPT                               = 3.025

EXIT: Optimal Solution Found.

Solving from OptimalControl model

We need first to import the needed packages and the problem.

using OptimalControlProblems
using OptimalControl

_, model = chain(OptimalControlBackend())
ADNLPModel - Model with automatic differentiation backend ADModelBackend{
  ReverseDiffADGradient,
  ReverseDiffADHvprod,
  ForwardDiffADJprod,
  ReverseDiffADJtprod,
  SparseADJacobian,
  SparseReverseADHessian,
  ForwardDiffADGHjvprod,
}
  Problem name: Generic
   All variables: ████████████████████ 404    All constraints: ████████████████████ 305   
            free: ████████████████████ 404               free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
         low/upp: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0              low/upp: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           fixed: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                fixed: ████████████████████ 305   
          infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0               infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
            nnzh: ( 99.75% sparsity)   202             linear: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
                                                    nonlinear: ████████████████████ 305   
                                                         nnzj: ( 98.86% sparsity)   1405  

  Counters:
             obj: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 grad: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 cons: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
        cons_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0             cons_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 jcon: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           jgrad: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                  jac: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0              jac_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
         jac_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                jprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0            jprod_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
       jprod_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0               jtprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0           jtprod_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
      jtprod_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 hess: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                hprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           jhess: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0               jhprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     

Then we can solve the problem using the ipopt function of NLPModelsIpopt package.

using NLPModelsIpopt

# Solve the model
sol = NLPModelsIpopt.ipopt(
    model;
    print_level=5,
    tol=1e-8,
    mu_strategy="adaptive",
    sb="yes",
    constr_viol_tol=1e-6,
)
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.7.3.

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

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

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  1.8000000e+01 2.00e+00 3.03e-04   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  5.4019494e+00 1.91e-02 6.74e-03 -11.0 1.26e+01    -  1.00e+00 1.00e+00f  1
   2  5.0865615e+00 2.68e-04 3.91e-03 -11.0 3.94e-01  -2.0 1.00e+00 1.00e+00h  1
   3  5.0686974e+00 5.01e-03 5.21e-04 -11.0 4.46e+00    -  1.00e+00 1.00e+00h  1
   4  5.0690826e+00 9.63e-04 3.77e-04 -11.0 2.46e+00    -  1.00e+00 1.00e+00h  1
   5  5.0697541e+00 2.90e-05 2.95e-05 -11.0 2.07e-01    -  1.00e+00 1.00e+00h  1
   6  5.0697845e+00 1.34e-07 1.37e-07 -11.0 2.25e-02    -  1.00e+00 1.00e+00h  1
   7  5.0697846e+00 5.88e-12 6.34e-12 -11.0 6.96e-05    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 7

                                   (scaled)                 (unscaled)
Objective...............:   5.0697846107338593e+00    5.0697846107338593e+00
Dual infeasibility......:   6.3382910031606343e-12    6.3382910031606343e-12
Constraint violation....:   5.8824056736739294e-12    5.8824056736739294e-12
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   6.3382910031606343e-12    6.3382910031606343e-12


Number of objective function evaluations             = 8
Number of objective gradient evaluations             = 8
Number of equality constraint evaluations            = 8
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 8
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 7
Total seconds in IPOPT                               = 5.264

EXIT: Optimal Solution Found.