-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from control-toolbox/move_load_save_to_base
Load / save OCP solution moved from CTDirect to CTBase
- Loading branch information
Showing
10 changed files
with
344 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>", "Jean-Baptiste Caillau <[email protected]>"] | ||
version = "0.14.0" | ||
version = "0.14.1" | ||
|
||
[deps] | ||
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" | ||
|
@@ -22,9 +22,12 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | |
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" | ||
|
||
[weakdeps] | ||
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" | ||
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" | ||
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
|
||
[extensions] | ||
CTBaseLoadSave = ["JLD2", "JSON3"] | ||
CTBasePlots = "Plots" | ||
|
||
[compat] | ||
|
@@ -33,6 +36,8 @@ DifferentiationInterface = "0.5" | |
DocStringExtensions = "0.9" | ||
ForwardDiff = "0.10" | ||
Interpolations = "0.15" | ||
JLD2 = "0.5" | ||
JSON3 = "1" | ||
MLStyle = "0.4" | ||
MacroTools = "0.5" | ||
Parameters = "0.12" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module CTBaseLoadSave | ||
|
||
using CTBase | ||
using DocStringExtensions | ||
|
||
using JLD2 | ||
using JSON3 | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
Export OCP solution in JLD / JSON format | ||
""" | ||
function CTBase.export_ocp_solution(sol::OptimalControlSolution; filename_prefix = "solution", format = :JLD) | ||
if format == :JLD | ||
save_object(filename_prefix * ".jld2", sol) | ||
elseif format == :JSON | ||
blob = Dict( | ||
"objective" => sol.objective, | ||
"time_grid" => sol.time_grid, | ||
"state" => state_discretized(sol), | ||
"control" => control_discretized(sol), | ||
"costate" => costate_discretized(sol)[1:(end - 1), :], | ||
"variable" => sol.variable, | ||
) | ||
open(filename_prefix * ".json", "w") do io | ||
JSON3.pretty(io, blob) | ||
end | ||
else | ||
error("Export_ocp_solution: unknow format (should be :JLD or :JSON): ", format) | ||
end | ||
return nothing | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
Read OCP solution in JLD / JSON format | ||
""" | ||
function CTBase.import_ocp_solution(ocp::OptimalControlModel; filename_prefix = "solution", format = :JLD) | ||
|
||
if format == :JLD | ||
return load_object(filename_prefix * ".jld2") | ||
elseif format == :JSON | ||
json_string = read(filename_prefix * ".json", String) | ||
blob = JSON3.read(json_string) | ||
|
||
# NB. convert vect{vect} to matrix | ||
return OptimalControlSolution( | ||
ocp, | ||
blob.time_grid, | ||
stack(blob.state, dims = 1), | ||
stack(blob.control, dims = 1), | ||
blob.variable, | ||
stack(blob.costate, dims = 1); | ||
objective = blob.objective, | ||
) | ||
else | ||
error("Export_ocp_solution: unknow format (should be :JLD or :JSON): ", format) | ||
end | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
{ | ||
"time_grid": [ | ||
0, | ||
0.1111111111111111, | ||
0.2222222222222222, | ||
0.3333333333333333, | ||
0.4444444444444444, | ||
0.5555555555555556, | ||
0.6666666666666666, | ||
0.7777777777777778, | ||
0.8888888888888888, | ||
1 | ||
], | ||
"objective": 1, | ||
"control": [ | ||
0, | ||
0.2222222222222222, | ||
0.4444444444444444, | ||
0.6666666666666666, | ||
0.8888888888888888, | ||
1.1111111111111112, | ||
1.3333333333333333, | ||
1.5555555555555556, | ||
1.7777777777777777, | ||
2 | ||
], | ||
"costate": [ | ||
[ | ||
0, | ||
-1 | ||
], | ||
[ | ||
0.1111111111111111, | ||
-0.8888888888888888 | ||
], | ||
[ | ||
0.2222222222222222, | ||
-0.7777777777777778 | ||
], | ||
[ | ||
0.3333333333333333, | ||
-0.6666666666666667 | ||
], | ||
[ | ||
0.4444444444444444, | ||
-0.5555555555555556 | ||
], | ||
[ | ||
0.5555555555555556, | ||
-0.4444444444444444 | ||
], | ||
[ | ||
0.6666666666666666, | ||
-0.33333333333333337 | ||
], | ||
[ | ||
0.7777777777777778, | ||
-0.2222222222222222 | ||
], | ||
[ | ||
0.8888888888888888, | ||
-0.11111111111111116 | ||
] | ||
], | ||
"variable": null, | ||
"state": [ | ||
[ | ||
0, | ||
1 | ||
], | ||
[ | ||
0.1111111111111111, | ||
1.1111111111111112 | ||
], | ||
[ | ||
0.2222222222222222, | ||
1.2222222222222223 | ||
], | ||
[ | ||
0.3333333333333333, | ||
1.3333333333333333 | ||
], | ||
[ | ||
0.4444444444444444, | ||
1.4444444444444444 | ||
], | ||
[ | ||
0.5555555555555556, | ||
1.5555555555555556 | ||
], | ||
[ | ||
0.6666666666666666, | ||
1.6666666666666665 | ||
], | ||
[ | ||
0.7777777777777778, | ||
1.7777777777777777 | ||
], | ||
[ | ||
0.8888888888888888, | ||
1.8888888888888888 | ||
], | ||
[ | ||
1, | ||
2 | ||
] | ||
] | ||
} |
Oops, something went wrong.