Skip to content

Commit e0b85bf

Browse files
committed
fix json
1 parent 03b15fc commit e0b85bf

File tree

10 files changed

+4364
-1240
lines changed

10 files changed

+4364
-1240
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
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 <[email protected]>"]
4-
version = "0.3.4"
4+
version = "0.3.5"
55

66
[deps]
77
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"
@@ -39,4 +39,4 @@ Parameters = "0.12"
3939
Plots = "1.40"
4040
PrettyTables = "2.4"
4141
RecipesBase = "1.3"
42-
julia = "1.10"
42+
julia = "1.10"

ext/CTModelsJSON.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,24 @@ function CTModels.import_ocp_solution(
6464
X = stack(blob["state"]; dims=1)
6565
if X isa Vector # if X is a Vector, convert it to a Matrix
6666
X = Matrix{Float64}(reduce(hcat, X)')
67+
else
68+
X = Matrix{Float64}(X)
6769
end
6870

6971
# get control
7072
U = stack(blob["control"]; dims=1)
7173
if U isa Vector # if U is a Vector, convert it to a Matrix
7274
U = Matrix{Float64}(reduce(hcat, U)')
75+
else
76+
U = Matrix{Float64}(U)
7377
end
7478

7579
# get costate
7680
P = stack(blob["costate"]; dims=1)
7781
if P isa Vector # if P is a Vector, convert it to a Matrix
7882
P = Matrix{Float64}(reduce(hcat, P)')
83+
else
84+
P = Matrix{Float64}(P)
7985
end
8086

8187
# get dual path constraints: convert to matrix
@@ -86,6 +92,8 @@ function CTModels.import_ocp_solution(
8692
end
8793
if path_constraints_dual isa Vector # if path_constraints_dual is a Vector, convert it to a Matrix
8894
path_constraints_dual = Matrix{Float64}(reduce(hcat, path_constraints_dual)')
95+
elseif !isnothing(path_constraints_dual)
96+
path_constraints_dual = Matrix{Float64}(path_constraints_dual)
8997
end
9098

9199
# get state constraints (and dual): convert to matrix
@@ -98,6 +106,8 @@ function CTModels.import_ocp_solution(
98106
state_constraints_lb_dual = Matrix{Float64}(
99107
reduce(hcat, state_constraints_lb_dual)'
100108
)
109+
elseif !isnothing(state_constraints_lb_dual)
110+
state_constraints_lb_dual = Matrix{Float64}(state_constraints_lb_dual)
101111
end
102112
state_constraints_ub_dual = if isnothing(blob["state_constraints_ub_dual"])
103113
nothing
@@ -108,6 +118,8 @@ function CTModels.import_ocp_solution(
108118
state_constraints_ub_dual = Matrix{Float64}(
109119
reduce(hcat, state_constraints_ub_dual)'
110120
)
121+
elseif !isnothing(state_constraints_ub_dual)
122+
state_constraints_ub_dual = Matrix{Float64}(state_constraints_ub_dual)
111123
end
112124

113125
# get control constraints (and dual): convert to matrix
@@ -120,6 +132,8 @@ function CTModels.import_ocp_solution(
120132
control_constraints_lb_dual = Matrix{Float64}(
121133
reduce(hcat, control_constraints_lb_dual)'
122134
)
135+
elseif !isnothing(control_constraints_lb_dual)
136+
control_constraints_lb_dual = Matrix{Float64}(control_constraints_lb_dual)
123137
end
124138
control_constraints_ub_dual = if isnothing(blob["control_constraints_ub_dual"])
125139
nothing
@@ -130,14 +144,25 @@ function CTModels.import_ocp_solution(
130144
control_constraints_ub_dual = Matrix{Float64}(
131145
reduce(hcat, control_constraints_ub_dual)'
132146
)
147+
elseif !isnothing(control_constraints_ub_dual)
148+
control_constraints_ub_dual = Matrix{Float64}(control_constraints_ub_dual)
133149
end
134150

135151
# get dual of boundary constraints: no conversion needed
136152
boundary_constraints_dual = blob["boundary_constraints_dual"]
153+
if !isnothing(boundary_constraints_dual)
154+
boundary_constraints_dual = Vector{Float64}(boundary_constraints_dual)
155+
end
137156

138157
# get variable constraints dual: no conversion needed
139158
variable_constraints_lb_dual = blob["variable_constraints_lb_dual"]
159+
if !isnothing(variable_constraints_lb_dual)
160+
variable_constraints_lb_dual = Vector{Float64}(blob["variable_constraints_lb_dual"])
161+
end
140162
variable_constraints_ub_dual = blob["variable_constraints_ub_dual"]
163+
if !isnothing(variable_constraints_ub_dual)
164+
variable_constraints_ub_dual = Vector{Float64}(blob["variable_constraints_ub_dual"])
165+
end
141166

142167
# NB. convert vect{vect} to matrix
143168
return CTModels.build_solution(

src/solution.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function build_solution(
140140
fscbd = if isnothing(state_constraints_lb_dual)
141141
nothing
142142
else
143-
if (dim_state_constraints_box(ocp) == 1)
143+
if (dim_x == 1)
144144
deepcopy(t -> state_constraints_lb_dual_fun(t)[1])
145145
else
146146
deepcopy(t -> state_constraints_lb_dual_fun(t))
@@ -157,7 +157,7 @@ function build_solution(
157157
fscud = if isnothing(state_constraints_ub_dual)
158158
nothing
159159
else
160-
if (dim_state_constraints_box(ocp) == 1)
160+
if (dim_x == 1)
161161
deepcopy(t -> state_constraints_ub_dual_fun(t)[1])
162162
else
163163
deepcopy(t -> state_constraints_ub_dual_fun(t))
@@ -174,7 +174,7 @@ function build_solution(
174174
fccbd = if isnothing(control_constraints_lb_dual)
175175
nothing
176176
else
177-
if (dim_control_constraints_box(ocp) == 1)
177+
if (dim_u == 1)
178178
deepcopy(t -> control_constraints_lb_dual_fun(t)[1])
179179
else
180180
deepcopy(t -> control_constraints_lb_dual_fun(t))
@@ -191,7 +191,7 @@ function build_solution(
191191
fccud = if isnothing(control_constraints_ub_dual)
192192
nothing
193193
else
194-
if (dim_control_constraints_box(ocp) == 1)
194+
if (dim_u == 1)
195195
deepcopy(t -> control_constraints_ub_dual_fun(t)[1])
196196
else
197197
deepcopy(t -> control_constraints_ub_dual_fun(t))

test/Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
[deps]
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"
4+
CTDirect = "790bbbee-bee9-49ee-8912-a9de031322d5"
45
CTParser = "32681960-a1b1-40db-9bff-a1ca817385d1"
56
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
67
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
8+
NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71"
79
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
810
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
911

1012
[compat]
1113
Aqua = "0.8"
1214
CTBase = "0.16"
15+
CTDirect = "0.14"
1316
CTParser = "0.2"
1417
JLD2 = "0.5"
1518
JSON3 = "1"
19+
NLPModelsIpopt = "0.10"
1620
Plots = "1.40"
21+
Test = "1"
22+
julia = "1.10"

test/extras/export_import.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Pkg
2+
Pkg.add("JSON3")
3+
Pkg.add("JLD2")
4+
using NLPModelsIpopt
5+
using CTModels
6+
using CTDirect
7+
import CTParser: CTParser, @def
8+
CTParser.set_prefix(:CTModels); # code generated by @def is prefixed by CTModels (not by OptimalControl - the default)
9+
10+
ocp = @def begin
11+
12+
t [0, 1], time
13+
x R², state
14+
u R, control
15+
16+
x₂(t) 1.2
17+
18+
x(0) == [-1, 0]
19+
x(1) == [0, 0]
20+
21+
(t) == [x₂(t), u(t)]
22+
23+
( 0.5u(t)^2 ) min
24+
25+
end;
26+
27+
sol = CTDirect.solve(ocp)
28+
29+
using JLD2
30+
CTModels.export_ocp_solution(sol; filename="my_solution")
31+
sol_jld = CTModels.import_ocp_solution(ocp; filename="my_solution")
32+
println("Objective from computed solution: ", CTModels.objective(sol))
33+
println("Objective from imported solution: ", CTModels.objective(sol_jld))
34+
35+
using JSON3
36+
CTModels.export_ocp_solution(sol; filename="my_solution", format=:JSON)
37+
sol_json = CTModels.import_ocp_solution(ocp; filename="my_solution", format=:JSON)
38+
println("Objective from computed solution: ", CTModels.objective(sol))
39+
println("Objective from imported solution: ", CTModels.objective(sol_json))
40+
41+
# Clean up
42+
Pkg.rm("JLD2")
43+
Pkg.rm("JSON3")

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Test
22
using Aqua
33
using CTBase
4+
using CTDirect
45
using CTModels
6+
using NLPModelsIpopt
57

68
import CTParser: CTParser, @def
79
CTParser.set_prefix(:CTModels); # code generated by @def is prefixed by CTModels (not by OptimalControl - the default)

test/solution_example_path_constraints.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function solution_example_path_constraints()
1515
[-3, 1] [x(t) + 1, u(t) + 1] [1, 2.5], (2)
1616
(t) == u(t)
1717
(-u(t)) min
18-
end true;
18+
end;
1919

2020
return ocp
2121
end

test/solution_test.jld2

266 KB
Binary file not shown.

0 commit comments

Comments
 (0)