-
Notifications
You must be signed in to change notification settings - Fork 0
/
mip.py
58 lines (50 loc) · 1.72 KB
/
mip.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
import sys
import math
import random
import itertools
from gurobipy import *
def subtourelim(model, where):
if where == GRB.Callback.MIPSOL:
vals = model.cbGetSolution(model._vars)
selected = tuplelist((i,j) for i,j in model._vars.keys() if vals[i,j] > 0.5)
tour = subtour(selected)
if len(tour) < n:
model.cbLazy(quicksum(model._vars[i,j]
for i,j in itertools.combinations(tour, 2))
<= len(tour)-1)
def subtour(edges):
unvisited = list(range(n))
cycle = range(n+1) # initial length has 1 more city
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(cycle) > len(thiscycle):
cycle = thiscycle
return cycle
n = 0
def gurobi_solution(points):
global n
n = len(points)
random.seed(1)
dist = {(i,j) :
math.sqrt(sum((points[i][k]-points[j][k])**2 for k in range(2)))
for i in range(n) for j in range(i)}
m = Model()
m.setParam('OutputFlag',False)
vars = m.addVars(dist.keys(), obj=dist, vtype=GRB.BINARY, name='e')
for i,j in vars.keys():
vars[j,i] = vars[i,j]
m.addConstrs(vars.sum(i,'*') == 2 for i in range(n))
m._vars = vars
m.Params.lazyConstraints = 1
m.optimize(subtourelim)
vals = m.getAttr('x', vars)
selected = tuplelist((i,j) for i,j in vals.keys() if vals[i,j] > 0.5)
tour = subtour(selected)
assert len(tour) == n
return (tour,m.objVal)