From 7ba4bce8488e08d533530a0bc68a25cc51745301 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Thu, 28 Aug 2025 16:24:36 +0200 Subject: [PATCH] replace build sol --- Project.toml | 4 ++++ docs/problems.jl | 4 ++-- docs/src/assets/Manifest.toml | 2 +- docs/src/index.md | 2 +- docs/src/tutorial-solve.md | 7 +------ src/OptimalControlProblems.jl | 24 ++++++++++++++++++++++-- test/figures/init/beam.pdf | Bin 229178 -> 229178 bytes test/figures/solution/beam.pdf | Bin 218417 -> 218417 bytes test/runtests.jl | 6 +++--- test/test_quick.jl | 7 +------ test/utils.jl | 7 +------ 11 files changed, 36 insertions(+), 27 deletions(-) diff --git a/Project.toml b/Project.toml index 80c2ce1c..0233be85 100644 --- a/Project.toml +++ b/Project.toml @@ -4,12 +4,14 @@ authors = ["Olivier Cots "] version = "0.2.1" [deps] +ADNLPModels = "54578032-b7ea-4c30-94aa-7cbd1cce6c9a" CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd" CTDirect = "790bbbee-bee9-49ee-8912-a9de031322d5" CTModels = "34c4fa32-2049-4079-8329-de33c2a22e2d" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" ExaModels = "1037b233-b668-4ce9-9b63-f9f681f55dd2" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +SolverCore = "ff4d7338-4cf1-434d-91df-b86cb86fb843" [weakdeps] JuMP = "4076af6c-e467-56ae-b986-b466b2749572" @@ -20,6 +22,7 @@ JuMPModels = "JuMP" OptimalControlModels = "OptimalControl" [compat] +ADNLPModels = "0.8" CTBase = "0.16" CTDirect = "0.16" CTModels = "0.6" @@ -28,4 +31,5 @@ ExaModels = "0.9" JuMP = "1" OptimalControl = "1" OrderedCollections = "1" +SolverCore = "0.3" julia = "1.10" diff --git a/docs/problems.jl b/docs/problems.jl index fa7596a0..ddb71766 100644 --- a/docs/problems.jl +++ b/docs/problems.jl @@ -76,7 +76,7 @@ function generate_documentation( ) # build an optimal control solution - ocp_sol = build_OCP_solution(docp; primal=nlp_sol.solution, dual=nlp_sol.multipliers, docp_solution=nlp_sol) + ocp_sol = build_ocp_solution(docp, nlp_sol) nothing # hide ``` @@ -246,7 +246,7 @@ function generate_documentation( ) # build an optimal control solution - ocp_sol = build_OCP_solution(docp; primal=nlp_sol.solution, dual=nlp_sol.multipliers, docp_solution=nlp_sol) + ocp_sol = build_ocp_solution(docp, nlp_sol) # plot the OptimalControl solution plt = plot( diff --git a/docs/src/assets/Manifest.toml b/docs/src/assets/Manifest.toml index fbc589ca..1f496d12 100644 --- a/docs/src/assets/Manifest.toml +++ b/docs/src/assets/Manifest.toml @@ -1042,7 +1042,7 @@ uuid = "5f98b655-cc9a-415a-b60e-744165666948" version = "1.1.1" [[deps.OptimalControlProblems]] -deps = ["CTBase", "CTDirect", "CTModels", "DocStringExtensions", "ExaModels", "OrderedCollections"] +deps = ["ADNLPModels", "CTBase", "CTDirect", "CTModels", "DocStringExtensions", "ExaModels", "OrderedCollections", "SolverCore"] path = "/Users/ocots/Research/logiciels/dev/control-toolbox/OptimalControlProblems" uuid = "59046045-fb9c-4c23-964f-ff0a25704f96" version = "0.2.1" diff --git a/docs/src/index.md b/docs/src/index.md index 22c5c109..96fa86e4 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -21,7 +21,7 @@ Pkg.add("OptimalControlProblems") - [0Yassine0](https://github.com/0Yassine0) - [frapac](https://github.com/frapac) - [BaptisteCbl](https://github.com/BaptisteCbl) -- [COPS: Large-Scale Optimization Problems](https://www.mcs.anl.gov/~more/cops) and [COPSBenchmark.jl](github.com/MadNLP/COPSBenchmark.jl) +- [COPS: Large-Scale Optimization Problems](https://www.mcs.anl.gov/~more/cops) and [COPSBenchmark.jl](https://github.com/MadNLP/COPSBenchmark.jl) - [BOCOP - A collection of examples](https://project.inria.fr/bocop/files/2017/05/Examples-BOCOP.pdf) ## Reproducibility diff --git a/docs/src/tutorial-solve.md b/docs/src/tutorial-solve.md index dd5bf529..33978239 100644 --- a/docs/src/tutorial-solve.md +++ b/docs/src/tutorial-solve.md @@ -44,12 +44,7 @@ sol.iter To recover the state, control, and costate, we recommend building an optimal control solution and using the associated getters: ```@example main -ocp_sol = build_OCP_solution( - docp; - primal=sol.solution, - dual=sol.multipliers, - docp_solution=sol, -) +ocp_sol = build_ocp_solution(docp, sol) t = time_grid(ocp_sol) # t0, ..., tN = tf x = state(ocp_sol) # function of time diff --git a/src/OptimalControlProblems.jl b/src/OptimalControlProblems.jl index d7e9f714..41cd1466 100644 --- a/src/OptimalControlProblems.jl +++ b/src/OptimalControlProblems.jl @@ -3,16 +3,36 @@ module OptimalControlProblems using CTBase using CTDirect import CTModels: CTModels, time_grid, state, control, costate -import ExaModels: ExaModels, variable +import ExaModels: ExaModels, ExaModel, variable using DocStringExtensions using OrderedCollections: OrderedDict +using SolverCore +import ADNLPModels: ADNLPModels, ADNLPModel # ----------------- # SHOULD NO BE HERE nlp_model(docp::CTDirect.DOCP) = docp.nlp ocp_model(docp::CTDirect.DOCP) = docp.ocp +function build_ocp_solution(docp::CTDirect.DOCP, nlp_solution::SolverCore.AbstractExecutionStats) + nlp_model_backend = if nlp_model(docp) isa ADNLPModel + CTDirect.ADNLPBackend() + elseif nlp_model(docp) isa ExaModel + CTDirect.ExaBackend() + else + throw(CTBase.IncorrectArgument("The NLP model is of unknown type.")) + end + return CTDirect.build_OCP_solution( + docp; + primal=nlp_solution.solution, + dual=nlp_solution.multipliers, + mult_LB=nlp_solution.multipliers_L, + mult_UB=nlp_solution.multipliers_U, + nlp_model=nlp_model_backend, + docp_solution=nlp_solution + ) +end -export nlp_model, ocp_model +export nlp_model, ocp_model, build_ocp_solution # ----------------- diff --git a/test/figures/init/beam.pdf b/test/figures/init/beam.pdf index 124652dae5c36e5bcd047b58be4eeac5c72a42bf..7ad55eaadc61fc7a8d95b0b4860f642facec41a1 100644 GIT binary patch delta 27 jcmdn>k9XHU-U-gEW`>p~W{n=L9*nIXOj|veKh*&MnePfi delta 27 jcmdn>k9XHU-U-gEriP|QMvWe=9*nIXOj|veKh*&MnMVpZ diff --git a/test/figures/solution/beam.pdf b/test/figures/solution/beam.pdf index 45c1bd9d23693b0c2dd7aad5dca8e4fb9f733497..3a5247aa444e33ab80706179438da228b61aaa7c 100644 GIT binary patch delta 27 jcmdnEig)8G-U-gEW`>p~W{n=L9*nIXOj|veUxokxhtvsH delta 27 jcmdnEig)8G-U-gEriP|QMvWe=9*nIXOj|veUxokxhb#$8 diff --git a/test/runtests.jl b/test/runtests.jl index f08c5c8d..000cc355 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -34,9 +34,9 @@ end # ] # list_of_problems = setdiff(list_of_problems, problems_to_exclude) -# list_of_problems = [ -# :beam -# ] +list_of_problems = [ +# :beam +] # The list of all the problems to test const LIST_OF_PROBLEMS = deepcopy(list_of_problems) diff --git a/test/test_quick.jl b/test/test_quick.jl index e70f066a..5760ab26 100644 --- a/test/test_quick.jl +++ b/test/test_quick.jl @@ -29,12 +29,7 @@ function test_quick() docp = OptimalControlProblems.eval(f)(OptimalControlBackend(); N=N) nlp = nlp_model(docp) nlp_sol = NLPModelsIpopt.ipopt(nlp; kwargs...) - sol = build_OCP_solution( - docp; - primal=nlp_sol.solution, - dual=nlp_sol.multipliers, - docp_solution=nlp_sol, - ) + sol = build_ocp_solution(docp, nlp_sol) o_oc = objective(sol) ############### JuMP ############### diff --git a/test/utils.jl b/test/utils.jl index f46bcc68..df8764cf 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -128,12 +128,7 @@ function comparison(; max_iter, test_name) nlp_sol = NLPModelsIpopt.ipopt(nlp; Options...) # build the solution - sol = build_OCP_solution( - docp; - primal=nlp_sol.solution, - dual=nlp_sol.multipliers, - docp_solution=nlp_sol, - ) + sol = build_ocp_solution(docp, nlp_sol) sol_oc = deepcopy(sol) # for plotting