简体   繁体   English

JuMP 是否可以使用 VariableRef 作为索引

[英]JuMP is it possible to use VariableRef as indices

I'm using Julia's JuMP solver to try to find an optimal path.我正在使用 Julia 的 JuMP 求解器来尝试找到最佳路径。

My problem is that the objective function is a sum of the elements of a matrix whose indices are variables from my optimization problem.我的问题是目标 function 是矩阵元素的总和,其索引是我的优化问题中的变量。

nb_dem, nb_prod, nb_mag, nb_noeuds, S, Q, R = read_data_24(inputfilepath, inputfilename)
@variable(model, produits[1:nb_mag,1:nb_prod,1:nb_dem] >= 0, Int)
@variable(model, chemins[1:nb_noeuds+1, 1:nb_mag], Int)
@variable(model, binaires[1:nb_mag,1:nb_dem], Bin)

# define objective function
@objective(model, Min, sum(sum(R[chemins[k,i],chemins[k+1,i]] for k in 1:nb_noeuds) for i in 1:nb_mag))

I've added some constraint but when I try to run, I get an error message telling me that I cannot use VariablesRef as indices.我添加了一些约束,但是当我尝试运行时,我收到一条错误消息,告诉我不能使用 VariablesRef 作为索引。

ArgumentError: invalid index: chemins[1,1] of type VariableRef

Is there anyway of converting VariablesRef into useable index?无论如何将 VariablesRef 转换为可用索引?

You need to create a binary vector to represent the indices and than multiply by it, for an example:您需要创建一个二进制向量来表示索引,然后乘以它,例如:

x = [2,5,7]
@variable(m, x_indices[1:length(x)])
@variable(m, y)
@constraint(m, y >= sum(x .* x_indices))
@constraint(m, sum(x_indices) == 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Julia JuMP 中使用 VariableRef 作为索引 - How to use VariableRef as indices in Julia JuMP 目标 function `GenericQuadExpr{Float64,VariableRef}[0 0 0; 0 0 0; JuMP 不支持 0 0 0]` - The objective function `GenericQuadExpr{Float64,VariableRef}[0 0 0; 0 0 0; 0 0 0]` is not supported by JuMP /(::VariableRef,::QuadExpr) 未定义 - /(::VariableRef,::QuadExpr) is not defined JULIA JUMP 中具有两个索引和不同索引长度的优化变量 - Optimisation Variable with two indices and different index length in JULIA JUMP MethodError: 没有方法匹配 -(::VariableRef, ::Nothing) - MethodError: no method matching -(::VariableRef, ::Nothing) BoundsError:尝试访问索引 [0, 0] 处的 2×2 Matrix{VariableRef} - BoundsError: attempt to access 2×2 Matrix{VariableRef} at index [0, 0] LoadError:MethodError:没有方法匹配Value(:: Array {VariableRef,1}) - LoadError: MethodError: no method matching Value(::Array{VariableRef,1}) 如何将预定义列表用于 JuMP @complements() 函数中的求和索引? - How can a predefined list can be used for the summation indices in a JuMP @complements() function? Julia (JuMP):具有多个条件值的指标约束(boolean 表达式可能吗?) - Julia (JuMP): Indicator constraints with multiple conditional values (is a boolean expression possible?) 错误尝试访问索引 [1, 0] 处的 30×26 Array{VariableRef,2} - ERROR attempt to access 30×26 Array{VariableRef,2} at index [1, 0]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM