Skip to content

Commit

Permalink
Merge pull request #273 from control-toolbox/272-update-test_onepassj…
Browse files Browse the repository at this point in the history
…l-to-replace-field-accesses-by-getters

Use getters for tests and remove old constraints, time in tests
  • Loading branch information
jbcaillau authored Aug 30, 2024
2 parents 8f728a8 + fdebad3 commit 468ae3b
Show file tree
Hide file tree
Showing 17 changed files with 1,078 additions and 1,214 deletions.
8 changes: 4 additions & 4 deletions bench/bench_nlp_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ function test_alloc_bad(ocp, N)
begin
v = Real[]

n = ocp.state_dimension
m = ocp.control_dimension
n = state_dimension(ocp)
m = control_dimension(ocp)

times = LinRange(0, 1, N)
XU = ones(N * (n + m))
Expand Down Expand Up @@ -456,8 +456,8 @@ function test_alloc_good(ocp, N)
begin
v = Real[]

n = ocp.state_dimension
m = ocp.control_dimension
n = state_dimension(ocp)
m = control_dimension(ocp)

times = LinRange(0, 1, N)
XU = zeros(N * (n + m))
Expand Down
8 changes: 4 additions & 4 deletions bench/bench_nlp_constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ function test_alloc_bad(ocp, N)
println(" declare variables")
begin
v = Real[]
n = ocp.state_dimension
m = ocp.control_dimension
n = state_dimension(ocp)
m = control_dimension(ocp)
times = LinRange(0, 1, N)
XU = ones(N * (n + m))
= length(ξl)
Expand Down Expand Up @@ -373,8 +373,8 @@ function test_alloc_good(ocp, N)
println(" declare variables")
begin
v = Real[]
n = ocp.state_dimension
m = ocp.control_dimension
n = state_dimension(ocp)
m = control_dimension(ocp)
times = LinRange(0, 1, N)
XU = zeros(N * (n + m))
= length(ξl)
Expand Down
40 changes: 20 additions & 20 deletions ext/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ function __initial_plot(
)

# parameters
n = sol.state_dimension
m = sol.control_dimension
n = state_dimension(sol)
m = control_dimension(sol)

