Skip to content

Commit 2af415d

Browse files
authored
Merge pull request #135 from control-toolbox/134-dev-autonomous
Add autonomous info
2 parents 5e27285 + 3a59c89 commit 2af415d

11 files changed

Lines changed: 153 additions & 16 deletions

src/CTModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ include("times.jl")
253253
include("dynamics.jl")
254254
include("objective.jl")
255255
include("constraints.jl")
256+
include("time_dependence.jl")
256257
include("print.jl")
257258
include("model.jl")
258259
include("solution.jl")

src/model.jl

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ function build_model(pre_ocp::PreModel)::Model
237237
@ensure __is_dynamics_set(pre_ocp) CTBase.UnauthorizedCall("the dynamics must be set before building the model.")
238238
@ensure __is_dynamics_complete(pre_ocp) CTBase.UnauthorizedCall("all the components of the dynamics must be set before building the model.")
239239
@ensure __is_objective_set(pre_ocp) CTBase.UnauthorizedCall("the objective must be set before building the model.")
240-
@ensure !isnothing(pre_ocp.definition) CTBase.UnauthorizedCall("the definition must be set before building the model.")
240+
@ensure __is_definition_set(pre_ocp) CTBase.UnauthorizedCall("the definition must be set before building the model.")
241+
@ensure __is_autonomous_set(pre_ocp) CTBase.UnauthorizedCall("the time dependence, autonomous=true or false, must be set before building the model.")
241242

242243
# extract components from PreModel
243244
times = pre_ocp.times
@@ -252,9 +253,10 @@ function build_model(pre_ocp::PreModel)::Model
252253
objective = pre_ocp.objective
253254
constraints = build_constraints(pre_ocp.constraints)
254255
definition = pre_ocp.definition
256+
TD = is_autonomous(pre_ocp) ? Autonomous : NonAutonomous
255257

