diff --git a/src/jump_model.jl b/src/jump_model.jl index 6ee280dd..606c8d4f 100644 --- a/src/jump_model.jl +++ b/src/jump_model.jl @@ -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] diff --git a/test/hs30.jl b/test/hs30.jl new file mode 100644 index 00000000..1d187d7b --- /dev/null +++ b/test/hs30.jl @@ -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 diff --git a/test/hs43.jl b/test/hs43.jl new file mode 100644 index 00000000..8aedda6c --- /dev/null +++ b/test/hs43.jl @@ -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 diff --git a/test/mgh07.jl b/test/mgh07.jl new file mode 100644 index 00000000..74093288 --- /dev/null +++ b/test/mgh07.jl @@ -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 \ No newline at end of file diff --git a/test/mgh35.jl b/test/mgh35.jl new file mode 100644 index 00000000..9f3b8cee --- /dev/null +++ b/test/mgh35.jl @@ -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 \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 72d37944..4ca1febf 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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" diff --git a/test/test_mathprognlsmodel.jl b/test/test_mathprognlsmodel.jl new file mode 100644 index 00000000..2b4713b5 --- /dev/null +++ b/test/test_mathprognlsmodel.jl @@ -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() \ No newline at end of file