-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support of user-defined functions in MathProgNLSModel
- Loading branch information
1 parent
ed39771
commit ebefee7
Showing
7 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |