Skip to content
Merged
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
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ ignore_errors = True

[mypy-tilings.strategies.row_and_col_separation]
ignore_errors = False

[mypy-tilings.strategies.requirement_placement]
ignore_errors = False
264 changes: 21 additions & 243 deletions tests/algorithms/test_requirement_placements.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from itertools import chain

import pytest

from permuta.misc import DIR_EAST, DIR_NORTH, DIR_SOUTH, DIR_WEST
from tilings import GriddedPerm, Tiling
from tilings.algorithms import RequirementPlacement
from tilings.map import RowColMap

# ------------------------------------------------
# Fixture and utility
Expand Down Expand Up @@ -155,122 +154,27 @@ def test_empty_col(placement1):
assert placement1.empty_col(1) == t


def test_point_translation(gp1, placement1, placement1owncol, placement1ownrow):
assert placement1._point_translation(gp1, 2, (0, 3)) == (3, 1)
assert placement1._point_translation(gp1, 2, (1, 2)) == (3, 3)
assert placement1._point_translation(gp1, 2, (2, 2)) == (3, 3)
assert placement1._point_translation(gp1, 2, (3, 0)) == (1, 3)
assert placement1._point_translation(gp1, 2, (4, 4)) == (1, 1)

assert placement1owncol._point_translation(gp1, 2, (0, 3)) == (3, 1)
assert placement1owncol._point_translation(gp1, 2, (1, 2)) == (3, 1)
assert placement1owncol._point_translation(gp1, 2, (2, 2)) == (3, 1)
assert placement1owncol._point_translation(gp1, 2, (3, 0)) == (1, 1)
assert placement1owncol._point_translation(gp1, 2, (4, 4)) == (1, 1)

assert placement1ownrow._point_translation(gp1, 2, (0, 3)) == (1, 1)
assert placement1ownrow._point_translation(gp1, 2, (1, 2)) == (1, 3)
assert placement1ownrow._point_translation(gp1, 2, (2, 2)) == (1, 3)
assert placement1ownrow._point_translation(gp1, 2, (3, 0)) == (1, 3)
assert placement1ownrow._point_translation(gp1, 2, (4, 4)) == (1, 1)
def test_multiplex_map(placement1, placement1owncol, placement1ownrow):
width = 2
height = 3
cell = (1, 1)

row_map = {0: 0, 1: 1, 2: 1, 3: 1, 4: 2}
col_map = {0: 0, 1: 1, 2: 1, 3: 1}
row_col_map = RowColMap(row_map, col_map)
print(row_col_map)
print(placement1.multiplex_map(width, height, cell))
assert placement1.multiplex_map(width, height, cell) == row_col_map

def test_gridded_perm_translation(gp1, placement1, placement1owncol, placement1ownrow):
assert placement1._gridded_perm_translation(gp1, (0, 3)) == GriddedPerm(
(3, 1, 2, 0, 4), ((2, 3), (2, 0), (3, 1), (3, 0), (3, 3))
)
assert placement1._gridded_perm_translation(gp1, (1, 1)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (2, 2), (3, 3), (3, 0), (3, 3))
)
assert placement1._gridded_perm_translation(gp1, (2, 2)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 0), (3, 3), (3, 0), (3, 3))
)
assert placement1._gridded_perm_translation(gp1, (3, 0)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 2), (1, 3), (3, 2), (3, 3))
)
assert placement1._gridded_perm_translation(gp1, (4, 4)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (3, 3))
)
assert placement1owncol._gridded_perm_translation(gp1, (0, 3)) == GriddedPerm(
(3, 1, 2, 0, 4), ((2, 1), (2, 0), (3, 1), (3, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation(gp1, (1, 1)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (2, 0), (3, 1), (3, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation(gp1, (2, 2)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (3, 1), (3, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation(gp1, (3, 0)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (3, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation(gp1, (4, 4)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (3, 1))
)
assert placement1ownrow._gridded_perm_translation(gp1, (0, 3)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 0), (1, 1), (1, 0), (1, 3))
)
assert placement1ownrow._gridded_perm_translation(gp1, (1, 1)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 2), (1, 3), (1, 0), (1, 3))
)
assert placement1ownrow._gridded_perm_translation(gp1, (2, 2)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 0), (1, 3), (1, 0), (1, 3))
)
assert placement1ownrow._gridded_perm_translation(gp1, (3, 0)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 2), (1, 3), (1, 2), (1, 3))
)
assert placement1ownrow._gridded_perm_translation(gp1, (4, 4)) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (1, 3))
)

