Skip to content

Commit

Permalink
Minor Refactoring of a few models
Browse files Browse the repository at this point in the history
  • Loading branch information
lecoutre committed Mar 16, 2024
1 parent 8adc362 commit 9387156
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
10 changes: 8 additions & 2 deletions realistic/OncallRostering/OncallRostering.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@
[x[i] != j for j in range(nWorkers) for i in unavailableDays[j]],

# counting working (regular) days by each worker
Cardinality([x[i] for i in range(nDays) if i not in weekends], occurrences=do),
Cardinality(
[x[i] for i in range(nDays) if i not in weekends],
occurrences=do
),

# counting working weekends by each worker
Cardinality(x[weekends], occurrences=wo),
Cardinality(
x[weekends],
occurrences=wo
),

# computing the balance violation wrt working days
[abs(workloads[j2] * do[j1] - workloads[j1] * do[j2]) <= 100 * db for j1, j2 in combinations(nWorkers, 2)],
Expand Down
4 changes: 2 additions & 2 deletions realistic/OpenShop/OpenShop.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@
# tasks on the same job cannot overlap
[
Cumulative(
tasks=[(x[i][j], durations[i][j], 1) for j in range(nMachines)]
tasks=[Task(origin=x[i][j], length=durations[i][j], height=1) for j in range(nMachines)]
) <= 1 for i in range(nJobs)
],

# tasks on the same machine cannot overlap
[
Cumulative(
tasks=[(x[i][j], durations[i][j], 1) for i in range(nJobs)]
tasks=[Task(origin=x[i][j], length=durations[i][j], height=1) for i in range(nJobs)]
) <= 1 for j in range(nMachines)
],

Expand Down
12 changes: 10 additions & 2 deletions realistic/OpenStacks/OpenStacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ def table2(t):
AllDifferent(p),

# computing starting times of stacks
[Minimum(p[j] for j in range(m) if orders[i][j]) == s[i] for i in range(n)],
[
s[i] == Minimum(
p[j] for j in range(m) if orders[i][j]
) for i in range(n)
],

# computing ending times of stacks
[Maximum(p[j] for j in range(m) if orders[i][j]) == e[i] for i in range(n)],
[
e[i] == Maximum(
p[j] for j in range(m) if orders[i][j]
) for i in range(n)
],

# inferring when stacks are open
[(s[i], e[i], o[i][t]) in table2(t) for i in range(n) for t in range(m)],
Expand Down
10 changes: 6 additions & 4 deletions realistic/OpenStacks/OpenStacks_z.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@

minimize(
Maximum(
Sum(both(
y[i][j - 1] < quantities[i],
y[i][j] > 0
) for i in range(nCustomers)) for j in range(1, nProducts + 1)
Sum(
both(
y[i][j - 1] < quantities[i],
y[i][j] > 0
) for i in range(nCustomers)
) for j in range(1, nProducts + 1)
)
)
17 changes: 9 additions & 8 deletions realistic/PhysicianSchedule/PhysicianSchedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
OFF = 0

sf = lambda t: sum(flatten(t)) # summing after flattening the argument array
sums0, sumsub = [nPersons - sf(demands[1:, 1:, d, 1:]) for d in range(nDays)], [sum(subDemand[:, d]) for d in range(nDays)]
sums0 = [nPersons - sf(demands[1:, 1:, d, 1:]) for d in range(nDays)]
# below, expected number of occurrences
Osh = [[sums0[d]] + [sf(demands[1:, s, d, 1:]) + sumsub[d] * (-1 if s == 1 else 1 if s == 2 else 0) for s in range(1, nShifts)] for d in range(nDays)]
Osh = [[sums0[d]] + [sf(demands[1:, s, d, 1:]) + sum(subDemand[:, d]) * (-1 if s == 1 else 1 if s == 2 else 0) for s in range(1, nShifts)] for d in
range(nDays)]
Ost = [[sums0[d]] + [sf(demands[s, 1:, d, 1:]) for s in range(1, nStations)] for d in range(nDays)]
Osk = [[sums0[d]] + [sf(demands[1:, 1:, d, s]) for s in range(1, nSkills)] for d in range(nDays)]
assert all(len(t) == nShifts for t in Osh) and all(len(t) == nStations for t in Ost) and all(len(t) == nSkills for t in Osk)
Expand Down Expand Up @@ -96,35 +97,35 @@

# shifts 1 and 2: individual assignments on common stations
[
Sum(
demands[v1][v2][j][v3] == Sum(
conjunction(
st[i][j] == v1,
sh[i][j] == v2,
sk[i][j] == v3
) for i in range(nPersons)
) == demands[v1][v2][j][v3] for v1 in range(1, nStations) if commons[v1] == 1 for v2 in (1, 2) for j in range(nDays) for v3 in range(1, nSkills)
) for v1 in range(1, nStations) if commons[v1] == 1 for v2 in (1, 2) for j in range(nDays) for v3 in range(1, nSkills)
],

# shift 1 may be subsumed by shift 2 on non-common stations
[
Sum(
demands[v1][1][j][v3] == Sum(
conjunction(
st[i][j] == v1,
sh[i][j] in {1, 2},
sk[i][j] == v3
) for i in range(nPersons)
) == demands[v1][1][j][v3] for v1 in range(1, nStations) if commons[v1] == 0 for j in range(nDays) for v3 in range(1, nSkills)
) for v1 in range(1, nStations) if commons[v1] == 0 for j in range(nDays) for v3 in range(1, nSkills)
],

# shifts 3+: regular assignments
[
Sum(
demands[v1][v2][j][v3] == Sum(
conjunction(
st[i][j] == v1,
sh[i][j] == v2,
sk[i][j] == v3
) for i in range(nPersons)
) == demands[v1][v2][j][v3] for v1 in range(1, nStations) for v2 in range(3, nShifts) for j in range(nDays) for v3 in range(1, nSkills)
) for v1 in range(1, nStations) for v2 in range(3, nShifts) for j in range(nDays) for v3 in range(1, nSkills)
],

# tag(redundant-constraints)
Expand Down

0 comments on commit 9387156

Please sign in to comment.