Skip to content

Commit

Permalink
added support of user-defined functions in MathProgNLSModel
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison authored and dpo committed Jun 7, 2018
1 parent ed39771 commit ebefee7
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/jump_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function MathProgNLSModel(cmodel :: JuMP.Model,
ev = cmodel.internalModel.eval

Fmodel = JuMP.Model()
@objective(Fmodel, Min, 0.0)
@NLobjective(Fmodel, Min, 0.0)
Fmodel.nlpdata.user_operators = cmodel.nlpdata.user_operators
@variable(Fmodel, x[1:MathProgBase.numvar(cmodel)])
for Fi in F
expr = ev.subexpressions_as_julia_expressions[Fi.index]
Expand Down
24 changes: 24 additions & 0 deletions test/hs30.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# HS problem 30 in NLS format
#
# Source:
# W. Hock and K. Schittkowski,
# Test examples for nonlinear programming codes,
# Lecture Notes in Economics and Mathematical Systems 187,
# Springer Verlag Berlin Heidelberg, 1981
# 10.1007/978-3-642-48320-2
#
# A. S. Siqueira, Curitiba/BR, 04/2018.

export hs30

"Hock-Schittkowski problem 30 in NLS format"
function hs30()

model = Model()
lvar = [1.0; -10.0; -10.0]
@variable(model, lvar[i] <= x[i=1:3] <= 10, start=1.0)
@NLexpression(model, F[i=1:3], x[i] + 0.0)
@NLconstraint(model, x[1]^2 + x[2]^2 >= 1.0)

return MathProgNLSModel(model, F, name="hs30")
end
31 changes: 31 additions & 0 deletions test/hs43.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# HS problem 43 in NLS format without constants in the objective
#
# Source:
# W. Hock and K. Schittkowski,
# Test examples for nonlinear programming codes,
# Lecture Notes in Economics and Mathematical Systems 187,
# Springer Verlag Berlin Heidelberg, 1981
# 10.1007/978-3-642-48320-2
#
# A. S. Siqueira, Curitiba/BR, 04/2018.

export hs43

"Hock-Schittkowski problem 43 in NLS format without constants in the objective"
function hs43()

model = Model()
@variable(model, x[1:4], start=0.0)
@NLexpression(model, F1, x[1] - 5/2)
@NLexpression(model, F2, x[2] - 5/2)
@NLexpression(model, F3, sqrt(2) * (x[3] - 21 / 4))
@NLexpression(model, F4, x[4] + 7/2)
@NLconstraint(model, 8 - x[1]^2 - x[2]^2 - x[3]^2 - x[4]^2 - x[1] +
x[2] - x[3] + x[4] >= 0.0)
@NLconstraint(model, 10 - x[1]^2 - 2 * x[2]^2 - x[3]^2 - 2 * x[4]^2 +
x[1] + x[4] >= 0.0)
@NLconstraint(model, 5 - 2 * x[1]^2 - x[2]^2 - x[3]^2 - 2 * x[1] +
x[2] + x[4] >= 0.0)

return MathProgNLSModel(model, [F1; F2; F3; F4], name="hs43")
end
27 changes: 27 additions & 0 deletions test/mgh07.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# MGH problem 7 - Helical valley function
#
# Source:
# J. J. Moré, B. S. Garbow and K. E. Hillstrom
# Testing Unconstrained Optimization Software
# ACM Transactions on Mathematical Software, 7(1):17-41, 1981
#
# A. Montoison, Montreal, 05/2018.

export mgh07

"Helical valley function"
function mgh07()

nls = Model()
x0 = [-1.0, 0.0, 0.0]
@variable(nls, x[i=1:3], start = x0[i])

θ_aux(t) = (t > 0 ? 0.0 : 0.5)
JuMP.register(nls, :θ_aux, 1, θ_aux, autodiff=true)

@NLexpression(nls, F1, 10*(x[3] - 10*(atan(x[2]/x[1])/(2*π) + θ_aux(x[1]))))
@NLexpression(nls, F2, 10*(sqrt(x[1]^2 + x[2]^2) - 1.0))
@NLexpression(nls, F3, 1*x[3])

return MathProgNLSModel(nls, [F1, F2, F3], name="mgh07")
end
45 changes: 45 additions & 0 deletions test/mgh35.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# MGH problem 35 - Chebyquad function
#
# Source:
# J. J. Moré, B. S. Garbow and K. E. Hillstrom
# Testing Unconstrained Optimization Software
# ACM Transactions on Mathematical Software, 7(1):17-41, 1981
#
# A. Montoison, Montreal, 05/2018.

export mgh35

"Chebyquad function"
function mgh35(m :: Int = 10, n :: Int = 10)
if m < n
warn(": number of function must be ≥ number of variables. Adjusting to m = n")
m = n
end

nls = Model()
x0 = collect(1:n)/(n+1)
@variable(nls, x[i=1:n], start = x0[i])

function Tsim(x, n)
if n == 0
return 1
elseif n == 1
return x
else
return 2*x*Tsim(x,n-1) - Tsim(x,n-2)
end
end

Ts = Vector{Function}(n)
Tnames = Vector{Symbol}(n)
for i = 1:n
Ts[i] = x -> Tsim(2*x-1, i)
Tnames[i] = gensym()
JuMP.register(nls, Tnames[i], 1, Ts[i], autodiff=true)
end

I = [i%2 == 0 ? -1/(i^2-1) : 0 for i = 1:n]
@NLexpression(nls, F[i=1:n], sum($(Tnames[i])(x[j]) for j = 1:n)/n - I[i])

return MathProgNLSModel(nls, F, name="mgh35")
end
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Base.Test, Ipopt, JuMP, MathProgBase, NLPModels, LinearOperators

# Including problems so that they won't be multiply loaded
for problem in [:brownden, :genrose, :hs5, :hs6, :hs10, :hs11, :hs14, :hs15]
for problem in [:brownden, :genrose, :hs5, :hs6, :hs10, :hs11, :hs14, :hs15, :hs30, :hs43, :mgh35, :mgh07]
include("$problem.jl")
end

Expand Down Expand Up @@ -40,6 +40,9 @@ end
@assert isa(hess_op(model, [0.]), LinearOperator)
@assert isa(jac_op(model, [0.]), LinearOperator)

# Tests of MathProgNLSModel with constraints and user-defined functions
include("test_mathprognlsmodel.jl")

# ADNLPModel with no functions
model = ADNLPModel(x->dot(x,x), zeros(2), name="square")
@assert model.meta.name == "square"
Expand Down
21 changes: 21 additions & 0 deletions test/test_mathprognlsmodel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
println()
println("Testing MathProgNLSModel")

@printf("%-15s %4s %4s %4s %10s %10s %10s\n",
"Problem", "nequ", "nvar", "ncon", "‖F(x₀)‖²", "‖JᵀF‖",
"‖c(x₀)‖")
# Test that every problem can be instantiated.
for prob in [:mgh07,:mgh35,:hs30,:hs43]
prob_fn = eval(prob)
nls = prob_fn()
N, n, m = nls.nls_meta.nequ, nls.meta.nvar, nls.meta.ncon
x = nls.meta.x0
Fx = residual(nls, x)
Jx = jac_op_residual(nls, x)
nFx = dot(Fx, Fx)
JtF = norm(Jx' * Fx)
ncx = m > 0 ? @sprintf("%10.4e", norm(cons(nls, x))) : "NA"
@printf("%-15s %4d %4d %4d %10.4e %10.4e %10s\n",
prob, N, n, m, nFx, JtF, ncx)
end
println()

0 comments on commit ebefee7

Please sign in to comment.