JuliaSmoothOptimizers / JuliaSmoothOptimizers/ADNLPModels.jl
Confusing error message on creation of `ADNLPModel` when matrix of linear constraints contains integers
- Dominant language
- Julia
- Stars
- 44
- Forks
- 19
- PR merge metrics
- No merged PRs in 30d
Description
# Code
```julia
import SparseArrays: sparse
import ADNLPModels
params0 = [1.0, 0.1, 0.9]
A = sparse([
0 -1 1 # NOTE: integers here!
])
problem = ADNLPModels.ADNLPModel(
x -> x' * x, params0, # f, x0
[0., 0, 0], [Inf, Inf, 1], # lvar, uvar
A, [0.], [Inf] # A, lcon, ucon
)
```
# Output
```
julia> A = sparse([
0 -1 1 # NOTE: integers here!
])
1×3 SparseArrays.SparseMatrixCSC{Int64, Int64} with 2 stored entries:
⋅ -1 1
julia> ADNLPModels.ADNLPModel(
x -> x' * x, params0, # f, x0
[0., 0, 0], [Inf, Inf, 1], # lvar, uvar
A, [0.], [Inf] # A, lcon, ucon
)
ERROR: MethodError: no method matching ADNLPModels.ADNLPModel(::var"#1#2", ::Vector{…}, ::Vector{…}, ::Vector{…}, ::Vector{…}, ::Vector{…}, ::Vector{…}, ::Vector{…}, ::Vector{…})
The type `ADNLPModels.ADNLPModel` exists, but no method is defined for this combination of argument types when trying to construct it.
Closest candidates are:
ADNLPModels.ADNLPModel(::Any, ::S, ::S, ::S, ::Any, ::Any, ::S, ::S, ::S; kwargs...) where S
@ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:330
ADNLPModels.ADNLPModel(::Any, ::S, ::S, ::S, ::Any, ::Any, ::S, ::Any, ::S, ::S; kwargs...) where S
@ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:427
ADNLPModels.ADNLPModel(::Any, ::Any, ::Any, ::Any, ::SparseArrays.AbstractSparseMatrix{Tv, Ti}, ::Any, ::Any, ::Any; kwargs...) where {Tv, Ti}
@ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:507
...
Stacktrace:
[1] ADNLPModels.ADNLPModel(f::Function, x0::Vector{…}, lvar::Vector{…}, uvar::Vector{…}, A::SparseArrays.SparseMatrixCSC{…}, lcon::Vector{…}, ucon::Vector{…}; kwargs::@Kwargs{})
@ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:368
[2] ADNLPModels.ADNLPModel(f::Function, x0::Vector{…}, lvar::Vector{…}, uvar::Vector{…}, A::SparseArrays.SparseMatrixCSC{…}, lcon::Vector{…}, ucon::Vector{…})
@ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:358
[3] top-level scope
@ REPL[8]:1
Some type information was truncated. Use `show(err)` to see complete types.
```
# Problem
[Turns out](https://discourse.julialang.org/t/unable-to-construct-adnlpmodel-with-a-matrix-of-linear-constraints/124361/3), this happens because `A` is a matrix of integers, but the error message doesn't tell me this.
Also, I'm _not_ calling `ADNLPModels.ADNLPModel(::var"#1#2", ::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64}, ::Vector{Int64}, ::Vector{Int64}, ::Vector{Int64}, ::Vector{Float64}, ::Vector{Float64})` - the _package_ tries to call it here: https://github.com/JuliaSmoothOptimizers/ADNLPModels.jl/blob/5d917cea82158630bab7a598b0400afc3478d6d0/src/nlp.jl#L358-L369
The signature of this method doesn't constrain `S` or `Tv` in any way. Why not write:
```julia
const AV{T} = AbstractVector{T}
ADNLPModel(
f, x0::AV{Tv}, lvar::AV{Tv}, uvar::AV{Tv},
A::AbstractSparseMatrix{Tv, Ti},
lcon::AV{Tv}, ucon::AV{Tv};
kwargs...
) where {Tv, Ti} = ...
```
This way, the error will move to user code, because then I'll be calling this constructor with `(Tv = eltype(x0)) === Float64`, but the `AbstractSparseMatrix` would need `Tv = eltype(A) == Int64`.
Contributor guide
Assessment
This issue has not been assessed yet.