MethodError: no method matching -(::VariableRef, ::Nothing)

I am new to julia and I am still trying to learn about it.

I tried to use a user-defined function in my constraint.

Basically, the lookup function returns the value of the same index as the input in the array:

function lookup(scenario,zw_set,sn_set)
for i= 1:length(sn_set)
    if      sn_set[i] == scenario
            return zw_set[i]
        end
    end
end

Thus I tried to apply this function in my optimization model:

m =Model(with_optimizer(CPLEX.Optimizer))
T = map(Int64, collect(1:1:5))
R = [1; 2]
@variable(m, sn[R,T] >=0,Int)
@variable(m,zw1[T]>=0, Int)
@constraint(m, [t in T], zw1[t]==lookup(sn[1,t],w1,snx1))

and I got this error:

   ERROR: LoadError: MethodError: no method matching -(::VariableRef, ::Nothing)
Closest candidates are:
  -(::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}, ::T) where T at C:\Users\alkin\.julia\packages\MathOptInterface\DmQBj\src\Utilities\functions.jl:1125
  -(::AbstractVariableRef) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\operators.jl:58
  -(::AbstractVariableRef, ::Union{Number, LinearAlgebra.UniformScaling}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\operators.jl:62
  ...
Stacktrace:
 [1] sub_mul(::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\MutableArithmetics.jl:31
 [2] operate(::typeof(MutableArithmetics.sub_mul), ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\interface.jl:88
 [3] operate_fallback!(::MutableArithmetics.NotMutable, ::Function, ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\interface.jl:275
 [4] operate!(::typeof(MutableArithmetics.sub_mul), ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\rewrite.jl:67
 [5] macro expansion at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\rewrite.jl:224 [inlined]
 [6] macro expansion at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\macros.jl:380 [inlined]
 [7] (::var"#5653#5654")(::Int64) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\macro.jl:183
 [8] #28 at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:85 [inlined]
 [9] iterate at .\generator.jl:47 [inlined]
 [10] collect(::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}},JuMP.Containers.var"#28#29"{var"#5653#5654"}}) at .\array.jl:622
 [11] map(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}) at .\abstractarray.jl:2099
 [12] container(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}, ::Type{JuMP.Containers.DenseAxisArray}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:85    
 [13] container(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:65
 [14] top-level scope at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\macros.jl:45

w1 and snx1 has the following data:

julia> print(w1)
[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,1.0, 1.0, 0.0, 1.0]

julia> print(snx1)
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0]

I realized that it might have to do something with the data types and array stuff?

Please help

Take a read of the first post in Please read: make it easier to help you.

It’s easier to help if you can provide a minimal working example that demonstrates the problem.

The main reason is that your function lookup is returning nothing. However, the bigger issue is that you cannot use functions like this in a JuMP model. In particular, you are passing the variable sn[1, t], not the value of the variable.

You will need to formulate the problem as a mixed integer linear program.

1 Like

I see, then I need to formulate this problem into a model.

thank you!