Skip to content

Commit

Permalink
Minor refactoring of recreational
Browse files Browse the repository at this point in the history
  • Loading branch information
lecoutre committed Jul 22, 2024
1 parent 6830653 commit c5edb4a
Show file tree
Hide file tree
Showing 43 changed files with 345 additions and 96,141 deletions.
2 changes: 1 addition & 1 deletion recreational/Lightup/Lightup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Execution:
python Lightup.py -data=<datafile.json>
python Lightup.py -data=<datafile.txt> -dataparser=Lightup_Parser.py
python Lightup.py -data=<datafile.txt> -parser=Lightup_Parser.py
## Links
- https://en.wikipedia.org/wiki/Light_Up_(puzzle)
Expand Down
1 change: 1 addition & 0 deletions recreational/MisteryShopper/MisteryShopper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
## Execution:
python MisteryShopper.py -data=<datafile.json>
python MisteryShopper.py -parser=MisteryShopper_Random.py <number> <number> <number> <number> <number>
## Links
- https://www.csplib.org/Problems/prob004
Expand Down
2 changes: 1 addition & 1 deletion recreational/PentominoesZayenz/PentominoesZayenz.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Execution
python PentominoesZayenz.py -data=<datafile.json>
python PentominoesZayenz.py -data=<datafile.dzn> -dataparser=PentominoesZayenz_ParserZ.py
python PentominoesZayenz.py -data=<datafile.dzn> -parser=PentominoesZayenz_ParserZ.py
## Links
- https://www.researchgate.net/publication/228523019_Modeling_irregular_shape_placement_problems_with_regular_constraints
Expand Down
2 changes: 1 addition & 1 deletion recreational/PseudoBoolean/PseudoBoolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
constraints: Sum
## Execution
python PseudoBoolean.py -data=<datafile.opb> -dataparser=PseudoBoolean_Parser.py
python PseudoBoolean.py -data=<datafile.opb> -parser=PseudoBoolean_Parser.py
## Links
- http://www.cril.univ-artois.fr/PB16/
Expand Down
2 changes: 1 addition & 1 deletion recreational/QuadraticAssignment/QuadraticAssignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Execution
python QuadraticAssignment.py -data=<datafile.json>
python QuadraticAssignment.py -data=<datafile.txt> -dataparser=QuadraticAssignment_Parser.py
python QuadraticAssignment.py -data=<datafile.txt> -parser=QuadraticAssignment_Parser.py
## Links
- https://en.wikipedia.org/wiki/Quadratic_assignment_problem
Expand Down
13 changes: 6 additions & 7 deletions recreational/RoomMate/RoomMate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
constraints: Element, Table
## Execution
- python RoomMate.py -data=<datafile.json>
- python RoomMate.py -data=<datafile.json> -variant=table
- python RoomMate.py -data=<datafile.txt> -parser=RoomMate_Parser.py
python RoomMate.py -data=<datafile.json>
python RoomMate.py -data=<datafile.json> -variant=table
python RoomMate.py -data=<datafile.txt> -parser=RoomMate_Parser.py
## Links
- https://en.wikipedia.org/wiki/Stable_roommates_problem
Expand All @@ -28,6 +28,8 @@
from pycsp3 import *
from pycsp3.dashboard import options

options.dontbuildsimilarconstraints = True

preferences = data
n = len(preferences)

Expand All @@ -51,7 +53,7 @@ def pref_rank():
x = VarArray(size=n, dom=lambda i: range(len(preferences[i])))

if not variant():
options.dontbuildsimilarconstraints = True

satisfy(
(
If(x[i] > rank[i][k], Then=x[k] < rank[k][i]),
Expand All @@ -60,8 +62,6 @@ def pref_rank():
)

elif variant('table'):
options.dontbuildsimilarconstraints = True


def T(i, k):
return [(a, ANY) for a in x[i].dom if a < rank[i][k]] + [(rank[i][k], rank[k][i])] + \
Expand All @@ -73,7 +73,6 @@ def T(i, k):
)

elif variant('hybrid'):
options.dontbuildsimilarconstraints = True

