Skip to content

Commit

Permalink
Merge pull request #55 from control-toolbox/auto-juliaformatter-pr
Browse files Browse the repository at this point in the history
[AUTO] JuliaFormatter.jl run
  • Loading branch information
ocots authored Aug 26, 2024
2 parents eeab964 + 9c5fbed commit 8c64dc3
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 208 deletions.
2 changes: 1 addition & 1 deletion ext/CTFlowsODE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const DCoTangent = ctVector
const ctgradient = CTBase.__ctgradient

# types
abstract type AbstractFlow{D,U} end
abstract type AbstractFlow{D, U} end

# from CTFlows
const __create_hamiltonian = CTFlows.__create_hamiltonian
Expand Down
31 changes: 9 additions & 22 deletions ext/concatenation.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
function __concat_rhs(
F::AbstractFlow{D,U},
G::AbstractFlow{D,U},
t_switch::Time,
) where {D,U}
function __concat_rhs(F::AbstractFlow{D, U}, G::AbstractFlow{D, U}, t_switch::Time) where {D, U}
function rhs!(du::D, u::U, p::Variable, t::Time)
t < t_switch ? F.rhs!(du, u, p, t) : G.rhs!(du, u, p, t)
end
return rhs!
end

function __concat_rhs(F::VectorFieldFlow, G::VectorFieldFlow, t_switch::Time)
return (x::State, v::Variable, t::Time) ->
(t < t_switch ? F.rhs(x, v, t) : G.rhs(x, v, t))
return (x::State, v::Variable, t::Time) -> (t < t_switch ? F.rhs(x, v, t) : G.rhs(x, v, t))
end

function __concat_rhs(F::ODEFlow, G::ODEFlow, t_switch::Time)
Expand All @@ -37,7 +32,7 @@ end
function __concat_jumps(
F::AbstractFlow,
G::AbstractFlow,
jump::Union{Nothing,Tuple{Time,Any}} = nothing,
jump::Union{Nothing, Tuple{Time, Any}} = nothing,
)
jumps = F.jumps
append!(jumps, G.jumps)
Expand All @@ -47,50 +42,42 @@ end

# --------------------------------------------------------------------------------------------
# concatenate two flows with a prescribed switching time
function concatenate(F::TF, g::Tuple{ctNumber,TF})::TF where {TF<:AbstractFlow}

function concatenate(F::TF, g::Tuple{ctNumber, TF})::TF where {TF <: AbstractFlow}
t_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
jumps = __concat_jumps(F, G) # jumps
return TF(F.f, rhs!, tstops, jumps) # we choose default values and options of F

end

function concatenate(F::TF, g::Tuple{ctNumber,Any,TF})::TF where {TF<:AbstractFlow}

function concatenate(F::TF, g::Tuple{ctNumber, Any, TF})::TF where {TF <: AbstractFlow}
t_switch, η_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
jumps = __concat_jumps(F, G, (t_switch, η_switch)) # jumps
return TF(F.f, rhs!, tstops, jumps) # we choose default values and options of F

end

# --------------------------------------------------------------------------------------------
# concatenate two flows with a prescribed switching time
function concatenate(F::TF, g::Tuple{ctNumber,TF})::TF where {TF<:OptimalControlFlow}

function concatenate(F::TF, g::Tuple{ctNumber, TF})::TF where {TF <: OptimalControlFlow}
t_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
feedback_u = __concat_feedback_control(F, G, t_switch) # concatenation of the feedback control
jumps = __concat_jumps(F, G) # jumps
return OptimalControlFlow(F.f, rhs!, feedback_u, F.ocp, F.kwargs_Flow, tstops, jumps) # we choose default values and options of F

end

function concatenate(F::TF, g::Tuple{ctNumber,Any,TF})::TF where {TF<:OptimalControlFlow}

function concatenate(F::TF, g::Tuple{ctNumber, Any, TF})::TF where {TF <: OptimalControlFlow}
t_switch, η_switch, G = g
rhs! = __concat_rhs(F, G, t_switch) # concatenation of the right and sides
tstops = __concat_tstops(F, G, t_switch) # times to break integration
feedback_u = __concat_feedback_control(F, G, t_switch) # concatenation of the feedback control
jumps = __concat_jumps(F, G, (t_switch, η_switch)) # jumps
return OptimalControlFlow(F.f, rhs!, feedback_u, F.ocp, F.kwargs_Flow, tstops, jumps) # we choose default values and options of F

end

# --------------------------------------------------------------------------------------------
*(F::TF, g::Tuple{ctNumber,TF}) where {TF<:AbstractFlow} = concatenate(F, g)
*(F::TF, g::Tuple{ctNumber,Any,TF}) where {TF<:AbstractFlow} = concatenate(F, g)
*(F::TF, g::Tuple{ctNumber, TF}) where {TF <: AbstractFlow} = concatenate(F, g)
*(F::TF, g::Tuple{ctNumber, Any, TF}) where {TF <: AbstractFlow} = concatenate(F, g)
3 changes: 1 addition & 2 deletions ext/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function ode_usage(alg, abstol, reltol, saveat; kwargs_Flow...)

