-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.py
58 lines (41 loc) · 1.33 KB
/
algo.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 numpy as np
import scipy as sp
from scipy.sparse import diags
import gurobipy as gp
from gurobipy import GRB
def ilp(B, alpha, gamma):
m = np.shape(B)[0]
n = np.shape(B)[1]
one_row = np.ones(m, dtype=int) # row vector of all ones
one_column = np.ones((n, 1), dtype=int) # column vector of all ones
alpha_vec = alpha * one_row
gamma_vec = gamma * one__column
# -------------------------------- #
# The model and variables #
# -------------------------------- #
M = gp.Model("SOLVE-ILP")
# Variables: {0, 1}
Y = M.addMVar(shape = (n, n), vtype = GRB.BINARY, name = "Y")
# ------------------ #
# Objective #
# ------------------ #
M.setObjective(Y.sum(), GRB.MINIMIZE)
# ------------------- #
# Constraints #
# ------------------- #
# Pairwise constratins is a little tricky
M.addConstr(Y @ one_column <= gamma_vec)
M.addConstr(one_row @ Y <= alpha_vec)
# -------------------- #
# Time limit #
# -------------------- #
#M.Params.timeLimit = 100.0
# -------------------- #
# Optimization #
# -------------------- #
M.optimize()
# ---------------- #
# Output #
# ---------------- #
print("The objective: {}".format(M.objVal))
return(x.X)