def test_sequential_rule_2d(self): cellular_automaton = cpl.init_simple2d(3, 3) r = cpl.AsynchronousRule( apply_rule=lambda n, c, t: cpl.totalistic_rule(n, k=2, rule=126), update_order=range(0, 9)) cellular_automaton = cpl.evolve2d(cellular_automaton, timesteps=18, neighbourhood='Moore', apply_rule=r.apply_rule) expected = [[[0, 0, 0], [0, 1, 0], [0, 0, 0]], [[1, 0, 0], [0, 1, 0], [0, 0, 0]], [[1, 1, 0], [0, 1, 0], [0, 0, 0]], [[1, 1, 1], [0, 1, 0], [0, 0, 0]], [[1, 1, 1], [1, 1, 0], [0, 0, 0]], [[1, 1, 1], [1, 1, 0], [0, 0, 0]], [[1, 1, 1], [1, 1, 1], [0, 0, 0]], [[1, 1, 1], [1, 1, 1], [1, 0, 0]], [[1, 1, 1], [1, 1, 1], [1, 0, 0]], [[1, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 0, 0]], [[0, 1, 1], [1, 1, 1], [1, 1, 0]]] np.testing.assert_equal(expected, cellular_automaton.tolist())
def test_sequential_left_to_right(self): expected = self._convert_to_numpy_matrix( "rule60_sequential_simple_init.ca") cellular_automaton = cpl.init_simple(21) r = cpl.AsynchronousRule( apply_rule=lambda n, c, t: cpl.nks_rule(n, 60), update_order=range(1, 20)) cellular_automaton = cpl.evolve(cellular_automaton, timesteps=19 * 20, apply_rule=r.apply_rule) np.testing.assert_equal(expected.tolist(), cellular_automaton[::19].tolist())
def test_sequential_random(self): expected = self._convert_to_numpy_matrix( "rule90_sequential_simple_init.ca") cellular_automaton = cpl.init_simple(21) update_order = [ 19, 11, 4, 9, 6, 16, 10, 2, 17, 1, 12, 15, 5, 3, 8, 18, 7, 13, 14 ] r = cpl.AsynchronousRule( apply_rule=lambda n, c, t: cpl.nks_rule(n, 90), update_order=update_order) cellular_automaton = cpl.evolve(cellular_automaton, timesteps=19 * 20, apply_rule=r.apply_rule) np.testing.assert_equal(expected.tolist(), cellular_automaton[::19].tolist())
import cellpylib as cpl cellular_automaton = cpl.init_simple2d(50, 50, val=0) # During each timestep, we'll check each cell if it should be the one updated according to the # update order. At the end of a timestep, the update order index is advanced, but if the # update order is randomized at the end of each timestep, then this is equivalent to picking # a cell randomly to update at each timestep. apply_rule = cpl.AsynchronousRule(apply_rule=lambda n, c, t: 1, num_cells=(50, 50), randomize_each_cycle=True) cellular_automaton = cpl.evolve2d(cellular_automaton, timesteps=50, neighbourhood='Moore', apply_rule=apply_rule) cpl.plot2d_animate(cellular_automaton, interval=200, autoscale=True)
import cellpylib as cpl # implements the rule 60 sequential automaton from the NKS Notes on # Chapter 9, section 10: "Sequential cellular automata" # http://www.wolframscience.com/nks/notes-9-10--sequential-cellular-automata/ cellular_automaton = cpl.init_simple(21) apply_rule = cpl.AsynchronousRule(apply_rule=lambda n, c, t: cpl.nks_rule(n, 60), update_order=range(1, 20)) cellular_automaton = cpl.evolve(cellular_automaton, timesteps=19*20, apply_rule=apply_rule) # get every 19th row, including the first, as a cycle is completed every 19 rows cpl.plot(cellular_automaton[::19])