Skip to content

Commit 91fc859

Browse files
committed
Refactoring
1 parent b7c9eb6 commit 91fc859

File tree

58 files changed

+583
-454
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+583
-454
lines changed

realistic/RACP/RACP.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
See EJOR paper cited below.
44
55
The model, below, is close to (can be seen as the close translation of) the one submitted to the 2018/2020 Minizinc challenges.
6-
The MZN model was proposed by Andreas Schutt.
7-
No Licence was explicitly mentioned (so, MIT Licence is currently assumed).
6+
The original MZN model was proposed by Andreas Schutt - no licence was explicitly mentioned (so, MIT Licence is currently assumed).
87
98
## Data Example
109
j30-13-6-1-25.json
@@ -18,27 +17,29 @@
1817
1918
## Links
2019
- https://www.sciencedirect.com/science/article/abs/pii/S037722171730927X?via%3Dihub
21-
- https://www.minizinc.org/challenge2020/results2020.html
20+
- https://www.minizinc.org/challenge/2020/results/
2221
2322
## Tags
2423
realistic, mzn18, mzn20
2524
"""
2625

2726
from pycsp3 import *
2827

29-
horizon, resourceCosts, durations, successors, needs = data
30-
nResources, nTasks = len(resourceCosts), len(durations)
28+
horizon, resourceCosts, durations, successors, needs = data or load_json_data("j30-13-6-1-25.json")
3129

32-
lb_usage, ub_usage = [max(needs[r]) for r in range(nResources)], [sum(needs[r]) for r in range(nResources)]
30+
nTasks, nResources = len(durations), len(resourceCosts)
31+
T, R = range(nTasks), range(nResources)
32+
33+
lb_usage, ub_usage = [max(needs[r]) for r in R], [sum(needs[r]) for r in R]
3334
lb_costs, ub_costs = resourceCosts * lb_usage, resourceCosts * ub_usage
3435

3536

3637
def unrelated_tasks():
37-
succs = lambda i: successors[i] + list({v for t in [succs(j) for j in successors[i]] for v in t})
38+
succs = lambda i: successors[i] + list({v for t in [succs(j) for j in successors[i]] for v in t}) # recursive function
3839

39-
all_successors = [succs(i) for i in range(nTasks)]
40-
all_predecessors = [[j for j in range(nTasks) if i in all_successors[j]] for i in range(nTasks)]
41-
return [[j for j in range(nTasks) if j not in all_successors[i] and j not in all_predecessors[i] and j != i] for i in range(nTasks)]
40+
all_successors = [succs(i) for i in T]
41+
all_predecessors = [[j for j in T if i in all_successors[j]] for i in T]
42+
return [[j for j in T if j not in all_successors[i] and j not in all_predecessors[i] and j != i] for i in T]
4243

4344

4445
unrelated = unrelated_tasks()
@@ -49,13 +50,12 @@ def compute_ub():
4950
lct = lambda i: horizon if len(successors[i]) == 0 else min(lct(j) - durations[j] for j in successors[i])
5051
overlap = lambda si, di, sj, dj: si < sj + dj and sj < si + di
5152

52-
predecessors = [[j for j in range(nTasks) if i in successors[j]] for i in range(nTasks)]
53-
es, ls = [est(i) for i in range(nTasks)], [lct(i) for i in range(nTasks)]
54-
rusage_es = [max(needs[r][i] + sum(needs[r][j] for j in unrelated[i] if overlap(es[i], durations[i], es[j], durations[j]))
55-
for i in range(nTasks)) for r in range(nResources)]
53+
predecessors = [[j for j in T if i in successors[j]] for i in T]
54+
es, ls = [est(i) for i in T], [lct(i) for i in T]
55+
rusage_es = [max(needs[r][i] + sum(needs[r][j] for j in unrelated[i] if overlap(es[i], durations[i], es[j], durations[j])) for i in T) for r in R]
5656
rusage_ls = [max(needs[r][i] + sum(needs[r][j] for j in unrelated[i] if overlap(ls[i] - durations[i], durations[i], ls[j] - durations[j], durations[j]))
57-
for i in range(nTasks)) for r in range(nResources)]
58-
return min(sum(resourceCosts[r] * rusage_es[r] for r in range(nResources)), sum(resourceCosts[r] * rusage_ls[r] for r in range(nResources)))
57+
for i in T) for r in R]
58+
return min(sum(resourceCosts[r] * rusage_es[r] for r in R), sum(resourceCosts[r] * rusage_ls[r] for r in R))
5959

6060

6161
# s[i] is the starting time of the ith task
@@ -69,10 +69,10 @@ def compute_ub():
6969

7070
satisfy(
7171
# ending tasks before the given horizon
72-
[s[i] + durations[i] <= horizon for i in range(nTasks)],
72+
[s[i] + durations[i] <= horizon for i in T],
7373

7474
# respecting precedence relations
75-
[s[i] + durations[i] <= s[j] for i in range(nTasks) for j in successors[i]],
75+
[s[i] + durations[i] <= s[j] for i in T for j in successors[i]],
7676

7777
# redundant non-overlapping constraints tag(redundant)
7878
[
@@ -82,7 +82,7 @@ def compute_ub():
8282
s[i] + durations[i] <= s[j],
8383
s[j] + durations[j] <= s[i]
8484
)
85-
) for i in range(nTasks) for j in unrelated[i] for r in range(nResources) if needs[r][i] + needs[r][j] > lb_usage[r]
85+
) for i in T for j in unrelated[i] for r in R if needs[r][i] + needs[r][j] > lb_usage[r]
8686
],
8787

8888
# redundant constraints on the lower bound of the resource capacities tag(redundant)
@@ -92,15 +92,15 @@ def compute_ub():
9292
s[j] + durations[j] > s[i],
9393
s[j] <= s[i]
9494
) for j in unrelated[i] if needs[r][j] > 0
95-
) >= needs[r][i] for i in range(nTasks) for r in range(nResources) if needs[r][i] > 0
95+
) >= needs[r][i] for i in T for r in R if needs[r][i] > 0
9696
],
9797

9898
[
9999
Cumulative(
100100
origins=s,
101101
lengths=durations,
102102
heights=needs[r]
103-
) <= u[r] for r in range(nResources)
103+
) <= u[r] for r in R
104104
],
105105

106106
# computing the value of the objective

realistic/RCPSP/RCPSP.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
from pycsp3 import *
2424

25-
jobs, horizon, capacities, _ = data
25+
jobs, horizon, capacities, _ = data or load_json_data("j030-01-01.json")
26+
2627
durations, successors, quantities = zip(*jobs) # [job.duration for job in jobs]
2728
nJobs = len(jobs)
2829

@@ -36,7 +37,13 @@
3637
# resource constraints
3738
[
3839
Cumulative(
39-
tasks=[Task(origin=s[i], length=durations[i], height=quantities[i][k]) for i in range(nJobs) if quantities[i][k] > 0]
40+
tasks=[
41+
Task(
42+
origin=s[i],
43+
length=durations[i],
44+
height=quantities[i][k]
45+
) for i in range(nJobs) if quantities[i][k] > 0
46+
]
4047
) <= capacity for k, capacity in enumerate(capacities)
4148
]
4249
)

realistic/RCPSP/RCPSP_z.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
The model, below, is close to (can be seen as the close translation of) the one submitted to the 2009/2013 Minizinc challenges.
3-
No Licence was explicitly mentioned (MIT Licence assumed).
3+
For the original MZN model, no licence was explicitly mentioned (MIT Licence assumed).
44
55
## Data Example
66
00.json
@@ -13,17 +13,18 @@
1313
python RCPSP_z.py -data=<datafile.dzn> -parser=RCPSP_ParserZ.py
1414
1515
## Links
16-
- https://www.minizinc.org/challenge2013/results2013.html
16+
- https://www.minizinc.org/challenge/2013/results/
1717
1818
## Tags
1919
realistic, mzn08, mzn13
2020
"""
2121