satisfy(
Table(
Expand Down
48 changes: 6 additions & 42 deletions recreational/RoomMate/sr0006.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,10 @@
{
"preferences": [
[
3,
4,
2,
6,
5
],
[
6,
5,
4,
1,
3
],
[
2,
4,
5,
1,
6
],
[
5,
2,
3,
6,
1
],
[
3,
1,
2,
4,
6
],
[
5,
1,
3,
4,
2
]
[3, 4, 2, 6, 5],
[6, 5, 4, 1, 3],
[2, 4, 5, 1, 6],
[5, 2, 3, 6, 1],
[3, 1, 2, 4, 6],
[5, 1, 3, 4, 2]
]
}
15 changes: 10 additions & 5 deletions recreational/RotationPuzzle/RotationPuzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@
from pycsp3 import *

# preset data (series of 4 instances, followed by 4 artificial instances)
series = [([7, 11, 6, 14, 0, 13, 15, 12, 2, 9, 10, 8, 4, 1, 3, 5], 11), ([1, 6, 3, 7, 0, 2, 10, 11, 4, 5, 8, 9, 12, 13, 14, 15], 4),
([7, 11, 6, 14, 0, 3, 1, 9, 2, 5, 10, 13, 4, 8, 12, 15], 8), ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 14], 55),
([5, 3, 1, 4, 0, 13, 15, 12, 2, 9, 10, 8, 14, 6, 11, 7], 21), ([3, 6, 7, 1, 0, 2, 10, 11, 4, 5, 8, 9, 12, 13, 14, 15], 17),
([3, 11, 6, 14, 0, 7, 1, 9, 2, 5, 10, 13, 4, 8, 12, 15], 20), ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 11, 12, 13, 14, 10], 18),
]
series = [
([7, 11, 6, 14, 0, 13, 15, 12, 2, 9, 10, 8, 4, 1, 3, 5], 11),
([1, 6, 3, 7, 0, 2, 10, 11, 4, 5, 8, 9, 12, 13, 14, 15], 4),
([7, 11, 6, 14, 0, 3, 1, 9, 2, 5, 10, 13, 4, 8, 12, 15], 8),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 14], 55),
([5, 3, 1, 4, 0, 13, 15, 12, 2, 9, 10, 8, 14, 6, 11, 7], 21),
([3, 6, 7, 1, 0, 2, 10, 11, 4, 5, 8, 9, 12, 13, 14, 15], 17),
([3, 11, 6, 14, 0, 7, 1, 9, 2, 5, 10, 13, 4, 8, 12, 15], 20),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 11, 12, 13, 14, 10], 18),
]
rotations = [[0, 1, 2, 6, 10, 9, 8, 4], [1, 2, 3, 7, 11, 10, 9, 5], [4, 5, 6, 10, 14, 13, 12, 8], [5, 6, 7, 11, 15, 14, 13, 9]]
nCells, nRotations = 16, len(rotations)

Expand Down
8 changes: 4 additions & 4 deletions recreational/SetCovering/SetCovering.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pycsp3 import *

subsets = data
vals = sorted({v for subset in subsets for v in subset})
V = sorted({v for subset in subsets for v in subset})
m = len(subsets)

# x[i] is 1 iff the ith subset is selected
Expand All @@ -32,15 +32,15 @@
Count(
within=scp,
value=1
) >= 1 for scp in [[x[i] for i, subset in enumerate(subsets) if v in subset] for v in vals]
) >= 1 for scp in [[x[i] for i, subset in enumerate(subsets) if v in subset] for v in V]
)

minimize(
# minimizing the number of selected subsets
Sum(x)
)

