Skip to content

Commit 7c734fd

Browse files
authored
Merge pull request #84 from control-toolbox/newmodel
Update with new CTModels and CTBase
2 parents 97d392c + 57807a9 commit 7c734fd

26 files changed

+3041
-231
lines changed

Project.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
name = "CTFlows"
22
uuid = "1c39547c-7794-42f7-af83-d98194f657c2"
33
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
4-
version = "0.7.0"
4+
version = "0.8.0"
55

66
[deps]
77
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"
8+
CTModels = "34c4fa32-2049-4079-8329-de33c2a22e2d"
89
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
10+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
911
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
12+
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1013

1114
[weakdeps]
1215
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
@@ -15,8 +18,11 @@ OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
1518
CTFlowsODE = "OrdinaryDiffEq"
1619

1720
[compat]
18-
CTBase = "0.14"
21+
CTBase = "0.15"
22+
CTModels = "0.2"
1923
DocStringExtensions = "0.9"
24+
ForwardDiff = "0.10"
2025
MLStyle = "0.4"
26+
MacroTools = "0.5"
2127
OrdinaryDiffEq = "6"
2228
julia = "1.10"

ext/CTFlowsODE.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ module CTFlowsODE
22

33
using CTBase
44
using CTFlows
5+
using CTModels
56
using OrdinaryDiffEq
67
using DocStringExtensions
78
using MLStyle
89
#
910
import CTFlows: Flow, CTFlows
1011
import Base: *
11-
import CTBase: OptimalControlSolution, CTBase
12+
import CTModels: Solution, CTModels
1213

1314
# --------------------------------------------------------------------------------------------------
1415
# Aliases
15-
const CoTangent = ctVector
16-
const DCoTangent = ctVector
17-
const ctgradient = CTBase.__ctgradient
16+
const CoTangent = CTFlows.ctVector
17+
const DCoTangent = CTFlows.ctVector
18+
const ctgradient = CTFlows.ctgradient
1819

1920
# types
2021
abstract type AbstractFlow{D,U} end
@@ -25,6 +26,10 @@ const __create_hamiltonian = CTFlows.__create_hamiltonian
2526
#
2627
rg(i::Int, j::Int) = i == j ? i : i:j
2728

29+
#
30+
const __autonomous = CTFlows.__autonomous
31+
const __variable = CTFlows.__variable
32+
2833
# --------------------------------------------------------------------------------------------#
2934
include("default.jl")
3035
include("types.jl")