row_map = {0: 0, 1: 1, 2: 2}
col_map = {0: 0, 1: 1, 2: 1, 3: 1}
row_col_map = RowColMap(row_map, col_map)
assert placement1owncol.multiplex_map(width, height, cell) == row_col_map

def test_gridded_perm_translation_with_point(
gp1, placement1, placement1owncol, placement1ownrow
):
assert placement1._gridded_perm_translation_with_point(gp1, 0) == GriddedPerm(
(3, 1, 2, 0, 4), ((1, 2), (2, 0), (3, 1), (3, 0), (3, 3))
)
assert placement1._gridded_perm_translation_with_point(gp1, 1) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (1, 1), (3, 3), (3, 0), (3, 3))
)
assert placement1._gridded_perm_translation_with_point(gp1, 2) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 0), (2, 2), (3, 0), (3, 3))
)
assert placement1._gridded_perm_translation_with_point(gp1, 3) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 2), (1, 3), (2, 1), (3, 3))
)
assert placement1._gridded_perm_translation_with_point(gp1, 4) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (2, 2))
)
assert placement1ownrow._gridded_perm_translation_with_point(gp1, 0) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 2), (0, 0), (1, 1), (1, 0), (1, 3))
)
assert placement1ownrow._gridded_perm_translation_with_point(gp1, 1) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 1), (1, 3), (1, 0), (1, 3))
)
assert placement1ownrow._gridded_perm_translation_with_point(gp1, 2) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 0), (1, 2), (1, 0), (1, 3))
)
assert placement1ownrow._gridded_perm_translation_with_point(gp1, 3) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 3), (0, 2), (1, 3), (1, 1), (1, 3))
)
assert placement1ownrow._gridded_perm_translation_with_point(gp1, 4) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (1, 2))
)
assert placement1owncol._gridded_perm_translation_with_point(gp1, 0) == GriddedPerm(
(3, 1, 2, 0, 4), ((1, 1), (2, 0), (3, 1), (3, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation_with_point(gp1, 1) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (1, 0), (3, 1), (3, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation_with_point(gp1, 2) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (2, 1), (3, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation_with_point(gp1, 3) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (2, 0), (3, 1))
)
assert placement1owncol._gridded_perm_translation_with_point(gp1, 4) == GriddedPerm(
(3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (2, 1))
)
row_map = {0: 0, 1: 1, 2: 1, 3: 1, 4: 2}
col_map = {0: 0, 1: 1}
row_col_map = RowColMap(row_map, col_map)
assert placement1ownrow.multiplex_map(width, height, cell) == row_col_map


def test_placed_cell(placement1, placement1owncol, placement1ownrow):
Expand Down Expand Up @@ -307,131 +211,6 @@ def test_point_requirements(placement1, placement1owncol, placement1ownrow):
]


def test_stretch_gridded_perm(gp1, placement1, placement1owncol, placement1ownrow):
assert set(placement1._stretch_gridded_perm(gp1, (0, 0))) == set(
[
GriddedPerm((3, 1, 2, 0, 4), ((2, 3), (2, 2), (3, 3), (3, 2), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((2, 3), (2, 2), (3, 3), (3, 0), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((2, 3), (2, 0), (3, 3), (3, 0), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (2, 2), (3, 3), (3, 2), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (2, 2), (3, 3), (3, 0), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (2, 0), (3, 3), (3, 0), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (0, 2), (3, 3), (3, 2), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (0, 2), (3, 3), (3, 0), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (0, 0), (3, 3), (3, 0), (3, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (1, 1), (3, 3), (3, 0), (3, 3))),
]
)
assert set(placement1owncol._stretch_gridded_perm(gp1, (1, 0))) == set(
[
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (3, 1), (3, 0), (3, 1))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (3, 0), (3, 1))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (3, 1))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (1, 1))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (2, 0), (3, 1))),
]
)
assert set(placement1ownrow._stretch_gridded_perm(gp1, (1, 1))) == set(
[
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (0, 0), (1, 3), (1, 0), (1, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (0, 0), (1, 1), (1, 0), (1, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (1, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (1, 1))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 3), (0, 0), (1, 2), (1, 0), (1, 3))),
GriddedPerm((3, 1, 2, 0, 4), ((0, 1), (0, 0), (1, 1), (1, 0), (1, 2))),
]
)


