Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a conversion mechanism between NLPModel to OptimizationProblem #792

Merged
merged 19 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9a7ee7b
Implemented conversion from NLPModel to OptimizationFunction
alonsoC1s Aug 7, 2024
ae29cad
Implemented OptimizationProblem constructor & initialized project
alonsoC1s Aug 7, 2024
5b0b867
Implemented conversion from NLPModel to OptimizationProblem
alonsoC1s Aug 7, 2024
074e533
Re-introduced adtype kwarg & fixed passing of constraints for unconst…
alonsoC1s Aug 14, 2024
13c9734
Expanded tests in problems and AD backends
alonsoC1s Aug 14, 2024
6dce7fe
Fixed tests by removing cons_h
alonsoC1s Aug 14, 2024
1660b14
Apply suggestions from code review
Vaibhavdixit02 Aug 14, 2024
4e1cef7
Added NLPModels to the CI job
alonsoC1s Aug 15, 2024
a205b4f
Fixed setting of bounds & expanded tests
alonsoC1s Aug 15, 2024
c32a4bb
Diversify solvers in tests a bit more
Vaibhavdixit02 Aug 15, 2024
578ae51
Apply suggestions from code review
Vaibhavdixit02 Aug 15, 2024
a7e286c
Added docstrings to OptimizationFunction and OptimizationProblem
alonsoC1s Aug 15, 2024
f406147
Documented translation layer
alonsoC1s Aug 15, 2024
182db2e
Update lib/OptimizationNLPModels/src/OptimizationNLPModels.jl
alonsoC1s Aug 15, 2024
72a9d3d
Added extension as docs dependency
alonsoC1s Aug 16, 2024
9d06ad5
Fixed typo and formatting issues on documentation & tests
alonsoC1s Aug 16, 2024
f38e372
Update docs/src/optimization_packages/nlpmodels.md
Vaibhavdixit02 Aug 16, 2024
f349c01
Removed unnecesary docstrings
alonsoC1s Aug 17, 2024
2c80a2b
Update lib/OptimizationNLPModels/src/OptimizationNLPModels.jl
Vaibhavdixit02 Aug 18, 2024
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
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
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"
]
]
58 changes: 58 additions & 0 deletions docs/src/optimization_packages/nlpmodels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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 containts no optimizers or models. Several packages

Check warning on line 18 in docs/src/optimization_packages/nlpmodels.md

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"containts" should be "contains".
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
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`s and
`OptimizationFunction`.

```@docs
SciMLBase.OptimizationFunction(::AbstractNLPModel, ::ADTypes.AbstractADType)
SciMLBase.OptimizationProblem(::AbstractNLPModel, ::ADTypes.AbstractADType)
```
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 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];
-1/4 * 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
Loading