if layout == :group
@match control begin
Expand Down Expand Up @@ -354,12 +354,12 @@ function Plots.plot!(
end

#
n = sol.state_dimension
m = sol.control_dimension
x_labels = sol.state_components_names
u_labels = sol.control_components_names
u_label = sol.control_name
t_label = sol.time_name
n = state_dimension(sol)
m = control_dimension(sol)
x_labels = state_components_names(sol)
u_labels = control_components_names(sol)
u_label = control_name(sol)
t_label = time_name(sol)

# split series attributes
series_attr = __keep_series_attributes(; kwargs...)
Expand Down Expand Up @@ -548,12 +548,12 @@ function Plots.plot!(
end

function __size_plot(sol::OptimalControlSolution, control::Symbol)
n = sol.state_dimension
#m = sol.control_dimension
n = state_dimension(sol)
#m = control_dimension(sol)
m = @match control begin
:components => sol.control_dimension
:components => control_dimension(sol)
:norm => 1
:all => sol.control_dimension + 1
:all => control_dimension(sol) + 1
_ => throw(IncorrectArgument("No such choice for control. Use :components, :norm or :all"))
end
return (600, 140 * (n + m))
Expand Down Expand Up @@ -643,10 +643,10 @@ function recipe_label(
end

label = @match s begin
:state => sol.state_components_names[i]
:control => sol.control_components_names[i]
:costate => "p" * sol.state_components_names[i]
:control_norm => "" * sol.control_name * ""
:state => state_components_names(sol)[i]
:control => control_components_names(sol)[i]
:costate => "p" * state_components_names(sol)[i]
:control_norm => "" * control_name(sol) * ""
_ => error("Internal error, no such choice for label")
end
end
Expand All @@ -664,10 +664,10 @@ function __get_data_plot(
xx::Union{Symbol, Tuple{Symbol, Integer}};
time::Symbol = :default,
)
T = sol.time_grid
X = sol.state.(T)
U = sol.control.(T)
P = sol.costate.(T)
T = time_grid(sol)
X = state(sol).(T)
U = control(sol).(T)
P = costate(sol).(T)

vv, ii = @match xx begin
::Symbol => (xx, 1)
Expand Down
4 changes: 2 additions & 2 deletions src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ export OptimalControlModel
export Model
export __OCPModel # redirection to Model to avoid confusion with other Model functions from other packages. Due to @def macro
export variable!,
time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!, constraint
time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!, model_expression!
export is_autonomous, is_fixed, is_time_independent, is_time_dependent, is_min, is_max
export is_variable_dependent, is_variable_independent
export nlp_constraints!, constraints_labels
export nlp_constraints!, constraints, constraints_labels, constraint
export has_free_final_time, has_free_initial_time, has_lagrange_cost, has_mayer_cost
export dim_control_constraints, dim_state_constraints, dim_mixed_constraints, dim_path_constraints
export dim_boundary_constraints, dim_variable_constraints, dim_control_range
Expand Down
12 changes: 6 additions & 6 deletions src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ mutable struct OptimalControlInit
"""
function OptimalControlInit(sol::OptimalControlSolution; unused_kwargs...)
return OptimalControlInit(
state = sol.state,
control = sol.control,
variable = sol.variable,
state_dim = sol.state_dimension,
control_dim = sol.control_dimension,
variable_dim = sol.variable_dimension,
state = state(sol),
control = control(sol),
variable = variable(sol),
state_dim = state_dimension(sol),
control_dim = control_dimension(sol),
variable_dim = variable_dimension(sol),
)
end
end
175 changes: 91 additions & 84 deletions src/optimal_control_model-getters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ function nlp_constraints!(ocp::OptimalControlModel)
# we check if the dimensions and times have been set
__check_all_set(ocp)

#
constraints = ocp.constraints

ξf = Vector{ControlConstraint}()
ξl = Vector{ctNumber}()
ξu = Vector{ctNumber}()
Expand All @@ -64,7 +61,7 @@ function nlp_constraints!(ocp::OptimalControlModel)
vl = Vector{ctNumber}()
vu = Vector{ctNumber}()

for (_, c) constraints
for (_, c) constraints(ocp)
@match c begin
(type, f::BoundaryConstraint, lb, ub) && if type [:initial, :final, :boundary]
end => begin
Expand Down Expand Up @@ -207,88 +204,12 @@ end
"""
$(TYPEDSIGNATURES)
Return `true` if the model is autonomous.
"""
is_autonomous(ocp::OptimalControlModel{Autonomous, <:VariableDependence}) = true
is_autonomous(ocp::OptimalControlModel{NonAutonomous, <:VariableDependence}) = false

"""
$(TYPEDSIGNATURES)
Return `true` if the model has been defined as time dependent.
"""
is_time_dependent(ocp::OptimalControlModel) = !is_autonomous(ocp)

"""
$(TYPEDSIGNATURES)
Return `true` if the model has been defined as time independent.
"""
is_time_independent(ocp::OptimalControlModel) = !is_time_dependent(ocp)

"""
$(TYPEDSIGNATURES)
Return `true` if the criterion type of `ocp` is `:min`.
"""
is_min(ocp::OptimalControlModel) = ocp.criterion == :min

"""
$(TYPEDSIGNATURES)
Return `true` if the criterion type of `ocp` is `:max`.
"""
is_max(ocp::OptimalControlModel) = !is_min(ocp)

"""
$(TYPEDSIGNATURES)
Return `true` if the model is fixed (= has no variable).
"""
is_fixed(ocp::OptimalControlModel{<:TimeDependence, Fixed}) = true
is_fixed(ocp::OptimalControlModel{<:TimeDependence, NonFixed}) = false
Return the constraints of the ocp or nothing.
"""
$(TYPEDSIGNATURES)
Return `true` if the model has been defined as variable dependent.
"""
is_variable_dependent(ocp::OptimalControlModel) = !is_fixed(ocp)

"""
$(TYPEDSIGNATURES)
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)
function constraints(ocp::OptimalControlModel)
return ocp.constraints
end

"""
$(TYPEDSIGNATURES)
Expand Down Expand Up @@ -587,3 +508,89 @@ Return the dynamics of the optimal control problem or `nothing`.
"""
dynamics(ocp::OptimalControlModel) = ocp.dynamics

"""
$(TYPEDSIGNATURES)
Return `true` if the model is autonomous.
"""
is_autonomous(ocp::OptimalControlModel{Autonomous, <:VariableDependence}) = true
is_autonomous(ocp::OptimalControlModel{NonAutonomous, <:VariableDependence}) = false

"""
$(TYPEDSIGNATURES)
Return `true` if the model has been defined as time dependent.
"""
is_time_dependent(ocp::OptimalControlModel) = !is_autonomous(ocp)

"""
$(TYPEDSIGNATURES)
Return `true` if the model has been defined as time independent.
"""
is_time_independent(ocp::OptimalControlModel) = !is_time_dependent(ocp)

"""
$(TYPEDSIGNATURES)
Return `true` if the criterion type of `ocp` is `:min`.
"""
is_min(ocp::OptimalControlModel) = criterion(ocp) == :min

"""
$(TYPEDSIGNATURES)
Return `true` if the criterion type of `ocp` is `:max`.
"""
is_max(ocp::OptimalControlModel) = !is_min(ocp)

"""
$(TYPEDSIGNATURES)
Return `true` if the model is fixed (= has no variable).
"""
is_fixed(ocp::OptimalControlModel{<:TimeDependence, Fixed}) = true
is_fixed(ocp::OptimalControlModel{<:TimeDependence, NonFixed}) = false

"""
$(TYPEDSIGNATURES)
Return `true` if the model has been defined as variable dependent.
"""
is_variable_dependent(ocp::OptimalControlModel) = !is_fixed(ocp)

"""
$(TYPEDSIGNATURES)
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(initial_time(ocp)) == Index)

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

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

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

0 comments on commit 468ae3b

Please sign in to comment.