Skip to content

Commit

Permalink
propagate ctbase v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Cots committed Mar 21, 2023
1 parent f6f56fb commit 291d949
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 34 deletions.
12 changes: 6 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name = "CTDirectShooting"
uuid = "e0e6c04b-5022-4cd2-bea2-4a09fff39444"
authors = ["Olivier Cots <olivier.cots@enseeiht.fr>"]
version = "0.1.3"
version = "0.1.4"

[deps]
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"
CTFlows = "1c39547c-7794-42f7-af83-d98194f657c2"
CTOptimization = "22f08de8-270f-4470-8fba-397dbc90d8e0"
HamiltonianFlows = "5fb78580-10a0-4606-82bc-07a60f425ab3"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[compat]
CTBase = "0.4"
CTBase = "0.5"
CTFlows = "0.2"
CTOptimization = "0.1"
HamiltonianFlows = "1.0"
LinearAlgebra = "1.8"
Plots = "1.38"
MLStyle = "0.4"
Printf = "1.8"
julia = "1.8"
15 changes: 4 additions & 11 deletions src/CTDirectShooting.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
module CTDirectShooting

# using
#
using CTBase

#
using CTFlows
using CTOptimization
using LinearAlgebra # for the norm for instance
using MLStyle
using Printf # to print iterations results for instance

# flows
using HamiltonianFlows

# nlp solvers
using CTOptimization
#import CTOptimization: solve #todo: remove this

# Other declarations
const nlp_constraints = CTBase.nlp_constraints
const __grid_size_direct_shooting = CTBase.__grid_size_direct_shooting
Expand All @@ -29,7 +22,7 @@ const __stagnationTolerance = CTBase.__stagnationTolerance
const expand = CTBase.expand
const vec2vec = CTBase.vec2vec

# includes
#
include("utils.jl")
include("init.jl")
include("problem.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ end

#
function CTOptimizationInit(t0::Time, tf::Time, m::Dimension, S::OptimalControlSolution, grid, interp::Function)
return CTOptimizationInit(t0, tf, m, control(S), grid, interp)
return CTOptimizationInit(t0, tf, m, S.control, grid, interp)
end

#
Expand Down
2 changes: 1 addition & 1 deletion src/solution.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# ------------------------------------------------------------------------------------
# Direct shooting solution
#
Expand Down Expand Up @@ -75,6 +74,7 @@ function _OptimalControlSolution(ocp::OptimalControlModel, dssol::DirectShooting
sol.state_dimension = dssol.state_dimension
sol.control_dimension = dssol.control_dimension
sol.times = dssol.T
sol.time_label = ocp.time_label
sol.state = t -> x(t)
sol.state_labels = ocp.state_labels # update CTBase to have a getter
sol.adjoint = t -> p(t)
Expand Down
6 changes: 3 additions & 3 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function CTDirectShooting.solve(
end

# Init - need some parsing
t0 = initial_time(ocp)
tf = final_time(ocp)
m = control_dimension(ocp)
t0 = ocp.initial_time
tf = ocp.final_time
m = ocp.control_dimension
opti_init, grid = CTOptimizationInit(t0, tf, m, init, grid, init_interpolation)

# Problem
Expand Down
35 changes: 29 additions & 6 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,35 @@
function parse_ocp_direct_shooting(ocp::OptimalControlModel)

# parsing ocp
dy = dynamics(ocp)
co = lagrange(ocp)
cf = final_constraint(ocp)
x0 = initial_condition(ocp)
n = state_dimension(ocp)
m = control_dimension(ocp)
dy = ocp.dynamics
co = ocp.lagrange
n = ocp.state_dimension
m = ocp.control_dimension

# initial_condition and final_constraint
# x0 = ocp.initial_condition
# cf = ocp.final_constraint
x0 = nothing
cf = nothing
constraints = ocp.constraints
for (_, c) constraints
@match c begin
(:initial, _, f, lb, ub) => begin
if lb ub
error("direct shooting is implemented for problems with initial condition")
else
x0 = lb
end
end
(:final, _, f, lb, ub) => begin
if lb ub
error("direct shooting is implemented for problems with final equality constraint")
else
cf = x -> f(x) - lb
end
end
end # match
end # for

return dy, co, cf, x0, n, m

Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[deps]
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"
CTProblemLibrary = "0649932a-8c77-4f67-b1e4-c19ddd080280"
CTProblems = "45d9ea3f-a92f-411f-833f-222dd4fb9cd8"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CTDirectShooting
using Test
using CTBase
using CTProblemLibrary
using CTProblems

# CTDirectShooting
const CTOptimizationInit = CTDirectShooting.CTOptimizationInit
Expand Down
6 changes: 3 additions & 3 deletions test/test_CTOptimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ ocp = prob.model
u_sol(t) = prob.solution.control(t)[1]

#
t0 = initial_time(ocp)
tf = final_time(ocp)
m = control_dimension(ocp)
t0 = ocp.initial_time
tf = ocp.final_time
m = ocp.control_dimension

# --------------------------------------------------------------------------------------------------
#
Expand Down
2 changes: 1 addition & 1 deletion test/test_plot_manual.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using CTDirectShooting
using CTProblemLibrary
using CTProblems
using CTBase

prob = Problem(:integrator, :dim2, :energy); ocp = prob.model
Expand Down

0 comments on commit 291d949

Please sign in to comment.