Skip to content

Commit ebefee7

Browse files
amontoisondpo
authored andcommitted
added support of user-defined functions in MathProgNLSModel
1 parent ed39771 commit ebefee7

File tree

7 files changed

+154
-2
lines changed

7 files changed

+154
-2
lines changed

src/jump_model.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function MathProgNLSModel(cmodel :: JuMP.Model,
2020
ev = cmodel.internalModel.eval
2121

2222
Fmodel = JuMP.Model()
23-
@objective(Fmodel, Min, 0.0)
23+
@NLobjective(Fmodel, Min, 0.0)
24+
Fmodel.nlpdata.user_operators = cmodel.nlpdata.user_operators
2425
@variable(Fmodel, x[1:MathProgBase.numvar(cmodel)])
2526
for Fi in F
2627
expr = ev.subexpressions_as_julia_expressions[Fi.index]

test/hs30.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# HS problem 30 in NLS format
2+
#
3+
# Source:
4+
# W. Hock and K. Schittkowski,
5+
# Test examples for nonlinear programming codes,
6+
# Lecture Notes in Economics and Mathematical Systems 187,
7+
# Springer Verlag Berlin Heidelberg, 1981
8+
# 10.1007/978-3-642-48320-2
9+
#
10+
# A. S. Siqueira, Curitiba/BR, 04/2018.
11+
12+
export hs30
13+
14+
"Hock-Schittkowski problem 30 in NLS format"
15+
function hs30()
16+
17+
model = Model()
18+
lvar = [1.0; -10.0; -10.0]
19+
@variable(model, lvar[i] <= x[i=1:3] <= 10, start=1.0)
20+
@NLexpression(model, F[i=1:3], x[i] + 0.0)
21+
@NLconstraint(model, x[1]^2 + x[2]^2 >= 1.0)
22+
23+
return MathProgNLSModel(model, F, name="hs30")
24+
end

test/hs43.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# HS problem 43 in NLS format without constants in the objective
2+
#
3+
# Source:
4+
# W. Hock and K. Schittkowski,
5+
# Test examples for nonlinear programming codes,
6+
# Lecture Notes in Economics and Mathematical Systems 187,
7+
# Springer Verlag Berlin Heidelberg, 1981
8+
# 10.1007/978-3-642-48320-2
9+
#
10+
# A. S. Siqueira, Curitiba/BR, 04/2018.
11+
12+
export hs43
13+
14+
"Hock-Schittkowski problem 43 in NLS format without constants in the objective"
15+
function hs43()
16+
17+
model = Model()
18+
@variable(model, x[1:4], start=0.0)
19+
@NLexpression(model, F1, x[1] - 5/2)
20+
@NLexpression(model, F2, x[2] - 5/2)
21+
@NLexpression(model, F3, sqrt(2) * (x[3] - 21 / 4))
22+
@NLexpression(model, F4, x[4] + 7/2)
23+
@NLconstraint(model, 8 - x[1]^2 - x[2]^2 - x[3]^2 - x[4]^2 - x[1] +
24+
x[2] - x[3] + x[4] >= 0.0)
25+
@NLconstraint(model, 10 - x[1]^2 - 2 * x[2]^2 - x[3]^2 - 2 * x[4]^2 +
26+
x[1] + x[4] >= 0.0)
27+
@NLconstraint(model, 5 - 2 * x[1]^2 - x[2]^2 - x[3]^2 - 2 * x[1] +
28+
x[2] + x[4] >= 0.0)
29+
30+
return MathProgNLSModel(model, [F1; F2; F3; F4], name="hs43")
31+
end

test/mgh07.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# MGH problem 7 - Helical valley function
2+
#
3+
# Source:
4+
# J. J. Moré, B. S. Garbow and K. E. Hillstrom
5+
# Testing Unconstrained Optimization Software
6+
# ACM Transactions on Mathematical Software, 7(1):17-41, 1981
7+
#
8+
# A. Montoison, Montreal, 05/2018.
9+
10+
export mgh07
11+
12+
"Helical valley function"
13+
function mgh07()
14+
15+
nls = Model()
16+
x0 = [-1.0, 0.0, 0.0]
17+
@variable(nls, x[i=1:3], start = x0[i])
18+
19+
θ_aux(t) = (t > 0 ? 0.0 : 0.5)
20+
JuMP.register(nls, :θ_aux, 1, θ_aux, autodiff=true)
21+
22+
@NLexpression(nls, F1, 10*(x[3] - 10*(atan(x[2]/x[1])/(2*π) + θ_aux(x[1]))))
23+
@NLexpression(nls, F2, 10*(sqrt(x[1]^2 + x[2]^2) - 1.0))
24+
@NLexpression(nls, F3, 1*x[3])
25+
26+
return MathProgNLSModel(nls, [F1, F2, F3], name="mgh07")
27+
end

test/mgh35.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# MGH problem 35 - Chebyquad function
2+
#
3+
# Source:
4+
# J. J. Moré, B. S. Garbow and K. E. Hillstrom
5+
# Testing Unconstrained Optimization Software
6+
# ACM Transactions on Mathematical Software, 7(1):17-41, 1981
7+
#
8+
# A. Montoison, Montreal, 05/2018.
9+
10+
export mgh35
11+
12+
"Chebyquad function"
13+
function mgh35(m :: Int = 10, n :: Int = 10)
14+
if m < n
15+
warn(": number of function must be ≥ number of variables. Adjusting to m = n")
16+
m = n
17+
end
18+
19+
nls = Model()
20+
x0 = collect(1:n)/(n+1)
21+
@variable(nls, x[i=1:n], start = x0[i])
22+
23+
function Tsim(x, n)
24+
if n == 0
25+
return 1
26+
elseif n == 1
27+
return x
28+
else
29+
return 2*x*Tsim(x,n-1) - Tsim(x,n-2)
30+
end
31+
end
32+
33+
Ts = Vector{Function}(n)
34+
Tnames = Vector{Symbol}(n)
35+
for i = 1:n
36+
Ts[i] = x -> Tsim(2*x-1, i)
37+
Tnames[i] = gensym()
38+
JuMP.register(nls, Tnames[i], 1, Ts[i], autodiff=true)
39+
end
40+
41+
I = [i%2 == 0 ? -1/(i^2-1) : 0 for i = 1:n]
42+
@NLexpression(nls, F[i=1:n], sum($(Tnames[i])(x[j]) for j = 1:n)/n - I[i])
43+
44+
return MathProgNLSModel(nls, F, name="mgh35")
45+
end

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Base.Test, Ipopt, JuMP, MathProgBase, NLPModels, LinearOperators
22

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

@@ -40,6 +40,9 @@ end
4040
@assert isa(hess_op(model, [0.]), LinearOperator)
4141
@assert isa(jac_op(model, [0.]), LinearOperator)
4242

43+
# Tests of MathProgNLSModel with constraints and user-defined functions
44+
include("test_mathprognlsmodel.jl")
45+
4346
# ADNLPModel with no functions
4447
model = ADNLPModel(x->dot(x,x), zeros(2), name="square")
4548
@assert model.meta.name == "square"

test/test_mathprognlsmodel.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
println()
2+
println("Testing MathProgNLSModel")
3+
4+
@printf("%-15s %4s %4s %4s %10s %10s %10s\n",
5+
"Problem", "nequ", "nvar", "ncon", "‖F(x₀)‖²", "‖JᵀF‖",
6+
"‖c(x₀)‖")
7+
# Test that every problem can be instantiated.
8+
for prob in [:mgh07,:mgh35,:hs30,:hs43]
9+
prob_fn = eval(prob)
10+
nls = prob_fn()
11+
N, n, m = nls.nls_meta.nequ, nls.meta.nvar, nls.meta.ncon
12+
x = nls.meta.x0
13+
Fx = residual(nls, x)
14+
Jx = jac_op_residual(nls, x)
15+
nFx = dot(Fx, Fx)
16+
JtF = norm(Jx' * Fx)
17+
ncx = m > 0 ? @sprintf("%10.4e", norm(cons(nls, x))) : "NA"
18+
@printf("%-15s %4d %4d %4d %10.4e %10.4e %10s\n",
19+
prob, N, n, m, nFx, JtF, ncx)
20+
end
21+
println()

0 commit comments

Comments
 (0)