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 Oct 9, 2024
1 parent aea6a16 commit 6cebadf
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 45 deletions.
2 changes: 1 addition & 1 deletion realistic/SAT/SAT.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ def check(clause1, clause2, a, b, links):
x = VarArray(size=e, dom=lambda i: range(1, 2 ** len(clauses[i])))

satisfy(
(x[i], x[j]) in T for i, j in combinations(e, 2) if (T := dual_table(i, j),) is not None
(x[i], x[j]) in T for i, j in combinations(e, 2) if (T := dual_table(i, j)) is not None
)
9 changes: 2 additions & 7 deletions realistic/SdnChain/SdnChain.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@
def dp(i, j):
l1 = [sl[k] for k, (a, b) in G if b[DOMAIN] == i and a[DOMAIN] == j]
l2 = [sl[k] & pth[a[DOMAIN]][j] for k, (a, b) in G if b[DOMAIN] == i and a[DOMAIN] != j]
if len(l1) == 0 and len(l2) == 0:
return 0
if len(l1) == 0:
return Count(l2) >= 1
if len(l2) == 0:
return Count(l1) >= 1
assert len(l1) > 0 and len(l2) > 0
return (Count(l1) >= 1) | (Count(l2) >= 1)


Expand Down Expand Up @@ -193,7 +188,7 @@ def dp(i, j):
If(
sd[a[DOMAIN]] == 0,
Then=sl[i] == 0
) for i, (a, b) in L
) for i, (a, _) in L
],
)

Expand Down
13 changes: 11 additions & 2 deletions realistic/SdnChain/d10n780-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@
"start_domain": 9,
"target_domain": 4,
"vnflist": [9, 2, 0, 4, 9],
"vnf_arcs": [[0, 1], [1, 2], [1, 3], [3, 4]],
"vnf_arcs": [
[0, 1],
[1, 2],
[1, 3],
[3, 4]
],
"proximity_to_source": [0, 0, 0, 0, 0],
"proximity_to_destination": [0, 0, 0, 0, 0],
"domain_constraints": [[8, 0, 0, 0], [8, 2, 0, 0], [0, 2, 0, 0]]
"domain_constraints": [
[8, 0, 0, 0],
[8, 2, 0, 0],
[0, 2, 0, 0]
]
}
2 changes: 1 addition & 1 deletion realistic/SeatMoving/SeatMoving.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
nSeats = len(seats)
horizon = (2 * nSeats) // (nSeats - nPersons + 1) + 1 # maximum number of steps

# x[t][i] is the person at the ith seat at time t i (-1 means 'empty seat')
# x[t][i] is the person at the ith seat at time t (-1 means 'empty seat')
x = VarArray(size=[horizon, nSeats], dom=range(-1, nPersons))

# y[t][j] is the seat for the jth person at time t
Expand Down
2 changes: 1 addition & 1 deletion realistic/ShipScheduling/ShipScheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
Sum(s[i] * d[i] * tonnesPerCmDraft[i] for i in range(nShips))
)

"""
""" Comments
1) Note that:
If(len(outShips) > 0,
Then=[Sum(Sum(tb[i][t]) + tbe[i] * (t == x[i]) for i in outShips) <= nTugs for t in range(horizon)]
Expand Down
15 changes: 8 additions & 7 deletions realistic/SkillAllocation/SkillAllocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

nTrainings, nMaxJobs, engineerSkills, engineerLocations, jobs = data
nEngineers, nSkills, nJobs, nOverseasCap = len(engineerSkills), len(engineerSkills[0]), len(jobs), 5
E, T, J = range(nEngineers), range(nTrainings), range(nJobs)

qualifiedEngineers = [[e for e in range(nEngineers) if engineerSkills[e][job[0]] == 1] for job in jobs]
qualifiedEngineers = [[e for e in E if engineerSkills[e][job[0]] == 1] for job in jobs]

# x[i] is the engineer assigned to the ith job
x = VarArray(size=nJobs, dom=range(nEngineers))
Expand All @@ -34,13 +35,13 @@

satisfy(
# computing new skills
[y[e][t] in {-1}.union(s for s in range(nSkills) if engineerSkills[e][s] == 0) for e in range(nEngineers) for t in range(nTrainings)],
[y[e][t] in {-1}.union(s for s in range(nSkills) if engineerSkills[e][s] == 0) for e in E for t in T],

# not exceeding the maximum number of jobs per engineer
[Sum(x[i] == e for i in range(nJobs)) <= nMaxJobs for e in range(nEngineers)],
[Sum(x[i] == e for i in J) <= nMaxJobs for e in E],

# not exceeding the maximum number of oversea jobs per engineer
[Sum(x[i] == e for i in range(nJobs) if jobs[i][4] == 1) <= nOverseasCap for e in range(nEngineers)],
[Sum(x[i] == e for i in J if jobs[i][4] == 1) <= nOverseasCap for e in E],

# ensuring that a qualified engineer is assigned to each job
[
Expand All @@ -50,13 +51,13 @@
both(
y[e][t] == jobs[i][0],
x[i] == e
) for e in range(nEngineers) for t in range(nTrainings)
) for e in E for t in T
)
) for i in range(nJobs)
) for i in J
]
)

