Adds active state and reactivation for automaton

This commit is contained in:
rfeistenauer 2020-10-24 21:11:57 +02:00
parent 14f9bb8ddc
commit 2204c4a048
4 changed files with 48 additions and 9 deletions

View File

@ -98,6 +98,11 @@ There ist still quite some work to do.
And for all others, don't hesitate to open issues when you have problems! And for all others, don't hesitate to open issues when you have problems!
## Changelog ## Changelog
#### 1.0.4
- Adds active state for automaton
- Adds reactivation method
- Fixes cells active state
#### 1.0.3 #### 1.0.3
- Fixes init_cell_state called twice the necessary amount - Fixes init_cell_state called twice the necessary amount

View File

@ -86,6 +86,17 @@ class CellularAutomaton(CellularAutomatonCreator, abc.ABC):
def __init__(self, neighborhood: Neighborhood, *args, **kwargs): def __init__(self, neighborhood: Neighborhood, *args, **kwargs):
super().__init__(neighborhood=neighborhood, *args, **kwargs) super().__init__(neighborhood=neighborhood, *args, **kwargs)
self._evolution_step = 0 self._evolution_step = 0
self._active = True
def is_active(self):
return self._active
def reactivate(self):
""" Sets all cells active again """
for cell in self._current_state.values():
cell.is_active = True
active = property(is_active)
def get_cells(self): def get_cells(self):
return self._current_state return self._current_state
@ -109,6 +120,7 @@ class CellularAutomaton(CellularAutomatonCreator, abc.ABC):
:param times: The number of evolution steps processed with one call of this method. :param times: The number of evolution steps processed with one call of this method.
""" """
for _ in itertools.repeat(None, times): for _ in itertools.repeat(None, times):
self._active = False
self.__evolve_cells(self._current_state, self._next_state) self.__evolve_cells(self._current_state, self._next_state)
self._current_state, self._next_state = self._next_state, self._current_state self._current_state, self._next_state = self._next_state, self._current_state
self._evolution_step += 1 self._evolution_step += 1
@ -122,12 +134,12 @@ class CellularAutomaton(CellularAutomatonCreator, abc.ABC):
old.is_active = False old.is_active = False
evolve_cell(old, new, new_state) evolve_cell(old, new, new_state)
@classmethod def __evolve_cell(self, old, cell, new_state):
def __evolve_cell(cls, old, cell, new_state): changed = new_state != old.state
changed = new_state != cell.state
cell.state = new_state cell.state = new_state
cell.is_dirty |= changed cell.is_dirty |= changed
old.is_dirty |= changed old.is_dirty |= changed
self._active |= changed
if changed: if changed:
cell.is_active = True cell.is_active = True
for n in cell.neighbors: for n in cell.neighbors:

View File

@ -7,7 +7,7 @@ with open('README.md') as f:
setup( setup(
name="cellular_automaton", name="cellular_automaton",
version="1.0.3", version="1.0.4",
author="Richard Feistenauer", author="Richard Feistenauer",
author_email="r.feistenauer@web.de", author_email="r.feistenauer@web.de",
packages=find_packages(exclude=('tests', 'docs', 'examples')), packages=find_packages(exclude=('tests', 'docs', 'examples')),

View File

@ -15,7 +15,7 @@ limitations under the License.
""" """
# pylint: disable=missing-function-docstring # pylint: disable=missing-function-docstring
# pylint: disable=redefined-outer-name # pylint: disable=missing-class-docstring
import pytest import pytest
@ -23,11 +23,11 @@ from .context import cellular_automaton as ca
class TAutomaton(ca.CellularAutomaton): class TAutomaton(ca.CellularAutomaton):
""" Simple Automaton for test purposes """
def evolve_rule(self, last_cell_state, neighbors_last_states): def evolve_rule(self, last_cell_state, __):
return [last_cell_state[0] + 1] return [last_cell_state[0] + 1]
def init_cell_state(self, cell_coordinate): def init_cell_state(self, __):
return [0] return [0]
@ -53,9 +53,31 @@ def test_dimensions(dimensions):
assert automaton.cells[(1, ) * dimensions].state[0] == 1 assert automaton.cells[(1, ) * dimensions].state[0] == 1
def test_process_evolution_calls(): def test_copy_cells():
automaton = TAutomaton(NEIGHBORHOOD, [3, 3]) automaton = TAutomaton(NEIGHBORHOOD, [3, 3])
automaton.evolve(5) automaton.evolve(5)
automaton2 = TAutomaton(NEIGHBORHOOD, [3, 3]) automaton2 = TAutomaton(NEIGHBORHOOD, [3, 3])
automaton2.cells = automaton.cells automaton2.cells = automaton.cells
assert automaton2.cells[(1, 1)].state[0] == 5 assert automaton2.cells[(1, 1)].state[0] == 5
def test_automaton_goes_inactive():
automaton = TAutomaton(NEIGHBORHOOD, [3, 3])
assert automaton.active
automaton.evolve_rule = lambda x, y: x
automaton.evolve()
assert not automaton.active
def test_reactivation():
automaton = TAutomaton(NEIGHBORHOOD, [3, 3])
rule, automaton.evolve_rule = automaton.evolve_rule, lambda x, y: x
automaton.evolve()
assert not automaton.active
automaton.reactivate()
automaton.evolve_rule = rule
automaton.evolve()
assert automaton.active