Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
name = "CTDirect"
uuid = "790bbbee-bee9-49ee-8912-a9de031322d5"
version = "0.18.0"
authors = ["Pierre Martinon <pierrecmartinon@gmail.com>"]
version = "0.17.3"

[workspace]
projects = ["test", "docs"]

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

[weakdeps]
Expand Down Expand Up @@ -38,8 +43,10 @@ MKL = "0.9"
MadNLP = "0.8"
MadNLPGPU = "0.7"
MadNLPMumps = "0.5"
NLPModels = "0.21"
NLPModelsIpopt = "0.11"
NLPModelsKnitro = "0.9"
SolverCore = "0.3.8"
SplitApplyCombine = "1"
julia = "1.10"

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

[targets]
test = ["Test", "SplitApplyCombine", "CTParser", "ADNLPModels", "ExaModels", "NLPModelsIpopt", "MadNLPMumps", "MadNLPGPU", "CUDA", "AMDGPU"]

[workspace]
projects = ["test", "docs"]
test = ["Test", "SplitApplyCombine", "CTParser", "ADNLPModels", "ExaModels", "NLPModels", "NLPModelsIpopt", "MadNLPMumps", "MadNLPGPU", "CUDA", "AMDGPU"]
10 changes: 6 additions & 4 deletions ext/CTDirectExtADNLP.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module CTDirectExtADNLP

using CTDirect

using DocStringExtensions

using ADNLPModels
using CTModels

