control-toolbox / CTBase.jl

Fundamentals of the control-toolbox ecosystem
http://control-toolbox.org/CTBase.jl/
MIT License
11 stars 3 forks source link

Preparing for inplace NLP #232

Closed jbcaillau closed 2 weeks ago

jbcaillau commented 1 month ago

@PierreMartinon aiming to providing inplace function as a result of nlp_constraints!, having inplace signatures for ξ, η, ψ, ϕ, θ:

julia> (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (θl, θ, θu),
    (ul, uind, uu), (xl, xind, xu), (vl, vind, vu) = nlp_constraints!(ocp)

Related to: https://github.com/control-toolbox/CTDirect.jl/issues/188

@ocots I'll need some help to update the unit tests.

jbcaillau commented 1 month ago

@jbcaillau To update:

In model.jl

5 possible nonlinear (= defined by functions, now inplace) constraints to treat, associated respectively with ξ, η, ψ, ϕ, θ:

        (type, f::BoundaryConstraint, lb, ub) && if type ∈ [:initial, :final, :boundary] end # ϕ
        (:control, f::ControlConstraint, lb, ub) # ξ
        (:state, f::StateConstraint, lb, ub) # η
        (:mixed, f::MixedConstraint, lb, ub) # ψ
        (:variable, f::VariableConstraint, lb, ub) # θ

Also (see fun_range):

function constraint!(
    ocp::OptimalControlModel{T, V}, 
    type::Symbol;
    rg::Union{OrdinalRange{<:Integer}, Index, Integer, Nothing}=nothing, 
    f::Union{Function, Nothing}=nothing, 
    lb::Union{ctVector, Nothing}=nothing, 
    ub::Union{ctVector, Nothing}=nothing,
    val::Union{ctVector, Nothing}=nothing,
    label::Symbol=__constraint_label()) where {T <: TimeDependence, V <: VariableDependence}

Also:

function constraint(ocp::OptimalControlModel{T, V}, label::Symbol) where {T <: TimeDependence, V <: VariableDependence}

In onepass.jl

p_constraint!(p, ocp, e1, e2, e3, label=gensym(); log=false)
p_dynamics!(p, ocp, x, t, e, label=nothing; log=false)
p_lagrange!(p, ocp, e, type; log=false)
p_mayer!(p, ocp, e, type; log=false)
p_bolza!(p, ocp, e1, e2, type; log=false)

NB.

Misc

Type changes

jbcaillau commented 1 month ago

@PierreMartinon any preliminary comments?

@PierreMartinon aiming to providing inplace function as a result of nlp_constraints!, having inplace signatures for ξ, η, ψ, ϕ, θ:

julia> (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (θl, θ, θu),
    (ul, uind, uu), (xl, xind, xu), (vl, vind, vu) = nlp_constraints!(ocp)

Related to: control-toolbox/CTDirect.jl#188

jbcaillau commented 3 weeks ago

@PierreMartinon back on this issue. on your side, ready for inplace in CTDirect.jl with trapezoidal scheme? Target above: https://github.com/control-toolbox/CTBase.jl/issues/232#issue-2458120989

jbcaillau commented 3 weeks ago

@ocots I'll need some help to update the unit tests.

@ocots we'll need to be able to run tests like

    @def o begin
        t ∈ [ 0, 1 ], time
        x = [ r, v ] ∈ R², state
        u ∈ R, control
        w = r + 2v
        r(0) == 0,    (1)
        v(0) == 1,    (♡)
        ẋ(t) == [ v(t), w(t)^2 ]
        ∫( u(t)^2 + x₁(t) ) → min
    end
    x = [ 1, 2 ]
    x0  = 2 * x
    xf  = 3 * x
    u = 3
    @test constraint(o, :eq1)(x0, xf) == x0[1]
    @test constraint(o, Symbol("♡"))(x0, xf) == x0[2]
    @test o.dynamics(x, u) == [ x[2], (x[1] + 2x[2])^2 ]
    @test o.lagrange(x, u) == u^2 + x[1]

It is better not to rewrite test but to update the getters (such as constraint) so that they take care of the functions now being in place. My question: can you please remind me what getters are available for dynamics and the three different costs? (Using getters in the test would be nicer, too.)

ocots commented 3 weeks ago

Getters:

https://github.com/control-toolbox/CTBase.jl/blob/5db9d88a096e8a515aa289dcbd30c72435679b5f/src/optimal_control_model-getters.jl#L565-L589

jbcaillau commented 3 weeks ago

@ocots excellent. do think you could vscode update test_onepass.jl to replace field accesses by these getters? Then it should be fairly easy to reuse the existing tests with in place functions.

jbcaillau commented 3 weeks ago

Getters: [...]

@ocots just for the record: no bolza getter, subsumed by lagrange + mayer ones, I suppose? YES

PierreMartinon commented 3 weeks ago

Hi guys, While you're at it, can you make it so that initial conditions are always before final conditions when building the boundary conditions ? Also, iirc mayer exists only in autonomous version ?

jbcaillau commented 3 weeks ago

@PierreMartinon

Hi guys, While you're at it, can you make it so that initial conditions are always before final conditions when building the boundary conditions ?

OK

Also, iirc mayer exists only in autonomous version ?

Not sure that the question makes sense:

Same thing for (nonlinear) boundary conditions, BTW.

Am I missing sth?

jbcaillau commented 3 weeks ago

@PierreMartinon meeting tomorrow (tuesday) 14:00 https://inria.webex.com/meet/jean-baptiste.caillau

@ocots @joseph-gergaud welcome to join if you're around!

ocots commented 2 weeks ago

@PierreMartinon Parametric type:

struct TypeParamExample{T<:Number}
    x::T
end

and for a function:

julia> function f(x::T, y::T) where {T <: Number}
       return x + y
       end
f (generic function with 1 method)

julia> f(1, 2)
3

julia> f(1, 2.0)
ERROR: MethodError: no method matching f(::Int64, ::Float64)

Closest candidates are:
  f(::T, ::T) where T<:Number
   @ Main REPL[5]:1

Stacktrace:
 [1] top-level scope
   @ REPL[7]:1
joseph-gergaud commented 2 weeks ago

@jbcaillau The scalar case for the constraints must be treated.

ocots commented 2 weeks ago

@PierreMartinon Voir :

jbcaillau commented 2 weeks ago

@jbcaillau The scalar case for the constraints must be treated.

@joseph-gergaud @PierreMartinon should be OK provided in place functions returned by nlp_constraints! are called with a (slice of) vector as first arg. may be an exception for Lagrange cost, to be checked. in any case, the possible generated error should be clear enough 🤞🏾

jbcaillau commented 2 weeks ago
jbcaillau commented 2 weeks ago

@PierreMartinon catch-up this Monday 9/9 at 14:00 on zoom https://univ-cotedazur.zoom.us/my/caillau

@ocots @joseph-gergaud welcome if you're around!

jbcaillau commented 2 weeks ago

Hi guys, While you're at it, can you make it so that initial conditions are always before final conditions when building the boundary conditions ? Also, iirc mayer exists only in autonomous version ?

@PierreMartinon closing this issue, in favour of https://github.com/control-toolbox/CTBase.jl/issues/288 (please answer there!)