4 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
6 from pyscipopt
import Model, quicksum, multidict
9 def rcs(J, P, R, T, p, c, a, RUB):
10 """rcs -- model for the resource constrained scheduling problem
13 - P: set of precedence constraints between jobs
15 - T: number of periods
16 - p[j]: processing time of job j
17 - c[j,t]: cost incurred when job j starts processing on period t.
18 - a[j,r,t]: resource r usage for job j on period t (after job starts)
19 - RUB[r,t]: upper bound for resource r on period t
20 Returns a model, ready to be solved.
22 model = Model(
"resource constrained scheduling")
26 s[j] = model.addVar(vtype=
"C", name=
"s(%s)" % j)
27 for t
in range(1, T - p[j] + 2):
28 x[j, t] = model.addVar(vtype=
"B", name=
"x(%s,%s)" % (j, t))
32 model.addCons(
quicksum(x[j, t]
for t
in range(1, T - p[j] + 2)) == 1,
"ConstrJob(%s,%s)" % (j, t))
35 model.addCons(
quicksum((t - 1) * x[j, t]
for t
in range(2, T - p[j] + 2)) == s[j],
"ConstrJob(%s,%s)" % (j, t))
38 for t
in range(1, T - p[j] + 2):
41 quicksum(a[j, r, t - t_] * x[j, t_]
for j
in J
for t_
in
42 range(max(t - p[j] + 1, 1), min(t + 1, T - p[j] + 2))) \
43 <= RUB[r, t],
"ResourceUB(%s)" % t)
47 model.addCons(s[k] - s[j] >= p[j],
"Precedence(%s,%s)" % (j, k))
49 model.setObjective(
quicksum(c[j, t] * x[j, t]
for (j, t)
in x),
"minimize")
56 """creates example data set 1"""
63 P = [(1, 2), (1, 3), (2, 4)]
68 for t
in range(1, T - p[j] + 2):
69 c[j, t] = 1 * (t - 1 + p[j])
72 (2, 1, 0): 2, (2, 1, 1): 1, (2, 1, 2): 1,
73 (3, 1, 0): 1, (3, 1, 1): 1,
74 (4, 1, 0): 1, (4, 1, 1): 2,
76 RUB = {(1, 1): 2, (1, 2): 2, (1, 3): 1, (1, 4): 2, (1, 5): 2, (1, 6): 2}
77 return (J, P, R, T, p, c, a, RUB)
81 """creates example data set 2"""
89 P = [(1, 2), (1, 3), (2, 4)]
94 for t
in range(1, T - p[j] + 2):
95 c[j, t] = 1 * (t - 1 + p[j])
98 (1, 1, 0): 2, (1, 1, 1): 2,
99 (2, 1, 0): 1, (2, 1, 1): 1,
100 (3, 1, 0): 1, (3, 1, 1): 1, (3, 1, 2): 1,
101 (4, 1, 0): 1, (4, 1, 1): 1,
102 (5, 1, 0): 0, (5, 1, 1): 0, (5, 1, 2): 1, (5, 1, 3): 0, (5, 1, 4): 0,
104 (1, 2, 0): 1, (1, 2, 1): 0,
105 (2, 2, 0): 1, (2, 2, 1): 1,
106 (3, 2, 0): 0, (3, 2, 1): 0, (3, 2, 2): 0,
107 (4, 2, 0): 1, (4, 2, 1): 2,
108 (5, 2, 0): 1, (5, 2, 1): 2, (5, 2, 2): 1, (5, 2, 3): 1, (5, 2, 4): 1,
110 RUB = {(1, 1): 2, (1, 2): 2, (1, 3): 2, (1, 4): 2, (1, 5): 2, (1, 6): 2, (1, 7): 2,
111 (2, 1): 2, (2, 2): 2, (2, 3): 2, (2, 4): 2, (2, 5): 2, (2, 6): 2, (2, 7): 2}
112 return (J, P, R, T, p, c, a, RUB)
115 if __name__ ==
"__main__":
117 model =
rcs(J, P, R, T, p, c, a, RUB)
121 print(
"Optimal value:", model.getObjVal())
123 if model.getVal(x[j, t]) > 0.5:
124 print(x[j, t].name,
"=", model.getVal(x[j, t]))
127 if model.getVal(s[j]) > 0.:
128 print(s[j].name,
"=", model.getVal(s[j]))