neuropercolation/src/cellular_automaton/ca_factory.py

30 lines
1.0 KiB
Python
Raw Normal View History

2019-02-03 17:26:31 +00:00
from cellular_automaton.ca_cell import Cell, CellState
from cellular_automaton.ca_neighborhood import Neighborhood
from typing import Type
2019-02-09 17:16:19 +00:00
import itertools
2019-02-03 17:26:31 +00:00
2019-02-09 17:16:19 +00:00
class CAFactory:
@staticmethod
def make_cellular_automaton(dimension,
2019-02-03 17:26:31 +00:00
neighborhood: Type[Neighborhood],
state_class: Type[CellState]):
2019-02-09 17:16:19 +00:00
cells = CAFactory._make_cells(dimension, state_class)
CAFactory._apply_neighbourhood_to_cells(cells, neighborhood, dimension)
return cells
2019-02-09 17:16:19 +00:00
@staticmethod
def _make_cells(dimension, state_class):
cells = {}
for c in itertools.product(*[range(d) for d in dimension]):
cells[tuple(c)] = Cell(state_class)
2019-02-09 17:16:19 +00:00
return cells
@staticmethod
def _apply_neighbourhood_to_cells(cells, neighborhood, dimension):
for coordinate, cell in cells.items():
n_coordinates = neighborhood.calculate_cell_neighbor_coordinates(coordinate, dimension)
cell.neighbours = [cells[tuple(nc)].state for nc in n_coordinates]
2019-02-03 17:26:31 +00:00