Models
The following general models are implemented in this package:
In addition, the following nonlinear least squares models are implemented in this package:
There are other external models implemented. In particular,
- AmplModel
- CUTEstModel
- MathProgNLPModel and MathProgNLSModel using
JuMP/MPB
.
There are currently two models implemented in this package, besides the external ones.
ADNLPModel
NLPModels.ADNLPModel
— Type.ADNLPModel is an AbstractNLPModel using ForwardDiff to compute the derivatives. In this interface, the objective function $f$ and an initial estimate are required. If there are constraints, the function $c:\mathbb{R}^n\rightarrow\mathbb{R}^m$ and the vectors $c_L$ and $c_U$ also need to be passed. Bounds on the variables and an inital estimate to the Lagrangian multipliers can also be provided.
ADNLPModel(f, x0; lvar = [-∞,…,-∞], uvar = [∞,…,∞], y0 = zeros,
c = NotImplemented, lcon = [-∞,…,-∞], ucon = [∞,…,∞], name = "Generic")
f :: Function
- The objective function $f$;x0 :: AbstractVector
- The initial point of the problem;lvar :: AbstractVector
- $\ell$, the lower bound of the variables;uvar :: AbstractVector
- $u$, the upper bound of the variables;c :: Function
- The constraints function $c$;y0 :: AbstractVector
- The initial value of the Lagrangian estimates;lcon :: AbstractVector
- $c_L$, the lower bounds of the constraints function;ucon :: AbstractVector
- $c_U$, the upper bounds of the constraints function;name :: String
- A name for the model.
The functions follow the same restrictions of ForwardDiff functions, summarised here:
- The function can only be composed of generic Julia functions;
- The function must accept only one argument;
- The function's argument must accept a subtype of AbstractVector;
- The function should be type-stable.
For contrained problems, the function $c$ is required, and it must return an array even when m = 1, and $c_L$ and $c_U$ should be passed, otherwise the problem is ill-formed. For equality constraints, the corresponding index of $c_L$ and $c_U$ should be the same.
Example
using NLPModels
f(x) = sum(x.^4)
x = [1.0; 0.5; 0.25; 0.125]
nlp = ADNLPModel(f, x)
grad(nlp, x)
4-element Array{Float64,1}:
4.0
0.5
0.0625
0.0078125
SimpleNLPModel
NLPModels.SimpleNLPModel
— Type.SimpleNLPModel is an AbstractNLPModel that uses only user-defined functions. In this interface, the objective function $f$ and an initial estimate are required. If the user wants to use derivatives, they need to be passed. The same goes for the Hessian and Hessian-AbstractVector product. For constraints, $c:\mathbb{R}^n\rightarrow\mathbb{R}^m$ and the vectors $c_L$ and $c_U$ also need to be passed. Bounds on the variables and an inital estimate to the Lagrangian multipliers can also be provided. The user can also pass the Jacobian and the Lagrangian Hessian and Hessian-AbstractVector product.
SimpleNLPModel(f, x0; lvar = [-∞,…,-∞], uvar = [∞,…,∞], y0=zeros,
lcon = [-∞,…,-∞], ucon = [∞,…,∞], name = "Generic",
[list of functions])
f :: Function
- The objective function $f$;x0 :: AbstractVector
- The initial point of the problem;lvar :: AbstractVector
- $\ell$, the lower bound of the variables;uvar :: AbstractVector
- $u$, the upper bound of the variables;y0 :: AbstractVector
- The initial value of the Lagrangian estimates;lcon :: AbstractVector
- $c_L$, the lower bounds of the constraints function;ucon :: AbstractVector
- $c_U$, the upper bounds of the constraints function;name :: String
- A name for the model.
All functions passed have a direct correlation with a NLP function. You don't have to define any more than you need, but calling an undefined function will throw a NotImplementedError
. The list is
g
andg!
: $\nabla f(x)$, the gradient of the objective function;gx = g(x) gx = g!(x, gx)
H
: The lower triangle of the Hessian of the objective function or of the Lagrangian;Hx = H(x; obj_weight=1.0) # if the problem is unconstrained Hx = H(x; obj_weight=1.0, y=zeros) # if the problem is constrained
Hcoord
- The lower triangle of the Hessian of the objective function or of the Lagrangian, in triplet format;(rows,cols,vals) = Hcoord(x; obj_weight=1.0) # if the problem is unconstrained (rows,cols,vals) = Hcoord(x; obj_weight=1.0, y=zeros) # if the problem is constrained
Hp
andHp!
- The product of the Hessian of the objective function or of the Lagrangian by a vector;Hv = Hp(x, v, obj_weight=1.0) # if the problem is unconstrained Hv = Hp!(x, v, Hv, obj_weight=1.0) # if the problem is unconstrained Hv = Hp(x, v, obj_weight=1.0, y=zeros) # if the problem is constrained Hv = Hp!(x, v, Hv, obj_weight=1.0, y=zeros) # if the problem is constrained
c
andc!
- $c(x)$, the constraints function;cx = c(x) cx = c!(x, cx)
J
- $J(x)$, the Jacobian of the constraints;Jx = J(x)
Jcoord
- $J(x)$, the Jacobian of the constraints, in triplet format;(rows,cols,vals) = Jcoord(x)
Jp
andJp!
- The Jacobian-vector product;Jv = Jp(x, v) Jv = Jp!(x, v, Jv)
Jtp
andJtp!
- The Jacobian-transposed-vector product;Jtv = Jtp(x, v) Jtv = Jtp!(x, v, Jtv)
For contrained problems, the function $c$ is required, and it must return an array even when m = 1, and $c_L$ and $c_U$ should be passed, otherwise the problem is ill-formed. For equality constraints, the corresponding index of $c_L$ and $c_U$ should be the same.
Example
using NLPModels
f(x) = sum(x.^4)
g(x) = 4*x.^3
x = [1.0; 0.5; 0.25; 0.125]
nlp = SimpleNLPModel(f, x, g=g)
grad(nlp, x)
4-element Array{Float64,1}:
4.0
0.5
0.0625
0.0078125
Derived Models
The following models are created from any given model, making some modification to that model.
SlackModel
NLPModels.SlackModel
— Type.A model whose only inequality constraints are bounds.
Given a model, this type represents a second model in which slack variables are introduced so as to convert linear and nonlinear inequality constraints to equality constraints and bounds. More precisely, if the original model has the form
\[ \min f(x) \mbox{ s. t. } cL \leq c(x) \leq cU \mbox{ and } \ell \leq x \leq u, \]
the new model appears to the user as
\[ \min f(X) \mbox{ s. t. } g(X) = 0 \mbox{ and } L \leq X \leq U. \]
The unknowns $X = (x, s)$ contain the original variables and slack variables $s$. The latter are such that the new model has the general form
\[ \min f(x) \mbox{ s. t. } c(x) - s = 0, cL \leq s \leq cU \mbox{ and } \ell \leq x \leq u, \]
although no slack variables are introduced for equality constraints.
The slack variables are implicitly ordered as [s(low), s(upp), s(rng)], where low
, upp
and rng
represent the indices of the constraints of the form $c_L \leq c(x) < \infty$, $-\infty < c(x) \leq c_U$ and $c_L \leq c(x) \leq c_U$, respectively.
Example
using NLPModels
f(x) = x[1]^2 + 4x[2]^2
c(x) = [x[1]*x[2] - 1]
x = [2.0; 2.0]
nlp = ADNLPModel(f, x, c=c, lcon=[0.0])
nlp_slack = SlackModel(nlp)
nlp_slack.meta.lvar
3-element Array{Float64,1}:
-Inf
-Inf
0.0
LBFGSModel
NLPModels.LBFGSModel
— Type.Construct a LBFGSModel
from another type of model.
LSR1Model
NLPModels.LSR1Model
— Type.Construct a LSR1Model
from another type of nlp.
ADNLSModel
NLPModels.ADNLSModel
— Type.ADNLSModel is an Nonlinear Least Squares model using ForwardDiff to compute the derivatives.
ADNLSModel(F, x0, m; lvar = [-∞,…,-∞], uvar = [∞,…,∞], y0 = zeros,
c = NotImplemented, lcon = [-∞,…,-∞], ucon = [∞,…,∞], name = "Generic")
F :: Function
- The residual function $F$;x0 :: AbstractVector
- The initial point of the problem;m :: Int
- The dimension of $F(x)$, i.e., the number of
equations in the nonlinear system.
The other parameters are as in ADNLPModel
.
using NLPModels
F(x) = [x[1] - 1; 10*(x[2] - x[1]^2)]
nlp = ADNLSModel(F, [-1.2; 1.0], 2)
residual(nlp, nlp.meta.x0)
2-element Array{Float64,1}:
-2.2
-4.3999999999999995
FeasibilityResidual
NLPModels.FeasibilityResidual
— Type.A feasibility residual model is created from a NLPModel of the form
min f(x)
s.t cℓ ≤ c(x) ≤ cu
bℓ ≤ x ≤ bu
by creating slack variables s and defining F(x,s) = c(x) - s. The resulting NLS problem is
min ¹/₂‖c(x) - s‖²
bℓ ≤ x ≤ bu
cℓ ≤ s ≤ bu
This is done using SlackModel first, and then defining the NLS. Notice that if bℓᵢ = buᵢ, no slack variable is created.
LLSModel
NLPModels.LLSModel
— Type.nls = LLSModel(A, b; lvar, uvar, C, lcon, ucon)
Creates a Linear Least Squares model ½‖Ax - b‖² with optional bounds lvar ≦ x ≦ y
and optional linear constraints lcon ≦ Cx ≦ ucon
.
SimpleNLSModel
NLPModels.SimpleNLSModel
— Type.nls = SimpleNLSModel(n; F=F, F! =F!, JF=JF, JFp=JFp, JFp! =JFp!,
JFtp=JFtp, JFtp! =JFtp!)
nls = SimpleNLSModel(x0; F=F, F! =F!, JF=JF, JFp=JFp, JFp! =JFp!,
JFtp=JFtp, JFtp! =JFtp!)
Creates a Nonlinear Linear Least Squares model to minimize ‖F(x)‖². If JF = JF(x) is passed, the Jacobian is available.
SlackNLSModel
NLPModels.SlackNLSModel
— Type.Like SlackModel
, this model converts inequalities into equalities and bounds.
FeasibilityFormNLS
NLPModels.FeasibilityFormNLS
— Type.Converts a nonlinear least-squares problem with residual F(x)
to a nonlinear optimization problem with constraints F(x) = r
and objective ¹/₂‖r‖²
. In other words, converts
min ¹/₂‖F(x)‖²
s.t cₗ ≤ c(x) ≤ cᵤ
ℓ ≤ x ≤ u
to
min ¹/₂‖r‖²
s.t F(x) - r = 0
cₗ ≤ c(x) ≤ cᵤ
ℓ ≤ x ≤ u
If you rather have the first problem, the nls
model already works as an NLPModel of that format.