Python 具有MIP的多旅行Salemans问题

Python 具有MIP的多旅行Salemans问题,python,optimization,traveling-salesman,Python,Optimization,Traveling Salesman,我一直在尝试使用MIP中已经生成的代码在普通TSP中实现mTSP 这就是我目前在python中的代码,它给我带来了一个我不理解的错误: places = ['Antwerp', 'Bruges', 'C-Mine', 'Dinant', 'Ghent', 'Grand-Place de Bruxelles', 'Hasselt', 'Leuven', 'Mechelen', 'Mons', 'Montagne de Bueren', 'Namur',

我一直在尝试使用MIP中已经生成的代码在普通TSP中实现mTSP

这就是我目前在python中的代码,它给我带来了一个我不理解的错误:

places = ['Antwerp', 'Bruges', 'C-Mine', 'Dinant', 'Ghent',
          'Grand-Place de Bruxelles', 'Hasselt', 'Leuven',
          'Mechelen', 'Mons', 'Montagne de Bueren', 'Namur',
          'Remouchamps', 'Waterloo']

salesman=['Salesman1','Salesman2']

# distances in an upper triangular matrix
dists = [[83, 81, 113, 52, 42, 73, 44, 23, 91, 105, 90, 124, 57],
         [161, 160, 39, 89, 151, 110, 90, 99, 177, 143, 193, 100],
         [90, 125, 82, 13, 57, 71, 123, 38, 72, 59, 82],
         [123, 77, 81, 71, 91, 72, 64, 24, 62, 63],
         [51, 114, 72, 54, 69, 139, 105, 155, 62],
         [70, 25, 22, 52, 90, 56, 105, 16],
         [45, 61, 111, 36, 61, 57, 70],
         [23, 71, 67, 48, 85, 29],
         [74, 89, 69, 107, 36],
         [117, 65, 125, 43],
         [54, 22, 84],
         [60, 44],
         [97],
         []]

# number of nodes and list of vertices
n, V, S = len(dists), set(range(len(dists))), set(range(len(salesman)))


# distances matrix
c = [[0 if i == j
      else dists[i][j-i-1] if j > i
      else dists[j][i-j-1]
      for j in V] for i in V]

model = Model()

# binary variables indicating if arc (i,j) is used on the route or not
x = [[[model.add_var(var_type=BINARY) for j in V] for i in V] for s in S]

# objective function: minimize the distance
model.objective = minimize(xsum(c[i][j]*x[i][j][s] for i in V for j in V for s in S))
错误是:

IndexError                                Traceback (most recent call last)
<ipython-input-52-8550246fcd90> in <module>
     48 
     49 # objective function: minimize the distance
---> 50 model.objective = minimize(xsum(c[i][j]*x[i][j][s] for i in V for j in V for s in S))
     51 
     52 

~/opt/anaconda3/lib/python3.7/site-packages/mip/model.py in xsum(terms)
   1453     """
   1454     result = mip.LinExpr()
-> 1455     for term in terms:
   1456         result.add_term(term)
   1457     return result

<ipython-input-52-8550246fcd90> in <genexpr>(.0)
     48 
     49 # objective function: minimize the distance
---> 50 model.objective = minimize(xsum(c[i][j]*x[i][j][s] for i in V for j in V for s in S))
     51 
     52 

IndexError: list index out of range
索引器错误回溯(最近一次调用)
在里面
48
49#目标函数:最小化距离
--->50 model.objective=最小化(xsum(c[i][j]*x[i][j][s]表示i在V中表示j在V中表示s在s中表示))
51
52
xsum中的~/opt/anaconda3/lib/python3.7/site-packages/mip/model.py(术语)
1453     """
1454结果=mip.LinExpr()
->1455就期限而言:
1456结果。添加术语(术语)
1457返回结果
英寸(.0)
48
49#目标函数:最小化距离
--->50 model.objective=最小化(xsum(c[i][j]*x[i][j][s]表示i在V中表示j在V中表示s在s中表示))
51
52
索引器:列表索引超出范围
这对我来说毫无意义,因为我只创造了另一个总和。
非常感谢您。

这是一个Python问题,而不是Gurobi问题。您不完全了解嵌套列表理解是如何工作的

我们可以通过以下方式重现这一点:

x = [[["x%s%s%s" % (i,j,k) for i in range(2)] for j in range(2)] for k in range(3)]
i = 0
j = 0
k = 2
x[i][j][k]
x
不是
x[i][j][k]
,而是
x[k][j][i]

[[['x000', 'x100'], ['x010', 'x110']], [['x001', 'x101'], ['x011', 'x111']], [['x002', 'x102'], ['x012', 'x112']]]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-13-a2e90cc605ea> in <module>()
      4 j = 0
      5 k = 2
----> 6 x[i][j][k]

IndexError: list index out of range
我们将看到:

x002
结论:尝试:

x = [[[model.add_var(var_type=BINARY) for s in S] for j in V] for i in V]
...
x[i][j][s]

也许这个问题看起来太难了,但只是我需要做一个三重求和,程序就爆炸了。谢谢!我忘了回答。我终于解决了:)
x = [[[model.add_var(var_type=BINARY) for s in S] for j in V] for i in V]
...
x[i][j][s]