-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #792 from alonsoC1s/wrap_nlpmodel
Add a conversion mechanism between NLPModel to OptimizationProblem
- Loading branch information
Showing
8 changed files
with
283 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |