diff --git a/cellular_automaton/__init__.py b/cellular_automaton/__init__.py index 99103bf..b2b8959 100644 --- a/cellular_automaton/__init__.py +++ b/cellular_automaton/__init__.py @@ -1,4 +1,5 @@ -from .neighborhood import Neighborhood, MooreNeighborhood, VonNeumannNeighborhood, EdgeRule +from .neighborhood import Neighborhood, MooreNeighborhood, VonNeumannNeighborhood, \ + EdgeRule, HexagonalNeighborhood, RadialNeighborhood from .rule import Rule from .factory import CAFactory from .display import CAWindow diff --git a/cellular_automaton/neighborhood.py b/cellular_automaton/neighborhood.py index ca99f54..64f1245 100644 --- a/cellular_automaton/neighborhood.py +++ b/cellular_automaton/neighborhood.py @@ -17,6 +17,7 @@ limitations under the License. import enum import operator import itertools +import math class EdgeRule(enum.Enum): @@ -43,23 +44,23 @@ class Neighborhood: :return: list of absolute coordinates for the cells neighbors. """ self.__grid_dimensions = grid_dimensions - return list(self.__neighbors_generator(cell_coordinate)) + return list(self._neighbors_generator(cell_coordinate)) def get_id_of_neighbor_from_relative_coordinate(self, rel_coordinate): return self._rel_neighbors.index(rel_coordinate) - def __neighbors_generator(self, cell_coordinate): - if not self.__does_ignore_edge_cell_rule_apply(cell_coordinate): + def _neighbors_generator(self, cell_coordinate): + if not self._does_ignore_edge_cell_rule_apply(cell_coordinate): for rel_n in self._rel_neighbors: - yield from self.__calculate_abs_neighbor_and_decide_validity(cell_coordinate, rel_n) + yield from self._calculate_abs_neighbor_and_decide_validity(cell_coordinate, rel_n) - def __calculate_abs_neighbor_and_decide_validity(self, cell_coordinate, rel_n): + def _calculate_abs_neighbor_and_decide_validity(self, cell_coordinate, rel_n): n = list(map(operator.add, rel_n, cell_coordinate)) n_folded = self.__apply_edge_overflow(n) if n == n_folded or self.__edge_rule == EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS: yield n_folded - def __does_ignore_edge_cell_rule_apply(self, coordinate): + def _does_ignore_edge_cell_rule_apply(self, coordinate): return self.__edge_rule == EdgeRule.IGNORE_EDGE_CELLS and self.__is_coordinate_on_an_edge(coordinate) def __is_coordinate_on_an_edge(self, coordinate): @@ -70,13 +71,12 @@ class Neighborhood: class MooreNeighborhood(Neighborhood): - """ Defines a Moore neighborhood: - Moore defined a neighborhood with a radius applied on a the non euclidean distance to other cells in the grid. + """ Moore defined a neighborhood with a radius applied on a the non euclidean distance to other cells in the grid. Example: - Moor neighborhood in 2 dimensions with radius 1 and 2 + 2 dimensions C = cell of interest - N = neighbour of cell - X = no neighbour of cell + N = neighbor of cell + X = no neighbor of cell Radius 1 Radius 2 X X X X X N N N N N @@ -86,20 +86,19 @@ class MooreNeighborhood(Neighborhood): X X X X X N N N N N """ - def __init__(self, edge_rule: EdgeRule = EdgeRule.IGNORE_EDGE_CELLS, range_=1, dimension=2): - super().__init__(tuple(_rel_neighbor_generator(dimension, range_, lambda rel_n: True)), + def __init__(self, edge_rule: EdgeRule = EdgeRule.IGNORE_EDGE_CELLS, radius=1, dimension=2): + super().__init__(tuple(_rel_neighbor_generator(dimension, radius, lambda rel_n: True)), edge_rule) class VonNeumannNeighborhood(Neighborhood): - """ Defines a Von Neumann neighborhood: - Von Neumann defined a neighborhood with a radius applied to Manhatten distance + """ Von Neumann defined a neighborhood with a radius applied to Manhatten distance (steps between cells without diagonal movement). Example: - Von Neumann neighborhood in 2 dimensions with radius 1 and 2 + 2 dimensions C = cell of interest - N = neighbour of cell - X = no neighbour of cell + N = neighbor of cell + X = no neighbor of cell Radius 1 Radius 2 X X X X X X X N X X @@ -109,19 +108,135 @@ class VonNeumannNeighborhood(Neighborhood): X X X X X X X N X X """ - def __init__(self, edge_rule: EdgeRule = EdgeRule.IGNORE_EDGE_CELLS, range_=1, dimension=2): - self.range_ = range_ - super().__init__(tuple(_rel_neighbor_generator(dimension, range_, self.neighbor_rule)), + def __init__(self, edge_rule: EdgeRule = EdgeRule.IGNORE_EDGE_CELLS, radius=1, dimension=2): + self.radius = radius + super().__init__(tuple(_rel_neighbor_generator(dimension, radius, self.neighbor_rule)), edge_rule) def neighbor_rule(self, rel_n): cross_sum = 0 for ci in rel_n: cross_sum += abs(ci) - return cross_sum <= self.range_ + return cross_sum <= self.radius + + +class RadialNeighborhood(Neighborhood): + """ Neighborhood with a radius applied to euclidean distance + delta + + Example: + 2 dimensions + C = cell of interest + N = neighbor of cell + X = no neighbor of cell + + Radius 2 Radius 3 + X X X X X X X X X N N N X X + X X N N N X X X N N N N N X + X N N N N N X N N N N N N N + X N N C N N X N N N C N N N + X N N N N N X N N N N N N N + X X N N N X X X N N N N N X + X X X X X X X X X N N N X X + """ + + def __init__(self, edge_rule: EdgeRule = EdgeRule.IGNORE_EDGE_CELLS, radius=1, delta_=.25, dimension=2): + self.radius = radius + self.delta = delta_ + super().__init__(tuple(_rel_neighbor_generator(dimension, radius, self.neighbor_rule)), + edge_rule) + + def neighbor_rule(self, rel_n): + cross_sum = 0 + for ci in rel_n: + cross_sum += pow(ci, 2) + + return math.sqrt(cross_sum) <= self.radius + self.delta + + +class HexagonalNeighborhood(Neighborhood): + """ Defines a Hexagonal neighborhood in a rectangular two dimensional grid: + + Example: + Von Nexagonal neighborhood in 2 dimensions with radius 1 and 2 + C = cell of interest + N = neighbor of cell + X = no neighbor of cell + + Radius 1 Radius 2 + X X X X X X N N N X + X N N X N N N N + X N C N X N N C N N + X N N X N N N N + X X X X X X N N N X + + + Rectangular representation: Radius 1 + + Row % 2 == 0 Row % 2 == 1 + N N X X N N + N C N N C N + N N X X N N + + Rectangular representation: Radius 2 + Row % 2 == 0 Row % 2 == 1 + X N N N X X N N N X + N N N N X X N N N N + N N C N N N N C N N + N N N N X X N N N N + X N N N X X N N N X + """ + + def __init__(self, edge_rule: EdgeRule = EdgeRule.IGNORE_EDGE_CELLS, radius=1): + neighbor_lists = [[(0, 0)], + [(0, 0)]] + + self.__calculate_hexagonal_neighborhood(neighbor_lists, radius) + + super().__init__(neighbor_lists, edge_rule) + + def __calculate_hexagonal_neighborhood(self, neighbor_lists, radius): + for r in range(1, radius + 1): + for i, n in enumerate(neighbor_lists): + n = _grow_neighbours(n) + n = self.__add_rectangular_neighbours(n, r, i % 2 == 1) + n = sorted(n, key=(lambda ne: [ne[1], ne[0]])) + n.remove((0, 0)) + neighbor_lists[i] = n + + def get_id_of_neighbor_from_relative_coordinate(self, rel_coordinate): + raise NotImplementedError + + def _neighbors_generator(self, cell_coordinate): + if not self._does_ignore_edge_cell_rule_apply(cell_coordinate): + for rel_n in self._rel_neighbors[cell_coordinate[1] % 2]: + yield from self._calculate_abs_neighbor_and_decide_validity(cell_coordinate, rel_n) + + @staticmethod + def __add_rectangular_neighbours(neighbours, radius, is_odd): + new_neighbours = [] + for x in range(0, radius + 1): + if is_odd: + x -= int(radius/2) + else: + x -= int((radius + 1) / 2) + + for y in range(-radius, radius + 1): + new_neighbours.append((x, y)) + new_neighbours.extend(neighbours) + return list(set(new_neighbours)) def _rel_neighbor_generator(dimension, range_, rule): for c in itertools.product(range(-range_, range_ + 1), repeat=dimension): if rule(c) and c != (0, ) * dimension: yield tuple(reversed(c)) + + +def _grow_neighbours(neighbours): + new_neighbours = neighbours[:] + for n in neighbours: + new_neighbours.append((n[0], n[1] - 1)) + new_neighbours.append((n[0] - 1, n[1])) + new_neighbours.append((n[0] + 1, n[1])) + new_neighbours.append((n[0], n[1] + 1)) + return list(set(new_neighbours)) diff --git a/test/test_neighborhood.py b/test/test_neighborhood.py index 4bb31e6..3135913 100644 --- a/test/test_neighborhood.py +++ b/test/test_neighborhood.py @@ -59,7 +59,7 @@ class TestNeighborhood(unittest.TestCase): self.assertTrue(self.check_neighbors(neighborhood, [n1])) def test_von_neumann_r2(self): - neighborhood = csn.VonNeumannNeighborhood(csn.EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS, range_=2) + neighborhood = csn.VonNeumannNeighborhood(csn.EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS, radius=2) n1 = [[2, 2], [[2, 0], [1, 1], [2, 1], [3, 1], [0, 2], [1, 2], [3, 2], [4, 2], [1, 3], [2, 3], [3, 3], [2, 4]]] self.assertTrue(self.check_neighbors(neighborhood, [n1], dimension=[5, 5])) @@ -69,6 +69,29 @@ class TestNeighborhood(unittest.TestCase): n1 = [[1, 1, 1], [[1, 1, 0], [1, 0, 1], [0, 1, 1], [2, 1, 1], [1, 2, 1], [1, 1, 2]]] self.assertTrue(self.check_neighbors(neighborhood, [n1], dimension=[3, 3, 3])) + def test_hexagonal(self): + neighborhood = csn.RadialNeighborhood(csn.EdgeRule.IGNORE_EDGE_CELLS, radius=2) + n1 = [[2, 2], [[1, 0], [2, 0], [3, 0], + [0, 1], [1, 1], [2, 1], [3, 1], [4, 1], + [0, 2], [1, 2], [3, 2], [4, 2], + [0, 3], [1, 3], [2, 3], [3, 3], [4, 3], + [1, 4], [2, 4], [3, 4]]] + self.assertTrue(self.check_neighbors(neighborhood, [n1], dimension=[5, 5])) + + def test_hexagonal(self): + neighborhood = csn.HexagonalNeighborhood(csn.EdgeRule.IGNORE_EDGE_CELLS, radius=2) + n1 = [[2, 2], [[1, 0], [2, 0], [3, 0], + [0, 1], [1, 1], [2, 1], [3, 1], + [0, 2], [1, 2], [3, 2], [4, 2], + [0, 3], [1, 3], [2, 3], [3, 3], + [1, 4], [2, 4], [3, 4]]] + n2 = [[2, 3], [[1, 1], [2, 1], [3, 1], + [1, 2], [2, 2], [3, 2], [4, 2], + [0, 3], [1, 3], [3, 3], [4, 3], + [1, 4], [2, 4], [3, 4], [4, 4], + [1, 5], [2, 5], [3, 5]]] + self.assertTrue(self.check_neighbors(neighborhood, [n1, n2], dimension=[6, 6])) + if __name__ == '__main__': unittest.main()