Skip to content

Commit

Permalink
Merge pull request #131 from control-toolbox/init_CTDirect
Browse files Browse the repository at this point in the history
v0.8.0
  • Loading branch information
PierreMartinon authored May 3, 2024
2 parents 3bf56b8 + d90070e commit 0b74099
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CTBase"
uuid = "54762871-cc72-4466-b8e8-f6c8b58076cd"
authors = ["Olivier Cots <[email protected]>"]
version = "0.7.12"
version = "0.8.0"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
5 changes: 5 additions & 0 deletions src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ include("ctparser_utils.jl")
##include("ctparser.jl")
include("onepass.jl")
include("repl.jl")
#
include("init.jl")

# numeric types
export ctNumber, ctVector, Time, Times, TimesDisc
Expand Down Expand Up @@ -153,6 +155,9 @@ export nlp_constraints, constraints_labels
export OptimalControlSolution
export plot, plot!

# initialization
export OCPInit

# utils
export ctgradient, ctjacobian, ctinterpolate, ctindices, ctupperscripts

Expand Down
69 changes: 69 additions & 0 deletions src/init.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
$(TYPEDSIGNATURES)
Initialization of the OCP solution that can be used when solving the discretized problem DOCP.
# Constructors:
- `OCPInit()`: default initialization
- `OCPInit(x_init, u_init, v_init)`: constant vector and/or function handles
- `OCPInit(sol)`: from existing solution
# Examples
```julia-repl
julia> init = OCPInit()
julia> init = OCPInit(x_init=[0.1, 0.2], u_init=0.3)
julia> init = OCPInit(x_init=[0.1, 0.2], u_init=0.3, v_init=0.5)
julia> init = OCPInit(x_init=[0.1, 0.2], u_init=t->sin(t), v_init=0.5)
julia> init = OCPInit(sol)
```
"""
mutable struct OCPInit

state_init::Function
control_init::Function
variable_init::Union{Nothing, ctVector}
costate_init::Function
multipliers_init::Union{Nothing, ctVector}
info::Symbol

# warm start from solution
function OCPInit(sol::OptimalControlSolution)

init = new()
init.info = :from_solution
init.state_init = t -> sol.state(t)
init.control_init = t -> sol.control(t)
init.variable_init = sol.variable
#+++ add costate and scalar multipliers

return init
end

# constant / functional init with explicit arguments
function OCPInit(; state::Union{Nothing, ctVector, Function}=nothing, control::Union{Nothing, ctVector, Function}=nothing, variable::Union{Nothing, ctVector}=nothing)

init = new()
init.info = :constant_or_function
init.state_init = (state isa Function) ? t -> state(t) : t -> state
init.control_init = (control isa Function) ? t -> control(t) : t -> control
init.variable_init = variable
#+++ add costate and scalar multipliers

return init
end

# version with arguments as collection/iterable
# (may be fused with version above ?)
function OCPInit(init)

x_init = :state keys(init) ? init[:state] : nothing
u_init = :control keys(init) ? init[:control] : nothing
v_init = :variable keys(init) ? init[:variable] : nothing
return OCPInit(state=x_init, control=u_init, variable=v_init)

end

end

0 comments on commit 0b74099

Please sign in to comment.