Skip to content

Commit 84108ff

Browse files
authored
Merge pull request #516 from control-toolbox/515-bug-max-bug-with-exa
[Bug] Max objective for exa
2 parents 16e52de + 24b1f7d commit 84108ff

File tree

14 files changed

+223
-95
lines changed

14 files changed

+223
-95
lines changed

Project.toml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
name = "CTDirect"
22
uuid = "790bbbee-bee9-49ee-8912-a9de031322d5"
3+
version = "0.18.0"
34
authors = ["Pierre Martinon <[email protected]>"]
4-
version = "0.17.3"
5+
6+
[workspace]
7+
projects = ["test", "docs"]
58

69
[deps]
710
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"
811
CTModels = "34c4fa32-2049-4079-8329-de33c2a22e2d"
912
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1013
HSL = "34c5aeac-e683-54a6-a0e9-6e0fdc586c50"
1114
MKL = "33e6dc65-8f57-5167-99aa-e5a354878fb2"
15+
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
16+
SolverCore = "ff4d7338-4cf1-434d-91df-b86cb86fb843"
1217
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1318

1419
[weakdeps]
@@ -38,8 +43,10 @@ MKL = "0.9"
3843
MadNLP = "0.8"
3944
MadNLPGPU = "0.7"
4045
MadNLPMumps = "0.5"
46+
NLPModels = "0.21"
4147
NLPModelsIpopt = "0.11"
4248
NLPModelsKnitro = "0.9"
49+
SolverCore = "0.3.8"
4350
SplitApplyCombine = "1"
4451
julia = "1.10"
4552

@@ -49,11 +56,9 @@ CTParser = "32681960-a1b1-40db-9bff-a1ca817385d1"
4956
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
5057
MadNLPGPU = "d72a61cc-809d-412f-99be-fd81f4b8a598"
5158
MadNLPMumps = "3b83494e-c0a4-4895-918b-9157a7a085a1"
59+
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
5260
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
5361
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5462

5563
[targets]
56-
test = ["Test", "SplitApplyCombine", "CTParser", "ADNLPModels", "ExaModels", "NLPModelsIpopt", "MadNLPMumps", "MadNLPGPU", "CUDA", "AMDGPU"]
57-
58-
[workspace]
59-
projects = ["test", "docs"]
64+
test = ["Test", "SplitApplyCombine", "CTParser", "ADNLPModels", "ExaModels", "NLPModels", "NLPModelsIpopt", "MadNLPMumps", "MadNLPGPU", "CUDA", "AMDGPU"]

ext/CTDirectExtADNLP.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
module CTDirectExtADNLP
22

33
using CTDirect
4-
54
using DocStringExtensions
6-
75
using ADNLPModels
6+
using CTModels
87

