Skip to content

Commit 5396c17

Browse files
authored
Merge pull request #158 from control-toolbox/157-dev-adding-f_exa-when-building
adding f_exa field
2 parents 1c15203 + 2c27d14 commit 5396c17

12 files changed

Lines changed: 145 additions & 72 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CTModels"
22
uuid = "34c4fa32-2049-4079-8329-de33c2a22e2d"
33
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
4-
version = "0.4.2"
4+
version = "0.4.3"
55

66
[deps]
77
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"

profiling/boundary.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ begin
4242
pre_ocp, :control; rg=1:2, lb=[0, 0], ub=[Inf, Inf], label=:control_rg
4343
)
4444
CTModels.definition!(pre_ocp, Expr(:simple_integrator_min_energy))
45-
ocp = CTModels.build_model(pre_ocp)
45+
ocp = CTModels.build(pre_ocp)
4646
return ocp, N
4747
end
4848

src/CTModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ include("dynamics.jl")
270270
include("objective.jl")
271271
include("constraints.jl")
272272
include("time_dependence.jl")
273+
include("definition.jl")
273274
include("print.jl")
274275
include("model.jl")
275276
include("solution.jl")

src/definition.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# ------------------------------------------------------------------------------ #
2+
# SETTER
3+
# ------------------------------------------------------------------------------ #
4+
5+
"""
6+
$(TYPEDSIGNATURES)
7+
8+
Set the model definition of the optimal control problem.
9+
10+
"""
11+
function definition!(ocp::PreModel, definition::Expr)::Nothing
12+
ocp.definition = definition
13+
return nothing
14+
end
15+
16+
# ------------------------------------------------------------------------------ #
17+
# GETTERS
18+
# ------------------------------------------------------------------------------ #
19+
20+
"""
21+
$(TYPEDSIGNATURES)
22+
23+
Return the model definition of the optimal control problem.
24+
25+
"""
26+
function definition(ocp::Model)::Expr
27+
return ocp.definition
28+
end
29+
30+
"""
31+
$(TYPEDSIGNATURES)
32+
33+
Return the model definition of the optimal control problem or `nothing`.
34+
35+
"""
36+
function definition(ocp::PreModel)
37+
return ocp.definition
38+
end

