簡體   English   中英

MIP 的多個旅行推銷員問題

[英]Multiple Traveling Salemans Problem with MIP

我一直在嘗試使用 MIP Link中已經制作的代碼在普通 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

這對我來說沒有意義,因為我只是創建了另一個總和。 非常感謝您提前。

這是 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

如果我們輸入:

i = 0
j = 0
k = 2
print(x[k][j][i])

我們會看到:

x002

結論:嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM