-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew-mtsp-gurobi.py
99 lines (75 loc) · 2.08 KB
/
new-mtsp-gurobi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
__author__ = 'nick'
import sys
import math
import random
from gurobipy import *
n = 7
L = 5
K = 0
salesmen = 3
def distance(points, i, j):
dx = points[i][0] - points[j][0]
dy = points[i][1] - points[j][1]
return math.sqrt(dx*dx + dy*dy)
random.seed(1)
points = []
points.append((0, 0))
points.append((1, 1))
points.append((1, 2))
points.append((1, 3))
points.append((-1, 1))
points.append((-1, 2))
points.append((-1, 3))
# print(n)
# for i in range(n-1):
# points.append((random.randint(0, 100), random.randint(0, 100)))
m = Model()
# Create variables
vars = {}
for i in range(n):
for j in range(n):
vars[i, j] = m.addVar(obj=distance(points, i, j), vtype=GRB.BINARY, name='e_'+str(i)+'_'+str(j))
m.update()
uVars = {}
for i in range(n):
uVars[i] = m.addVar(vtype=GRB.INTEGER, name='u_'+str(i))#lb=K, ub=L, vtype=GRB.INTEGER, name='u_'+str(i))
#vars[i, i].ub = 0
m.update()
m.addConstr(quicksum(vars[0, i] for i in range(1, n)) == salesmen)
m.update()
m.addConstr(quicksum(vars[i, 0] for i in range(1, n)) == salesmen)
m.update()
for j in range(1, n):
m.addConstr(quicksum(vars[i, j] for i in range(n)) == 1)
m.update()
for i in range(1, n):
m.addConstr(quicksum(vars[i, j] for j in range(n)) == 1)
m.update()
for i in range(1, n):
m.addConstr(uVars[i] + (L - 2)*vars[0, i] - vars[i, 0] <= (L - 1))
m.update()
for i in range(1, n):
m.addConstr(uVars[i] + vars[0, i] + (2 - K)*vars[i, 0] >= 2)
m.update()
for i in range(1, n):
m.addConstr(vars[0, i] + vars[i, 0] <= 1)
m.update()
for i in range(1, n):
for j in range(1, n):
if i != j:
m.addConstr(uVars[i] - uVars[j] + L*vars[i, j] + (L - 2)*vars[j, i] <= (L - 1))
m.update()
m.write("mtsp.lp")
m.update()
totVars = dict(list(vars.items())+list(uVars.items()))
m._vars = vars
m._uvars = uVars
m.optimize()
solution = m.getAttr('x', vars)
selected = [(i,j) for i in range(n) for j in range(n) if solution[i,j] > 0.5]
uValues = m.getAttr('x', uVars)
print("U values: ", uValues)
print('')
print('Optimal tour: %s' % str(selected))
print('Optimal cost: %g' % m.objVal)
print('')