-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlpscheduler.py
278 lines (248 loc) · 9.75 KB
/
lpscheduler.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
lpscheduler: a simple linear-programming-based scheduling utility
"""
import numpy as np
from copy import deepcopy
from cvxopt import glpk, matrix, spmatrix
class Expression:
"""
Sparse linear expression as a sum of terms of the form `coeff * x[index]`,
where `x` is the variable array and `index` is a (who, when, what) integer
triplet. Expressions can be scaled and added together. Internally
represented by a dictionary mapping each `index` to `coeff`.
"""
def __init__(self, coeffs):
self.coeffs = coeffs # dict: index -> coeff
def __str__(self):
return 'Expression({})'.format(self.coeffs)
def __iter__(self):
"""Iterate over (index, coeff) pairs."""
return ((index, coeff) for (index, coeff) in self.coeffs.items())
def __add__(self, other):
expr = deepcopy(self)
expr += other
return expr
def __radd__(self, other):
return self + other
def __iadd__(self, other):
if other == 0: return self # for use with `sum`
for (index, coeff) in other:
if index in self.coeffs: self.coeffs[index] += coeff
else: self.coeffs[index] = coeff
return self
def __sub__(self, other):
return self + (-other)
def __isub__(self, other):
self += -other
return self
def __neg__(self):
self *= -1
return self
def __mul__(self, a):
"""Scalar multiplication."""
return a * self
def __rmul__(self, a):
expr = deepcopy(self)
expr *= a
return expr
def __imul__(self, a):
for index in self.coeffs: self.coeffs[index] *= a
return self
def pretty_str(self, scheduler):
"""
Like __str__ but uses Scheduler instance to translate indices to human-
readable (who, when, what) triplets.
"""
def _pretty_str(index, coeff):
who = scheduler.who [index[0]]
when = scheduler.when[index[1]]
what = scheduler.what[index[2]]
return '({}, {}, {}): {}'.format(who, when, what, coeff)
s = ', '.join((_pretty_str(index, coeff) for (index, coeff) in self))
return 'Expression({})'.format(s)
class Constraint:
"""
Sparse linear constraint of the form `expr ctype cval`, where:
- `expr` : sparse linear expression
- `ctype`: constraint type ('=', '<=', or '>=')
- `val` : constraint value
"""
def __init__(self, expr, ctype, val):
if ctype not in ('=', '<=', '>='):
raise ValueError('unsupported constraint type {}'.format(ctype))
self.expr = expr
self.ctype = ctype
self.val = val
def __str__(self):
return 'Constraint({} {} {})'.format(self.expr, self.ctype, self.val)
def pretty_str(self, scheduler):
"""Like Expression.pretty_str for human-readable formatting."""
return 'Constraint({} {} {})'.format(self.expr.pretty_str(scheduler),
self.ctype, self.val)
class Scheduler:
"""
Scheduler for "who-when-what" assignment problems.
Variable space is set of binary variables `x[i,j,k]`, where `(i, j, k)`
indexes (who, when, what). A value of `x[i,j,k] = 1` means that person `i`
at time `j` is assigned to job `k`. Structural constraints:
`sum(x[i,j,:]) = 1` for each `(i, j)`.
"""
def __init__(self, who, when, what):
"""
Initialize scheduler. Problem space defined by (who, when, what) arrays
of human-readable identifiers, to be referenced by __call__.
"""
self.who = np.array(who )
self.when = np.array(when)
self.what = np.array(what)
self.whod = dict(zip(self.who , range(len(self.who ))))
self.whend = dict(zip(self.when, range(len(self.when))))
self.whatd = dict(zip(self.what, range(len(self.what))))
self.cost = None
self.cons = []
for who in self.who:
for when in self.when:
expr = sum((self(who, when, what) for what in self.what))
self.addcons(Constraint(expr, '=', 1))
def __call__(self, who, when, what):
"""
Main interface for model building. Provides translation from
(who, when, what) triplet to internal model index. Technically returns
an Expression for that index with unit coefficient.
"""
i = self.whod [who ]
j = self.whend[when]
k = self.whatd[what]
return Expression({(i,j,k): 1})
def addcons(self, cons):
"""Add constraint."""
self.cons.append(cons)
def setcost(self, expr):
"""Set linear cost function (of type Expression)."""
self.cost = expr
def solve(self, **kwargs):
"""
Solve scheduling problem. Launches GLPK backend to solve corresponding
ILP. Returns solution as "who-when" table with entries corresponding to
assigned "what" indices. Can pass keyword arguments to control solver
options.
"""
# convert to standard form
I = len(self.who )
J = len(self.when)
K = len(self.what)
index1 = np.reshape(range(I*J*K), (I,J,K))
N = index1.size
c = matrix(np.zeros(N))
if self.cost is not None:
for (index, coeff) in self.cost: c[int(index1[index])] = coeff
An, Ax, Ai, Aj, b = 0, [], [], [], []
Gn, Gx, Gi, Gj, h = 0, [], [], [], []
for cons in self.cons:
if cons.ctype == '=':
for (index, coeff) in cons.expr:
Ax.append(coeff)
Ai.append(An)
Aj.append(index1[index])
b.append(cons.val)
An += 1
elif cons.ctype == '<=' or cons.ctype == '>=':
s = 1 if cons.ctype == '<=' else -1
for (index, coeff) in cons.expr:
Gx.append(s*coeff)
Gi.append(Gn)
Gj.append(index1[index])
h.append(s*cons.val)
Gn += 1
A = spmatrix(np.array(Ax), np.array(Ai), np.array(Aj))
b = matrix(b, tc='d')
if Gn == 0:
G = matrix(np.zeros((1,N)))
h = matrix(0, tc='d')
else:
G = spmatrix(np.array(Gx), np.array(Gi), np.array(Gj), size=(Gn,N))
h = matrix(h, tc='d')
# solve with GLPK
glpk.options.update(kwargs)
status, x = glpk.ilp(c, G, h, A, b, B=set(range(N)))
# process output
if x is None:
raise RuntimeError('no solution, exit status "{}"'.format(status))
t = np.empty((I,J), dtype=int)
X = np.reshape(x, index1.shape)
for i in range(I):
for j in range(J):
what = np.nonzero(X[i,j,:])[0][0]
t[i,j] = what
return t
def format(self, t):
"""Show solution as human-readable formatted table."""
wholen = max(len(who ) for who in self.who )
whenlen = max(len(when) for when in self.when)
whatlen = max(len(what) for what in self.what)
l = max(whenlen, whatlen)
header = ([' '*wholen, '|'] + [when.center(l) for when in self.when])
header = ' '.join(header)
s = header + '\n'
s += ('-'*(wholen + 1) + '+' + '-'*len(header))[:len(header)]
for (i, who) in enumerate(self.who):
line = [who.center(wholen), '|']
for (j, when) in enumerate(self.when):
what = self.what[t[i,j]]
line.append(what.center(l))
s += '\n' + ' '.join(line)
return s
def formatcsv(self, t, sep=','):
"""
Like `format` but shows as CSV (or any other separator); useful for
importing into other programs.
"""
s = sep + sep.join(self.when)
for (i, who) in enumerate(self.who):
line = who
for (j, when) in enumerate(self.when):
what = self.what[t[i,j]]
line += sep + what
s += '\n' + line
return s
def setrandcost(self):
"""
Set random cost function. Useful for sampling from solution space of
pure-constraint problem.
"""
I = len(self.who )
J = len(self.when)
K = len(self.what)
r = np.random.rand(I, J, K)
self.cost = sum([Expression({(i,j,k): r[i,j,k] for i in range(I)
for j in range(J)
for k in range(K)})])
def shuffle(self):
"""
Randomly shuffle indices. Useful for sampling from general solution
space.
"""
def _shuffle(expr, whop, whenp, whatp):
coeffs = expr.coeffs
_expr = {}
for index in coeffs:
i = whop [index[0]]
j = whenp[index[1]]
k = whatp[index[2]]
_expr[(i,j,k)] = coeffs[index]
return Expression(_expr)
whop = np.random.permutation(len(self.who ))
whenp = np.random.permutation(len(self.when))
whatp = np.random.permutation(len(self.what))
self.who = self.who [np.argsort(whop )]
self.when = self.when[np.argsort(whenp)]
self.what = self.what[np.argsort(whatp)]
self.whod = {key: whop [self.whod [key]] for key in self.whod }
self.whend = {key: whenp[self.whend[key]] for key in self.whend}
self.whatd = {key: whatp[self.whatd[key]] for key in self.whatd}
if self.cost: self.cost = _shuffle(self.cost, whop, whenp, whatp)
_cons = []
for cons in self.cons:
expr = _shuffle(cons.expr, whop, whenp, whatp)
_cons.append(Constraint(expr, cons.ctype, cons.val))
self.cons = _cons