Skip to content

Commit 6b117e9

Browse files
committed
Minor refactoring
1 parent 381e50b commit 6b117e9

19 files changed

Lines changed: 141 additions & 45 deletions

File tree

crafted/LargeScaleScheduling/LargeScaleScheduling.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030

3131
satisfy(
3232
# resource cumulative constraint
33-
Cumulative(origins=x, lengths=durations, heights=heights) <= limit
33+
Cumulative(
34+
origins=x,
35+
lengths=durations,
36+
heights=heights
37+
) <= limit
3438
)
3539

3640
minimize(

crafted/LargeScaleScheduling/LargeScaleScheduling_z.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838

3939
satisfy(
4040
# resource cumulative constraint
41-
Cumulative(origins=x, lengths=durations, heights=heights) <= limit,
41+
Cumulative(
42+
origins=x,
43+
lengths=durations,
44+
heights=heights
45+
) <= limit,
4246

4347
# computing the make-span
4448
z == Maximum(x[i] + durations[i] for i in range(nTasks)),

crafted/NFC/NFC.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
shift, workers = data
2525
nPeriods = len(workers)
2626

27-
arcs = [(i, (i + 1) % nPeriods) for i in range(nPeriods)] + [(i, (i + nPeriods - shift) % nPeriods) for i in range(nPeriods)]
27+
A = [(i, (i + 1) % nPeriods) for i in range(nPeriods)] + [(i, (i + nPeriods - shift) % nPeriods) for i in range(nPeriods)]
2828

2929
# w[i] is the number of workers at the ith period
3030
w = VarArray(size=nPeriods, dom=range(max(workers) + 1))
@@ -39,12 +39,25 @@
3939
[w[i] >= workers[i] for i in range(nPeriods)],
4040

4141
# computing the cost of the flow
42-
Flow(w + f, balance=0, arcs=arcs, weights=[1] * nPeriods + [0] * nPeriods) == z,
43-
44-
[w[i] == Sum(f[(i + k) % nPeriods] for k in range(1, shift + 1)) for i in range(nPeriods)]
42+
Flow(
43+
w + f,
44+
balance=0,
45+
arcs=A,
46+
weights=[1] * nPeriods + [0] * nPeriods
47+
) == z,
48+
49+
[w[i] == Sum(f[i + 1:i + 1 + shift]) for i in range(nPeriods)]
4550
)
4651

4752
minimize(
4853
# minimizing the cost of the flow
4954
z
5055
)
56+
57+
""" Comments
58+
1) Note that:
59+
Sum(f[i + 1:i + 1 + shift])
60+
is equivalent to:
61+
Sum(f[(i + k) % nPeriods] for k in range(1, shift + 1))
62+
Here, automatic index auto-adjustment is used
63+
"""

crafted/NurseRostering_p/NurseRostering_p.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ def automaton():
6060
x = VarArray(size=[n, n], dom=range(n))
6161

6262
satisfy(
63+
# respecting preset tasks
6364
[x[i][j] == k for (i, j, k) in preset],
6465

66+
# respecting forbidden assignments
6567
[x[i][j] != k for (i, j, k) in forbidden],
6668

69+
# respecting job rules for each employee
6770
[x[i] in A for i in range(n)],
6871

72+
# all tasks are different at any time
6973
[AllDifferent(x[:, j]) for j in range(n)]
7074
)

crafted/Rubik/Rubik.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929

3030
satisfy(
3131
# respecting all clauses
32-
Clause([x[abs(v) - 1] for v in clause], phases=[v > 0 for v in clause]) for clause in clauses
32+
Clause(
33+
[x[abs(v) - 1] for v in clause],
34+
phases=[v > 0 for v in clause]
35+
) for clause in clauses
3336
)
3437

3538
"""

realistic/AircraftAssemblyLine/AircraftAssemblyLine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ def station_of_task(i):
6767
# respecting limit capacities of areas
6868
[
6969
Cumulative(
70-
Task(origin=x[t], length=durations[t], height=usedAreaRooms[t][i]) for t in areaTasks[i]
70+
tasks=[Task(origin=x[t], length=durations[t], height=usedAreaRooms[t][i]) for t in areaTasks[i]]
7171
) <= areaCapacities[i] for i in range(nAreas) if len(areaTasks[i]) > 1
7272
],
7373

7474
# computing/restricting the number of operators at each station
7575
[
7676
Cumulative(
77-
Task(origin=x[t], length=durations[t], height=operators[t] * (x[t] // takt == j)) for t in range(nTasks)
77+
tasks=[Task(origin=x[t], length=durations[t], height=operators[t] * (x[t] // takt == j)) for t in range(nTasks)]
7878
) <= z[j] for j in range(nStations)
7979
],
8080

realistic/Benzenoide/Benzenoide.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def T1(i, j):
7676

7777
# tag(symmetry-breaking)
7878
[
79-
[LexDecreasing(x, [x[row] for row in sym]) for sym in symmetries],
79+
[LexDecreasing(x, x[symmetry]) for symmetry in symmetries],
8080

8181
[Precedence(y, values=(1, v)) for v in range(2, n + 1)]
8282

@@ -110,6 +110,10 @@ def T1(i, j):
110110
is a shortcut for:
111111
[y[k][l] for k, l in neighbors[i][j]]
112112
3) Data for the XCSP23 competition are : [6,7,8,9,10,11,12,13,14,15]
113+
4) Note that:
114+
x[symmetry]
115+
is equivalent to:
116+
[x[row] for row in sym]
113117
"""
114118

115119
# [(y[i][j], y[k][l]) in [(ne(1), ANY), (1, 2)] for i in range(n) for j in range(widths[i])

realistic/CVRP/CVRP.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ def max_tour():
5050
Cardinality(c, occurrences={i: 1 if i > 0 else n0s for i in range(nNodes)}),
5151

5252
# no holes permitted during tours
53-
[If(c[i][j] == 0, Then=c[i][j + 1] == 0) for i in range(nVehicles) for j in range(nSteps - 1)],
53+
[
54+
If(
55+
c[i][j] == 0,
56+
Then=c[i][j + 1] == 0
57+
) for i in range(nVehicles) for j in range(nSteps - 1)
58+
],
5459

5560
# computing the collected demands
5661
[demands[c[i][j]] == d[i][j] for i in range(nVehicles) for j in range(nSteps)],

realistic/Cargo/Cargo.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,31 @@
105105
[ft[j] - eta[j] - vrd[j] <= limits.maxDelay for j in range(nVessels)],
106106

107107
# not overlapping stockpiles in space and time
108-
NoOverlap(origins=(r_st, y), lengths=(r_dt, lengths)),
108+
NoOverlap(
109+
origins=(r_st, y),
110+
lengths=(r_dt, lengths)
111+
),
109112

110113
# not exceeding the pad lengths
111-
Cumulative(origins=r_st, lengths=r_dt, heights=lengths) <= H // coeffs.meter,
114+
Cumulative(
115+
origins=r_st,
116+
lengths=r_dt,
117+
heights=lengths
118+
) <= H // coeffs.meter,
112119

113120
# not exceeding the daily stacking capacity
114-
Cumulative(origins=r_st, lengths=r_sd, heights=dailyStackingTonnages) <= limits.dailyStacking,
121+
Cumulative(
122+
origins=r_st,
123+
lengths=r_sd,
124+
heights=dailyStackingTonnages
125+
) <= limits.dailyStacking,
115126

116127
# ensuring that there are always enough reclaimers
117-
Cumulative(origins=rt, lengths=rd, heights=1) <= limits.nReclaimers
128+
Cumulative(
129+
origins=rt,
130+
lengths=rd,
131+
heights=1
132+
) <= limits.nReclaimers
118133
)
119134

120135
minimize(

realistic/CityPosition/CityPosition.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def approx_distance(x1, y1, x2, y2):
4141
[
4242
y[-3] == Maximum(y),
4343
x[-2] * 2 > x[-3] + x[-1],
44-
x[-1] == 0, y[-1] == 0
44+
x[-1] == 0,
45+
y[-1] == 0
4546
]
4647
)
4748

0 commit comments

Comments
 (0)