256258
# create the model
257-
model = Model(
259+
model = Model{TD}(
258260
times, state, control, variable, dynamics, objective, constraints, definition
259261
)
260262

@@ -265,6 +267,40 @@ end
265267
# Getters
266268
# ------------------------------------------------------------------------------ #
267269

270+
# time dependence
271+
"""
272+
$(TYPEDSIGNATURES)
273+
274+
Return `true` if the model is autonomous.
275+
"""
276+
function is_autonomous(
277+
::Model{
278+
Autonomous,
279+
<:TimesModel,
280+
<:AbstractStateModel,
281+
<:AbstractControlModel,
282+
<:AbstractVariableModel,
283+
<:Function,
284+
<:AbstractObjectiveModel,
285+
<:AbstractConstraintsModel,
286+
})
287+
return true
288+
end
289+
290+
function is_autonomous(
291+
::Model{
292+
NonAutonomous,
293+
<:TimesModel,
294+
<:AbstractStateModel,
295+
<:AbstractControlModel,
296+
<:AbstractVariableModel,
297+
<:Function,
298+
<:AbstractObjectiveModel,
299+
<:AbstractConstraintsModel,
300+
})
301+
return false
302+
end
303+
268304
# State
269305
"""
270306
$(TYPEDSIGNATURES)
@@ -273,6 +309,7 @@ Get the state from the model.
273309
"""
274310
function state(
275311
ocp::Model{
312+
<:TimeDependence,
276313
<:TimesModel,
277314
T,
278315
<:AbstractControlModel,
@@ -320,6 +357,7 @@ Get the control from the model.
320357
"""
321358
function control(
322359
ocp::Model{
360+
<:TimeDependence,
323361
<:TimesModel,
324362
<:AbstractStateModel,
325363
T,
@@ -367,6 +405,7 @@ Get the variable from the model.
367405
"""
368406
function variable(
369407
ocp::Model{
408+
<:TimeDependence,
370409
<:TimesModel,
371410
<:AbstractStateModel,
372411
<:AbstractControlModel,
@@ -414,6 +453,7 @@ Get the times from the model.
414453
"""
415454
function times(
416455
ocp::Model{
456+
<:TimeDependence,
417457
T,
418458
<:AbstractStateModel,
419459
<:AbstractControlModel,
@@ -452,6 +492,7 @@ Get the initial time from the model, for a fixed initial time.
452492
"""
453493
function initial_time(
454494
ocp::Model{
495+
<:TimeDependence,
455496
<:TimesModel{FixedTimeModel{T},<:AbstractTimeModel},
456497
<:AbstractStateModel,
457498
<:AbstractControlModel,
@@ -471,6 +512,7 @@ Get the initial time from the model, for a free initial time.
471512
"""
472513
function initial_time(
473514
ocp::Model{
515+
<:TimeDependence,
474516
<:TimesModel{FreeTimeModel,<:AbstractTimeModel},
475517
<:AbstractStateModel,
476518
<:AbstractControlModel,
@@ -491,6 +533,7 @@ Get the initial time from the model, for a free initial time.
491533
"""
492534
function initial_time(
493535
ocp::Model{
536+
<:TimeDependence,
494537
<:TimesModel{FreeTimeModel,<:AbstractTimeModel},
495538
<:AbstractStateModel,
496539
<:AbstractControlModel,
@@ -555,6 +598,7 @@ Get the final time from the model, for a fixed final time.
555598
"""
556599
function final_time(
557600
ocp::Model{
601+
<:TimeDependence,
558602
<:TimesModel{<:AbstractTimeModel,FixedTimeModel{T}},
559603
<:AbstractStateModel,
560604
<:AbstractControlModel,
@@ -574,6 +618,7 @@ Get the final time from the model, for a free final time.
574618
"""
575619
function final_time(
576620
ocp::Model{
621+
<:TimeDependence,
577622
<:TimesModel{<:AbstractTimeModel,FreeTimeModel},
578623
<:AbstractStateModel,
579624
<:AbstractControlModel,
@@ -594,6 +639,7 @@ Get the final time from the model, for a free final time.
594639
"""
595640
function final_time(
596641
ocp::Model{
642+
<:TimeDependence,
597643
<:TimesModel{<:AbstractTimeModel,FreeTimeModel},
598644
<:AbstractStateModel,
599645
<:AbstractControlModel,
@@ -642,6 +688,7 @@ Get the objective from the model.
642688
"""
643689
function objective(
644690
ocp::Model{
691+
<:TimeDependence,
645692
<:AbstractTimesModel,
646693
<:AbstractStateModel,
647694
<:AbstractControlModel,
@@ -675,6 +722,7 @@ Get the Mayer cost from the model.
675722
"""
676723
function mayer(
677724
ocp::Model{
725+
<:TimeDependence,
678726
<:AbstractTimesModel,
679727
<:AbstractStateModel,
680728
<:AbstractControlModel,
@@ -694,6 +742,7 @@ Get the Mayer cost from the model.
694742
"""
695743
function mayer(
696744
ocp::Model{
745+
<:TimeDependence,
697746
<:AbstractTimesModel,
698747
<:AbstractStateModel,
699748
<:AbstractControlModel,
@@ -727,6 +776,7 @@ Get the Lagrange cost from the model.
727776
"""
728777
function lagrange(
729778
ocp::Model{
779+
<:TimeDependence,
730780
<:AbstractTimesModel,
731781
<:AbstractStateModel,
732782
<:AbstractControlModel,
@@ -746,6 +796,7 @@ Get the Lagrange cost from the model.
746796
"""
747797
function lagrange(
748798
ocp::Model{
799+
<:TimeDependence,
749800
<:AbstractTimesModel,
750801
<:AbstractStateModel,
751802
<:AbstractControlModel,
@@ -775,6 +826,7 @@ Get the dynamics from the model.
775826
"""
776827
function dynamics(
777828
ocp::Model{
829+
<:TimeDependence,
778830
<:AbstractTimesModel,
779831
<:AbstractStateModel,
780832
<:AbstractControlModel,
@@ -795,6 +847,7 @@ Get the constraints from the model.
795847
"""
796848
function constraints(
797849
ocp::Model{
850+
<:TimeDependence,
798851
<:AbstractTimesModel,
799852
<:AbstractStateModel,
800853
<:AbstractControlModel,
@@ -823,6 +876,7 @@ Get the nonlinear path constraints from the model.
823876
"""
824877
function path_constraints_nl(
825878
ocp::Model{
879+
<:TimeDependence,
826880
<:TimesModel,
827881
<:AbstractStateModel,
828882
<:AbstractControlModel,
@@ -842,6 +896,7 @@ Get the nonlinear boundary constraints from the model.
842896
"""
843897
function boundary_constraints_nl(
844898
ocp::Model{
899+
<:TimeDependence,
845900
<:TimesModel,
846901
<:AbstractStateModel,
847902
<:AbstractControlModel,
@@ -861,6 +916,7 @@ Get the box constraints on state from the model.
861916
"""
862917
function state_constraints_box(
863918
ocp::Model{
919+
<:TimeDependence,
864920
<:TimesModel,
865921
<:AbstractStateModel,
866922
<:AbstractControlModel,
@@ -880,6 +936,7 @@ Get the box constraints on control from the model.
880936
"""
881937
function control_constraints_box(
882938
ocp::Model{
939+
<:TimeDependence,
883940
<:TimesModel,
884941
<:AbstractStateModel,
885942
<:AbstractControlModel,
@@ -899,6 +956,7 @@ Get the box constraints on variable from the model.
899956
"""
900957
function variable_constraints_box(
901958
ocp::Model{
959+
<:TimeDependence,
902960
<:TimesModel,
903961
<:AbstractStateModel,
904962
<:AbstractControlModel,

src/print.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::Model)
9191
vi_names = variable_components(ocp)
9292

9393
# dependencies
94-
t_ = t_name * ", "
94+
t_ = !is_autonomous(ocp) ? t_name * ", " : ""
9595
_v = is_variable_dependent ? ", " * v_name : ""
9696

9797
# other names
@@ -104,6 +104,9 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::Model)
104104
#
105105
some_printing && println(io)
106106
printstyled(io, "The "; bold=true)
107+
!is_autonomous(ocp) ?
108+
printstyled(io, "(non autonomous) ", bold = true) :
109+
printstyled(io, "(autonomous) ", bold = true)
107110
printstyled(io, "optimal control problem is of the form:\n"; bold=true)
108111
println(io)
109112

@@ -361,7 +364,7 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::PreModel)
361364
vi_names = components(ocp.variable)
362365

363366
# dependencies
364-
t_ = t_name * ", "
367+
t_ = !is_autonomous(ocp) ? t_name * ", " : ""
365368
_v = is_variable_dependent ? ", " * v_name : ""
366369

367370
# other names
@@ -376,6 +379,9 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::PreModel)
376379
#
377380
some_printing && println(io)
378381
printstyled(io, "The "; bold=true)
382+
!is_autonomous(ocp) ?
383+
printstyled(io, "(non autonomous) ", bold = true) :
384+
printstyled(io, "(autonomous) ", bold = true)
379385
printstyled(io, "optimal control problem is of the form:\n"; bold=true)
380386
println(io)
381387

src/state.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ function state!(
7373
return nothing
7474
end
7575

76-
7776
# ------------------------------------------------------------------------------ #
7877
# GETTERS
7978
# ------------------------------------------------------------------------------ #

src/time_dependence.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function time_dependence!(
2+
ocp::PreModel;
3+
autonomous::Bool
4+
)::Nothing
5+
6+
# checkings
7+
@ensure !__is_autonomous_set(ocp) CTBase.UnauthorizedCall("the time dependence has already been set.")
8+
9+
# set the state
10+
ocp.autonomous = autonomous
11+
12+
return nothing
13+
end
14+
15+
is_autonomous(ocp::PreModel) = ocp.autonomous

0 commit comments

Comments
 (0)