def test_stretch_gridded_perms(placement1, placement1owncol, placement1ownrow):
gps = [
GriddedPerm((0, 1), [(0, 0), (1, 1)]),
GriddedPerm((0, 1), [(1, 1), (2, 2)]),
]
for p in (placement1, placement1ownrow, placement1owncol):
assert set(p._stretch_gridded_perms(gps, (1, 1))) == set(
chain.from_iterable(p._stretch_gridded_perm(gp, (1, 1)) for gp in gps)
)


def test_stretched_obstructions(placement1, placement1owncol, placement1ownrow):
orig_obs = placement1._tiling.obstructions
assert sorted(placement1.stretched_obstructions((1, 1))) == sorted(
placement1._stretch_gridded_perms(orig_obs, (1, 1))
)
assert sorted(placement1owncol.stretched_obstructions((1, 1))) == sorted(
placement1owncol._stretch_gridded_perms(orig_obs, (1, 1))
)
assert sorted(placement1ownrow.stretched_obstructions((1, 1))) == sorted(
placement1ownrow._stretch_gridded_perms(orig_obs, (1, 1))
)


def test_stretched_requirements(placement1, placement1owncol, placement1ownrow):
orig_reqs = placement1._tiling.requirements
assert sorted(placement1.stretched_requirements((1, 1))) == sorted(
placement1._stretch_gridded_perms(orig_reqs, (1, 1))
)
orig_reqs = placement1owncol._tiling.requirements
assert sorted(placement1owncol.stretched_requirements((1, 1))) == sorted(
placement1owncol._stretch_gridded_perms(orig_reqs, (1, 1))
)
orig_reqs = placement1ownrow._tiling.requirements
assert sorted(placement1ownrow.stretched_requirements((1, 1))) == sorted(
placement1ownrow._stretch_gridded_perms(orig_reqs, (1, 1))
)


def test_stretched_obstructions_and_parameters(
placement1, placement1owncol, placement1ownrow
):
obs, reqs, _ = placement1._stretched_obstructions_requirements_and_parameters(
(1, 1)
)
assert set(obs) == set(
placement1.stretched_obstructions((1, 1))
+ [
GriddedPerm.single_cell((0, 1), (2, 2)),
GriddedPerm.single_cell((1, 0), (2, 2)),
]
)
assert sorted(reqs) == sorted(
placement1.stretched_requirements((1, 1)) + [[GriddedPerm((0,), ((2, 2),))]]
)
(
obs,
reqs,
_,
) = placement1ownrow._stretched_obstructions_requirements_and_parameters((1, 1))
assert set(obs) == set(
placement1ownrow.stretched_obstructions((1, 1))
+ [
GriddedPerm.single_cell((0, 1), (1, 2)),
GriddedPerm.single_cell((1, 0), (1, 2)),
]
)
assert sorted(reqs) == sorted(
placement1ownrow.stretched_requirements((1, 1))
+ [[GriddedPerm((0,), ((1, 2),))]]
)
(
obs,
reqs,
_,
) = placement1owncol._stretched_obstructions_requirements_and_parameters((1, 1))
assert set(obs) == set(
placement1owncol.stretched_obstructions((1, 1))
+ [
GriddedPerm.single_cell((0, 1), (2, 1)),
GriddedPerm.single_cell((1, 0), (2, 1)),
]
)
assert sorted(reqs) == sorted(
placement1owncol.stretched_requirements((1, 1))
+ [[GriddedPerm((0,), ((2, 1),))]]
)