src/model.jl

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ constraints = OrderedDict(
4545
:c1 => (:path, f1, [0.0], [1.0]),
4646
:c2 => (:state, 1:2, [-1.0, -1.0], [1.0, 1.0])
4747
)
48-
model = build_constraints(constraints)
48+
model = build(constraints)
4949
```
5050
"""
51-
function build_constraints(constraints::ConstraintsDictType)::ConstraintsModel
51+
function build(constraints::ConstraintsDictType)::ConstraintsModel
5252
LocalNumber = Float64
5353

5454
path_cons_nl_f = Vector{Function}() # nonlinear path constraints
@@ -240,7 +240,7 @@ end
240240
"""
241241
$(TYPEDSIGNATURES)
242242
243-
Builds a concrete `Model` from a `PreModel`.
243+
Converts a mutable `PreModel` into an immutable `Model`.
244244
245245
This function finalizes a pre-defined optimal control problem (`PreModel`) by verifying that all necessary components (times, state, control, dynamics) are set. It then constructs a `Model` instance, incorporating optional components like objective and constraints if they are defined.
246246
@@ -257,10 +257,10 @@ times!(pre_ocp, 0.0, 1.0, 100)
257257
state!(pre_ocp, 2, "x", ["x1", "x2"])
258258
control!(pre_ocp, 1, "u", ["u1"])
259259
dynamics!(pre_ocp, (dx, t, x, u, v) -> dx .= x + u)
260-
model = build_model(pre_ocp)
260+
model = build(pre_ocp)
261261
```
262262
"""
263-
function build_model(pre_ocp::PreModel)::Model
263+
function build(pre_ocp::PreModel; build_examodel = nothing)::Model
264264
@ensure __is_times_set(pre_ocp) CTBase.UnauthorizedCall(
265265
"the times must be set before building the model."
266266
)
@@ -297,13 +297,13 @@ function build_model(pre_ocp::PreModel)::Model
297297
__build_dynamics_from_parts(pre_ocp.dynamics)
298298
end
299299
objective = pre_ocp.objective
300-
constraints = build_constraints(pre_ocp.constraints)
300+
constraints = build(pre_ocp.constraints)
301301
definition = pre_ocp.definition
302302
TD = is_autonomous(pre_ocp) ? Autonomous : NonAutonomous
303303

304304
# create the model
305305
model = Model{TD}(
306-
times, state, control, variable, dynamics, objective, constraints, definition
306+
times, state, control, variable, dynamics, objective, constraints, definition, build_examodel
307307
)
308308

309309
return model
@@ -329,6 +329,7 @@ function is_autonomous(
329329
<:Function,
330330
<:AbstractObjectiveModel,
331331
<:AbstractConstraintsModel,
332+
<:Union{Function,Nothing},
332333
},
333334
)
334335
return true
@@ -344,6 +345,7 @@ function is_autonomous(
344345
<:Function,
345346
<:AbstractObjectiveModel,
346347
<:AbstractConstraintsModel,
348+
<:Union{Function,Nothing},
347349
},
348350
)
349351
return false
@@ -365,6 +367,7 @@ function state(
365367
<:Function,
366368
<:AbstractObjectiveModel,
367369
<:AbstractConstraintsModel,
370+
<:Union{Function,Nothing},
368371
},
369372
)::T where {T<:AbstractStateModel}
370373
return ocp.state
@@ -413,6 +416,7 @@ function control(
413416
<:Function,
414417
<:AbstractObjectiveModel,
415418
<:AbstractConstraintsModel,
419+
<:Union{Function,Nothing},
416420
},
417421
)::T where {T<:AbstractControlModel}
418422
return ocp.control
@@ -461,6 +465,7 @@ function variable(
461465
<:Function,
462466
<:AbstractObjectiveModel,
463467
<:AbstractConstraintsModel,
468+
<:Union{Function,Nothing},
464469
},
465470
)::T where {T<:AbstractVariableModel}
466471
return ocp.variable
@@ -509,6 +514,7 @@ function times(
509514
<:Function,
510515
<:AbstractObjectiveModel,
511516
<:AbstractConstraintsModel,
517+
<:Union{Function,Nothing},
512518
},
513519
)::T where {T<:TimesModel}
514520
return ocp.times
@@ -548,6 +554,7 @@ function initial_time(
548554
<:Function,
549555
<:AbstractObjectiveModel,
550556
<:AbstractConstraintsModel,
557+
<:Union{Function,Nothing},
551558
},
552559
)::T where {T<:Time}
553560
return initial_time(times(ocp))
@@ -568,6 +575,7 @@ function initial_time(
568575
<:Function,
569576
<:AbstractObjectiveModel,
570577
<:AbstractConstraintsModel,
578+
<:Union{Function,Nothing},
571579
},
572580
variable::AbstractVector{T},
573581
)::T where {T<:ctNumber}
@@ -589,6 +597,7 @@ function initial_time(
589597
<:Function,
590598
<:AbstractObjectiveModel,
591599
<:AbstractConstraintsModel,
600+
<:Union{Function,Nothing},
592601
},
593602
variable::T,
594603
)::T where {T<:ctNumber}
@@ -654,6 +663,7 @@ function final_time(
654663
<:Function,
655664
<:AbstractObjectiveModel,
656665
<:AbstractConstraintsModel,
666+
<:Union{Function,Nothing},
657667
},
658668
)::T where {T<:Time}
659669
return final_time(times(ocp))
@@ -674,6 +684,7 @@ function final_time(
674684
<:Function,
675685
<:AbstractObjectiveModel,
676686
<:AbstractConstraintsModel,
687+
<:Union{Function,Nothing},
677688
},
678689
variable::AbstractVector{T},
679690
)::T where {T<:ctNumber}
@@ -695,6 +706,7 @@ function final_time(
695706
<:Function,
696707
<:AbstractObjectiveModel,
697708
<:AbstractConstraintsModel,
709+
<:Union{Function,Nothing},
698710
},
699711
variable::T,
700712
)::T where {T<:ctNumber}
@@ -744,6 +756,7 @@ function objective(
744756
<:Function,
745757
O,
746758
<:AbstractConstraintsModel,
759+
<:Union{Function,Nothing},
747760
},
748761
)::O where {O<:AbstractObjectiveModel}
749762
return ocp.objective
@@ -778,6 +791,7 @@ function mayer(
778791
<:Function,
779792
<:MayerObjectiveModel{M},
780793
<:AbstractConstraintsModel,
794+
<:Union{Function,Nothing},
781795
},
782796
)::M where {M<:Function}
783797
return mayer(objective(ocp))
@@ -798,6 +812,7 @@ function mayer(
798812
<:Function,
799813
<:BolzaObjectiveModel{M,<:Function},
800814
<:AbstractConstraintsModel,
815+
<:Union{Function,Nothing},
801816
},
802817
)::M where {M<:Function}
803818
return mayer(objective(ocp))
@@ -832,6 +847,7 @@ function lagrange(
832847
<:Function,
833848
LagrangeObjectiveModel{L},
834849
<:AbstractConstraintsModel,
850+
<:Union{Function,Nothing},
835851
},
836852
)::L where {L<:Function}
837853
return lagrange(objective(ocp))
@@ -852,6 +868,7 @@ function lagrange(
852868
<:Function,
853869
<:BolzaObjectiveModel{<:Function,L},
854870
<:AbstractConstraintsModel,
871+
<:Union{Function,Nothing},
855872
},
856873
)::L where {L<:Function}
857874
return lagrange(objective(ocp))
@@ -882,11 +899,55 @@ function dynamics(
882899
D,
883900
<:AbstractObjectiveModel,
884901
<:AbstractConstraintsModel,
902+
<:Union{Function,Nothing},
885903
},
886904
)::D where {D<:Function}
887905
return ocp.dynamics
888906
end
889907

908+
# build_examodel
909+
"""
910+
$(TYPEDSIGNATURES)
911+
912+
Get the build_examodel from the model.
913+
"""
914+
function get_build_examodel(
915+
ocp::Model{
916+
<:TimeDependence,
917+
<:AbstractTimesModel,
918+
<:AbstractStateModel,
919+
<:AbstractControlModel,
920+
<:AbstractVariableModel,
921+
<:Function,
922+
<:AbstractObjectiveModel,
923+
<:AbstractConstraintsModel,
924+
<:Function,
925+
},
926+
)
927+
return ocp.build_examodel
928+
end
929+
930+
"""
931+
$(TYPEDSIGNATURES)
932+
933+
Return an error (UnauthorizedCall) since the model is not built with the :exa backend.
934+
"""
935+
function get_build_examodel(
936+
::Model{
937+
<:TimeDependence,
938+
<:AbstractTimesModel,
939+
<:AbstractStateModel,
940+
<:AbstractControlModel,
941+
<:AbstractVariableModel,
942+
<:Function,
943+
<:AbstractObjectiveModel,
944+
<:AbstractConstraintsModel,
945+
<:Nothing,
946+
},
947+
)
948+
throw(CTBase.UnauthorizedCall("first parse with :exa backend"))
949+
end
950+
890951
# Constraints
891952
"""
892953
$(TYPEDSIGNATURES)
@@ -903,6 +964,7 @@ function constraints(
903964
<:Function,
904965
<:AbstractObjectiveModel,
905966
C,
967+
<:Union{Function,Nothing},
906968
},
907969
)::C where {C<:AbstractConstraintsModel}
908970
return ocp.constraints
@@ -932,6 +994,7 @@ function path_constraints_nl(
932994
<:Function,
933995
<:AbstractObjectiveModel,
934996
<:ConstraintsModel{TP,<:Tuple,<:Tuple,<:Tuple,<:Tuple},
997+
<:Union{Function,Nothing},
935998
},
936999
)::TP where {TP<:Tuple}
9371000
return constraints(ocp).path_nl
@@ -952,6 +1015,7 @@ function boundary_constraints_nl(
9521015
<:Function,
9531016
<:AbstractObjectiveModel,
9541017
<:ConstraintsModel{<:Tuple,TB,<:Tuple,<:Tuple,<:Tuple},
1018+
<:Union{Function,Nothing},
9551019
},
9561020
)::TB where {TB<:Tuple}
9571021
return constraints(ocp).boundary_nl
@@ -972,6 +1036,7 @@ function state_constraints_box(
9721036
<:Function,
9731037
<:AbstractObjectiveModel,
9741038
<:ConstraintsModel{<:Tuple,<:Tuple,TS,<:Tuple,<:Tuple},
1039+
<:Union{Function,Nothing},
9751040
},
9761041
)::TS where {TS<:Tuple}
9771042
return constraints(ocp).state_box
@@ -992,6 +1057,7 @@ function control_constraints_box(
9921057
<:Function,
9931058
<:AbstractObjectiveModel,
9941059
<:ConstraintsModel{<:Tuple,<:Tuple,<:Tuple,TC,<:Tuple},
1060+
<:Union{Function,Nothing},
9951061
},
9961062
)::TC where {TC<:Tuple}
9971063
return constraints(ocp).control_box
@@ -1012,6 +1078,7 @@ function variable_constraints_box(
10121078
<:Function,
10131079
<:AbstractObjectiveModel,
10141080
<:ConstraintsModel{<:Tuple,<:Tuple,<:Tuple,<:Tuple,TV},
1081+
<:Union{Function,Nothing},
10151082
},
10161083
)::TV where {TV<:Tuple}
10171084
return constraints(ocp).variable_box

0 commit comments

Comments
 (0)