control-toolbox / CTBase.jl

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

Box constraints for the direct method & more #4

Closed ocots closed 1 year ago

ocots commented 1 year ago

The constraints have to be added in the CTBase package. Then, it will be possible to update the CTDirect package.

Remarks.

jbcaillau commented 1 year ago

Doing, on this base (see this issue in the last sprint):

A296BDE1-EDED-4B29-9E6A-4AF3BBB49AD9

jbcaillau commented 1 year ago

Updates are in order for the NLP construction as

Testing whether there is a range of a function allows to decide whether the constraint is linear (a box) or not when constructing the NLP model.

NB. The current version of the code factorises several kind of constraints which is hard to maintain / update 😬

jbcaillau commented 1 year ago

@PierreMartinon @joseph-gergaud any idea of the format you would like to retrieve box constraints on state and control

jbcaillau commented 1 year ago

@PierreMartinon @joseph-gergaud nlp_constraints now returns two additional triples:

Check this commit. Should be ready for CTDirect.jl update 🤞🏾

@PierreMartinon @joseph-gergaud any idea of the format you would like to retrieve box constraints on state and control

  • at t0 / tf
  • for all times?
jbcaillau commented 1 year ago

@ocots Some minor changes in CTBase.jl (undergoing tests)

Check this commit

ocots commented 1 year ago

At this line, we have

function constraint!(ocp::OptimalControlModel, type::Symbol, lb::Real, ub::Real, label::Symbol=gensym(:anonymous))
    if type ∈ [ :initial, :final ]
        ocp.constraints[label] = (type, :ineq, x -> x, ub, lb)
    elseif type ∈ [ :control, :state ]
        ocp.constraints[label] = (type, :ineq, 1:state_dimension(ocp), ub, lb)
    else
        throw(IncorrectArgument("the following type of constraint is not valid: " * String(type) *
        ". Please choose in [ :initial, :final ] or check the arguments of the constraint! method."))
    end
end

Isn't?

function constraint!(ocp::OptimalControlModel, type::Symbol, lb::Real, ub::Real, label::Symbol=gensym(:anonymous))
    if type ∈ [ :initial, :final ]
        ocp.constraints[label] = (type, :ineq, x -> x, ub, lb)
    elseif type ∈ [ :control]
        ocp.constraints[label] = (type, :ineq, 1:control_dimension(ocp), ub, lb)
    elseif type ∈ [ :state ]
        ocp.constraints[label] = (type, :ineq, 1:state_dimension(ocp), ub, lb)
    else
        throw(IncorrectArgument("the following type of constraint is not valid: " * String(type) *
        ". Please choose in [ :initial, :final ] or check the arguments of the constraint! method."))
    end
end
jbcaillau commented 1 year ago

Indeed! fixed 🙏🏽

PierreMartinon commented 1 year ago

I'll start integrating the box constraints in the direct part next week !

joseph-gergaud commented 1 year ago

Je voulais aussi m'y remettre la semaine prochaine. On pourrait travaille ensemble comme à Nice (voir mes dispo en pièce jointe).

Calendrier — semaine — de 12:03:2023 à 18:03:2023.pdf

PierreMartinon commented 1 year ago

