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_init_simple2d_2x3(self): arr = cpl.init_simple2d(rows=2, cols=3) self.assertEqual(len(arr), 1) self.assertEqual(len(arr[0]), 2) self.assertEqual(len(arr[0][0]), 3) self.assertEqual(len(arr[0][1]), 3) self.assertEqual(arr[0][0].tolist(), [0, 0, 0]) self.assertEqual(arr[0][1].tolist(), [0, 1, 0])
def __init__(self, nb_size: int = 10, tr: float = 1/20, rr: float = 1/2): self.sandbox = cpl.init_simple2d(nb_size, nb_size) center = int(nb_size/2) self.sandbox[:, center, center] = self.INFECTIOUS self.population = nb_size * nb_size self.susceptible = self.population - 1 self.infectious = 1 self.recovered = 0 self.transmission_rate = tr self.recover_rate = rr self.time = 1
import cellpylib as cpl # Glider cellular_automaton = cpl.init_simple2d(60, 60) cellular_automaton[:, [28, 29, 30, 30], [30, 31, 29, 31]] = 1 # Blinker cellular_automaton[:, [40, 40, 40], [15, 16, 17]] = 1 # Light Weight Space Ship (LWSS) cellular_automaton[:, [18, 18, 19, 20, 21, 21, 21, 21, 20], [45, 48, 44, 44, 44, 45, 46, 47, 48]] = 1 # evolve the cellular automaton for 60 time steps cellular_automaton = cpl.evolve2d(cellular_automaton, timesteps=60, neighbourhood='Moore', apply_rule=cpl.game_of_life_rule) cpl.plot2d_animate(cellular_automaton) #%% rule table import cellpylib as cpl rule_table, actual_lambda, quiescent_state = cpl.random_rule_table( lambda_val=0.45, k=4, r=2, strong_quiescence=True, isotropic=True) cellular_automaton = cpl.init_random(128, k=4) # use the built-in table_rule to use the generated rule table cellular_automaton = cpl.evolve( cellular_automaton, timesteps=200, apply_rule=lambda n, c, t: cpl.table_rule(n, rule_table), r=2)
import sys sys.path.append('/Users/samyak/Documents/FireCellularAutomata/cellpylib-master') import cellpylib as cpl # Glider cellular_automaton = cpl.init_simple2d(120, 120) cellular_automaton[:, [28,29,30,30], [30,31,29,31]] = 1 # Blinker cellular_automaton[:, [40,40,40], [15,16,17]] = 1 # Light Weight Space Ship (LWSS) cellular_automaton[:, [18,18,19,20,21,21,21,21,20], [45,48,44,44,44,45,46,47,48]] = 1 # evolve the cellular automaton for 60 time steps cellular_automaton = cpl.evolve2d(cellular_automaton, timesteps=120, neighbourhood='Moore', apply_rule=cpl.game_of_life_rule) cpl.plot2d_animate(cellular_automaton)
import cellpylib as cpl ctrbl_rule = cpl.CTRBLRule(rule_table={ (0, 1, 0, 0, 0): 1, (1, 1, 0, 0, 0): 0, (0, 0, 0, 0, 0): 0, (1, 0, 0, 0, 0): 1, (0, 0, 1, 1, 0): 0, (1, 1, 1, 1, 1): 1, (0, 1, 0, 1, 0): 0, (1, 1, 1, 0, 1): 1, (1, 0, 1, 0, 1): 1, (0, 1, 1, 1, 1): 1, (0, 0, 1, 1, 1): 0, (1, 1, 0, 0, 1): 1 }, add_rotations=True) cellular_automaton = cpl.init_simple2d(rows=10, cols=10) cellular_automaton = cpl.evolve2d(cellular_automaton, timesteps=60, apply_rule=ctrbl_rule, neighbourhood="von Neumann") cpl.plot2d_animate(cellular_automaton)
import cellpylib as cpl """ We repeatedly drop a grain of sand in the middle, allowing the sandpile to grow. After a grain is dropped, the system is allowed to evolve until a fixed point, where no further change occurs, before the next grain is dropped. """ n = 50 sandpile = cpl.Sandpile(n, n) ca = cpl.init_simple2d(n, n, val=5) for i in range(300): ca[-1, n // 2, n // 2] += 1 ca = cpl.evolve2d(ca, apply_rule=sandpile, timesteps=cpl.until_fixed_point(), neighbourhood='Moore') cpl.plot2d_animate(ca)
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)
def test_init_simple2d_1x1_val2(self): arr = cpl.init_simple2d(rows=1, cols=1, val=2) self.assertEqual(len(arr), 1) self.assertEqual(len(arr[0]), 1) self.assertEqual(len(arr[0][0]), 1) self.assertEqual(arr[0][0][0], 2)
# 4 for death ''' n = 200 # size Tp1 = 1 # cell 2 -> 2, 3 is so big that program exits early Tp2 = 1 # cell 2 -> 1 Tm = 2 # migration # state transfer matrix, trans_1 stands for probability of changing from state 1 to 1,2,3 trans_1 = (2, 7, 1) trans_2 = (0, 1, 0) trans_3 = (0, 0, 1) epoch = 20 # number of period # %% easy stimulation state_init = cpl.init_simple2d(n, n, val=2, dtype='uint8') states = stimulation_v1(n, state_init, epoch=epoch) # %% result # cpl.plot2d(states, timestep=1) # visualize single timestep # cpl.plot2d_animate(states) # a dynamic graph # np.mean(states[0]==states[5]) # %% 3-stage rule with single BS and no nutrition # the init_simple2d added a dimension to a state, which fits the original evolution function # put a state 2 cell in the center, but we can write our own # each time I run the simulation, state_init changed ????! have to put it here state_init = cpl.init_simple2d(n, n, val=2, dtype='uint8').squeeze() # states_epoch, states_stage = stimulation_v2(n=n, state_init=state_init, Tp1=Tp1,
def init_configuration(width=BOARD_WIDTH, height=BOARD_HEIGHT): return cpl.init_simple2d(width, height, val=0)