ext/concatenation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function __concat_feedback_control(F::AbstractFlow, G::AbstractFlow, t_switch::T
3232
G.feedback_control(t, x, u, v)
3333
end
3434
end
35-
feedback_control = ControlLaw(_feedback_control, NonAutonomous, NonFixed)
35+
feedback_control = CTFlows.ControlLaw(_feedback_control, CTFlows.NonAutonomous, CTFlows.NonFixed)
3636
return feedback_control
3737
end
3838

ext/default.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ Default variable from ocp.
1717
"""
1818
function __variable(t0, x0, p0, tf, ocp)
1919
# if tf is free and ocp has only one variable, then return tf
20-
CTBase.has_free_final_time(ocp) && CTBase.variable_dimension(ocp) == 1 && return tf
20+
CTModels.has_free_final_time(ocp) && CTModels.variable_dimension(ocp) == 1 && return tf
2121

2222
# if t0 is free and ocp has only one variable, then return t0
23-
CTBase.has_free_initial_time(ocp) && CTBase.variable_dimension(ocp) == 1 && return t0
23+
CTModels.has_free_initial_time(ocp) && CTModels.variable_dimension(ocp) == 1 && return t0
2424

2525
# if t0 and tf are free and ocp has only two variables, then return [t0, tf]
26-
CTBase.has_free_final_time(ocp) &&
27-
CTBase.has_free_initial_time(ocp) &&
28-
CTBase.variable_dimension(ocp) == 2 &&
29-
return [t0, tf]
26+
CTModels.has_free_final_time(ocp) &&
27+
CTModels.has_free_initial_time(ocp) &&
28+
CTModels.variable_dimension(ocp) == 2 &&
29+
return [t0, tf]
3030

3131
# otherwise return an empty vector of right type to avoid warning performance message from OrdinaryDiffEq
3232
z0 = [x0; p0]

ext/function.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ end
5555
# --------------------------------------------------------------------------------------------
5656
function CTFlows.Flow(
5757
dyn::Function;
58-
autonomous=true,
59-
variable=false,
58+
autonomous=__autonomous(),
59+
variable=__variable(),
6060
alg=__alg(),
6161
abstol=__abstol(),
6262
reltol=__reltol(),

ext/hamiltonian.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ $(TYPEDSIGNATURES)
6464
6565
The right and side from a Hamiltonian.
6666
"""
67-
function rhs(h::AbstractHamiltonian)
67+
function rhs(h::CTFlows.AbstractHamiltonian)
6868
function rhs!(dz::DCoTangent, z::CoTangent, v::Variable, t::Time)
6969
n = size(z, 1) ÷ 2
7070
foo(z) = h(t, z[rg(1, n)], z[rg(n + 1, 2n)], v)
@@ -78,7 +78,7 @@ end
7878
# --------------------------------------------------------------------------------------------
7979
# Flow from a Hamiltonian
8080
function CTFlows.Flow(
81-
h::AbstractHamiltonian;
81+
h::CTFlows.AbstractHamiltonian;
8282
alg=__alg(),
8383
abstol=__abstol(),
8484
reltol=__reltol(),
@@ -94,7 +94,7 @@ end
9494
# --------------------------------------------------------------------------------------------
9595
# Flow from a Hamiltonian Vector Field
9696
function CTFlows.Flow(
97-
hv::HamiltonianVectorField;
97+
hv::CTFlows.HamiltonianVectorField;
9898
alg=__alg(),
9999
abstol=__abstol(),
100100
reltol=__reltol(),

ext/optimal_control_problem.jl

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,33 @@ julia> f = Flow(ocp, (x, p) -> p)
1515
The dimension of the output of the control function must be consistent with the dimension usage of the control of the optimal control problem.
1616
"""
1717
function CTFlows.Flow(
18-
ocp::OptimalControlModel{T,V},
19-
u_::Union{Function,ControlLaw{<:Function,T,V}};
18+
ocp::CTModels.Model,
19+
u_::CTFlows.ControlLaw;
2020
alg=__alg(),
2121
abstol=__abstol(),
2222
reltol=__reltol(),
2323
saveat=__saveat(),
2424
kwargs_Flow...,
25-
) where {T,V}
25+
)
2626
h, u = __create_hamiltonian(ocp, u_) # construction of the Hamiltonian
2727
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)
2828
end
2929

30+
function CTFlows.Flow(
31+
ocp::CTModels.Model,
32+
u_::Function;
33+
autonomous::Bool=__autonomous(),
34+
variable::Bool=__variable(ocp),
35+
alg=__alg(),
36+
abstol=__abstol(),
37+
reltol=__reltol(),
38+
saveat=__saveat(),
39+
kwargs_Flow...,
40+
)
41+
h, u = __create_hamiltonian(ocp, u_; autonomous=autonomous, variable=variable) # construction of the Hamiltonian
42+
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)
43+
end
44+
3045
# ---------------------------------------------------------------------------------------------------
3146
"""
3247
$(TYPEDSIGNATURES)
@@ -46,10 +61,10 @@ julia> f = Flow(ocp, (t, x, p) -> p[1], (t, x, u) -> x[1] - 1, (t, x, p) -> x[1]
4661
The dimension of the output of the control function must be consistent with the dimension usage of the control of the optimal control problem.
4762
"""
4863
function CTFlows.Flow(
49-
ocp::OptimalControlModel{T,V},
50-
u_::Union{Function,ControlLaw{<:Function,T,V},FeedbackControl{<:Function,T,V}},
51-
g_::Union{Function,MixedConstraint{<:Function,T,V},StateConstraint{<:Function,T,V}},
52-
μ_::Union{Function,Multiplier{<:Function,T,V}};
64+
ocp::CTModels.Model,
65+
u_::Union{CTFlows.ControlLaw{<:Function,T,V},CTFlows.FeedbackControl{<:Function,T,V}},
66+
g_::Union{CTFlows.MixedConstraint{<:Function,T,V},CTFlows.StateConstraint{<:Function,T,V}},
67+
μ_::Union{CTFlows.Multiplier{<:Function,T,V}};
5368
alg=__alg(),
5469
abstol=__abstol(),
5570
reltol=__reltol(),
@@ -60,17 +75,34 @@ function CTFlows.Flow(
6075
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)
6176
end
6277

78+
function CTFlows.Flow(
79+
ocp::CTModels.Model,
80+
u_::Function,
81+
g_::Function,
82+
μ_::Function;
83+
autonomous::Bool=__autonomous(),
84+
variable::Bool=__variable(ocp),
85+
alg=__alg(),
86+
abstol=__abstol(),
87+
reltol=__reltol(),
88+
saveat=__saveat(),
89+
kwargs_Flow...,
90+
)
91+
h, u = __create_hamiltonian(ocp, u_, g_, μ_; autonomous=autonomous, variable=variable) # construction of the Hamiltonian
92+
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)
93+
end
94+
6395
# ---------------------------------------------------------------------------------------------------
6496
function __ocp_Flow(
65-
ocp::OptimalControlModel{T,V},
66-
h::Hamiltonian,
67-
u::ControlLaw,
97+
ocp::CTModels.Model,
98+
h::CTFlows.Hamiltonian,
99+
u::CTFlows.ControlLaw,
68100
alg,
69101
abstol,
70102
reltol,
71103
saveat;
72104
kwargs_Flow...,
73-
) where {T,V}
105+
)
74106
rhs! = rhs(h) # right and side: same as for a flow from a Hamiltonian
75107
f = hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...) # flow function
76108
kwargs_Flow = (kwargs_Flow..., alg=alg, abstol=abstol, reltol=reltol, saveat=saveat)

0 commit comments

Comments
 (0)