Skip to content

Commit

Permalink
Merge pull request #792 from alonsoC1s/wrap_nlpmodel
Browse files Browse the repository at this point in the history
Add a conversion mechanism between NLPModel to OptimizationProblem
  • Loading branch information
Vaibhavdixit02 authored Aug 19, 2024
2 parents d8cf4fb + 2c80a2b commit 990b907
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- OptimizationQuadDIRECT
- OptimizationSpeedMapping
- OptimizationPolyalgorithms
- OptimizationNLPModels
version:
- '1'
steps:
Expand Down
4 changes: 4 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Juniper = "2ddba703-00a4-53a7-87a5-e8b9971dde84"
Manifolds = "1cead3c2-87b3-11e9-0ccd-23c62b72b94e"
Manopt = "0fc0a36d-df90-57f3-8f93-d78a9fc72bb5"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
NLPModelsTest = "7998695d-6960-4d3a-85c4-e1bceb8cd856"
NLopt = "76087f3c-5699-56af-9a33-bf431cd00edd"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationBBO = "3e6eede4-6085-4f62-9a71-46d9bc1eb92b"
Expand All @@ -25,6 +27,7 @@ OptimizationManopt = "e57b7fff-7ee7-4550-b4f0-90e9476e9fb6"
OptimizationMetaheuristics = "3aafef2f-86ae-4776-b337-85a36adf0b55"
OptimizationMultistartOptimization = "e4316d97-8bbb-4fd3-a7d8-3851d2a72823"
OptimizationNLopt = "4e6fcdb7-1186-4e1f-a706-475e75c168bb"
OptimizationNLPModels = "064b21be-54cf-11ef-1646-cdfee32b588f"
OptimizationNOMAD = "2cab0595-8222-4775-b714-9828e6a9e01b"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
OptimizationOptimisers = "42dfb2eb-d2b4-4451-abcd-913932933ac1"
Expand Down Expand Up @@ -66,6 +69,7 @@ OptimizationManopt = "0.0.2, 0.0.3"
OptimizationMetaheuristics = "0.1, 0.2"
OptimizationMultistartOptimization = "0.1, 0.2"
OptimizationNLopt = "0.1, 0.2"
OptimizationNLPModels = "0.0.1"
OptimizationNOMAD = "0.1, 0.2"
OptimizationOptimJL = "0.1, 0.2, 0.3"
OptimizationOptimisers = "0.1, 0.2"
Expand Down
3 changes: 2 additions & 1 deletion docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pages = ["index.md",
"PRIMA.jl" => "optimization_packages/prima.md",
"Polyalgorithms.jl" => "optimization_packages/polyopt.md",
"QuadDIRECT.jl" => "optimization_packages/quaddirect.md",
"SpeedMapping.jl" => "optimization_packages/speedmapping.md"
"SpeedMapping.jl" => "optimization_packages/speedmapping.md",
"NLPModels.jl" => "optimization_packages/nlpmodels.md"
]
]
54 changes: 54 additions & 0 deletions docs/src/optimization_packages/nlpmodels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# NLPModels.jl

