# Unable to construct \`ADNLPModel\` with a matrix of linear constraints

**URL:** <https://discourse.julialang.org/t/unable-to-construct-adnlpmodel-with-a-matrix-of-linear-constraints/124361>\
**Category:** Optimization (Mathematical)\
**Created:** [January 2, 2025, 11:46am UTC](https://discourse.julialang.org/t/unable-to-construct-adnlpmodel-with-a-matrix-of-linear-constraints/124361 "2025-01-02T11:46:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [January 2, 2025, 11:46am UTC](https://discourse.julialang.org/t/unable-to-construct-adnlpmodel-with-a-matrix-of-linear-constraints/124361/1 "2025-01-02T11:46:32Z")

</div>

The [documentation](https://jso.dev/ADNLPModels.jl/dev/reference/#ADNLPModels.ADNLPModel-Union%7BTuple%7BS%7D,%20Tuple%7BAny,%20S%7D%7D%20where%20S) of `ADNLPModels.ADNLPModel` suggests that there are methods that accept a matrix `A` to signify linear constraints:

```
ADNLPModel(f, x0, A, lcon, ucon)
ADNLPModel(f, x0, lvar, uvar, A, lcon, ucon)

```

These constructors are also [successfully tested](https://github.com/JuliaSmoothOptimizers/ADNLPModels.jl/blob/5d917cea82158630bab7a598b0400afc3478d6d0/test/nlp/basic.jl#L333-L343) on GitHub.  
However, I get an error when I try to use this:

```julia
params0 = [1.0, 0.1, 0.9]
A = sparse([
	0 -1 1
])
problem = ADNLPModels.ADNLPModel(
	nll, params0, # f, x0
	[0, 0, 0], [Inf, Inf, 1], # lvar, uvar
	A, [0], [Inf] # A, lcon, ucon
)

```

Error:

```julia
MethodError: no method matching ADNLPModels.ADNLPModel(::Main.var"workspace#51".var"#nll#5"{Main.var"workspace#51".GARCHLearner{Float64}, Vector{Float64}}, ::Vector{Float64}, ::Vector{Int64}, ::Vector{Float64}, ::SparseArrays.SparseMatrixCSC{Int64, Int64}, ::Vector{Int64}, ::Vector{Float64})

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, ::Any, ::Any, ::Any, ::SparseArrays.AbstractSparseMatrix{Tv, Ti}, ::Any, ::Any, ::Any; kwargs...) where {Tv, Ti}
   @ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:507

  ADNLPModels.ADNLPModel(::Any, ::S, ::Any, ::Any, ::S, ::Any, ::S, ::S; kwargs...) where S
   @ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:240

  ADNLPModels.ADNLPModel(::Any, ::S, ::S, ::S, ::Any, ::Any, ::S, ::S, ::S; kwargs...) where S
   @ ADNLPModels ~/.julia/packages/ADNLPModels/bOFzz/src/nlp.jl:330
  ...

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![tmigot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tmigot/32/23914_2.png) [@tmigot](https://discourse.julialang.org/u/tmigot)\
**Post date:** [January 2, 2025, 12:56pm UTC](https://discourse.julialang.org/t/unable-to-construct-adnlpmodel-with-a-matrix-of-linear-constraints/124361/2 "2025-01-02T12:56:24Z")

</div>

Hey @ForceBru  
I cannot test it in right now but from the error message I am suspecting the problem is that your matrix A and lcon are vectors of Int while their should be vectors of Float64

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [January 2, 2025, 1:02pm UTC](https://discourse.julialang.org/t/unable-to-construct-adnlpmodel-with-a-matrix-of-linear-constraints/124361/3 "2025-01-02T13:02:15Z")

</div>

Yes, that is exactly right - I’ve literally discovered this just now!

This throws the error:

```julia
params0 = [1.0, 0.1, 0.9]
A = sparse([
	0 -1 1 # integers here
])
problem = ADNLPModels.ADNLPModel(
	nll, params0,
	[0, 0, 0], # these are integers
        [Inf, Inf, 1],
	A, [0], [Inf] # 0 is an integer!
)

```

Change ints to float - and now it works:

```julia
A = sparse([
	0. -1 1 # note floating-point literal here
])
problem = ADNLPModels.ADNLPModel(
	nll, params0,
	[0, 0, 0.], [Inf, Inf, 1], # floats everywhere
	A, [0.], [Inf] # 0. is a float
)

```

IMO this is extremely confusing. The error message doesn’t say anything about element types at all.

---

<div class="post-metadata">

**Author:** ![tmigot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tmigot/32/23914_2.png) [@tmigot](https://discourse.julialang.org/u/tmigot)\
**Post date:** [January 2, 2025, 1:14pm UTC](https://discourse.julialang.org/t/unable-to-construct-adnlpmodel-with-a-matrix-of-linear-constraints/124361/4 "2025-01-02T13:14:04Z")

</div>

Yes, agreed it’s not 100% clear. Can you open an issue to improve the error message on ADNLPModels.jl? We could make a tailored one.
