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
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "CTDirect"
uuid = "790bbbee-bee9-49ee-8912-a9de031322d5"
version = "1.0.1-beta"
version = "1.0.2-beta"
authors = ["Pierre Martinon <pierrecmartinon@gmail.com>"]

[workspace]
Expand All @@ -22,9 +22,9 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
ADNLPModels = "0.8"
AMDGPU = "2"
CTBase = "0.18"
CTModels = "0.8"
CTModels = "0.9"
CTParser = "0.8"
CTSolvers = "0.2"
CTSolvers = "0.3"
CUDA = "5"
CommonSolve = "0.2"
DocStringExtensions = "0.9"
Expand Down
14 changes: 7 additions & 7 deletions src/CTDirect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ using NLPModels

# ----------------------------------------------------------------------
# TYPES
const AbstractOptimalControlProblem = CTModels.AbstractModel
const AbstractModel = CTModels.AbstractModel

# ---------------------------------------------------------------------------
# Abstract discretizer type
# ---------------------------------------------------------------------------
abstract type AbstractOptimalControlDiscretizer <: Strategies.AbstractStrategy end
abstract type AbstractDiscretizer <: Strategies.AbstractStrategy end

function discretize(
ocp::AbstractOptimalControlProblem,
discretizer::AbstractOptimalControlDiscretizer
ocp::AbstractModel,
discretizer::AbstractDiscretizer
)
return discretizer(ocp)
end

__discretizer()::AbstractOptimalControlDiscretizer = Collocation()
__discretizer()::AbstractDiscretizer = Collocation()

function discretize(
ocp::AbstractOptimalControlProblem;
discretizer::AbstractOptimalControlDiscretizer=__discretizer(),
ocp::AbstractModel;
discretizer::AbstractDiscretizer=__discretizer(),
)
return discretize(ocp, discretizer)
end
Expand Down
16 changes: 8 additions & 8 deletions src/collocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ---------------------------------------------------------------------------
# Collocation discretizer
# ---------------------------------------------------------------------------
struct Collocation <: AbstractOptimalControlDiscretizer
struct Collocation <: AbstractDiscretizer
options::Strategies.StrategyOptions
end

Expand Down Expand Up @@ -56,7 +56,7 @@ Strategies.options(c::Collocation) = c.options
# ==========================================================================================
# Build core DOCP structure with discretization information (ADNLP)
# ==========================================================================================
function get_docp(discretizer::Collocation, ocp::AbstractOptimalControlProblem)
function get_docp(discretizer::Collocation, ocp::AbstractModel)

# recover discretization scheme and options
scheme = Strategies.options(discretizer)[:scheme]
Expand All @@ -77,7 +77,7 @@ end
# Build initial guess for discretized problem
# ==========================================================================================
function get_docp_initial_guess(modeler::Symbol, docp,
initial_guess::Union{CTModels.AbstractOptimalControlInitialGuess,Nothing},
initial_guess::Union{CTModels.AbstractInitialGuess,Nothing},
)

ocp = ocp_model(docp)
Expand Down Expand Up @@ -117,19 +117,19 @@ function get_docp_initial_guess(modeler::Symbol, docp,
# ==========================================================================================
# Build discretizer API (return sets of model/solution builders)
# ==========================================================================================
function (discretizer::Collocation)(ocp::AbstractOptimalControlProblem)
function (discretizer::Collocation)(ocp::AbstractModel)

# common parts for builders
docp = get_docp(discretizer, ocp)
exa_getter = nothing # will be set in build_exa_model

# ==========================================================================================
# The needed builders for the construction of the final DiscretizedOptimalControlProblem
# The needed builders for the construction of the final DiscretizedModel
# ==========================================================================================

# NLP builder for ADNLPModels
function build_adnlp_model(
initial_guess::CTModels.AbstractOptimalControlInitialGuess;
initial_guess::CTModels.AbstractInitialGuess;
backend,
kwargs...
)::ADNLPModels.ADNLPModel
Expand Down Expand Up @@ -211,7 +211,7 @@ function (discretizer::Collocation)(ocp::AbstractOptimalControlProblem)
# NLP builder for ExaModels
function build_exa_model(
::Type{BaseType},
initial_guess::CTModels.AbstractOptimalControlInitialGuess;
initial_guess::CTModels.AbstractInitialGuess;
backend
)::ExaModels.ExaModel where {BaseType<:AbstractFloat}

Expand Down Expand Up @@ -260,7 +260,7 @@ function (discretizer::Collocation)(ocp::AbstractOptimalControlProblem)
end

#NB. it would be better to return builders as model/solution pairs since they are linked
return CTSolvers.DiscretizedOptimalControlProblem(
return CTSolvers.DiscretizedModel(
ocp,
CTSolvers.ADNLPModelBuilder(build_adnlp_model),
CTSolvers.ExaModelBuilder(build_exa_model),
Expand Down
2 changes: 1 addition & 1 deletion src/collocation_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ julia> DOCP_initial_guess(docp)
[0.1, 0.1, …]
```
"""
function DOCP_initial_guess(docp::DOCP, init::CTModels.OptimalControlInitialGuess)
function DOCP_initial_guess(docp::DOCP, init::CTModels.InitialGuess)

# default initialization (including internal variables such as k_i for RK schemes)
# NB. passing nothing to the setters below will leave this default values
Expand Down
2 changes: 1 addition & 1 deletion test/manual_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ include("./problems/beam.jl")
include("./problems/goddard.jl")
include("./problems/double_integrator.jl")

#sol = solve_problem(goddard(); display=true)
sol = solve_problem(goddard(); display=true)
sol1 = solve_problem(goddard2(); display=true, modeler=:exa, solver=:madnlp, scheme=:trapeze)

8 changes: 4 additions & 4 deletions test/test_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ function solve_problem(prob;

# NLP modeler
if modeler == :adnlp
my_modeler = CTSolvers.ADNLPModeler(; backend=adnlp_backend) # kwargs
my_modeler = CTSolvers.ADNLP(; backend=adnlp_backend) # kwargs
elseif modeler == :exa
my_modeler = CTSolvers.ExaModeler(; backend=exa_backend) # kwargs
my_modeler = CTSolvers.Exa(; backend=exa_backend) # kwargs
else
error("Unknown modeler: ", modeler)
end
Expand All @@ -66,7 +66,7 @@ function solve_problem(prob;
:linear_solver => "mumps",
:sb => "yes",
)
my_solver = CTSolvers.IpoptSolver(; ipopt_options...)
my_solver = CTSolvers.Ipopt(; ipopt_options...)
elseif solver == :madnlp
if isnothing(linear_solver)
solver = MumpsSolver
Expand All @@ -82,7 +82,7 @@ function solve_problem(prob;
madnlp_options[:bound_relax_factor] = bound_relax_factor
madnlp_options[:mode] = :permissive
end
my_solver = CTSolvers.MadNLPSolver(; madnlp_options...)
my_solver = CTSolvers.MadNLP(; madnlp_options...)
else
error("Unknown solver: ", solver)
end
Expand Down
10 changes: 5 additions & 5 deletions test/tmp/test_collocation.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Unit tests for Collocation discretizer wiring from OCP to discretized OCP and builders.
struct DummyOCPCollocation <: CTModels.AbstractOptimalControlProblem end
struct DummyOCPCollocation <: CTModels.AbstractModel end

struct DummyOCPExaRouting <: CTModels.AbstractOptimalControlProblem end
struct DummyOCPExaRouting <: CTModels.AbstractModel end

struct DummyDOCPCollocationRouting end

Expand Down Expand Up @@ -51,8 +51,8 @@ function test_ctdirect_collocation()

docp = discretizer(ocp)

# The call operator on Collocation should return a DiscretizedOptimalControlProblem
Test.@test docp isa CTModels.DiscretizedOptimalControlProblem
# The call operator on Collocation should return a DiscretizedModel
Test.@test docp isa CTModels.DiscretizedModel
Test.@test CTModels.ocp_model(docp) === ocp

# The model and solution builders should be correctly wired with both
Expand Down Expand Up @@ -81,7 +81,7 @@ function test_ctdirect_collocation()
exa_builder = CTModels.get_exa_model_builder(docp)

# Minimal initial guess: functions for state/control and empty variable
init_guess = CTModels.OptimalControlInitialGuess(t -> 0.0, t -> 0.0, Float64[])
init_guess = CTModels.InitialGuess(t -> 0.0, t -> 0.0, Float64[])

BaseType = Float32
exa_nlp = exa_builder(BaseType, init_guess; backend=:gpu, foo=1)
Expand Down
6 changes: 3 additions & 3 deletions test/tmp/test_core_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function test_ctdirect_core_types()
# Trapeze is an alias to Trapezoidal
Test.@test CTDirect.TrapezeScheme === CTDirect.TrapezoidalScheme

# AbstractOptimalControlDiscretizer should be abstract
Test.@test isabstracttype(CTDirect.AbstractOptimalControlDiscretizer)
# AbstractDiscretizer should be abstract
Test.@test isabstracttype(CTDirect.AbstractDiscretizer)

# Collocation should be a concrete discretizer subtype
Test.@test CTDirect.Collocation <: CTDirect.AbstractOptimalControlDiscretizer
Test.@test CTDirect.Collocation <: CTDirect.AbstractDiscretizer
end

# ========================================================================
Expand Down
12 changes: 6 additions & 6 deletions test/tmp/test_discretization_api.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Unit tests for the discretization API (discretize with custom and default discretizers).
struct DummyOCPDiscretize <: CTModels.AbstractOptimalControlProblem end
struct DummyOCPDiscretize <: CTModels.AbstractModel end

struct DummyDiscretizer <: CTDirect.AbstractOptimalControlDiscretizer
struct DummyDiscretizer <: CTDirect.AbstractDiscretizer
calls::Base.RefValue{Int}
tag::Symbol
end

function (d::DummyDiscretizer)(ocp::CTModels.AbstractOptimalControlProblem)
function (d::DummyDiscretizer)(ocp::CTModels.AbstractModel)
d.calls[] += 1
return (ocp, d.tag)
end
Expand Down Expand Up @@ -37,13 +37,13 @@ function test_ctdirect_discretization_api()

docp = CTDirect.discretize(ocp)

# The default discretizer should produce a DiscretizedOptimalControlProblem
Test.@test docp isa CTModels.DiscretizedOptimalControlProblem
# The default discretizer should produce a DiscretizedModel
Test.@test docp isa CTModels.DiscretizedModel
Test.@test CTModels.ocp_model(docp) === ocp

# And the low-level __discretizer() helper should return a Collocation
disc = CTDirect.__discretizer()
Test.@test disc isa CTDirect.AbstractOptimalControlDiscretizer
Test.@test disc isa CTDirect.AbstractDiscretizer
Test.@test disc isa CTDirect.Collocation
end
end
8 changes: 4 additions & 4 deletions test/tmp/test_solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ function test_solve()
init = CTModels.initial_guess(ocp; beam_data.init...)
discretizer = CTDirect.Collocation()
docp = CTDirect.discretize(ocp, discretizer)
Test.@test docp isa CTModels.DiscretizedOptimalControlProblem
Test.@test docp isa CTModels.DiscretizedModel

# NLP solver
solvers = [CTSolvers.IpoptSolver(; ipopt_options...)]
solvers = [CTSolvers.Ipopt(; ipopt_options...)]
solvers_names = ["Ipopt"]

# NLP modelers
modelers = [CTModels.ADNLPModeler(), CTModels.ExaModeler()]
modelers_names = ["ADNLPModeler", "ExaModeler (CPU)"]
modelers = [CTModels.ADNLP(), CTModels.Exa()]
modelers_names = ["ADNLP", "Exa (CPU)"]

# solve DOCP with common solve and NLP modelers
Test.@testset "DOCP level (solve)" verbose=VERBOSE showtiming=SHOWTIMING begin
Expand Down
Loading