"""
1) we avoid using values instead of vals as name for the list of bid values
""" Comments
1) We avoid using values instead of V (or vals) as name for the list of bid values
as it may enter in conflict with the function values() in a notebook
"""
16 changes: 8 additions & 8 deletions recreational/SetCovering/example.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"subsets" : [
[1,2],
[2,8],
[5,6],
[6,7,10,11],
[9,10],
[1,2,3,5,9,10,11],
[3,4,7,8,11,12]
"subsets": [
[1, 2],
[2, 8],
[5, 6],
[6, 7, 10, 11],
[9, 10],
[1, 2, 3, 5, 9, 10, 11],
[3, 4, 7, 8, 11, 12]
]
}
8 changes: 4 additions & 4 deletions recreational/SetPacking/SetPacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@
from pycsp3 import *

subsets = data
vals = sorted({v for subset in subsets for v in subset})
V = sorted({v for subset in subsets for v in subset})
m = len(subsets)

# x[i] is 1 iff the ith subset is selected
x = VarArray(size=m, dom={0, 1})

satisfy(
# avoiding intersection of subsets
Count(scp, value=1) <= 1 for scp in [[x[i] for i, subset in enumerate(subsets) if v in subset] for v in vals]
Count(scp, value=1) <= 1 for scp in [[x[i] for i, subset in enumerate(subsets) if v in subset] for v in V]
)

maximize(
# maximizing the number of selected subsets
Sum(x)
)

"""
1) we avoid using values instead of vals as name for the list of bid values
""" Comments
1) We avoid using values instead of V (or vals) as name for the list of bid values
as it may enter in conflict with the function values() in a notebook
"""
16 changes: 8 additions & 8 deletions recreational/SetPacking/example.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"subsets" : [
[1,2],
[2,8],
[5,6],
[6,7,10,11],
[9,10],
[1,2,3,5,9,10,11],
[3,4,7,8,11,12]
"subsets": [
[1, 2],
[2, 8],
[5, 6],
[6, 7, 10, 11],
[9, 10],
[1, 2, 3, 5, 9, 10, 11],
[3, 4, 7, 8, 11, 12]
]
}
44 changes: 22 additions & 22 deletions recreational/Shikaku/Shikaku.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
constraints: Table
## Execution:
python3 Shikaku.py -data=<datafile.json>
python Shikaku.py -data=<datafile.json>
## Links
- https://en.wikipedia.org/wiki/Shikaku
Expand All @@ -26,48 +26,48 @@
def no_overlapping(i, j):
leftmost = i if rooms[i].col <= rooms[j].col else j
rightmost = j if leftmost == i else i
p = r[leftmost] <= l[rightmost]
p = rgt[leftmost] <= lft[rightmost]
if rooms[leftmost].row == rooms[rightmost].row:
return p
if rooms[leftmost].row > rooms[rightmost].row:
return p | (t[leftmost] >= b[rightmost])
return p | (b[leftmost] <= t[rightmost])
return p | (top[leftmost] >= bot[rightmost])
return p | (bot[leftmost] <= top[rightmost])


# l[i] is the position of the left border of the ith room
l = VarArray(size=nRooms, dom=range(nCols + 1))
# lft[i] is the position of the left border of the ith room
lft = VarArray(size=nRooms, dom=range(nCols + 1))

# r[i] is the position of the right border of the ith room
r = VarArray(size=nRooms, dom=range(nCols + 1))
# rgt[i] is the position of the right border of the ith room
rgt = VarArray(size=nRooms, dom=range(nCols + 1))

# t[i] is the position of the top border of the ith room
t = VarArray(size=nRooms, dom=range(nRows + 1))
# top[i] is the position of the top border of the ith room
top = VarArray(size=nRooms, dom=range(nRows + 1))

# b[i] is the position of the bottom border of the ith room
b = VarArray(size=nRooms, dom=range(nRows + 1))
# bot[i] is the position of the bottom border of the ith room
bot = VarArray(size=nRooms, dom=range(nRows + 1))

satisfy(
# each room must be surrounded by its borders
[
(
l[i] <= col,
r[i] > col,
t[i] <= row,
b[i] > row
lft[i] <= col,
rgt[i] > col,
top[i] <= row,
bot[i] > row
) for i, (row, col, _) in enumerate(rooms)
],

# respecting the surface of each room
[(r[i] - l[i]) * (b[i] - t[i]) == v for i, (_, _, v) in enumerate(rooms)],
[(rgt[i] - lft[i]) * (bot[i] - top[i]) == v for i, (_, _, v) in enumerate(rooms)],

# rooms must not overlap
[no_overlapping(i, j) for i, j in combinations(nRooms, 2)]
)

""" Comments
1) it is also possible to write (but this is less compact):
[l[i] <= rooms[i].col for i in range(nRooms)],
[r[i] > rooms[i].col for i in range(nRooms)],
[t[i] <= rooms[i].row for i in range(nRooms)],
[b[i] > rooms[i].row for i in range(nRooms)],
1) It is also possible to write (but this is less compact):
[lft[i] <= rooms[i].col for i in range(nRooms)],
[rgt[i] > rooms[i].col for i in range(nRooms)],
[top[i] <= rooms[i].row for i in range(nRooms)],
[bot[i] > rooms[i].row for i in range(nRooms)],
"""
Loading

0 comments on commit c5edb4a

Please # to comment.