98
"""
109
$(TYPEDSIGNATURES)
@@ -16,8 +15,11 @@ Build the NLP model for the DOCP (ADNLPModels version)
1615
* `adnlp_backend`: backend for ADNLPModels ([`:optimized`], `:manual`, `:default`)
1716
"""
1817
function CTDirect.build_nlp!(
19-
docp::CTDirect.DOCP,
20-
nlp_model::CTDirect.ADNLPBackend,
18+
docp::CTDirect.DOCP{
19+
<:CTDirect.Discretization,
20+
<:CTModels.Model,
21+
<:CTDirect.ADNLPBackend,
22+
},
2123
x0;
2224
adnlp_backend=CTDirect.__adnlp_backend(),
2325
show_time=false, #+default

ext/CTDirectExtExa.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ module CTDirectExtExa
22

33
using CTDirect
44
using CTModels: CTModels
5-
65
using DocStringExtensions
7-
86
using ExaModels
7+
using SolverCore
98

109
"""
1110
$(TYPEDSIGNATURES)
@@ -18,8 +17,11 @@ Build the NLP model for the DOCP (ExaModels version)
1817
* `exa_backend`: backend for ExaModels ([`nothing`])
1918
"""
2019
function CTDirect.build_nlp!(
21-
docp::CTDirect.DOCP,
22-
nlp_model::CTDirect.ExaBackend,
20+
docp::CTDirect.DOCP{
21+
<:CTDirect.Discretization,
22+
<:CTModels.Model,
23+
<:CTDirect.ExaBackend,
24+
},
2325
x0;
2426
grid_size=CTDirect.__grid_size(),
2527
disc_method=CTDirect.__disc_method(),
@@ -60,12 +62,26 @@ function CTDirect.build_nlp!(
6062
return nothing
6163
end
6264

63-
function CTDirect.get_time_grid_exa(docp_solution, docp)
65+
"""
66+
$(TYPEDSIGNATURES)
67+
68+
Retrieve the time grid from the given DOCP solution.
69+
70+
# Arguments
71+
72+
- `nlp_solution`: The DOCP solution.
73+
- `docp`: The DOCP.
74+
75+
# Returns
76+
77+
- `::Vector{Float64}`: The time grid.
78+
"""
79+
function CTDirect.get_time_grid_exa(nlp_solution::SolverCore.AbstractExecutionStats, docp::CTDirect.DOCP)
6480
grid = zeros(docp.time.steps+1)
6581
ocp = docp.ocp
6682

6783
if docp.flags.freet0 || docp.flags.freetf
68-
v = docp.exa_getter(docp_solution; val=:variable)
84+
v = docp.exa_getter(nlp_solution; val=:variable)
6985
end
7086

7187
if docp.flags.freet0

ext/CTDirectExtIpopt.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ $(TYPEDSIGNATURES)
3535
Solve a discretized optimal control problem (Ipopt version).
3636
"""
3737
function CTDirect.solve_docp(
38-
solver_backend::CTDirect.IpoptBackend,
38+
::CTDirect.IpoptBackend,
3939
docp::CTDirect.DOCP;
4040
display::Bool=CTDirect.__display(),
4141
max_iter::Integer=CTDirect.__max_iterations(),
@@ -75,7 +75,7 @@ function CTDirect.solve_docp(
7575
solver = IpoptSolver(nlp)
7676

7777
# solve discretized problem with NLP solver
78-
docp_solution = solve!(
78+
nlp_solution = solve!(
7979
solver,
8080
nlp;
8181
print_level=print_level,
@@ -88,8 +88,8 @@ function CTDirect.solve_docp(
8888
kwargs...,
8989
)
9090

91-
# return DOCP solution
92-
return docp_solution
91+
# return NLP solution
92+
return nlp_solution
9393
end
9494

9595
end

ext/CTDirectExtKnitro.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $(TYPEDSIGNATURES)
2121
Solve a discretized optimal control problem with Ipopt
2222
"""
2323
function CTDirect.solve_docp(
24-
solver_backend::CTDirect.KnitroBackend,
24+
::CTDirect.KnitroBackend,
2525
docp::CTDirect.DOCP;
2626
display::Bool=CTDirect.__display(),
2727
max_iter::Integer=CTDirect.__max_iterations(),
@@ -44,10 +44,10 @@ function CTDirect.solve_docp(
4444
)
4545

4646
# solve discretized problem with NLP solver
47-
docp_solution = solve!(solver, nlp)
47+
nlp_solution = solve!(solver, nlp)
4848

49-
# return DOCP solution
50-
return docp_solution
49+
# return NLP solution
50+
return nlp_solution
5151
end
5252

5353
end

ext/CTDirectExtMadNLP.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using DocStringExtensions
77
using MadNLP
88
using HSL
99
using MKL
10+
using NLPModels
1011

1112
"""
1213
$(TYPEDSIGNATURES)
@@ -22,7 +23,7 @@ $(TYPEDSIGNATURES)
2223
Solve a discretized optimal control problem DOCP
2324
"""
2425
function CTDirect.solve_docp(
25-
solver_backend::CTDirect.MadNLPBackend,
26+
::CTDirect.MadNLPBackend,
2627
docp::CTDirect.DOCP;
2728
display::Bool=CTDirect.__display(),
2829
max_iter::Integer=CTDirect.__max_iterations(),
@@ -43,19 +44,19 @@ function CTDirect.solve_docp(
4344
)
4445

4546
# solve discretized problem with NLP solver
46-
docp_solution = solve!(solver)
47+
nlp_solution = solve!(solver)
4748

48-
# return DOCP solution
49-
return docp_solution
49+
# return NLP solution
50+
return nlp_solution
5051
end
5152

52-
function CTDirect.SolverInfos(nlp_solution::MadNLP.MadNLPExecutionStats)
53-
objective = nlp_solution.objective # NB sign is incorrect for max problems !
53+
function CTDirect.SolverInfos(nlp_solution::MadNLP.MadNLPExecutionStats, nlp::NLPModels.AbstractNLPModel)
54+
minimize = NLPModels.get_minimize(nlp)
55+
objective = minimize ? nlp_solution.objective : -nlp_solution.objective # sign depends on minimization for MadNLP
5456
iterations = nlp_solution.iter
5557
constraints_violation = nlp_solution.primal_feas
5658
status = Symbol(nlp_solution.status)
5759
successful = (status == :SOLVE_SUCCEEDED) || (status == :SOLVED_TO_ACCEPTABLE_LEVEL)
58-
5960
return objective, iterations, constraints_violation, "MadNLP", status, successful
6061
end
6162

src/CTDirect.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ using CTBase
44
using CTModels: CTModels
55
using DocStringExtensions
66
using SparseArrays
7+
using SolverCore: SolverCore
8+
using NLPModels: NLPModels
79

810
# ----------------------------------------------------------------------
911
# EXTENSIONS

src/docp.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ Struct representing a discretized optimal control problem (DOCP).
277277
278278
- `discretization::D`: The discretization scheme.
279279
- `ocp::O`: The original OCP model.
280-
- `nlp_model::N`: The NLP model backend.
280+
- `nlp_model_backend::N`: The NLP model backend.
281281
- `nlp`: The constructed NLP instance.
282282
- `exa_getter::Union{Nothing,Function}`: Getter for ExaModels if used.
283283
- `flags::DOCPFlags`: Boolean flags describing problem structure.
@@ -290,11 +290,11 @@ Struct representing a discretized optimal control problem (DOCP).
290290
# Example
291291
292292
```julia-repl
293-
julia> DOCP(ocp, nlp_model)
293+
julia> DOCP(ocp, nlp_model_backend)
294294
DOCP{...}(...)
295295
```
296296
"""
297-
mutable struct DOCP{D<:Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPModelBackend}
297+
mutable struct DOCP{D<:CTDirect.Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPModelBackend}
298298

299299
# discretization scheme
300300
discretization::D
@@ -303,7 +303,7 @@ mutable struct DOCP{D<:Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPM
303303
ocp::O # parametric instead of just qualifying reduces allocations (but not time). Specialization ?
304304

305305
# NLP
306-
nlp_model::N
306+
nlp_model_backend::N
307307
nlp
308308
exa_getter::Union{Nothing,Function} # getter for ExaModels (if used)
309309

@@ -326,7 +326,7 @@ mutable struct DOCP{D<:Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPM
326326
# constructor
327327
function DOCP(
328328
ocp::CTModels.Model,
329-
nlp_model;
329+
nlp_model_backend::CTDirect.AbstractNLPModelBackend;
330330
grid_size=__grid_size(),
331331
time_grid=__time_grid(),
332332
disc_method=__disc_method(),
@@ -423,10 +423,10 @@ mutable struct DOCP{D<:Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPM
423423
)
424424

425425
# call constructor with const fields
426-
docp = new{typeof(discretization),typeof(ocp),typeof(nlp_model)}(
426+
docp = new{typeof(discretization),typeof(ocp),typeof(nlp_model_backend)}(
427427
discretization,
428428
ocp,
429-
nlp_model,
429+
nlp_model_backend,
430430
nothing, # nlp
431431
nothing, # exa_getter
432432
flags,

0 commit comments

Comments
 (0)