-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxCov.py
114 lines (91 loc) · 3.37 KB
/
MaxCov.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
import sys
import math
import gurobipy as gp
from gurobipy import GRB
"""
input file:
u,n
w[0,0], .., .., w[0, n-1] w[0, n] w[0, n + 1] ... w[0, n + u -1] // consider also depots
w[1,0], .., .., w[1, n-1] ...
........................
........................
........................
........................
w[n -1, 0], .. w[u -1, n-1] ...
b0, b1, .., b_{u - 1}
"""
def solve_model(n, U, w, b_data):
assert(n > 0 and U > 0)
print((n, U))
print(b_data)
with gp.Model("MaxCov") as model:
delta = model.addVars(n, vtype=GRB.BINARY, name="delta")
# z does not require depot
z = model.addVars(U, n, lb=1, ub=n, vtype=GRB.INTEGER, name="z")
# X_{u}[i,j]
# n + 1 because of depot
X = model.addVars(U, n + 1, n + 1, vtype=GRB.BINARY, name="X")
b = model.addVars(U, vtype=GRB.CONTINUOUS, name="b")
model.addConstrs((b[i] == b_data[i] for i in range(U)), name="b_const")
model.addConstrs((X.sum(u, '*', j) == X.sum(u, j, "*")
for u in range(U)
for j in range(n + 1)), name="X_req0")
#depot constr
model.addConstrs((X.sum(u, n, '*') == 1
for u in range(U)),
name="depot_constr")
# hamilton constrs
model.addConstrs((z[u, j] - z[u, i] >= X[u, i, j] + n * (X[u, i, j] - 1)
for u in range(U)
for i in range(n) # depot is excluded
for j in range(n)), name="hamilton_constr")
# cost constraints
model.addConstrs(gp.quicksum(w[i][j] * X[u, i, j]
for i in range(n)
for j in range(n)) +
#depot start
gp.quicksum(w[n + u][k] * X[u, n, k] for k in range(n)) +
#depot arrive
gp.quicksum(w[i][ n + u] * X[u, i, n] for i in range(n)) <=
b[u] for u in range(U))
# delta constraint
model.addConstrs(delta[i] <= X.sum("*", i, '*')
for i in range(n))
# add delta maximisation objective
model.ModelSense = GRB.MAXIMIZE
model.setObjectiveN(delta.sum("*"), index=0, priority=2, name= "MaxDelta")
model.update()
model.write("test.lp")
model.optimize()
status = model.Status
if status in (GRB.INF_OR_UNBD, GRB.INFEASIBLE, GRB.UNBOUNDED):
print('Model cannot be solved because it is infeasible or unbounded')
sys.exit(0)
if status != GRB.OPTIMAL:
print('Optimization was stopped with status ' + str(status))
sys.exit(0)
return _parse_results(model)
def _parse_results(model):
return {
"obj_val" : model.ObjVal,
"var_map" : dict(map(lambda x: (x.VarName, x.X), model.getVars()))
}
"""
solve_model(n=3, U=2,
w_data= [[0.0, 0.5, 0.9, 1.0, 1.3],
[0.5, 0.0, 0.5, 1.5, 1.5],
[0.9, 0.5, 0.0, 1.5, 0.9],
[1.0, 1.5, 1.5, 0.0, 9.0],
[1.3, 1.5, 0.9, 9.0, 0.0]],
b_data= [20, 20])
"""
if __name__ == "__main__":
res = solve_model(n=3, U=2,
w= [[0.0, 0.5, 0.9, 1.0, 1.3],
[0.5, 0.0, 0.5, 1.5, 1.5],
[0.9, 0.5, 0.0, 1.5, 0.9],
[1.0, 1.5, 1.5, 0.0, 9.0],
[1.3, 1.5, 0.9, 9.0, 0.0]],
b_data = [2, 3])
print(res)