Skip to content

Commit e72b249

Browse files
authored
Merge pull request #66 from control-toolbox/65-dev-builder-solution
add builder solution with functions
2 parents 0c2c1c2 + e360827 commit e72b249

6 files changed

Lines changed: 42 additions & 15 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.2.2"
4+
version = "0.2.3"
55

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

src/solution.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ Build a solution from the optimal control problem, the time grid, the state, con
3636
function build_solution(
3737
ocp::Model,
3838
T::Vector{Float64},
39-
X::Matrix{Float64},
40-
U::Matrix{Float64},
39+
X::TX,
40+
U::TU,
4141
v::Vector{Float64},
42-
P::Matrix{Float64};
42+
P::TP;
4343
objective::Float64,
4444
iterations::Int,
4545
constraints_violation::Float64,
@@ -56,7 +56,11 @@ function build_solution(
5656
control_constraints_ub_dual::Matrix{Float64}=zeros(0, 0),
5757
variable_constraints_lb_dual::Vector{Float64}=zeros(0),
5858
variable_constraints_ub_dual::Vector{Float64}=zeros(0),
59-
)
59+
) where {
60+
TX <: Union{Matrix{Float64}, Function},
61+
TU <: Union{Matrix{Float64}, Function},
62+
TP <: Union{Matrix{Float64}, Function},
63+
}
6064

6165
# get dimensions
6266
dim_x = state_dimension(ocp)
@@ -75,9 +79,10 @@ function build_solution(
7579
end
7680

7781
# variables: remove additional state for lagrange objective
78-
x = ctinterpolate(T, matrix2vec(X[:, 1:dim_x], 1))
79-
p = ctinterpolate(T[1:(end - 1)], matrix2vec(P[:, 1:dim_x], 1))
80-
u = ctinterpolate(T, matrix2vec(U[:, 1:dim_u], 1))
82+
x = TX <: Function ? X : ctinterpolate(T, matrix2vec(X[:, 1:dim_x], 1))
83+
p = TU <: Function ? P :
84+
length(T) == 2 ? t -> P[1, 1:dim_x] : ctinterpolate(T[1:(end - 1)], matrix2vec(P[:, 1:dim_x], 1))
85+
u = TP <: Function ? U : ctinterpolate(T, matrix2vec(U[:, 1:dim_u], 1))
8186

8287
# force scalar output when dimension is 1
8388
fx = (dim_x == 1) ? deepcopy(t -> x(t)[1]) : deepcopy(t -> x(t))

test/extras/plot_manual.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using CTModels
22
using Plots
33

4+
FUN=true
5+
46
# create a pre-model
57
pre_ocp = CTModels.PreModel()
68

@@ -66,21 +68,21 @@ function x(t)
6668
b + β * (t - t0) - α * (t - t0)^2 / 2.0,
6769
]
6870
end
69-
X = vcat([x(t)' for t in T]...)
71+
X = FUN ? x : vcat([x(t)' for t in T]...)
7072

7173
# costate: P Matrix{Float64}
7274
P = zeros(N, 2)
7375
function p(t)
7476
return [α, -α * (t - t0) + β]
7577
end
76-
P = vcat([p(t)' for t in T[1:(end - 1)]]...)
78+
P = FUN ? p : vcat([p(t)' for t in T[1:(end - 1)]]...)
7779

7880
# control: U Matrix{Float64}
7981
U = zeros(N, 1)
8082
function u(t)
8183
return [p(t)[2]]
8284
end
83-
U = vcat([u(t)' for t in T]...)
85+
U = FUN ? u : vcat([u(t)' for t in T]...)
8486

8587
# variable: v Vector{Float64}
8688
v = Float64[]

test/solution_example.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function solution_example()
1+
function solution_example(;fun=false)
22

33
# create a pre-model
44
pre_ocp = CTModels.PreModel()
@@ -84,21 +84,21 @@ function solution_example()
8484
b + β * (t - t0) - α * (t - t0)^2 / 2.0,
8585
]
8686
end
87-
X = vcat([x(t)' for t in T]...)
87+
X = fun ? x : vcat([x(t)' for t in T]...)
8888

8989
# costate: P Matrix{Float64}
9090
P = zeros(N, 2)
9191
function p(t)
9292
return [α, -α * (t - t0) + β]
9393
end
94-
P = vcat([p(t)' for t in T[1:(end - 1)]]...)
94+
P = fun ? p : vcat([p(t)' for t in T[1:(end - 1)]]...)
9595

9696
# control: U Matrix{Float64}
9797
U = zeros(N, 1)
9898
function u(t)
9999
return [p(t)[2]]
100100
end
101-
U = vcat([u(t)' for t in T]...)
101+
U = fun ? u : vcat([u(t)' for t in T]...)
102102

103103
# variable: v Vector{Float64}
104104
v = Float64[]

test/solution_test.jld2

-81.6 KB
Binary file not shown.

test/test_export_import.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
function test_export_import()
2+
3+
#
24
ocp, sol = solution_example()
35

46
# JSON
@@ -14,4 +16,22 @@ function test_export_import()
1416
ocp; filename_prefix="solution_test", format=:JLD
1517
)
1618
@test sol.objective == sol_reloaded.objective
19+
20+
#
21+
ocp, sol = solution_example(fun=true)
22+
23+
# JSON
24+
CTModels.export_ocp_solution(sol; filename_prefix="solution_test", format=:JSON)
25+
sol_reloaded = CTModels.import_ocp_solution(
26+
ocp; filename_prefix="solution_test", format=:JSON
27+
)
28+
@test sol.objective == sol_reloaded.objective
29+
30+
# JLD
31+
CTModels.export_ocp_solution(sol; filename_prefix="solution_test", format=:JLD)
32+
sol_reloaded = CTModels.import_ocp_solution(
33+
ocp; filename_prefix="solution_test", format=:JLD
34+
)
35+
@test sol.objective == sol_reloaded.objective
36+
1737
end

0 commit comments

Comments
 (0)