From 8a0b685b6445411dcb8592d0ec4605997bd32eb2 Mon Sep 17 00:00:00 2001 From: Pierre Martinon Date: Mon, 3 Jun 2024 13:07:20 +0200 Subject: [PATCH] basic load/save solution in julia JLD2 format (#103) --- .gitignore | 3 +++ Project.toml | 1 + src/CTDirect.jl | 1 + test/Project.toml | 2 +- test/suite/misc.jl | 7 +++++++ test/test_misc.jl | 7 +++++++ 6 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c69aaa7..af7c426 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ *.jl.mem *.jl.*.mem +# Julia data files +*.jld2 + # System-specific files and directories generated by the BinaryProvider and BinDeps packages # They contain absolute paths specific to the host computer, and so should not be committed deps/deps.jl diff --git a/Project.toml b/Project.toml index cda7075..c11ef95 100644 --- a/Project.toml +++ b/Project.toml @@ -7,6 +7,7 @@ version = "0.6.0" ADNLPModels = "54578032-b7ea-4c30-94aa-7cbd1cce6c9a" CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6" NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71" diff --git a/src/CTDirect.jl b/src/CTDirect.jl index cebd390..915ce0e 100644 --- a/src/CTDirect.jl +++ b/src/CTDirect.jl @@ -44,5 +44,6 @@ export dynamics! export objective! export remove_constraint! export OCPInit +export plot end \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml index 5533bba..dca6ca7 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,4 +1,4 @@ [deps] -LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/suite/misc.jl b/test/suite/misc.jl index 5618e79..90c78e4 100644 --- a/test/suite/misc.jl +++ b/test/suite/misc.jl @@ -1,4 +1,5 @@ using CTDirect +using JLD2 println("Test: misc") @@ -42,3 +43,9 @@ nlp = getNLP(docp) dsol = solve(docp, print_level=0, tol=1e-12) sol_raw = OCPSolutionFromDOCP_raw(docp, dsol.solution) +# test save / load solution in JLD2 format +@testset verbose = true showtiming = true ":save_load :JLD2" begin + save_object("sol.jld2", sol0) + sol_reloaded = load_object("sol.jld2") + @test sol0.objective == sol_reloaded.objective +end diff --git a/test/test_misc.jl b/test/test_misc.jl index c0223c6..e9d0042 100644 --- a/test/test_misc.jl +++ b/test/test_misc.jl @@ -1,4 +1,5 @@ using CTDirect +using JLD2 println("Test: misc") @@ -41,5 +42,11 @@ dsol2 = solve(docp2, print_level=5, tol=1e-12) println("\nRebuild OCP solution from raw vector") sol3 = OCPSolutionFromDOCP_raw(docp2, dsol2.solution) +# save / load solution in JLD2 format (solution includes complex data such as interpolated functions which are less suitable for more generic formats such as JSON) +save_object("sol.jld2", sol) +sol4 = load_object("sol.jld2") +plot(sol4, show=true) +println(sol.objective == sol4.objective) + println("") # \ No newline at end of file