首页 > 解决方案 > MethodError: 没有方法匹配 -(::VariableRef, ::Nothing)

问题描述

我是朱莉娅的新手,我仍在努力了解它。

我试图在我的约束中使用用户定义的函数。

基本上,查找函数返回与数组中输入相同的索引值:

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

因此我试图在我的优化模型中应用这个函数:

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))

我得到了这个错误:

   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 和 snx1 有以下数据:

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]

我意识到它可能与数据类型和数组有关?

请帮忙

标签: juliajulia-jump

解决方案


Can you elaborate on what you are trying to do in the lookup function?

Your scenario in the lookup function is a JuMP variable, i.e. a VariableRef. And you are trying to do a comparison with a Julia Int.

if scenario == sn_set[i] # this is always `false`
    return zw_set[i]
end

This will always not enter the if block. And your lookup function will always return nothing. screenshot

When JuMP tries to create the constraint, it does

@constraint(m, [t in T], zw1[t] == nothing)

which causes your error.

See example below that throws an error in the lookup:

using JuMP

function lookup(scenario, zw_set, sn_set)
    for i in 1:length(sn_set)
        if scenario == sn_set[i]
            return zw_set[i]
        end
    end
    error("Cannot find $scenario in $sn_set.")
end

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]

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]

m = Model()
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))

推荐阅读