Problem setting constraint in simple LP using JuMP

Hi there,

Im trying to construct a simple LP using JuMP but I am running into an issue with the constraint in the function. I checked this is the line causing the issue since if I delete it, I can define the function in the REPL but otherwise I can not (error attached below the code)

function quantile_regression_linear()
    # retrieve views of the relevant data 
    y = rand(1000)
    x = rand(1000)
    N = length(y)
    # initialize the optimization model and solve it 
    model = Model(Gurobi.Optimizer)
    set_silent(model)
    @variable(model, u_plus[1:N] ≥ 0.0)
    @variable(model, u_minus[1:N] ≤ 0.0)
    @variable(model, θ[1:2])
    @constraint(model, [i in 1:N], y[i] - θ[1] - θ[2]*x[i] = u_plus[i] + u_minus[i])
end

When defining this function in the REPL I get the following error:

ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  Symbol(::Any...)
   @ Base strings/basic.jl:229
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:126

Stacktrace:
 [1] setindex!(h::Dict{Symbol, Any}, v0::Expr, key0::Expr)
   @ Base ./dict.jl:346
 [2] parse_macro_arguments(error_fn::JuMP.Containers.var"#error_fn#98"{…}, args::Tuple{…}; valid_kwargs::Nothing, num_positional_args::Nothing)
   @ JuMP.Containers ~/.julia/packages/JuMP/i68GU/src/Containers/macro.jl:58
 [3] parse_macro_arguments(error_fn::Function, args::Tuple{Symbol, Expr, Expr})
   @ JuMP.Containers ~/.julia/packages/JuMP/i68GU/src/Containers/macro.jl:41
 [4] var"@constraint"(__source__::LineNumberNode, __module__::Module, input_args::Vararg{Any})
   @ JuMP ~/.julia/packages/JuMP/i68GU/src/macros/@constraint.jl:103
in expression starting at REPL[153]:12
Some type information was truncated. Use `show(err)` to see complete types.

Thanks in advance!

Could it be that you just need to replace = with == in your constraint?

Absolutely, could not pick up this silly mistake. Thanks!

Absolutely, could not pick up this silly mistake

The error message didn’t help! I’ll see if we can improve it :smile:

It looks like we catch some cases but not all

julia> using JuMP

julia> begin
           model = Model()
           @variable(model, x[1:1])
           @constraint(model, x[1] = 1)
       end
ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  Symbol(::Any...)
   @ Base strings/basic.jl:229

Stacktrace:
 [1] setindex!(h::Dict{Symbol, Any}, v0::Int64, key0::Expr)
   @ Base ./dict.jl:367
 [2] parse_macro_arguments(error_fn::JuMP.Containers.var"#error_fn#98"{…}, args::Tuple{…}; valid_kwargs::Nothing, num_positional_args::Nothing)
   @ JuMP.Containers ~/.julia/packages/JuMP/FEKLB/src/Containers/macro.jl:58
 [3] parse_macro_arguments(error_fn::Function, args::Tuple{Symbol, Expr})
   @ JuMP.Containers ~/.julia/packages/JuMP/FEKLB/src/Containers/macro.jl:41
 [4] var"@constraint"(__source__::LineNumberNode, __module__::Module, input_args::Vararg{Any})
   @ JuMP ~/.julia/packages/JuMP/FEKLB/src/macros/@constraint.jl:103
in expression starting at REPL[137]:4
Some type information was truncated. Use `show(err)` to see complete types.

julia> begin
           model = Model()
           @variable(model, x)
           @constraint(model, x = 1)
       end
ERROR: LoadError: At REPL[138]:4: `@constraint(model, x = 1)`: No constraint expression detected. If you are trying to construct an equality constraint, use `==` instead of `=`.
Stacktrace:

thats great that the error message is so informative and specific in some cases! I feel like an error like mine should only slip in on a Monday (if at all) haha but better error messages are always welcomed so thanks a lot!

This will be fixed by Improve error message when = is used instead of == by odow · Pull Request #3892 · jump-dev/JuMP.jl · GitHub

The next release of JuMP will give:

julia> function quantile_regression_linear()
           # retrieve views of the relevant data 
           y = rand(1000)
           x = rand(1000)
           N = length(y)
           # initialize the optimization model and solve it 
           model = Model()
           set_silent(model)
           @variable(model, u_plus[1:N] ≥ 0.0)
           @variable(model, u_minus[1:N] ≤ 0.0)
           @variable(model, θ[1:2])
           @constraint(model, [i in 1:N], y[i] - θ[1] - θ[2]*x[i] = u_plus[i] + u_minus[i])
       end
ERROR: LoadError: At REPL[11]:12: `@constraint(model, [i in 1:N], (y[i] - θ[1]) - θ[2] * x[i] = begin
        #= REPL[11]:12 =#
        u_plus[i] + u_minus[i]
    end)`: Invalid keyword argument detected. If you are trying to construct an equality constraint, use `==` instead of `=`.
Stacktrace:
 [1] error(::String, ::String)
   @ Base ./error.jl:44
 [2] (::JuMP.Containers.var"#error_fn#98"{String})(str::String)
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/macro.jl:331
 [3] parse_macro_arguments(error_fn::JuMP.Containers.var"#error_fn#98"{…}, args::Tuple{…}; valid_kwargs::Nothing, num_positional_args::Nothing)
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/macro.jl:58
 [4] parse_macro_arguments(error_fn::Function, args::Tuple{Symbol, Expr, Expr})
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/macro.jl:41
 [5] var"@constraint"(__source__::LineNumberNode, __module__::Module, input_args::Vararg{Any})
   @ JuMP ~/.julia/dev/JuMP/src/macros/@constraint.jl:103
in expression starting at REPL[11]:12
Some type information was truncated. Use `show(err)` to see complete types.