neuropercolation/src/cellular_automaton/ca_factory.py

29 lines
1.0 KiB
Python
Raw Normal View History

2019-02-16 17:05:26 +00:00
from cellular_automaton import *
2019-02-03 17:26:31 +00:00
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-16 17:05:26 +00:00
neighborhood: Neighborhood,
state_class: Type[CellState],
rule: Type[Rule]):
2019-02-09 17:16:19 +00:00
cells = CAFactory._make_cells(dimension, state_class)
2019-02-16 17:05:26 +00:00
CAFactory._apply_neighborhood_to_cells(cells, neighborhood, dimension)
return CellularAutomatonState(cells, dimension, rule)
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
2019-02-16 17:05:26 +00:00
def _apply_neighborhood_to_cells(cells, neighborhood, dimension):
for coordinate, cell in cells.items():
n_coordinates = neighborhood.calculate_cell_neighbor_coordinates(coordinate, dimension)
2019-02-16 17:05:26 +00:00
cell.neighbor_states = [cells[tuple(nc)].state for nc in n_coordinates]
2019-02-03 17:26:31 +00:00