# ManualNLPModels

This package exists to simplify a little the process of creating an user-defined NLPModel. The advantages of using this package is that you avoid dealing with the internals of a JSO-compliant model. The disadvantages are that you do not have as much flexibility.

## Usage

We provide one structure/constructor: NLPModel. You have to provide the starting point and the objective function. The additional functions are passed through keyword arguments. You can also pass bounds on the variables.

• nlp = NLPModel(x ,f): Only the objective.
• nlp = NLPModel(x, f, grad=my_grad): Also the gradient (in place).
• nlp = NLPModel(x, f, grad=my_grad, hprod=my_hprod): Also the Hessian-vector product (in place).

Check the details in the reference of NLPModel.

### Objective and gradient of a function

using ManualNLPModels, JSOSolvers

f(x) = (x - 1)^2 + 4 * (x - x^2)^2
g!(gx, x) = begin
gx = 2 * (x - 1) - 16 * x * (x - x^2)
gx = 8 * (x - x^2)
gx
end

nlp = NLPModel(
[-1.2; 1.0],
f,
)

output = lbfgs(nlp)
println(output)
Generic Execution stats
status: first-order stationary
objective value: 2.239721910559509e-18
primal feasibility: 0.0
dual feasibility: 4.018046284781729e-9
solution: [0.9999999986742657  0.9999999970013461]
iterations: 18
elapsed time: 0.13563203811645508

### Objective and gradient at the same time

using ManualNLPModels, JSOSolvers

f(x) = (x - 1)^2 + 4 * (x - x^2)^2
fg!(gx, x) = begin
y1, y2 = x - 1, x - x^2
f = y1^2 + 4 * y2^2
gx = 2 * y1 - 16 * x * y2
gx = 8 * y2
return f, gx
end

nlp = NLPModel(
[-1.2; 1.0],
f,
)

output = lbfgs(nlp)
println(output)
Generic Execution stats
status: first-order stationary
objective value: 2.239721910559509e-18
primal feasibility: 0.0
dual feasibility: 4.018046284781729e-9
solution: [0.9999999986742657  0.9999999970013461]
iterations: 18
elapsed time: 0.023638010025024414

### Objective, gradient, and Hessian-vector product

using ManualNLPModels, JSOSolvers

f(x) = (x - 1)^2 + 4 * (x - x^2)^2
fg!(gx, x) = begin
y1, y2 = x - 1, x - x^2
f = y1^2 + 4 * y2^2
gx = 2 * y1 - 16 * x * y2
gx = 8 * y2
return f, gx
end
hv!(hv, x, v; obj_weight = 1.0) = begin
h11 = 2 - 16 * x + 48 * x^2
h12 = -16 * x
h22 = 8.0
hv = (h11 * v + h12 * v) * obj_weight
hv = (h12 * v + h22 * v) * obj_weight
return hv
end

nlp = NLPModel(
[-1.2; 1.0],
f,
println(output)
Generic Execution stats
elapsed time: 0.025630950927734375