PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
rcs.py
Go to the documentation of this file.
1 
3 """
4 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
5 """
6 from pyscipopt import Model, quicksum, multidict
7 
8 
9 def rcs(J, P, R, T, p, c, a, RUB):
10  """rcs -- model for the resource constrained scheduling problem
11  Parameters:
12  - J: set of jobs
13  - P: set of precedence constraints between jobs
14  - R: set of resources
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.
21  """
22  model = Model("resource constrained scheduling")
23 
24  s, x = {}, {} # s - start time variable; x=1 if job j starts on period t
25  for j in J:
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))
29 
30  for j in J:
31  # job execution constraints
32  model.addCons(quicksum(x[j, t] for t in range(1, T - p[j] + 2)) == 1, "ConstrJob(%s,%s)" % (j, t))
33 
34  # start time constraints
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))
36 
37  # resource upper bound constraints
38  for t in range(1, T - p[j] + 2):
39  for r in R:
40  model.addCons(
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)
44 
45  # time (precedence) constraints, i.e., s[k]-s[j] >= p[j]
46  for (j, k) in P:
47  model.addCons(s[k] - s[j] >= p[j], "Precedence(%s,%s)" % (j, k))
48 
49  model.setObjective(quicksum(c[j, t] * x[j, t] for (j, t) in x), "minimize")
50 
51  model.data = x, s
52  return model
53 
54 
55 def make_1r():
56  """creates example data set 1"""
57  J, p = multidict({ # jobs, processing times
58  1: 1,
59  2: 3,
60  3: 2,
61  4: 2,
62  })
63  P = [(1, 2), (1, 3), (2, 4)]
64  R = [1]
65  T = 6
66  c = {}
67  for j in J:
68  for t in range(1, T - p[j] + 2):
69  c[j, t] = 1 * (t - 1 + p[j])
70  a = {
71  (1, 1, 0): 2,
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,
75  }
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)
78 
79 
80 def make_2r():
81  """creates example data set 2"""
82  J, p = multidict({ # jobs, processing times
83  1: 2,
84  2: 2,
85  3: 3,
86  4: 2,
87  5: 5,
88  })
89  P = [(1, 2), (1, 3), (2, 4)]
90  R = [1, 2]
91  T = 6
92  c = {}
93  for j in J:
94  for t in range(1, T - p[j] + 2):
95  c[j, t] = 1 * (t - 1 + p[j])
96  a = {
97  # resource 1:
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,
103  # resource 2:
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,
109  }
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)
113 
114 
115 if __name__ == "__main__":
116  (J, P, R, T, p, c, a, RUB) = make_2r()
117  model = rcs(J, P, R, T, p, c, a, RUB)
118  model.optimize()
119  x, s = model.data
120 
121  print("Optimal value:", model.getObjVal())
122  for (j, t) in x:
123  if model.getVal(x[j, t]) > 0.5:
124  print(x[j, t].name, "=", model.getVal(x[j, t]))
125 
126  for j in s:
127  if model.getVal(s[j]) > 0.:
128  print(s[j].name, "=", model.getVal(s[j]))
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
rcs.rcs
def rcs(J, P, R, T, p, c, a, RUB)
Definition: rcs.py:9
rcs.make_2r
def make_2r()
Definition: rcs.py:80
pyscipopt.Multidict.multidict
def multidict(D)
Definition: Multidict.py:3
rcs.make_1r
def make_1r()
Definition: rcs.py:55
rcs
Definition: rcs.py:1