[NLPModels](https://jso.dev/NLPModels.jl/latest/), similarly to Optimization.jl itself,
provides a standardized modeling interface for representing Non-Linear Programs that
facilitates using different solvers on the same problem. The Optimization.jl extension of
NLPModels aims to provide a thin translation layer to make `NLPModel`s, the main export of
the package, compatible with the optimizers in the Optimization.jl ecosystem.

## Installation: NLPModels.jl

To translate an `NLPModel`, install the OptimizationNLPModels package:

```julia
import Pkg;
Pkg.add("OptimizationNLPModels")
```

The package NLPModels.jl itself contains no optimizers or models. Several packages
provide optimization problem ([CUTEst.jl](https://jso.dev/CUTEst.jl/stable/),
[NLPModelsTest.jl](https://jso.dev/NLPModelsTest.jl/dev/)) which can then be solved with
any optimizer supported by Optimization.jl

## Usage

For example, solving a problem defined in `NLPModelsTest` with
[`Ipopt.jl`](https://github.com/jump-dev/Ipopt.jl). First, install the packages like so:

```julia
import Pkg;
Pkg.add("NLPModelsTest", "Ipopt")
```

We instantiate [problem
10](https://jso.dev/NLPModelsTest.jl/dev/reference/#NLPModelsTest.HS10) in the
Hock--Schittkowski optimization suite available from `NLPModelsTest` as `HS10`, then
translate it to an `OptimizationProblem`.

```@example NLPModels
using OptimizationNLPModels, Optimization, NLPModelsTest, Ipopt
using Optimization: OptimizationProblem
nlpmodel = NLPModelsTest.HS10()
prob = OptimizationProblem(nlpmodel, AutoForwardDiff())
```

which can now be solved like any other `OptimizationProblem`:

```@example NLPModels
sol = solve(prob, Ipopt.Optimizer())
```

## API

Problems represented as `NLPModel`s can be used to create [`OptimizationProblem`](@ref)s and
[`OptimizationFunction`](@ref).
21 changes: 21 additions & 0 deletions lib/OptimizationNLPModels/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Vaibhav Dixit <[email protected]> and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions lib/OptimizationNLPModels/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name = "OptimizationNLPModels"
uuid = "064b21be-54cf-11ef-1646-cdfee32b588f"
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
version = "0.0.1"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[extras]
NLPModelsTest = "7998695d-6960-4d3a-85c4-e1bceb8cd856"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1"

[targets]
test = ["Test", "NLPModelsTest", "OptimizationOptimJL", "Zygote", "Ipopt", "OptimizationMOI"]
63 changes: 63 additions & 0 deletions lib/OptimizationNLPModels/src/OptimizationNLPModels.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module OptimizationNLPModels

using Reexport
@reexport using NLPModels, Optimization, ADTypes

"""
OptimizationFunction(nlpmodel::AbstractNLPModel, adtype::AbstractADType = NoAD())
Returns an `OptimizationFunction` from the `NLPModel` defined in `nlpmodel` where the
available derivates are re-used from the model, and the rest are populated with the
Automatic Differentiation backend specified by `adtype`.
"""
function SciMLBase.OptimizationFunction(nlpmodel::AbstractNLPModel,
adtype::ADTypes.AbstractADType = SciMLBase.NoAD(); kwargs...)
f(x, p) = NLPModels.obj(nlpmodel, x)
grad(G, u, p) = NLPModels.grad!(nlpmodel, u, G)
hess(H, u, p) = (H .= NLPModels.hess(nlpmodel, u))
hv(Hv, u, v, p) = NLPModels.hprod!(nlpmodel, u, v, Hv)

if !unconstrained(nlpmodel) && !bound_constrained(nlpmodel)
cons(res, x, p) = NLPModels.cons!(nlpmodel, x, res)
cons_j(J, x, p) = (J .= NLPModels.jac(nlpmodel, x))
cons_jvp(Jv, v, x, p) = NLPModels.jprod!(nlpmodel, x, v, Jv)

return OptimizationFunction(
f, adtype; grad, hess, hv, cons, cons_j, cons_jvp, kwargs...)
end

return OptimizationFunction(f, adtype; grad, hess, hv, kwargs...)
end

"""
OptimizationProblem(nlpmodel::AbstractNLPModel, adtype::AbstractADType = NoAD())
Returns an `OptimizationProblem` with the bounds and constraints defined in `nlpmodel`.
The optimization function and its derivatives are re-used from `nlpmodel` when available
or populated wit the Automatic Differentiation backend specified by `adtype`.
"""
function SciMLBase.OptimizationProblem(nlpmodel::AbstractNLPModel,
adtype::ADTypes.AbstractADType = SciMLBase.NoAD(); kwargs...)
f = OptimizationFunction(nlpmodel, adtype; kwargs...)
u0 = nlpmodel.meta.x0
lb, ub = if has_bounds(nlpmodel)
(nlpmodel.meta.lvar, nlpmodel.meta.uvar)
else
(nothing, nothing)
end

lcons, ucons = if has_inequalities(nlpmodel) || has_equalities(nlpmodel)
(nlpmodel.meta.lcon, nlpmodel.meta.ucon)
else
(nothing, nothing)
end
sense = nlpmodel.meta.minimize ? Optimization.MinSense : Optimization.MaxSense

# The number of variables, geometry of u0, etc.. are valid and were checked when the
# nlpmodel was created.

return Optimization.OptimizationProblem(
f, u0; lb = lb, ub = ub, lcons = lcons, ucons = ucons, sense = sense, kwargs...)
end

end
117 changes: 117 additions & 0 deletions lib/OptimizationNLPModels/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using OptimizationNLPModels, Optimization, NLPModelsTest, Ipopt, OptimizationMOI, Zygote,
OptimizationOptimJL
using Test

@testset "NLPModels" begin
# First problem: Problem 5 in the Hock-Schittkowski suite
# https://jso.dev/NLPModelsTest.jl/dev/reference/#NLPModelsTest.HS5
# Problem with box bounds
hs5f(u, p) = sin(u[1] + u[2]) + (u[1] - u[2])^2 - (3 / 2) * u[1] + (5 / 2)u[2] + 1
f = Optimization.OptimizationFunction(hs5f, Optimization.AutoZygote())
lb = [-1.5; -3]
ub = [4.0; 3.0]
u0 = [0.0; 0.0]
oprob = Optimization.OptimizationProblem(
f, u0, lb = lb, ub = ub, sense = Optimization.MinSense)

nlpmo = NLPModelsTest.HS5()
converted = OptimizationNLPModels.OptimizationProblem(nlpmo, Optimization.AutoZygote())

sol_native = solve(oprob, Optimization.LBFGS(), maxiters = 1000)
sol_converted = solve(converted, Optimization.LBFGS(), maxiters = 1000)

@test sol_converted.retcode == sol_native.retcode
@test sol_converted.u sol_native.u

# Second problem: Brown and Dennis function
# https://jso.dev/NLPModelsTest.jl/dev/reference/#NLPModelsTest.BROWNDEN
# Problem without bounds
function brown_dennis(u, p)
return sum([((u[1] + (i / 5) * u[2] - exp(i / 5))^2 +
(u[3] + sin(i / 5) * u[4] - cos(i / 5))^2)^2 for i in 1:20])
end
f = Optimization.OptimizationFunction(brown_dennis, Optimization.AutoZygote())
u0 = [25.0; 5.0; -5.0; -1.0]
oprob = Optimization.OptimizationProblem(f, u0, sense = Optimization.MinSense)

nlpmo = NLPModelsTest.BROWNDEN()
converted = OptimizationNLPModels.OptimizationProblem(nlpmo, Optimization.AutoZygote())

sol_native = solve(oprob, BFGS())
sol_converted = solve(converted, BFGS())

@test sol_converted.retcode == sol_native.retcode
@test sol_converted.u sol_native.u

# Third problem: Problem 10 in the Hock-Schittkowski suite
# https://jso.dev/NLPModelsTest.jl/dev/reference/#NLPModelsTest.HS10
# Problem with inequality bounds
hs10(u, p) = u[1] - u[2]
hs10_cons(res, u, p) = (res .= -3.0 * u[1]^2 + 2.0 * u[1] * u[2] - u[2]^2 + 1.0)
lcons = [0.0]
ucons = [Inf]
u0 = [-10.0; 10.0]
f = Optimization.OptimizationFunction(
hs10, Optimization.AutoForwardDiff(); cons = hs10_cons)
oprob = Optimization.OptimizationProblem(
f, u0, lcons = lcons, ucons = ucons, sense = Optimization.MinSense)

nlpmo = NLPModelsTest.HS10()
converted = OptimizationNLPModels.OptimizationProblem(
nlpmo, Optimization.AutoForwardDiff())

sol_native = solve(oprob, Ipopt.Optimizer())
sol_converted = solve(converted, Ipopt.Optimizer())

@test sol_converted.retcode == sol_native.retcode
@test sol_converted.u sol_native.u

# Fourth problem: Problem 13 in the Hock-Schittkowski suite
# https://jso.dev/NLPModelsTest.jl/dev/reference/#NLPModelsTest.HS13
# Problem with box & inequality bounds
hs13(u, p) = (u[1] - 2.0)^2 + u[2]^2
hs13_cons(res, u, p) = (res .= (1.0 - u[1])^3 - u[2])
lcons = [0.0]
ucons = [Inf]
lb = [0.0; 0.0]
ub = [Inf; Inf]
u0 = [-2.0; -2.0]
f = Optimization.OptimizationFunction(
hs13, Optimization.AutoForwardDiff(); cons = hs13_cons)
oprob = Optimization.OptimizationProblem(f, u0, lb = lb, ub = ub, lcons = lcons,
ucons = ucons, sense = Optimization.MinSense)

nlpmo = NLPModelsTest.HS13()
converted = OptimizationNLPModels.OptimizationProblem(
nlpmo, Optimization.AutoForwardDiff())

sol_native = solve(oprob, Ipopt.Optimizer())
sol_converted = solve(converted, Ipopt.Optimizer())

@test sol_converted.retcode == sol_native.retcode
@test sol_converted.u sol_native.u

# Fifth problem: Problem 14 in the Hock-Schittkowski suite
# https://jso.dev/NLPModelsTest.jl/dev/reference/#NLPModelsTest.HS14
# Problem with mixed equality & inequality constraints
hs14(u, p) = (u[1] - 2.0)^2 + (u[2] - 1.0)^2
hs14_cons(res, u, p) = (res .= [u[1] - 2.0 * u[2];
-0.25 * u[1]^2 - u[2]^2 + 1.0])
lcons = [-1.0; 0.0]
ucons = [-1.0; Inf]
u0 = [2.0; 2.0]
f = Optimization.OptimizationFunction(
hs14, Optimization.AutoForwardDiff(); cons = hs14_cons)
oprob = Optimization.OptimizationProblem(
f, u0, lcons = lcons, ucons = ucons, sense = Optimization.MinSense)

nlpmo = NLPModelsTest.HS14()
converted = OptimizationNLPModels.OptimizationProblem(
nlpmo, Optimization.AutoForwardDiff())

sol_native = solve(oprob, Ipopt.Optimizer())
sol_converted = solve(converted, Ipopt.Optimizer())

@test sol_converted.retcode == sol_native.retcode
@test sol_converted.u sol_native.u
end

0 comments on commit 990b907

Please sign in to comment.