2222
from pycsp3 import *
2323

24-
capacities, durations, requirements, successors = data
25-
nResources, nTasks = len(capacities), len(durations)
24+
capacities, durations, requirements, successors = data or load_json_data("00.json")
25+
2626
horizon = sum(durations) + 1
27+
nTasks, nResources = len(durations), len(capacities)
2728

2829
# x[i] is the starting time of the ith task
2930
x = VarArray(size=nTasks, dom=range(horizon))

realistic/RCPSP_MAX/RCPSP_MAX.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@
1919
python RCPSP_MAX.py -data=<datafile.dzn> -parser=RCPSP_MAX_ParserZ.py
2020
2121
## Links
22-
- https://www.minizinc.org/challenge2010/results2010.html
22+
- https://www.minizinc.org/challenge/2010/results/
2323
2424
## Tags
2525
realistic, mzn10
2626
"""
2727

2828
from pycsp3 import *
2929

30-
capacities, durations, requirements, precedences = data
31-
nResources, nTasks = len(capacities), len(durations)
30+
capacities, durations, requirements, precedences = data or load_json_data("psp-j20-34.json")
31+
32+
nTasks, nResources = len(durations), len(capacities)
33+
T, R = range(nTasks), range(nResources)
3234

3335
# trivial upper bound for the project duration
34-
horizon = sum(max([durations[i]] + [d for (j, d, _) in precedences if j == i]) for i in range(nTasks)) + 1
36+
horizon = sum(max([durations[i]] + [d for (j, d, _) in precedences if j == i]) for i in T) + 1
3537

36-
P = [(i, j) for i, j in combinations(nTasks, 2) if any(requirements[r][i] + requirements[r][j] > capacities[r] for r in range(nResources))]
38+
P = [(i, j) for i, j in combinations(T, 2) if any(requirements[r][i] + requirements[r][j] > capacities[r] for r in R)]
3739
P1 = [(i, j) for i, j in P if any(k == i and l == j and -durations[j] < d < durations[i] for (k, d, l) in precedences)]
3840
P2 = [(i, j) for i, j in P if any(k == j and l == i and -durations[i] < d < durations[j] for (k, d, l) in precedences)]
3941
P3 = [(i, j) for i, j in P if (i, j) not in P1 and (i, j) not in P2]
@@ -65,12 +67,16 @@
6567
# cumulative resource constraints
6668
[
6769
Cumulative(
68-
Task(origin=s[i], length=durations[i], height=requirements[r][i]) for i in range(nTasks)
69-
) <= capacities[r] for r in range(nResources)
70+
Task(
71+
origin=s[i],
72+
length=durations[i],
73+
height=requirements[r][i]
74+
) for i in T
75+
) <= capacities[r] for r in R
7076
],
7177

7278
# constraining the objective value
73-
[s[i] + durations[i] <= z for i in range(nTasks)]
79+
[s[i] + durations[i] <= z for i in T]
7480
)
7581

7682
minimize(

realistic/RCPSP_WET/RCPSP_WET.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
the given weight for earliness and tardiness per task.
77
88
The model, below, is close to (can be seen as the close translation of) the one submitted to the 2016/2017 challenges.
9-
The MZN model was proposed by University of Melbourne and NICTA (seems to be a MIT Licence).
9+
The original MZN model was proposed by University of Melbourne and NICTA (seems to be a MIT Licence).
1010
1111
## Data Example
1212
j30-27-5.json
@@ -19,48 +19,55 @@
1919
python RCPSP_WET.py -data=<datafile.dzn> -parser=RCPSP_WET_ParserZ.py
2020
2121
## Links
22-
- https://www.minizinc.org/challenge2017/results2017.html
22+
- https://www.minizinc.org/challenge/2017/results/
2323
2424
## Tags
2525
realistic, mzn16, mzn17
2626
"""
2727