Oui ! Je suis libre sauf vendredi, donc je pourrais passer mercredi ? (lundi et mardi ont l'air un peu pris) Pierre

On 10-Mar-23 10:15, joseph-gergaud wrote:

Je voulais aussi m'y remettre la semaine prochaine. On pourrait travaille ensemble comme à Nice (voir mes dispo en pièce jointe).

Calendrier — semaine — de 12:03:2023 à 18:03:2023.pdf https://github.com/control-toolbox/CTBase.jl/files/10940445/Calendrier.semaine.de.12.03.2023.a.18.03.2023.pdf

— Reply to this email directly, view it on GitHub https://github.com/control-toolbox/CTBase.jl/issues/4#issuecomment-1463502636, or unsubscribe https://github.com/notifications/unsubscribe-auth/AECBGHMANWSISSUMPZA5DG3W3LWJ7ANCNFSM6AAAAAAVVU36Y4. You are receiving this because you were mentioned.Message ID: @.***>

joseph-gergaud commented 1 year ago

Pierre,

On dit mercredi à 9h00 dans mon bureau ?

Joseph

Le 10 mars 2023 à 12:14, Pierre Martinon @.***> a écrit :

Oui ! Je suis libre sauf vendredi, donc je pourrais passer mercredi ? (lundi et mardi ont l'air un peu pris) Pierre

On 10-Mar-23 10:15, joseph-gergaud wrote:

Je voulais aussi m'y remettre la semaine prochaine. On pourrait travaille ensemble comme à Nice (voir mes dispo en pièce jointe).

Calendrier — semaine — de 12:03:2023 à 18:03:2023.pdf https://github.com/control-toolbox/CTBase.jl/files/10940445/Calendrier.semaine.de.12.03.2023.a.18.03.2023.pdf

— Reply to this email directly, view it on GitHub https://github.com/control-toolbox/CTBase.jl/issues/4#issuecomment-1463502636, or unsubscribe https://github.com/notifications/unsubscribe-auth/AECBGHMANWSISSUMPZA5DG3W3LWJ7ANCNFSM6AAAAAAVVU36Y4. You are receiving this because you were mentioned.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/control-toolbox/CTBase.jl/issues/4#issuecomment-1463653353, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJC65POOIHY4I5AGSJQAYV3W3MEINANCNFSM6AAAAAAVVU36Y4. You are receiving this because you were mentioned.

ocots commented 1 year ago

Of course, before integrating the changes from CTBase.jl a new release of it has to be done.

jbcaillau commented 1 year ago

@ocots Looks like these functions are only used in the test: correct? With the additional type of constraints, these computations must be updated (and might not completely make sense: case of inequalities...) My first guess would be to suppress them ☠️.

function initial_condition(ocp::OptimalControlModel)
function final_condition(ocp::OptimalControlModel)
function initial_constraint(ocp::OptimalControlModel)
function final_constraint(ocp::OptimalControlModel)

Note that labelled constraint can be retrieved:

constraint!(ocp, :initial, x0, :eq1)
x -> constraint(ocp, :eq1) - x0 === x -> x - x0

constraint!(ocp, :initial, 3:5, y0, :eq2)
x -> constraint(ocp, :eq2) - y0 === x -> x[3:5] - y0

etc.

jbcaillau commented 1 year ago

@ocots At least used in CTDirectShooting.jl. I am:

ocots commented 1 year ago

The functionsfinal_constraint and initial_condition are used in CTDirectShooting/src/utils.jl in the following function.

function parse_ocp_direct_shooting(ocp::OptimalControlModel)
    # parsing ocp
    dy = dynamics(ocp)
    co = lagrange(ocp)
    cf = final_constraint(ocp)
    x0 = initial_condition(ocp)
    n = state_dimension(ocp)
    m = control_dimension(ocp)
    return dy, co, cf, x0, n, m
end

This function parse_ocp_direct_shooting is used to define the direct shooting problem which is given to the unconstrained optimization solver CTOptimization.jl and is also used to build the solution.

Remark. The functionsfinal_constraint and initial_condition are not used to check the validity of the optimal control problem like I have said. It is true than with the direct shooting method we cannot solve problems with constraints for the moment, but the checking is done simply by:

    # check validity
    ξ, ψ, ϕ = nlp_constraints(ocp)
    dim_ξ = length(ξ[1])      # dimension of the boundary constraints
    dim_ψ = length(ψ[1])
    if dim_ξ != 0 && dim_ψ != 0
        error("direct shooting is implemented for problems without constraints")
    end
jbcaillau commented 1 year ago

@ocots OK I if simply move the two functions initial_condition and final_constraint (that assume the simple form you used for the problem) into CTDirectShooting/src/utils.jl? Will it suffice?

ocots commented 1 year ago

We can write the functions here to keep them in mind and you can remove them from CTBase.jl. If you have a better way to get the initial / final conditions / constraints, you can give it below.

function initial_condition(ocp::OptimalControlModel) 
    cs = constraints(ocp)
    x0 = nothing
    for (_, c) ∈ cs
        type, _, _, val = c
        if type == :initial
             x0 = val
        end
    end
    return x0
end
function final_condition(ocp::OptimalControlModel) 
    cs = constraints(ocp)
    xf = nothing
    for (_, c) ∈ cs
        type, _, _, val = c
        if type == :final
             xf = val
        end
    end
    return xf
end
function initial_constraint(ocp::OptimalControlModel) 
    cs = constraints(ocp)
    c0 = nothing
    for (_, c) ∈ cs
        type, _, f, val = c
        if type == :initial
            c0 = x -> f(x) - val
        end
    end
    return c0
end
function final_constraint(ocp::OptimalControlModel) 
    cs = constraints(ocp)
    cf = nothing
    for (_, c) ∈ cs
        type, _, f, val = c
        if type == :final
            cf = x -> f(x) - val
        end
    end
    return cf
end
PierreMartinon commented 1 year ago

@jbcaillau A bit late to the party, but with Joseph we started a branch in CTDirect that is compatible with the new nlp_constraints function including the pure state constraints and boxes

Concerning your question about the boxes at t0 / tf and all t, there is no distinction in the NLP: the time grid includes t0 and tf so there is only boxes 'for all t' and no specific boxes for t0/tf (there are however the generic boundary conditions at t0/tf)

For the format, the triple (LB,IND,UB) could be a triple of flattened values or use vectors of vectors. Example: assume the boxes

-1 <= x[1,3] <= 2 , 0 <= x[2] <= Inf

Flat format: (no need for ordering)

LB = (-1,-1,0) IND = (1,3,2) UB = (2,2,Inf)

Nested format:

LB = (-1,0) IND = ((1,3),2)  UB=(2,Inf)

Either form works for us.

ocots commented 1 year ago

@jbcaillau Not sure that this should be a Vector{Int} since you append some range: https://github.com/control-toolbox/CTBase.jl/blob/constraints/src/model.jl#L229

ocots commented 1 year ago

Scalar bounds or vectorial:

jbcaillau commented 1 year ago

OK: we can

ocots commented 1 year ago

For me it was ok to give a vector of the same size as the range. Still, the most important for @PierreMartinon and @joseph-gergaud is just in which format we give them the constraints, via nlp_constraints function.

Remark. the name nlp_constraints is pas terrible terrible :-)

PierreMartinon commented 1 year ago

Flat non-ordered is ok for us.

Remark. the name nlp_constraints is pas terrible terrible :-) ocp_constraints ?

