From c56d05a9a9843ac56b91cee234d09cf599d00ee4 Mon Sep 17 00:00:00 2001 From: Arnav Kapoor Date: Fri, 27 Feb 2026 22:58:15 +0530 Subject: [PATCH 1/6] Fix #354: Add warnings when dimension is modified on NZF1, spmsrtls, and bearing - NZF1: Warn when n is not a multiple of 13 (adjusted to nearest multiple) - spmsrtls: Warn when n is adjusted due to minimum dimension requirement (n >= 100) - bearing: Warn when grid dimensions are adjusted to ensure nx > 0 and ny > 0 These warnings follow the pattern already established in dixmaan* problems. --- src/ADNLPProblems/NZF1.jl | 2 ++ src/ADNLPProblems/bearing.jl | 5 +++++ src/ADNLPProblems/spmsrtls.jl | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/src/ADNLPProblems/NZF1.jl b/src/ADNLPProblems/NZF1.jl index e56c47ea..bcb84c83 100644 --- a/src/ADNLPProblems/NZF1.jl +++ b/src/ADNLPProblems/NZF1.jl @@ -6,6 +6,7 @@ function NZF1(; use_nls::Bool = false, kwargs...) end function NZF1(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} + (n % 13 == 0) || @warn("NZF1: number of variables adjusted to be a multiple of 13") nbis = max(2, div(n, 13)) n = 13 * nbis l = div(n, 13) @@ -29,6 +30,7 @@ function NZF1(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwarg end function NZF1(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} + (n % 13 == 0) || @warn("NZF1: number of variables adjusted to be a multiple of 13") nbis = max(2, div(n, 13)) n = 13 * nbis l = div(n, 13) diff --git a/src/ADNLPProblems/bearing.jl b/src/ADNLPProblems/bearing.jl index 40ab56ca..d11d401f 100644 --- a/src/ADNLPProblems/bearing.jl +++ b/src/ADNLPProblems/bearing.jl @@ -9,6 +9,11 @@ function bearing(; ) where {T} # nx > 0 # grid points in 1st direction # ny > 0 # grid points in 2nd direction + + # Ensure nx and ny are at least 1, and warn if they need adjustment + (nx > 0 && ny > 0) || @warn("bearing: grid dimensions adjusted to ensure nx > 0 and ny > 0") + nx = max(1, nx) + ny = max(1, ny) b = 10 # grid is (0,2*pi)x(0,2*b) e = 1 // 10 # eccentricity diff --git a/src/ADNLPProblems/spmsrtls.jl b/src/ADNLPProblems/spmsrtls.jl index d5516102..2a72f28a 100644 --- a/src/ADNLPProblems/spmsrtls.jl +++ b/src/ADNLPProblems/spmsrtls.jl @@ -6,8 +6,10 @@ function spmsrtls(; use_nls::Bool = false, kwargs...) end function spmsrtls(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} + n_orig = n m = max(Int(round((n + 2) / 3)), 34) n = m * 3 - 2 + (n == n_orig) || @warn("spmsrtls: number of variables adjusted from $n_orig to $n") p = [sin(i^2) for i = 1:n] x0 = T[p[i] / 5 for i = 1:n] @@ -59,8 +61,10 @@ function spmsrtls(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, k end function spmsrtls(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} + n_orig = n m = max(Int(round((n + 2) / 3)), 34) n = m * 3 - 2 + (n == n_orig) || @warn("spmsrtls: number of variables adjusted from $n_orig to $n") p = [sin(i^2) for i = 1:n] x0 = T[p[i] / 5 for i = 1:n] From d23f4596cfe51ed7d07cfdaecbe3a25565e28463 Mon Sep 17 00:00:00 2001 From: Arnav Kapoor Date: Sat, 28 Feb 2026 01:01:29 +0530 Subject: [PATCH 2/6] Update src/ADNLPProblems/bearing.jl Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/ADNLPProblems/bearing.jl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ADNLPProblems/bearing.jl b/src/ADNLPProblems/bearing.jl index d11d401f..c241bc0c 100644 --- a/src/ADNLPProblems/bearing.jl +++ b/src/ADNLPProblems/bearing.jl @@ -11,9 +11,20 @@ function bearing(; # ny > 0 # grid points in 2nd direction # Ensure nx and ny are at least 1, and warn if they need adjustment - (nx > 0 && ny > 0) || @warn("bearing: grid dimensions adjusted to ensure nx > 0 and ny > 0") + nx_orig = nx + ny_orig = ny nx = max(1, nx) ny = max(1, ny) + if nx != nx_orig || ny != ny_orig + msg_parts = String[] + if nx != nx_orig + push!(msg_parts, "nx from $(nx_orig) to $(nx)") + end + if ny != ny_orig + push!(msg_parts, "ny from $(ny_orig) to $(ny)") + end + @warn("bearing: grid dimensions adjusted: " * join(msg_parts, ", ")) + end b = 10 # grid is (0,2*pi)x(0,2*b) e = 1 // 10 # eccentricity From 7222fc4272781ba40c87b8dd50c83a7d55aeb450 Mon Sep 17 00:00:00 2001 From: Arnav Kapoor Date: Sat, 28 Feb 2026 01:04:45 +0530 Subject: [PATCH 3/6] n%13 removal --- src/ADNLPProblems/NZF1.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ADNLPProblems/NZF1.jl b/src/ADNLPProblems/NZF1.jl index bcb84c83..3bf813f5 100644 --- a/src/ADNLPProblems/NZF1.jl +++ b/src/ADNLPProblems/NZF1.jl @@ -6,9 +6,10 @@ function NZF1(; use_nls::Bool = false, kwargs...) end function NZF1(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 13 == 0) || @warn("NZF1: number of variables adjusted to be a multiple of 13") + n_orig = n nbis = max(2, div(n, 13)) n = 13 * nbis + (n == n_orig) || @warn("NZF1: number of variables adjusted from $n_orig to $n") l = div(n, 13) function f(x; l = l) return sum( @@ -30,9 +31,10 @@ function NZF1(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwarg end function NZF1(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 13 == 0) || @warn("NZF1: number of variables adjusted to be a multiple of 13") + n_orig = n nbis = max(2, div(n, 13)) n = 13 * nbis + (n == n_orig) || @warn("NZF1: number of variables adjusted from $n_orig to $n") l = div(n, 13) function F!(r, x; l = l) for i = 1:l From 039a60c8fef1faa92827d23b559bf3d08c64080f Mon Sep 17 00:00:00 2001 From: Arnav Kapoor Date: Sat, 28 Feb 2026 01:11:03 +0530 Subject: [PATCH 4/6] Add @adjust_nvar_warn macro and update all dimension adjustment warnings - Created @adjust_nvar_warn macro in ADNLPProblems module to standardize dimension adjustment warnings across all problems - Updated warning messages to consistently show both original and adjusted values following pattern: 'problem_name: number of variables adjusted from {n_orig} to {n}' - Applied macro to all problems with dimension adjustments: - NZF1 (multiple of 13) - spmsrtls (adjusted formula) - chainwoo (multiple of 4, both :nlp and :nls variants) - woods (multiple of 4) - srosenbr (multiple of 2) - catenary (multiple of 3, minimum 6) - clplatea, clplateb, clplatec (perfect squares) - fminsrf2 (minimum 4, then perfect square) - powellsg (multiple of 4, both :nlp and :nls variants) - watson (clamped between 2 and 31, both :nlp and :nls variants) Addresses issue #354 --- src/ADNLPProblems/ADNLPProblems.jl | 20 ++++++++++++++++++++ src/ADNLPProblems/NZF1.jl | 4 ++-- src/ADNLPProblems/catenary.jl | 4 ++-- src/ADNLPProblems/chainwoo.jl | 6 ++++-- src/ADNLPProblems/clplatea.jl | 3 ++- src/ADNLPProblems/clplateb.jl | 3 ++- src/ADNLPProblems/clplatec.jl | 3 ++- src/ADNLPProblems/fminsrf2.jl | 5 ++--- src/ADNLPProblems/powellsg.jl | 5 +++-- src/ADNLPProblems/spmsrtls.jl | 4 ++-- src/ADNLPProblems/srosenbr.jl | 3 ++- src/ADNLPProblems/watson.jl | 4 ++++ src/ADNLPProblems/woods.jl | 3 ++- 13 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/ADNLPProblems/ADNLPProblems.jl b/src/ADNLPProblems/ADNLPProblems.jl index 824468a1..52a0e0f5 100644 --- a/src/ADNLPProblems/ADNLPProblems.jl +++ b/src/ADNLPProblems/ADNLPProblems.jl @@ -26,6 +26,26 @@ end @require ADNLPModels = "54578032-b7ea-4c30-94aa-7cbd1cce6c9a" begin using JLD2, LinearAlgebra, SparseArrays, SpecialFunctions + """ + @adjust_nvar_warn(problem_name, n_orig, n) + + Issue a warning if the number of variables was adjusted, showing both original and adjusted values. + This macro provides consistent warning messages across all problems with dimension adjustments. + + # Example + ```julia + n_orig = n + n = 4 * max(1, div(n, 4)) + @adjust_nvar_warn("woods", n_orig, n) + ``` + """ + macro adjust_nvar_warn(problem_name, n_orig, n) + return quote + ($(esc(n)) == $(esc(n_orig))) || + @warn($(esc(problem_name)) * ": number of variables adjusted from " * string($(esc(n_orig))) * " to " * string($(esc(n)))) + end + end + path = dirname(@__FILE__) files = filter(x -> x[(end - 2):end] == ".jl", readdir(path)) for file in files diff --git a/src/ADNLPProblems/NZF1.jl b/src/ADNLPProblems/NZF1.jl index 3bf813f5..5edd35f6 100644 --- a/src/ADNLPProblems/NZF1.jl +++ b/src/ADNLPProblems/NZF1.jl @@ -9,7 +9,7 @@ function NZF1(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwarg n_orig = n nbis = max(2, div(n, 13)) n = 13 * nbis - (n == n_orig) || @warn("NZF1: number of variables adjusted from $n_orig to $n") + @adjust_nvar_warn("NZF1", n_orig, n) l = div(n, 13) function f(x; l = l) return sum( @@ -34,7 +34,7 @@ function NZF1(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwarg n_orig = n nbis = max(2, div(n, 13)) n = 13 * nbis - (n == n_orig) || @warn("NZF1: number of variables adjusted from $n_orig to $n") + @adjust_nvar_warn("NZF1", n_orig, n) l = div(n, 13) function F!(r, x; l = l) for i = 1:l diff --git a/src/ADNLPProblems/catenary.jl b/src/ADNLPProblems/catenary.jl index 0c4756f6..345a0bdc 100644 --- a/src/ADNLPProblems/catenary.jl +++ b/src/ADNLPProblems/catenary.jl @@ -8,10 +8,10 @@ function catenary( FRACT = 0.6, kwargs..., ) where {T} - (n % 3 == 0) || @warn("catenary: number of variables adjusted to be a multiple of 3") + n_orig = n n = 3 * max(1, div(n, 3)) - (n < 6) || @warn("catenary: number of variables adjusted to be greater or equal to 6") n = max(n, 6) + @adjust_nvar_warn("catenary", n_orig, n) ## Model Parameters N = div(n, 3) - 2 diff --git a/src/ADNLPProblems/chainwoo.jl b/src/ADNLPProblems/chainwoo.jl index 9d1520fe..154b71ba 100644 --- a/src/ADNLPProblems/chainwoo.jl +++ b/src/ADNLPProblems/chainwoo.jl @@ -6,8 +6,9 @@ function chainwoo(; use_nls::Bool = false, kwargs...) end function chainwoo(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 4 == 0) || @warn("chainwoo: number of variables adjusted to be a multiple of 4") + n_orig = n n = 4 * max(1, div(n, 4)) + @adjust_nvar_warn("chainwoo", n_orig, n) function f(x; n = length(x)) return 1 + sum( 100 * (x[2 * i] - x[2 * i - 1]^2)^2 + @@ -23,8 +24,9 @@ function chainwoo(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, k end function chainwoo(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 4 == 0) || @warn("chainwoo: number of variables adjusted to be a multiple of 4") + n_orig = n n = 4 * max(1, div(n, 4)) + @adjust_nvar_warn("chainwoo", n_orig, n) function F!(r, x; n = length(x)) nb = div(n, 2) - 1 r[1] = 1 diff --git a/src/ADNLPProblems/clplatea.jl b/src/ADNLPProblems/clplatea.jl index a54e08ad..7174be03 100644 --- a/src/ADNLPProblems/clplatea.jl +++ b/src/ADNLPProblems/clplatea.jl @@ -6,9 +6,10 @@ function clplatea(; wght = -0.1, kwargs..., ) where {T} + n_orig = n p = max(floor(Int, sqrt(n)), 3) - p * p != n && @warn("clplatea: number of variables adjusted from $n to $(p*p)") n = p * p + @adjust_nvar_warn("clplatea", n_orig, n) hp2 = (1 // 2) * p^2 function f(x; p = p, hp2 = hp2, wght = wght) return (eltype(x)(wght) * x[p + (p - 1) * p]) + diff --git a/src/ADNLPProblems/clplateb.jl b/src/ADNLPProblems/clplateb.jl index a732e431..f1fbc012 100644 --- a/src/ADNLPProblems/clplateb.jl +++ b/src/ADNLPProblems/clplateb.jl @@ -6,9 +6,10 @@ function clplateb(; wght = -0.1, kwargs..., ) where {T} + n_orig = n p = max(floor(Int, sqrt(n)), 3) - p * p != n && @warn("clplateb: number of variables adjusted from $n to $(p*p)") n = p * p + @adjust_nvar_warn("clplateb", n_orig, n) hp2 = 1 // 2 * p^2 function f(x; p = p, hp2 = hp2, wght = wght) return sum(eltype(x)(wght) / (p - 1) * x[p + (j - 1) * p] for j = 1:p) + diff --git a/src/ADNLPProblems/clplatec.jl b/src/ADNLPProblems/clplatec.jl index 5d77a3f3..43d73eae 100644 --- a/src/ADNLPProblems/clplatec.jl +++ b/src/ADNLPProblems/clplatec.jl @@ -8,9 +8,10 @@ function clplatec(; l = 0.01, kwargs..., ) where {T} + n_orig = n p = max(floor(Int, sqrt(n)), 3) - p * p != n && @warn("clplatec: number of variables adjusted from $n to $(p*p)") n = p * p + @adjust_nvar_warn("clplatec", n_orig, n) hp2 = 1 // 2 * p^2 function f(x; p = p, hp2 = hp2, wght = wght, r = r, l = l) diff --git a/src/ADNLPProblems/fminsrf2.jl b/src/ADNLPProblems/fminsrf2.jl index c7fe806e..eae7709b 100644 --- a/src/ADNLPProblems/fminsrf2.jl +++ b/src/ADNLPProblems/fminsrf2.jl @@ -1,12 +1,11 @@ export fminsrf2 function fminsrf2(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - n < 4 && @warn("fminsrf2: number of variables must be ≥ 4") + n_orig = n n = max(4, n) - p = floor(Int, sqrt(n)) - p * p != n && @warn("fminsrf2: number of variables adjusted from $n down to $(p*p)") n = p * p + @adjust_nvar_warn("fminsrf2", n_orig, n) h00 = 1 slopej = 4 diff --git a/src/ADNLPProblems/powellsg.jl b/src/ADNLPProblems/powellsg.jl index 38ac7db6..b64333d0 100644 --- a/src/ADNLPProblems/powellsg.jl +++ b/src/ADNLPProblems/powellsg.jl @@ -6,8 +6,9 @@ function powellsg(; use_nls::Bool = false, kwargs...) end function powellsg(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 4 == 0) || @warn("powellsg: number of variables adjusted to be a multiple of 4") - n = 4 * max(1, div(n, 4)) # number of variables adjusted to be a multiple of 4 + n_orig = n + n = 4 * max(1, div(n, 4)) + @adjust_nvar_warn("powellsg", n_orig, n) function f(x; n = length(x)) return sum( (x[j] + 10 * x[j + 1])^2 + diff --git a/src/ADNLPProblems/spmsrtls.jl b/src/ADNLPProblems/spmsrtls.jl index 2a72f28a..8cbc967c 100644 --- a/src/ADNLPProblems/spmsrtls.jl +++ b/src/ADNLPProblems/spmsrtls.jl @@ -9,7 +9,7 @@ function spmsrtls(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, k n_orig = n m = max(Int(round((n + 2) / 3)), 34) n = m * 3 - 2 - (n == n_orig) || @warn("spmsrtls: number of variables adjusted from $n_orig to $n") + @adjust_nvar_warn("spmsrtls", n_orig, n) p = [sin(i^2) for i = 1:n] x0 = T[p[i] / 5 for i = 1:n] @@ -64,7 +64,7 @@ function spmsrtls(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, k n_orig = n m = max(Int(round((n + 2) / 3)), 34) n = m * 3 - 2 - (n == n_orig) || @warn("spmsrtls: number of variables adjusted from $n_orig to $n") + @adjust_nvar_warn("spmsrtls", n_orig, n) p = [sin(i^2) for i = 1:n] x0 = T[p[i] / 5 for i = 1:n] diff --git a/src/ADNLPProblems/srosenbr.jl b/src/ADNLPProblems/srosenbr.jl index 451d0a6e..8c51d9e6 100644 --- a/src/ADNLPProblems/srosenbr.jl +++ b/src/ADNLPProblems/srosenbr.jl @@ -1,8 +1,9 @@ export srosenbr function srosenbr(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 2 == 0) || @warn("srosenbr: number of variables adjusted to be even") + n_orig = n n = 2 * max(1, div(n, 2)) + @adjust_nvar_warn("srosenbr", n_orig, n) function f(x; n = length(x)) return sum(100 * (x[2 * i] - x[2 * i - 1]^2)^2 + (x[2 * i - 1] - 1)^2 for i = 1:div(n, 2)) end diff --git a/src/ADNLPProblems/watson.jl b/src/ADNLPProblems/watson.jl index 56e1f275..cf75edc3 100644 --- a/src/ADNLPProblems/watson.jl +++ b/src/ADNLPProblems/watson.jl @@ -6,7 +6,9 @@ function watson(; use_nls::Bool = false, kwargs...) end function watson(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} + n_orig = n n = min(max(n, 2), 31) + @adjust_nvar_warn("watson", n_orig, n) function f(x; n = n) Ti = eltype(x) return 1 // 2 * sum( @@ -31,7 +33,9 @@ function watson(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwa end function watson(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} + n_orig = n n = min(max(n, 2), 31) + @adjust_nvar_warn("watson", n_orig, n) function F!(r, x; n = n) Ti = eltype(x) for i = 1:29 diff --git a/src/ADNLPProblems/woods.jl b/src/ADNLPProblems/woods.jl index 42616656..57563009 100644 --- a/src/ADNLPProblems/woods.jl +++ b/src/ADNLPProblems/woods.jl @@ -1,8 +1,9 @@ export woods function woods(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 4 == 0) || @warn("woods: number of variables adjusted to be a multiple of 4") + n_orig = n n = 4 * max(1, div(n, 4)) + @adjust_nvar_warn("woods", n_orig, n) function f(x; n = length(x)) return sum( 100 * (x[4 * i - 2] - x[4 * i - 3]^2)^2 + From 4d99e6de4ceac0b93e744f4056a85cbdd03a4d66 Mon Sep 17 00:00:00 2001 From: Arnav Kapoor Date: Sat, 28 Feb 2026 01:36:44 +0530 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/ADNLPProblems/ADNLPProblems.jl | 3 ++- src/ADNLPProblems/bearing.jl | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ADNLPProblems/ADNLPProblems.jl b/src/ADNLPProblems/ADNLPProblems.jl index 52a0e0f5..69e0a9d1 100644 --- a/src/ADNLPProblems/ADNLPProblems.jl +++ b/src/ADNLPProblems/ADNLPProblems.jl @@ -42,7 +42,8 @@ end macro adjust_nvar_warn(problem_name, n_orig, n) return quote ($(esc(n)) == $(esc(n_orig))) || - @warn($(esc(problem_name)) * ": number of variables adjusted from " * string($(esc(n_orig))) * " to " * string($(esc(n)))) + @warn(string($(esc(problem_name)), ": number of variables adjusted from ", + $(esc(n_orig)), " to ", $(esc(n)))) end end diff --git a/src/ADNLPProblems/bearing.jl b/src/ADNLPProblems/bearing.jl index c241bc0c..bccc33a6 100644 --- a/src/ADNLPProblems/bearing.jl +++ b/src/ADNLPProblems/bearing.jl @@ -9,7 +9,7 @@ function bearing(; ) where {T} # nx > 0 # grid points in 1st direction # ny > 0 # grid points in 2nd direction - + # Ensure nx and ny are at least 1, and warn if they need adjustment nx_orig = nx ny_orig = ny From a9c3d38a15283251480aad51fc4d10a769673f84 Mon Sep 17 00:00:00 2001 From: Arnav Kapoor Date: Sat, 28 Feb 2026 01:36:58 +0530 Subject: [PATCH 6/6] Update powellsg :nls variant to use @adjust_nvar_warn macro Ensures consistent warning messages between :nlp and :nls variants, showing both original and adjusted dimension values. --- src/ADNLPProblems/powellsg.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ADNLPProblems/powellsg.jl b/src/ADNLPProblems/powellsg.jl index b64333d0..e95cc1d5 100644 --- a/src/ADNLPProblems/powellsg.jl +++ b/src/ADNLPProblems/powellsg.jl @@ -25,8 +25,9 @@ function powellsg(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, k end function powellsg(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T} - (n % 4 == 0) || @warn("powellsg: number of variables adjusted to be a multiple of 4") + n_orig = n n = 4 * max(1, div(n, 4)) + @adjust_nvar_warn("powellsg", n_orig, n) function F!(r, x; n = length(x)) @inbounds for j = 1:4:n r[j] = x[j] + 10 * x[j + 1]