2828
from pycsp3 import *
2929

30-
resources, tasks, horizon = data
30+
resources, tasks, horizon = data or load_json_data("j30-27-5.json")
31+
3132
requirements, capacities = zip(*resources)
3233
durations, successors, deadlines = zip(*tasks)
34+
3335
nTasks, nResources = len(tasks), len(resources)
36+
T, R = range(nTasks), range(nResources)
3437

35-
relevantTasks = [[i for i in range(nTasks) if requirements[r][i] > 0 and durations[i] > 0] for r in range(nResources)]
38+
relevantTasks = [[i for i in T if requirements[r][i] > 0 and durations[i] > 0] for r in R]
3639

3740
# s[i] is the starting time of the ith task
3841
s = VarArray(size=nTasks, dom=range(horizon))
3942

4043
satisfy(
4144
# enforcing precedence relations
42-
[s[i] + durations[i] <= s[j] for i in range(nTasks) for j in successors[i]],
45+
[s[i] + durations[i] <= s[j] for i in T for j in successors[i]],
4346

4447
# cumulative resource constraints
4548
[
4649
Cumulative(
47-
tasks=[Task(origin=s[i], length=durations[i], height=requirements[r][i]) for i in relevantTasks[r]]
48-
) <= capacities[r] for r in range(nResources) if sum(requirements[r][relevantTasks[r]]) > capacities[r]
50+
Task(
51+
origin=s[i],
52+
length=durations[i],
53+
height=requirements[r][i]
54+
) for i in relevantTasks[r]
55+
) <= capacities[r] for r in R if sum(requirements[r][relevantTasks[r]]) > capacities[r]
4956
],
5057

5158
# redundant non-overlapping constraints tag(redundant)
5259
[
5360
either(
5461
s[i] + durations[i] <= s[j],
5562
s[j] + durations[j] <= s[i]
56-
) for i, j in combinations(nTasks, 2) if any(requirements[r][i] + requirements[r][j] > capacities[r] for r in range(nResources))
63+
) for i, j in combinations(T, 2) if any(requirements[r][i] + requirements[r][j] > capacities[r] for r in R)
5764
],
5865
)
5966

6067
minimize(
6168
# minimizing the weighted earliness/tardiness objective
6269
Sum(
63-
deadlines[i][1] * max(0, deadlines[i][0] - s[i]) + deadlines[i][2] * max(0, s[i] - deadlines[i][0]) for i in range(nTasks)
70+
deadlines[i][1] * max(0, deadlines[i][0] - s[i]) + deadlines[i][2] * max(0, s[i] - deadlines[i][0]) for i in T
6471
)
6572
)
6673

0 commit comments

Comments
 (0)