Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - add some boolean indicators for OCP #153

Merged
merged 12 commits into from
Jun 20, 2024
2 changes: 2 additions & 0 deletions src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export Model
export variable!, time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!, constraint
export is_time_independent, is_time_dependent, is_min, is_max, is_variable_dependent, is_variable_independent
export nlp_constraints, constraints_labels
export has_free_final_time, has_free_initial_time, has_lagrange_cost, has_mayer_cost
export dim_boundary_conditions, dim_control_constraints, dim_state_constraints, dim_variable_constraints, dim_mixed_constraints, dim_path_constraints, dim_control_box, dim_state_box, dim_variable_box

# solution
export OptimalControlSolution
Expand Down
6 changes: 6 additions & 0 deletions src/init.jl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would replace OCPInit by OptimalControlInit.

Copy link
Member Author

@PierreMartinon PierreMartinon Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done :D

Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ mutable struct OCPInit

end

# trivial version that just returns its argument
# used for unified syntax in caller functions
function OCPInit(init::OCPInit)
return init
end

end
52 changes: 52 additions & 0 deletions src/model.jl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering about consistency since it seems that the fields containing infos about dimensions and so the getters will return the right values only after the function constraint has been called once. We may add a function to set the dimensions and set them when:

  • a getter is called if it was not set
  • a constraint is added
  • a constraint is removed
  • ...

But I am not sure that this is the right thing to do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PierreMartinon I think you haven't reply to this comment. I think it is quite important.

Copy link
Member Author

@PierreMartinon PierreMartinon Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ocots Indeed I missed it. More precisely, the constraints dimensions are set in nlp_constraints since this is where the different kinds of constraints are parsed.

Ideally, the various dimensions would be updated at each constraint! call, but I don't think it's worth the effort. These are pretty internal parameters, and are supposed to be called only after the ocp is fully defined.

Maybe change the default value to -1 instead of 0 to make it more obvious ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: erf, -1 is not compatible with Dimension type. Just let it be ? If you prefer I will revert to dimensions internal to CTDirect

Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,36 @@ Return `true` if the model has been defined as variable independent.
"""
is_variable_independent(ocp::OptimalControlModel) = !is_variable_dependent(ocp)


"""
$(TYPEDSIGNATURES)

Return `true` if the model has been defined with free initial time.
"""
has_free_initial_time(ocp::OptimalControlModel) = (typeof(ocp.initial_time)==Index)

"""
$(TYPEDSIGNATURES)

Return `true` if the model has been defined with free final time.
"""
has_free_final_time(ocp::OptimalControlModel) = (typeof(ocp.final_time)==Index)

"""
$(TYPEDSIGNATURES)

Return `true` if the model has been defined with lagrange cost.
"""
has_lagrange_cost(ocp::OptimalControlModel) = !isnothing(ocp.lagrange)

"""
$(TYPEDSIGNATURES)

Return `true` if the model has been defined with mayer cost.
"""
has_mayer_cost(ocp::OptimalControlModel) = !isnothing(ocp.mayer)


"""
$(TYPEDSIGNATURES)

Expand Down Expand Up @@ -1182,6 +1212,28 @@ function nlp_constraints(ocp::OptimalControlModel)
return val
end

# set specific constraints dimensions
ocp.dim_control_constraints = length(ξl)
ocp.dim_state_constraints = length(ηl)
ocp.dim_mixed_constraints = length(ψl)
ocp.dim_path_constraints = ocp.dim_control_constraints + ocp.dim_state_constraints + ocp.dim_mixed_constraints
ocp.dim_boundary_conditions = length(ϕl)
ocp.dim_variable_constraints = length(θl)
ocp.dim_control_box = length(ul)
ocp.dim_state_box = length(xl)
ocp.dim_variable_box = length(vl)

return (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (θl, θ, θu), (ul, uind, uu), (xl, xind, xu), (vl, vind, vu)

end

# getters for constraints dimensions
dim_control_constraints(ocp::OptimalControlModel) = ocp.dim_control_constraints
dim_state_constraints(ocp::OptimalControlModel) = ocp.dim_state_constraints
dim_mixed_constraints(ocp::OptimalControlModel) = ocp.dim_mixed_constraints
dim_path_constraints(ocp::OptimalControlModel) = ocp.dim_path_constraints
dim_boundary_conditions(ocp::OptimalControlModel) = ocp.dim_boundary_conditions
dim_variable_constraints(ocp::OptimalControlModel) = ocp.dim_variable_constraints
dim_control_box(ocp::OptimalControlModel) = ocp.dim_control_box
dim_state_box(ocp::OptimalControlModel) = ocp.dim_state_box
dim_variable_box(ocp::OptimalControlModel) = ocp.dim_variable_box
11 changes: 11 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,17 @@ $(TYPEDFIELDS)
criterion::Union{Symbol,Nothing}=nothing
dynamics::Union{Dynamics,Nothing}=nothing
constraints::Dict{Symbol, Tuple{Vararg{Any}}}=Dict{Symbol, Tuple{Vararg{Any}}}()

# internal dimensions for constraints
dim_control_constraints::Dimension=0
dim_state_constraints::Dimension=0
dim_mixed_constraints::Dimension=0
dim_path_constraints::Dimension=0
dim_boundary_conditions::Dimension=0
dim_variable_constraints::Dimension=0
dim_control_box::Dimension=0
dim_state_box::Dimension=0
dim_variable_box::Dimension=0
end

# used for checkings
Expand Down
Loading