-
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.
- Loading branch information
Showing
12 changed files
with
352 additions
and
139 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
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
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.6" | |
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
Oops, something went wrong.