# kwargs has priority wrt kwargs_flow
function f(
tspan::Tuple{Time,Time},
tspan::Tuple{Time, Time},
x0,
v = nothing;
jumps,
Expand Down Expand Up @@ -48,7 +48,6 @@ function ode_usage(alg, abstol, reltol, saveat; kwargs_Flow...)
end

return f

end

# --------------------------------------------------------------------------------------------
Expand Down
11 changes: 4 additions & 7 deletions ext/hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ $(TYPEDSIGNATURES)
Returns a function that solves ODE problem associated to Hamiltonian vector field.
"""
function hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...)

function f(
tspan::Tuple{Time,Time},
tspan::Tuple{Time, Time},
x0::State,
p0::Costate,
v::Variable = __variable(x0, p0);
Expand All @@ -24,8 +23,7 @@ function hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...)

# jumps and callbacks
n = size(x0, 1)
cb, t_stops_all =
__callbacks(callback, jumps, rg(n + 1, 2n), _t_stops_interne, tstops)
cb, t_stops_all = __callbacks(callback, jumps, rg(n + 1, 2n), _t_stops_interne, tstops)

# solve
sol = OrdinaryDiffEq.solve(
Expand Down Expand Up @@ -57,7 +55,6 @@ function hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...)
end

return f

end

"""
Expand All @@ -70,8 +67,8 @@ function rhs(h::AbstractHamiltonian)
n = size(z, 1) ÷ 2
foo(z) = h(t, z[rg(1, n)], z[rg(n + 1, 2n)], v)
dh = ctgradient(foo, z)
dz[1:n] = dh[n+1:2n]
dz[n+1:2n] = -dh[1:n]
dz[1:n] = dh[(n + 1):(2n)]
dz[(n + 1):(2n)] = -dh[1:n]
end
return rhs!
end
Expand Down
29 changes: 11 additions & 18 deletions ext/optimal_control_problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ julia> f = Flow(ocp, (x, p) -> p)
The dimension of the output of the control function must be consistent with the dimension usage of the control of the optimal control problem.
"""
function CTFlows.Flow(
ocp::OptimalControlModel{T,V},
u_::Union{Function,ControlLaw{T,V}};
ocp::OptimalControlModel{T, V},
u_::Union{Function, ControlLaw{T, V}};
alg = __alg(),
abstol = __abstol(),
reltol = __reltol(),
saveat = __saveat(),
kwargs_Flow...,
) where {T,V}

) where {T, V}
h, u = __create_hamiltonian(ocp, u_) # construction of the Hamiltonian
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)

end

# ---------------------------------------------------------------------------------------------------
Expand All @@ -48,38 +46,33 @@ julia> f = Flow(ocp, (t, x, p) -> p[1], (t, x, u) -> x[1] - 1, (t, x, p) -> x[1]
The dimension of the output of the control function must be consistent with the dimension usage of the control of the optimal control problem.
"""
function CTFlows.Flow(
ocp::OptimalControlModel{T,V},
u_::Union{Function,ControlLaw{T,V},FeedbackControl{T,V}},
g_::Union{Function,MixedConstraint{T,V},StateConstraint{T,V}},
μ_::Union{Function,Multiplier{T,V}};
ocp::OptimalControlModel{T, V},
u_::Union{Function, ControlLaw{T, V}, FeedbackControl{T, V}},
g_::Union{Function, MixedConstraint{T, V}, StateConstraint{T, V}},
μ_::Union{Function, Multiplier{T, V}};
alg = __alg(),
abstol = __abstol(),
reltol = __reltol(),
saveat = __saveat(),
kwargs_Flow...,
) where {T,V}

) where {T, V}
h, u = __create_hamiltonian(ocp, u_, g_, μ_) # construction of the Hamiltonian
return __ocp_Flow(ocp, h, u, alg, abstol, reltol, saveat; kwargs_Flow...)

end

# ---------------------------------------------------------------------------------------------------
function __ocp_Flow(
ocp::OptimalControlModel{T,V},
ocp::OptimalControlModel{T, V},
h::Hamiltonian,
u::ControlLaw,
alg,
abstol,
reltol,
saveat;
kwargs_Flow...,
) where {T,V}

) where {T, V}
rhs! = rhs(h) # right and side: same as for a flow from a Hamiltonian
f = hamiltonian_usage(alg, abstol, reltol, saveat; kwargs_Flow...) # flow function
kwargs_Flow =
(kwargs_Flow..., alg = alg, abstol = abstol, reltol = reltol, saveat = saveat)
kwargs_Flow = (kwargs_Flow..., alg = alg, abstol = abstol, reltol = reltol, saveat = saveat)
return OptimalControlFlow(f, rhs!, u, ocp, kwargs_Flow)

end
Loading

0 comments on commit 8c64dc3

Please sign in to comment.