-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathILP_solver.py
47 lines (37 loc) · 1.4 KB
/
ILP_solver.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Chao
@contact: [email protected]
@time: 1/28/2019 1:30 PM
"""
import gurobipy
def ILPSolve(s, d, l, _lambda):
input_len = len(s)
MODEL = gurobipy.Model("Summary")
# create variables
x = MODEL.addVars(input_len, lb=0, ub=1, vtype=gurobipy.GRB.BINARY, name='x')
y = MODEL.addVars(input_len, input_len, lb=0, ub=1, vtype=gurobipy.GRB.BINARY, name='y')
# update the variable environment
MODEL.update()
# create the objective
# MODEL.setObjective(x.prod(s) + 1 * y.prod(d), gurobipy.GRB.MAXIMIZE)
if _lambda:
MODEL.setObjective(x.prod(s) - \
_lambda * gurobipy.quicksum(
y[i, j] * d[i][j] for i in range(input_len) for j in range(input_len)), \
gurobipy.GRB.MAXIMIZE)
else:
MODEL.setObjective(x.prod(s), gurobipy.GRB.MAXIMIZE)
# create the constrains
MODEL.addConstr(x.prod(l) <= 100)
MODEL.addConstrs(y[i, j] >= x[i] + x[j] - 1 for i in range(input_len) for j in range(input_len))
MODEL.addConstrs(y[i, j] <= (x[i] + x[j]) / 2.0 for i in range(input_len) for j in range(input_len))
# run the model
MODEL.optimize()
x_val = []
print("Obj:", MODEL.objVal)
for v in MODEL.getVars():
if 'x' in v.varName:
x_val.append(v.x)
return x_val