"""
$(TYPEDSIGNATURES)
Expand All @@ -16,8 +15,11 @@ Build the NLP model for the DOCP (ADNLPModels version)
* `adnlp_backend`: backend for ADNLPModels ([`:optimized`], `:manual`, `:default`)
"""
function CTDirect.build_nlp!(
docp::CTDirect.DOCP,
nlp_model::CTDirect.ADNLPBackend,
docp::CTDirect.DOCP{
<:CTDirect.Discretization,
<:CTModels.Model,
<:CTDirect.ADNLPBackend,
},
x0;
adnlp_backend=CTDirect.__adnlp_backend(),
show_time=false, #+default
Expand Down
28 changes: 22 additions & 6 deletions ext/CTDirectExtExa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module CTDirectExtExa

using CTDirect
using CTModels: CTModels

using DocStringExtensions

using ExaModels
using SolverCore

"""
$(TYPEDSIGNATURES)
Expand All @@ -18,8 +17,11 @@ Build the NLP model for the DOCP (ExaModels version)
* `exa_backend`: backend for ExaModels ([`nothing`])
"""
function CTDirect.build_nlp!(
docp::CTDirect.DOCP,
nlp_model::CTDirect.ExaBackend,
docp::CTDirect.DOCP{
<:CTDirect.Discretization,
<:CTModels.Model,
<:CTDirect.ExaBackend,
},
x0;
grid_size=CTDirect.__grid_size(),
disc_method=CTDirect.__disc_method(),
Expand Down Expand Up @@ -60,12 +62,26 @@ function CTDirect.build_nlp!(
return nothing
end

function CTDirect.get_time_grid_exa(docp_solution, docp)
"""
$(TYPEDSIGNATURES)

Retrieve the time grid from the given DOCP solution.

# Arguments

- `nlp_solution`: The DOCP solution.
- `docp`: The DOCP.

# Returns

- `::Vector{Float64}`: The time grid.
"""
function CTDirect.get_time_grid_exa(nlp_solution::SolverCore.AbstractExecutionStats, docp::CTDirect.DOCP)
grid = zeros(docp.time.steps+1)
ocp = docp.ocp

if docp.flags.freet0 || docp.flags.freetf
v = docp.exa_getter(docp_solution; val=:variable)
v = docp.exa_getter(nlp_solution; val=:variable)
end

if docp.flags.freet0
Expand Down
8 changes: 4 additions & 4 deletions ext/CTDirectExtIpopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $(TYPEDSIGNATURES)
Solve a discretized optimal control problem (Ipopt version).
"""
function CTDirect.solve_docp(
solver_backend::CTDirect.IpoptBackend,
::CTDirect.IpoptBackend,
docp::CTDirect.DOCP;
display::Bool=CTDirect.__display(),
max_iter::Integer=CTDirect.__max_iterations(),
Expand Down Expand Up @@ -75,7 +75,7 @@ function CTDirect.solve_docp(
solver = IpoptSolver(nlp)

# solve discretized problem with NLP solver
docp_solution = solve!(
nlp_solution = solve!(
solver,
nlp;
print_level=print_level,
Expand All @@ -88,8 +88,8 @@ function CTDirect.solve_docp(
kwargs...,
)

# return DOCP solution
return docp_solution
# return NLP solution
return nlp_solution
end

end
8 changes: 4 additions & 4 deletions ext/CTDirectExtKnitro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $(TYPEDSIGNATURES)
Solve a discretized optimal control problem with Ipopt
"""
function CTDirect.solve_docp(
solver_backend::CTDirect.KnitroBackend,
::CTDirect.KnitroBackend,
docp::CTDirect.DOCP;
display::Bool=CTDirect.__display(),
max_iter::Integer=CTDirect.__max_iterations(),
Expand All @@ -44,10 +44,10 @@ function CTDirect.solve_docp(
)

# solve discretized problem with NLP solver
docp_solution = solve!(solver, nlp)
nlp_solution = solve!(solver, nlp)

# return DOCP solution
return docp_solution
# return NLP solution
return nlp_solution
end

end
15 changes: 8 additions & 7 deletions ext/CTDirectExtMadNLP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using DocStringExtensions
using MadNLP
using HSL
using MKL
using NLPModels

"""
$(TYPEDSIGNATURES)
Expand All @@ -22,7 +23,7 @@ $(TYPEDSIGNATURES)
Solve a discretized optimal control problem DOCP
"""
function CTDirect.solve_docp(
solver_backend::CTDirect.MadNLPBackend,
::CTDirect.MadNLPBackend,
docp::CTDirect.DOCP;
display::Bool=CTDirect.__display(),
max_iter::Integer=CTDirect.__max_iterations(),
Expand All @@ -43,19 +44,19 @@ function CTDirect.solve_docp(
)

# solve discretized problem with NLP solver
docp_solution = solve!(solver)
nlp_solution = solve!(solver)

# return DOCP solution
return docp_solution
# return NLP solution
return nlp_solution
end

function CTDirect.SolverInfos(nlp_solution::MadNLP.MadNLPExecutionStats)
objective = nlp_solution.objective # NB sign is incorrect for max problems !
function CTDirect.SolverInfos(nlp_solution::MadNLP.MadNLPExecutionStats, nlp::NLPModels.AbstractNLPModel)
minimize = NLPModels.get_minimize(nlp)
objective = minimize ? nlp_solution.objective : -nlp_solution.objective # sign depends on minimization for MadNLP
iterations = nlp_solution.iter
constraints_violation = nlp_solution.primal_feas
status = Symbol(nlp_solution.status)
successful = (status == :SOLVE_SUCCEEDED) || (status == :SOLVED_TO_ACCEPTABLE_LEVEL)

return objective, iterations, constraints_violation, "MadNLP", status, successful
end

Expand Down
2 changes: 2 additions & 0 deletions src/CTDirect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using CTBase
using CTModels: CTModels
using DocStringExtensions
using SparseArrays
using SolverCore: SolverCore
using NLPModels: NLPModels

# ----------------------------------------------------------------------
# EXTENSIONS
Expand Down
14 changes: 7 additions & 7 deletions src/docp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Struct representing a discretized optimal control problem (DOCP).

- `discretization::D`: The discretization scheme.
- `ocp::O`: The original OCP model.
- `nlp_model::N`: The NLP model backend.
- `nlp_model_backend::N`: The NLP model backend.
- `nlp`: The constructed NLP instance.
- `exa_getter::Union{Nothing,Function}`: Getter for ExaModels if used.
- `flags::DOCPFlags`: Boolean flags describing problem structure.
Expand All @@ -290,11 +290,11 @@ Struct representing a discretized optimal control problem (DOCP).
# Example

```julia-repl
julia> DOCP(ocp, nlp_model)
julia> DOCP(ocp, nlp_model_backend)
DOCP{...}(...)
```
"""
mutable struct DOCP{D<:Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPModelBackend}
mutable struct DOCP{D<:CTDirect.Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPModelBackend}

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

# NLP
nlp_model::N
nlp_model_backend::N
nlp
exa_getter::Union{Nothing,Function} # getter for ExaModels (if used)

Expand All @@ -326,7 +326,7 @@ mutable struct DOCP{D<:Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPM
# constructor
function DOCP(
ocp::CTModels.Model,
nlp_model;
nlp_model_backend::CTDirect.AbstractNLPModelBackend;
grid_size=__grid_size(),
time_grid=__time_grid(),
disc_method=__disc_method(),
Expand Down Expand Up @@ -423,10 +423,10 @@ mutable struct DOCP{D<:Discretization,O<:CTModels.Model,N<:CTDirect.AbstractNLPM
)

# call constructor with const fields
docp = new{typeof(discretization),typeof(ocp),typeof(nlp_model)}(
docp = new{typeof(discretization),typeof(ocp),typeof(nlp_model_backend)}(
discretization,
ocp,
nlp_model,
nlp_model_backend,
nothing, # nlp
nothing, # exa_getter
flags,
Expand Down
Loading
Loading