minimize(
# minimizing the number of new skills
Sum(y[e][t] >= 0 for e in range(nEngineers) for t in range(nTrainings))
Sum(y[e][t] >= 0 for e in E for t in T)
)
17 changes: 11 additions & 6 deletions realistic/Smelt/Smelt.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
def production_rule(i):
assert rt[i] in range(4)
if rt[i] == 0:
return If(f[i], Then=re[rk1[i]] + rd[i] <= rs[rk2[i]])
return re[rk1[i]] + rd[i] <= rs[rk2[i]]
if rt[i] == 1:
return If(f[i], Then=re[rk1[i]] + rd[i] >= rs[rk2[i]])
return re[rk1[i]] + rd[i] >= rs[rk2[i]]
if rt[i] == 2:
return If(f[i], Then=rs[rk1[i]] - rd[i] <= rs[rk2[i]])
return rs[rk1[i]] - rd[i] <= rs[rk2[i]]
if rt[i] == 3:
return If(f[i], Then=re[rk1[i]] - rd[i] <= re[rk2[i]])
return re[rk1[i]] - rd[i] <= re[rk2[i]]


satisfy(
Expand Down Expand Up @@ -97,7 +97,12 @@ def production_rule(i):
],

# managing production rules
[production_rule(i) for i in range(nRules)],
[
If(
f[i],
Then=production_rule(i)
) for i in range(nRules)
],

# computing the make-span
[re[i] <= m for i in range(nRecipes)]
Expand All @@ -107,7 +112,7 @@ def production_rule(i):
Sum(f[i] == 0 for i in range(nRules)) * H + m
)

"""
""" Comments
1) It seems that it would be simpler to change 0 and 1 for f
"""

Expand Down
20 changes: 10 additions & 10 deletions realistic/Soccer/Soccer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@

from pycsp3 import *

print(data)
games, iPoints, positions = data
nGames, nTeams, nPositions = len(games), len(iPoints), len(positions)

pt = [0, 1, 3]
P = [0, 1, 3]

lb_score = min(iPoints[i] + sum(min(pt) for j in range(nGames) if i in games[j]) for i in range(nTeams))
ub_score = max(iPoints[i] + sum(max(pt) for j in range(nGames) if i in games[j]) for i in range(nTeams))
lb_score = min(iPoints[i] + sum(min(P) for j in range(nGames) if i in games[j]) for i in range(nTeams))
ub_score = max(iPoints[i] + sum(max(P) for j in range(nGames) if i in games[j]) for i in range(nTeams))

# points[j][0] and points[j][1] are the points for the two teams (indexes 0 and 1) of the jth game
points = VarArray(size=[nGames, 2], dom=pt)
points = VarArray(size=[nGames, 2], dom=P)

# score[i] is the final score of the ith team
score = VarArray(size=nTeams, dom=range(lb_score, ub_score + 1))
Expand All @@ -51,15 +52,14 @@
satisfy(
# assigning rights points for each game
[
(points[j][0], points[j][1]) in {
(0, 3),
(1, 1),
(3, 0)
} for j in range(nGames)
Table(
scope=(points[j][0], points[j][1]),
supports={(0, 3), (1, 1), (3, 0)}
) for j in range(nGames)
],

# computing final points
[score[i] - Sum(points[j][0 if i == games[j][0] else 1] for j in range(nGames) if i in games[j]) == iPoints[i] for i in range(nTeams)],
[score[i] - Sum(points[j][0 if i == game[0] else 1] for j, game in enumerate(games) if i in game) == iPoints[i] for i in range(nTeams)],

# computing worst positions (the number of teams with greater total points)
[wp[i] == Sum(score[j] >= score[i] for j in range(nTeams)) for i in range(nTeams)],
Expand Down
5 changes: 2 additions & 3 deletions realistic/StableGoods/StableGoods.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
satisfy(
# computing the distribution of goods
[
Sum(
quantities[j] == Sum(
(tg[i] == j) * ng[i] for i in range(nPersons)
) + z[j] == quantities[j] for j in range(nGoods)
) + z[j] for j in range(nGoods)
]
)

Expand Down Expand Up @@ -105,6 +105,5 @@ def T(p1, p2):

""" Comments
1) z * values is a shortcut for Sum(z[g] * values[g] for g in range(nGoods))
2) the table variant is more efficient
"""
5 changes: 4 additions & 1 deletion realistic/SteinerTree/SteinerTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@
prt[i] == NO,
dst[i] == 0
],
Else=If(i != root, Then=prt[i] != NO)
Else=If(
i != root,
Then=prt[i] != NO
)
) for i in range(n)
],

Expand Down
9 changes: 3 additions & 6 deletions realistic/StochasticVRP/StochasticVRP.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
satisfy(
# pre-assigning a few variables
[
[prv[k][i] == i + nVehicles - 1 for k in range(nSetups) for i in range(nCustomers + 1, nCustomers + nVehicles)],
[prv[k][nCustomers] == nCustomers + 2 * nVehicles - 1 for k in range(nSetups)],

[nxt[k][i] == i - nVehicles + 1 for k in range(nSetups) for i in range(nCustomers + nVehicles, nCustomers + 2 * nVehicles - 1)],
[nxt[k][-1] == nCustomers for k in range(nSetups)],
[prv[k][i] == v for k in range(nSetups) for i in StartDepots if (v := i + nVehicles - 1 + (nVehicles if i == nCustomers else 0),)],
[nxt[k][i] == v for k in range(nSetups) for i in EndDepots if (v := i - nVehicles + 1 - (nVehicles if i == nNodes - 1 else 0),)],

[veh[i] == i - nCustomers for i in StartDepots],
[veh[i] == i - nCustomers - nVehicles for i in EndDepots],
Expand Down Expand Up @@ -90,6 +87,6 @@
Sum(weights[i] * Maximum(arr[i]) for i in range(nSetups))
)

"""
""" Comments
1) note that data have been transferred in JSON
"""

0 comments on commit 6cebadf

Please sign in to comment.