def test_init_simple_3(self): arr = cpl.init_simple(3) self.assertEqual(len(arr), 1) self.assertEqual(len(arr[0]), 3) self.assertEqual(arr[0][0], 0) self.assertEqual(arr[0][1], 1) self.assertEqual(arr[0][2], 0)
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())
def test_binary_rule_powers_of_two_nks(self): rule_number = 30 radius = 1 size = 149 timesteps = 149 expected = cpl.evolve(cpl.init_simple(size=size), timesteps=timesteps, apply_rule=lambda n, c, t: cpl.binary_rule( n, rule_number, scheme="nks"), r=radius) powers_of_two = 2**np.arange(radius * 2 + 1)[::-1] rule = list(map(int, bin(rule_number)[2:])) rule_bin_array = np.pad(rule, ((2**(radius * 2 + 1)) - len(rule), 0), 'constant').tolist() actual = cpl.evolve( cpl.init_simple(size=size), timesteps=timesteps, apply_rule=lambda n, c, t: cpl.binary_rule( n, rule_bin_array, scheme="nks", powers_of_two=powers_of_two), r=radius) np.testing.assert_equal(expected.tolist(), actual.tolist())
def test_dtype(self): cellular_automaton = cpl.init_simple(11, dtype=np.float32) cellular_automaton = cpl.evolve( cellular_automaton, timesteps=6, apply_rule=lambda n, c, t: sum(n) / len(n)) np.testing.assert_almost_equal( cellular_automaton.tolist(), [[0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.333, 0.333, 0.333, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.111, 0.222, 0.333, 0.222, 0.111, 0.0, 0.0, 0.0], [ 0.0, 0.0, 0.037, 0.111, 0.222, 0.259, 0.222, 0.111, 0.037, 0.0, 0.0 ], [ 0.0, 0.012, 0.049, 0.123, 0.198, 0.235, 0.198, 0.123, 0.049, 0.0123, 0.0 ], [ 0.004, 0.021, 0.062, 0.123, 0.185, 0.210, 0.185, 0.123, 0.062, 0.021, 0.004 ]], decimal=3)
import numpy as np import cellpylib as cpl # NKS page 437 - Rule 214R # run the CA forward for 32 steps to get the initial condition for the next evolution cellular_automaton = cpl.init_simple(63) r = cpl.ReversibleRule(cellular_automaton[0], 214) cellular_automaton = cpl.evolve(cellular_automaton, timesteps=32, apply_rule=r.apply_rule) # use the last state of the CA as the initial, previous state for this evolution r = cpl.ReversibleRule(cellular_automaton[-1], 214) cellular_automaton = np.array([cellular_automaton[-2]]) cellular_automaton = cpl.evolve(cellular_automaton, timesteps=62, apply_rule=r.apply_rule) cpl.plot(cellular_automaton)
import cellpylib as cpl import time start = time.time() cpl.evolve(cpl.init_simple(1000), timesteps=500, apply_rule=lambda n, c, t: cpl.nks_rule(n, 30), memoize=True) print(f"Elapsed: {time.time() - start:.2f} seconds")
import cellpylib as cpl import time start = time.time() cpl.evolve(cpl.init_simple(600), timesteps=300, apply_rule=lambda n, c, t: cpl.nks_rule(n, 30)) print(f"Without memoization: {time.time() - start:.2f} seconds elapsed") start = time.time() cpl.evolve(cpl.init_simple(600), timesteps=300, apply_rule=lambda n, c, t: cpl.nks_rule(n, 30), memoize=True) print(f"With memoization: {time.time() - start:.2f} seconds elapsed")
def test_init_simple_1_val2(self): arr = cpl.init_simple(1, val=2) self.assertEqual(len(arr), 1) self.assertEqual(len(arr[0]), 1) self.assertEqual(arr[0][0], 2)
def test_init_simple_1(self): arr = cpl.init_simple(1) self.assertEqual(len(arr), 1) self.assertEqual(len(arr[0]), 1) self.assertEqual(arr[0][0], 1)
import math from pprint import pprint import numpy as np import cellpylib as cpl cellular_automaton = cpl.init_simple(200, dtype=np.float32) # NKS page 157 def apply_rule(n, c, t): result = (sum(n) / len(n)) * (3 / 2) frac, whole = math.modf(result) return frac cellular_automaton = cpl.evolve(cellular_automaton, timesteps=100, apply_rule=apply_rule) pprint(cellular_automaton[:6, 95:106].tolist(), width=100) cpl.plot(cellular_automaton)