Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual info on splits #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions examples/split_problems.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
To make computation more tractable, a tiling problem can be split into two or more sub-problems. Each solution to the original problem is also the solution to exactly one of the sub-problems.

```
>>> from polyomino.board import Chessboard
>>> from polyomino.constant import TETROMINOS, ALL_PENTOMINOS

```

First a problem which involves no repeated tiles. It can be solved without splitting, but let us look at how splitting works:
```
>>> problem = Chessboard().tile_with(ALL_PENTOMINOS + [TETROMINOS['Square']])
>>> subproblems = problem.split(at_least=2)
>>> len(subproblems)
3
>>> example = subproblems[0]
>>> example
<polyomino.problem.TilingSubProblem object at ...>
>>> print(example.display())
+-+-+-+-+-+-+-+-+
| | | |
+ +-+-+-+-+-+ +
| | | |
+-+ +-+ +-+-+-+-+
| | | | | |
+ +-+-+-+ +-+-+ +
| | | |
+-+-+-+-+-+-+ + +
| | | | |
+-+ +-+-+ +-+-+-+
| | | | | | |
+ +-+ + +-+ +-+ +
| | | | | |
+ +-+ +-+ +-+ +
| | | | |
+-+-+-+-+-+-+-+-+
>>> sub_solution = example.solve()
>>> sub_solution
<polyomino.solution.Solution object at ...>
>>> example.is_solution(sub_solution.array)
True
>>> sub_solution.tiling
[[(0, 0), (1, 1), (1, 0), (2, 0)], [(0, 3), (1, 2), (0, 2), (0, 1)], [(3, 3), (2, 2), (2, 3), (1, 3)], [(3, 0), (2, 1), (3, 1), (3, 2)]]
>>> sub_solution.display()
+-+-+-+-+-+-+-+-+
| | | |
+ +-+-+-+-+-+ +
| | | |
+-+ +-+ +-+-+-+-+
| | | | | |
+ +-+-+-+ +-+-+ +
| | | |
+-+-+-+-+-+-+ + +
| | | | |
+-+ +-+-+ +-+-+-+
| | | | | | |
+ +-+ + +-+ +-+ +
| | | | | |
+ +-+ +-+ +-+ +
| | | | |
+-+-+-+-+-+-+-+-+
>>> solution = example.original_solution(sub_solution)
>>> solution
<polyomino.solution.Solution object at ...>
>>> problem.is_solution(solution.array)
True
>>> solution.tiling
[[(0, 0), (1, 1), (1, 0), (2, 0)], [(0, 3), (1, 2), (0, 2), (0, 1)], [(3, 3), (2, 2), (2, 3), (1, 3)], [(3, 0), (2, 1), (3, 1), (3, 2)]]
>>> solution.display()
+-+-+-+-+-+-+-+-+
| | | |
+ +-+-+-+-+-+ +
| | | |
+-+ +-+ +-+-+-+-+
| | | | | |
+ +-+-+-+ +-+-+ +
| | | |
+-+-+-+-+-+-+ + +
| | | | |
+-+ +-+-+ +-+-+-+
| | | | | | |
+ +-+ + +-+ +-+ +
| | | | | |
+ +-+ +-+ +-+ +
| | | | |
+-+-+-+-+-+-+-+-+
```
9 changes: 9 additions & 0 deletions polyomino/problem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np

from exact_cover import get_exact_cover
from exact_cover.helpers import split_problem

from .error import CantPlaceSinglePiece
from .solution import Solution
Expand Down Expand Up @@ -76,6 +77,14 @@ def output_array(self, filename):
def set_name(self, name):
self._name = name

def split(self, at_least=1):
arrays = split_problem(self.array, at_least)
for array in arrays:
problem = TilingProblem(self.board, self.tileset)
problem.array = array
problem.key = self.key
yield problem

@property
def size(self):
self.make_problem()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"

[tool.poetry.dependencies]
python = "^3.7.1"
exact-cover = "0.4.3"
exact-cover = "0.8.0"
numpy = "^1.20"
pretty-poly = "0.2.0"

Expand Down