KeyError when running TSP example (tsp.ipynb) during m.optimize(subtourelim)
回答済みHi everyone,
I'm trying to run the Traveling Salesman Problem (TSP) example notebook (tsp.ipynb
) from the Gurobi examples, but I encountered an error on the line m.optimize(subtourelim)
:
KeyError Traceback (most recent call last)
<ipython-input-8-1587b32d4cc3> in <cell line: 3>()
1 m._vars = vars
2 m.Params.lazyConstraints = 1
----> 3 m.optimize(subtourelim)
src/gurobipy/_model.pyx in gurobipy._model.Model.optimize()
src/gurobipy/_model.pyx in gurobipy._model.Model.update()
KeyError: 0
This error occurs both on my local machine (Windows) and on Google Colab. The environment details are as follows:
- Gurobi version: 12.0.0
- Python version: 3.9.13
- Operating system: Windows 10 (or 11, please specify)
- Google Colab: Error occurs on Colab as well
I have not modified the original tsp.ipynb
example code except for providing the callback function, which is exactly the same as the one in the original example. Here’s the callback function I am using:
# Callback - use lazy constraints to eliminate sub-tours
def subtourelim(model, where):
if where == GRB.Callback.MIPSOL:
print(111)
# make a list of edges selected in the solution
vals = model.cbGetSolution(model._vars)
selected = gp.tuplelist((i, j) for i, j in model._vars.keys()
if vals[i, j] > 0.5)
# find the shortest cycle in the selected edge list
tour = subtour(selected)
if len(tour) < len(capitals):
# add subtour elimination constr. for every pair of cities in subtour
model.cbLazy(gp.quicksum(model._vars[i, j] for i, j in combinations(tour, 2))
<= len(tour)-1)
# Given a tuplelist of edges, find the shortest subtour
def subtour(edges):
unvisited = capitals[:]
cycle = capitals[:] # Dummy - guaranteed to be replaced
while unvisited: # true if list is non-empty
thiscycle = []
neighbors = unvisited
while neighbors:
current = neighbors[0]
thiscycle.append(current)
unvisited.remove(current)
neighbors = [j for i, j in edges.select(current, '*')
if j in unvisited]
if len(thiscycle) <= len(cycle):
cycle = thiscycle # New shortest subtour
return cycle
Additional Details:
- Callback Function: The callback function is exactly the same as in the original example.
-
Issue: The error occurs when I call
m.optimize(subtourelim)
, and both my local machine and Google Colab report the sameKeyError: 0
.
I suspect the issue may be related to how variables are indexed or passed to the callback function, or possibly an issue with Gurobi's configuration. Does anyone know how to resolve this error or have any suggestions?
Thank you in advance for your help!
-
Hi Huang,
I suspect the error is related to the usage of model._vars. This particular attribute name will be a problem in v12 of Gurobi. You can avoid this by using another attribute name instead, e.g. model._myvars, but best practice would be to actually use a callback class:
class Callback:
def __init__(self, vars):
self.vars = vars
def __call__(self, model, where):
if where == gp.GRB.Callback.MIPSOL:
print("variables", self.vars)
m = gp.Model()
x = m.addVars(2, vtype="B", name="x")
cb = Callback({"x":x})
m.optimize(cb)A more full example is shown in the tsp.py example from our Example Source Code in the documentation.
- Riley
0
サインインしてコメントを残してください。
コメント
1件のコメント