def farther(placement1):
assert placement1._farther((0, 0), (2, 0), DIR_EAST) is False
assert placement1._farther((0, 0), (2, 0), DIR_NORTH) is False
Expand Down Expand Up @@ -498,13 +277,12 @@ def test_forced_obstructions_from_patt(
)


def test_forced_obstructions_from_list(
gp1, placement1, placement1owncol, placement1ownrow
):
def test_forced_obstructions_from_list(placement1, placement1owncol, placement1ownrow):
req_list_row = [
GriddedPerm((0,), ((0, 0),)),
GriddedPerm((0,), ((1, 0),)),
]
print(placement1._tiling)
assert set(
placement1.forced_obstructions_from_requirement(
req_list_row, (0, 0), (0, 0), DIR_NORTH
Expand Down
53 changes: 45 additions & 8 deletions tests/strategies/test_point_placements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from comb_spec_searcher.strategies import Rule
from permuta.misc import DIR_EAST, DIR_NORTH, DIR_SOUTH, DIR_WEST, DIRS
from tilings import GriddedPerm, Tiling
from tilings.assumptions import TrackingAssumption
from tilings.map import RowColMap
from tilings.parameter_counter import ParameterCounter, PreimageCounter
from tilings.strategies import (
AllPlacementsFactory,
PatternPlacementFactory,
Expand Down Expand Up @@ -1334,7 +1335,6 @@ def test_reverse_rule_non_empty_children():
GriddedPerm((1, 0), ((2, 6), (2, 0))),
),
),
assumptions=(),
)
rule = strategy(tiling)
eqv_rule = rule.to_equivalence_rule()
Expand Down Expand Up @@ -1371,13 +1371,22 @@ def test_multiple_parent_parameters_to_same_child_parameter():
GriddedPerm((0, 2, 3, 1), ((0, 0), (0, 0), (3, 0), (3, 0))),
),
requirements=(),
assumptions=(
TrackingAssumption((GriddedPerm((0,), ((2, 0),)),)),
TrackingAssumption(
(GriddedPerm((0,), ((2, 0),)), GriddedPerm((0,), ((3, 0),)))
),
),
)

row_map = {0: 0}
col_map1 = {0: 0, 1: 1, 2: 2, 3: 2, 4: 3}
col_map2 = {0: 0, 1: 1, 2: 2, 3: 3, 4: 3}

row_col_map1 = RowColMap(row_map, col_map1)
row_col_map2 = RowColMap(row_map, col_map2)

preimage1 = PreimageCounter(row_col_map1.preimage_tiling(tiling), row_col_map1)
preimage2 = PreimageCounter(row_col_map2.preimage_tiling(tiling), row_col_map2)

param1 = ParameterCounter([preimage1])
param2 = ParameterCounter([preimage1, preimage2])

tiling = tiling.add_parameters([param1, param2])
strategy = RequirementPlacementStrategy(
gps=(
GriddedPerm((0,), ((1, 0),)),
Expand All @@ -1395,3 +1404,31 @@ def test_multiple_parent_parameters_to_same_child_parameter():
rule = strategy(tiling)
for i in range(6):
rule.sanity_check(i)


def test_place_with_params():
tiling = Tiling(
obstructions=(GriddedPerm((0, 1, 2), ((0, 0), (0, 0), (0, 0))),),
requirements=(),
parameters=[
ParameterCounter(
[
PreimageCounter(
Tiling(
obstructions=(
GriddedPerm((0, 1), ((0, 0), (0, 0))),
GriddedPerm((0, 1, 2), ((0, 0), (0, 1), (0, 1))),
GriddedPerm((0, 1, 2), ((0, 1), (0, 1), (0, 1))),
),
requirements=(),
parameters=(),
),
RowColMap({0: 0, 1: 0}, {0: 0}),
)
]
)
],
)
for rule in AllPlacementsFactory()(tiling):
for i in range(5):
assert rule.sanity_check(i)
Loading