jbcaillau commented 1 year ago

@PierreMartinon @joseph-gergaud For the record:

jbcaillau commented 1 year ago

@ocots tests passed in branch constraints:

ocots commented 1 year ago

I will handle CTDirectShooting. No problemo. I have made the review but I prefer not to change the code myself.

jbcaillau commented 1 year ago

@ocots Cannot see the review...?

I have made the review but I prefer not to change the code myself.

ocots commented 1 year ago

You should see comments directly in the PR: https://github.com/control-toolbox/CTBase.jl/pull/10

jbcaillau commented 1 year ago

@ocots please merge this PR after checking comments in review

jbcaillau commented 1 year ago

@ocots @joseph-gergaud @PierreMartinon OK for a meeting next Thursday (30/3)?

/polls "10:00" "15:00" "17:00"

ocots commented 1 year ago

Any time.

jbcaillau commented 1 year ago

Any time.

good. installed this poll app, does not seem to work 💩

jbcaillau commented 1 year ago

@ocots have you eventually moved this somewhere in CTDirect.jl?

ocots commented 1 year ago

I didn't move it but you have something similar here:

I will replace these pieces of code (piece of codes / piece of code) when I will have something good.

ocots commented 1 year ago

Test:

/polls Option1 'Option 2' "Option 3"

ocots commented 1 year ago

Maybe poll is only in the description of a new issue.

jbcaillau commented 1 year ago

@PierreMartinon @joseph-gergaud https://github.com/control-toolbox/CTBase.jl/issues/4#issuecomment-1483923387

PierreMartinon commented 1 year ago

Pas de contrainte pour moi

On 27-Mar-23 14:06, Jean-Baptiste Caillau wrote:

@PierreMartinon https://github.com/PierreMartinon @joseph-gergaud https://github.com/joseph-gergaud #4 (comment) https://github.com/control-toolbox/CTBase.jl/issues/4#issuecomment-1483923387

— Reply to this email directly, view it on GitHub https://github.com/control-toolbox/CTBase.jl/issues/4#issuecomment-1485026726, or unsubscribe https://github.com/notifications/unsubscribe-auth/AECBGHNS4ET3IO3XJ233Z63W6F7DLANCNFSM6AAAAAAVVU36Y4. You are receiving this because you were mentioned.Message ID: @.***>

joseph-gergaud commented 1 year ago

C'est ok pour moi jeudi à 10h00

jbcaillau commented 1 year ago

@ocots @PierreMartinon @joseph-gergaud 10:00 it is, this Thursday webex