control-toolbox / CTDirect.jl

Direct transcription of an optimal control problem and resolution
http://control-toolbox.org/CTDirect.jl/
MIT License
9 stars 6 forks source link

New release of CTDirect and OptimalControl #22

Closed ocots closed 1 year ago

ocots commented 1 year ago

This example fails:

using OptimalControl

ocp = Model()
state!(ocp, 2)   # dimension of the state
control!(ocp, 1) # dimension of the control
time!(ocp, [0, 1]) # time interval
constraint!(ocp, :initial, [-1, 0]) # initial condition
constraint!(ocp, :final,   [0, 0]) # final condition
A = [ 0 1
      0 0 ]
B = [ 0
      1 ]
constraint!(ocp, :dynamics, (x, u) -> A*x + B*u) # dynamics
objective!(ocp, :lagrange, (x, u) -> 0.5u^2) # objective

sol = solve(ocp)

plot(sol, size=(700, 700))

Output

Method = (:direct, :ADNLProblem, :ipopt)
This is Ipopt version 3.14.4, running with linear solver MUMPS 5.5.1.

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

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.1000000e+00 2.10e+00 2.78e-13   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1 -6.0500000e-01 2.46e-01 3.50e-14 -11.0 7.04e+00    -  1.00e+00 1.00e+00h  1
   2  6.0023829e+00 8.88e-16 1.78e-15 -11.0 6.61e+00    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 2

                                   (scaled)                 (unscaled)
Objective...............:   6.0023829460295737e+00    6.0023829460295737e+00
Dual infeasibility......:   1.7763568394002505e-15    1.7763568394002505e-15
Constraint violation....:   8.8817841970012523e-16    8.8817841970012523e-16
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   1.7763568394002505e-15    1.7763568394002505e-15

Number of objective function evaluations             = 3
Number of objective gradient evaluations             = 3
Number of equality constraint evaluations            = 3
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 3
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 2
Total seconds in IPOPT                               = 29.188

EXIT: Optimal Solution Found.
ERROR: LoadError: BoundsError: attempt to access 0-element Vector{Float64} at index [1:3]
Stacktrace:
  [1] throw_boundserror(A::Vector{Float64}, I::Tuple{UnitRange{Int64}})
    @ Base ./abstractarray.jl:703
  [2] checkbounds
    @ ./abstractarray.jl:668 [inlined]
  [3] getindex
    @ ./array.jl:930 [inlined]
  [4] get_state_at_time_step(xu::Vector{Float64}, i::Int64, nx::Int64, N::Int64)
    @ CTDirect ~/.julia/packages/CTDirect/jiSFZ/src/utils.jl:7
  [5] parse_ipopt_sol(ctd::CTDirect.CTDirect_data)
    @ CTDirect ~/.julia/packages/CTDirect/jiSFZ/src/solution.jl:95
  [6] _OptimalControlSolution(ocp::OptimalControlModel{:autonomous, :scalar}, ipopt_solution::SolverCore.GenericExecutionStats{Float64, Vector{Float64}, Vector{Float64}, Any}, ctd::CTDirect.CTDirect_data)
    @ CTDirect ~/.julia/packages/CTDirect/jiSFZ/src/solution.jl:17
  [7] solve(ocp::OptimalControlModel{:autonomous, :scalar}, description::Tuple{Symbol, Symbol}; grid_size::Int64, print_level::Int64, mu_strategy::String, display::Bool, init::Nothing, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ CTDirect ~/.julia/packages/CTDirect/jiSFZ/src/solve.jl:47
  [8] solve(::OptimalControlModel{:autonomous, :scalar}; display::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ OptimalControl ~/Boulot/recherche/logiciels/dev/control-toolbox/OptimalControl/src/solve.jl:31
  [9] solve(::OptimalControlModel{:autonomous, :scalar})
    @ OptimalControl ~/Boulot/recherche/logiciels/dev/control-toolbox/OptimalControl/src/solve.jl:16
 [10] top-level scope
    @ ~/Boulot/recherche/logiciels/dev/control-toolbox/OptimalControl/examples/doc.jl:16
 [11] include(fname::String)
    @ Base.MainInclude ./client.jl:476
 [12] top-level scope
    @ REPL[1]:1
in expression starting at /Users/ocots/Boulot/recherche/logiciels/dev/control-toolbox/OptimalControl/examples/doc.jl:16
PierreMartinon commented 1 year ago

Should be fixed in https://github.com/control-toolbox/CTDirect.jl/pull/23

On a side note, the CTFlows dependency makes CTProblems much slower to compile, maybe we should remove the computation of the indirect solution for goddard ? Maybe store it in a file or something.

ocots commented 1 year ago

That's a possibility. It should compile once, no? If it is a problem we will store the solution instead of computing it.

ocots commented 1 year ago

Problem fixed! I make a new release of OptimalControl.jl soon.

PierreMartinon commented 1 year ago

ok, I juste compared using local problems vs CTProblems (with CTFlows). It is indeed faster but only marginally (about 12 min vs 14 min), so we can keep/put back the dependency if it's useful ! @ocots