KeyError: 0e
回答済みHello, I define the binary variables x and y as follows:
y = g.addVars(10, 5, GRB.BINARY, name='y_tl')
x = g.addVars(10, GRB.BINARY, name='x_l')
Here is the constraint I'd like to define:
and the formulation I did in Gurobi is:
task_assignment=g.addConstrs((y.sum('*',l)<= x[l]*1000 for l in locations), name="task_assignment")
but it's giving error:
KeyError: 0
What could be the problem here?
Thanks in advance!
-
Hi Maide,
If you were to print the keys of your y tupledict you would see the following
>> print(y.keys())
<gurobi.tuplelist (50 tuples, 3 values each):
( 0 , 0 , B )
( 0 , 1 , B )
( 0 , 2 , B )
( 0 , 3 , B )
...There is a third index with values "B" that you did not intend and this will give you a clue as to what is happening.
Let's review the method signature for addVars from the reference manual:
addVars ( *indices, lb=0.0, ub=float('inf'), obj=0.0, vtype=GRB.CONTINUOUS, name="" )The * has special meaning here and allows an arbitrary number of arguments, to index the variables, to be passed to the function. However this means that every parameter that comes after it must have arguments passed via keyword. This is why GRB.BINARY ( = "B") is being interpreted as another indice.
So to fix this pass the vtype via keyword, i.e. vtype=GRB.BINARY (or vtype="B").
- Riley
0
サインインしてコメントを残してください。
コメント
1件のコメント