[英]ERROR attempt to access 30×26 Array{VariableRef,2} at index [1, 0]
I am having problems when I want to assign workers to different shifts according to the satisfy the required.当我想根据满足要求将工人分配到不同的班次时,我遇到了问题。 They can be in two shifts per day.
他们可以每天两班倒。
But I do not know why the array problem, I am new in Julia但是不知道为什么是数组问题,我是Julia新手
empleados=26;
turnos=30;
requerimiento=[3,4,1,1,2,2,4,3,1,3,3,1,2,4,2,4,3,2,1,2,2,2,2,2,2,3]
costo=28;
using JuMP
using Gurobi
m = Model(with_optimizer(Gurobi.Optimizer))
@variable(m, x[1:turnos,1:empleados]<=1,Bin)
@objective(m, Min, costo * sum(x))
for i in 1:turnos+1,j in 1:empleados
@constraint(m,x[i,j] + x[i,j-1] + x[i,j+1] <= 2)
end
for i in 1:turnos+3,j in 1:empleados
@constraint(m, x[i,j]+x[i,j-2]+x[i,j-3]+x[i,j+3]+x[i,j+2] <= 1)
end
@constraint(m, sum(x[i,:]) for i in i:turnos >=requerimiento[i])
***ERROR***
BoundsError: attempt to access 30×26 Array{VariableRef,2} at index [1, 0]
Stacktrace:
[1] getindex(::Array{VariableRef,2}, ::Int64, ::Int64) at .\array.jl:729
[2] macro expansion at C:\Users\DELL\.julia\packages\JuMP\MsUSY\src\macros.jl:390 [inlined]
[3] top-level scope at .\In[103]:15
The first error appears in this line:第一个错误出现在这一行:
for i in 1:turnos+1,j in 1:empleados
@constraint(m,x[i,j] + x[i,j-1] + x[i,j+1] <= 2)
end
In Julia, arrays are usually indexed starting from 1 (look for 1-based indexing).在 Julia 中,arrays 通常从 1 开始索引(查找基于 1 的索引)。 This is different from other languages like python.
这与 python 等其他语言不同。
This means that if your array is of size (30,26), you can only use indices from 1 to 30 in the first dimension and from 1 to 26 y the second one.这意味着如果您的数组大小为 (30,26),则您只能在第一个维度使用 1 到 30 的索引,在第二个维度使用 1 到 26 y 的索引。
Your variable x
has a size of 30x26
, but in your loop you are trying to call the element x[1,0]
(because you are asking x[i,j - 1]
and j starts from 1).您的变量
x
的大小为30x26
,但在您的循环中,您试图调用元素x[1,0]
(因为您要询问x[i,j - 1]
并且 j 从 1 开始)。 Once you fix this by either reformulating your indices or your range, you will run into another problem with the part x[i,j+1]
because you will try to access x[1,27]
which also does not exist.一旦通过重新制定索引或范围来解决此问题,您将遇到
x[i,j+1]
部分的另一个问题,因为您将尝试访问也不存在的x[1,27]
。 You also need to fix the range of i, which currently goes from 1 to 31 in the first index ( for i in 1:turnos + 1
).您还需要修复 i 的范围,该范围目前在第一个索引中从 1 到 31 (
for i in 1:turnos + 1
)。 This is also going to break your code because your array only has a size 30 in the first dimension.这也会破坏您的代码,因为您的数组在第一维中只有 30 大小。
Finally, in the last line you are constructing最后,在您正在构建的最后一行
@constraint(m, sum(x[i,:]) for i in i:turnos >=requerimiento[i])
This will give an error beacuse you are asking i
to go from i
to another value, which doesn't make sense.这将给出一个错误,因为您要求
i
到 go 从i
到另一个值,这是没有意义的。
So you only need to pay careful attention to how you are iterating your arrays.所以你只需要仔细注意你是如何迭代你的 arrays 的。
One way to make this line work is doing:使这条线工作的一种方法是:
for i in 1:turnos,j in 2:empleados-1
@constraint(m,x[i,j] + x[i,j-1] + x[i,j+1] <= 2)
end
But I don't understand your model so I don't know if that is what you are trying to do.但我不明白你的 model 所以我不知道这是否是你想要做的。 The best advice I can give you is to familiarize better with the indexing on the official docs using simple examples.
我能给你的最好建议是使用简单的示例更好地熟悉官方文档中的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.