コード例 #1
0
 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())
コード例 #2
0
 def _create_ca(self, expected, rule, neighbourhood):
     steps, _, _ = expected.shape
     cellular_automaton = np.array([expected[0]])
     return cpl.evolve2d(
         cellular_automaton,
         timesteps=steps,
         r=1,
         neighbourhood=neighbourhood,
         apply_rule=lambda n, c, t: cpl.totalistic_rule(n, k=2, rule=rule))
コード例 #3
0
def run_GOL(
    initial_condition: np.ndarray,
    timesteps: list,
    board_width: int,
    board_height: int,
    rules: str,
):
    """
    Runs a game of life simulation for the given time steps
    :param initial_condition: the gene from the GA
    :param timesteps: the initial time step, followed by the difference from initial to each next
    :param board_width: the width of the board
    :param board_height: the height of the board
    :param rules: rules to use for the simulation
    :return: Chromosome object for that individual
    """
    full_board = np.zeros((1, board_width * 2, board_height * 2))
    # shape from list to array
    initial_board = initial_condition.reshape((board_height, board_width))
    # Needs to be this format for the cpl package
    initial_board = np.array([initial_board])

    full_board[:,
               int(board_width / 2):int(board_width * 1.5),
               int(board_height / 2):int(board_height * 1.5)] = initial_board

    rule_function = None
    if rules == "b3s23":
        rule_function = game_of_life_rule
    elif rules == "b38s23":
        rule_function = b38s23
    elif rules == "b36s23":
        rule_function = b36s23

    # evolve the cellular automaton for max given time steps
    result = cpl.evolve2d(
        full_board,
        timesteps=(timesteps[-1] + 1),
        neighbourhood="Moore",
        apply_rule=rule_function,
    )

    # return only the time steps requested
    ca_at_time_steps = []
    ca_at_time_steps_1d = []
    for time_step in timesteps:
        ca_at_time_steps.append(result[time_step])
        # TODO: Change board size
        ca_at_time_steps_1d.append(result[time_step].reshape(
            (1, board_width * 2 * board_width * 2)))
    return ca_at_time_steps, ca_at_time_steps_1d
コード例 #4
0
#run_chromosome = np.array(np.random.randint(2, size=50 * 50))

# Board conditions
max_timestep = 100
board_width = int(math.sqrt(len(run_chromosome)))
board_height = int(math.sqrt(len(run_chromosome)))

full_board = np.zeros((board_width * 2, board_height * 2))
# shape from list to array
initial_board = run_chromosome.reshape((board_height, board_width))
# Needs to be this format for the cpl package

full_board[int(board_width / 2):int(board_width * 1.5),
           int(board_height / 2):int(board_height * 1.5)] = initial_board
cellular_automaton = np.array([full_board])

# evolve the cellular automaton
cellular_automaton = cpl.evolve2d(
    cellular_automaton,
    timesteps=max_timestep,
    neighbourhood="Moore",
    apply_rule=b38s23,
)

cpl.plot2d_animate(cellular_automaton)
"""
for ca in cellular_automaton:
    plt.imshow(ca, cmap='Greys')
    plt.pause(.2)
    plt.show()
"""
コード例 #5
0
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)
コード例 #6
0
import cellpylib as cpl

sdsr_loop = cpl.SDSRLoop()

# the initial conditions consist of a single loop
cellular_automaton = sdsr_loop.init_loops(1, (100, 100), [40], [40])

cellular_automaton = cpl.evolve2d(cellular_automaton, timesteps=700,
                                  apply_rule=sdsr_loop, memoize="recursive")

cpl.plot2d_animate(cellular_automaton)
コード例 #7
0
import cellpylib as cpl

cellular_automaton = cpl.init_simple2d(60, 60)

# evolve the cellular automaton for 30 time steps
cellular_automaton = cpl.evolve2d(
    cellular_automaton,
    timesteps=30,
    neighbourhood='Moore',
    apply_rule=lambda n, c, t: cpl.totalistic_rule(n, k=2, rule=126))

cpl.plot2d(cellular_automaton)
コード例 #8
0
import cellpylib as cpl
import numpy as np

n_rows = 45
n_cols = 45
sandpile = cpl.Sandpile(n_rows, n_cols)

initial = np.random.randint(5, size=n_rows * n_cols).reshape(
    (1, n_rows, n_cols))
# we're using a closed boundary, so make the boundary cells 0
initial[0, 0, :], initial[0,
                          n_rows - 1, :], initial[0, :,
                                                  0], initial[0, :, n_cols -
                                                              1] = 0, 0, 0, 0

ca = cpl.evolve2d(initial,
                  timesteps=50,
                  apply_rule=sandpile,
                  neighbourhood="von Neumann")

cpl.plot2d_animate(ca)
コード例 #9
0
ca_rows = 50
ca_cols = 50
num_timesteps = 120
num_layers = 6
val_spread = 'uniform'
cellular_automaton = cpl.init_random_fire_map2d(ca_rows,
                                                ca_cols,
                                                spread=val_spread)
val_string = ""
state_string = ""
neighborhood_string = ""
fire_cells = np.array([])
topo_cells = np.array([])

cellular_automaton = cpl.evolve2d(cellular_automaton,
                                  timesteps=num_timesteps,
                                  neighbourhood='Moore',
                                  apply_rule=cpl.fire_spread_rule)
for i in range(num_timesteps):
    #print (cellular_automaton[i][2][:][:])
    fire_cells = np.append(fire_cells, (cellular_automaton[i][1][:][:]))
    #topo_cells = np.append(topo_cells, (cellular_automaton[i][5][:][:]))
fire_cells = np.reshape(fire_cells, (num_timesteps, ca_rows, ca_cols))

#topo_cells = np.reshape(topo_cells, (num_timesteps, ca_rows, ca_cols))
cpl.plot2d_animate(fire_cells)
#cpl.plot2d(fire_cells, title = 'Uniform Fire Load (Wind and Topographic Features Considered)')
#cpl.plot2d_animate(topo_cells)
"""
#Prints out fuel load probability values
for row in range(ca_rows):
	for col in range(ca_cols):
コード例 #10
0
import cellpylib as cpl
import numpy as np
np.random.seed(0)

n_rows = 45
n_cols = 45
sandpile = cpl.Sandpile(n_rows, n_cols)

initial = np.random.randint(5, size=n_rows * n_cols).reshape(
    (1, n_rows, n_cols))
# we're using a closed boundary, so make the boundary cells 0
initial[0, 0, :], initial[0,
                          n_rows - 1, :], initial[0, :,
                                                  0], initial[0, :, n_cols -
                                                              1] = 0, 0, 0, 0

ca = cpl.evolve2d(initial,
                  timesteps=cpl.until_fixed_point(),
                  apply_rule=sandpile,
                  neighbourhood="von Neumann")

print("Number of timesteps to reach fixed point: %s" % len(ca))
cpl.plot2d_animate(ca)
コード例 #11
0
import cellpylib as cpl

langtons_loop = cpl.LangtonsLoop()

# the initial conditions consist of a single loop
cellular_automaton = langtons_loop.init_loops(1, (75, 75), [40], [25])

cellular_automaton = cpl.evolve2d(cellular_automaton,
                                  timesteps=500,
                                  apply_rule=langtons_loop,
                                  memoize="recursive")

cpl.plot2d_animate(cellular_automaton)
コード例 #12
0
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)
コード例 #13
0
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)
コード例 #14
0
import cellpylib as cpl
import numpy as np

cellular_automaton = cpl.init_simple2d(60, 60)
# the letter "E"
cellular_automaton[0][28][28] = 1
cellular_automaton[0][28][29] = 1
cellular_automaton[0][28][30] = 1
cellular_automaton[0][29][28] = 1
cellular_automaton[0][30][28] = 1
cellular_automaton[0][30][29] = 1
cellular_automaton[0][30][30] = 1
cellular_automaton[0][31][28] = 1
cellular_automaton[0][32][28] = 1
cellular_automaton[0][32][29] = 1
cellular_automaton[0][32][30] = 1


def activity_rule(n, c, t):
    current_activity = n[1][1]
    return (np.sum(n) - current_activity) % 2


cellular_automaton = cpl.evolve2d(cellular_automaton,
                                  timesteps=20,
                                  apply_rule=activity_rule,
                                  neighbourhood="Moore")

cpl.plot2d_animate(cellular_automaton, interval=350)
コード例 #15
0
 def evolve(self, ts: int = 1):
     self.sandbox = cpl.evolve2d(self.sandbox, timesteps=ts, neighbourhood='Moore',
                                 apply_rule=lambda n,c,t: self.virus_spread_rule(n,c,t))
コード例 #16
0
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)
コード例 #17
0
        electron_head_count = np.count_nonzero(n == 1)
        return 1 if electron_head_count == 1 or electron_head_count == 2 else 3


cellular_automata = np.array(
    [[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 3, 1, 2, 3, 3, 3, 3, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3, 3, 3, 3, 2],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 2, 1, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
      [0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0]]])

cellular_automata = cpl.evolve2d(cellular_automata,
                                 timesteps=25,
                                 apply_rule=wireworld_rule,
                                 neighbourhood="Moore")

cpl.plot2d_animate(cellular_automata,
                   show_grid=True,
                   show_margin=False,
                   scale=0.3,
                   colormap=ListedColormap(["black", "blue", "red", "yellow"]))
コード例 #18
0
import cellpylib as cpl
import numpy as np

cellular_automaton = cpl.init_simple2d(60, 60)
# the letter "E"
cellular_automaton[0][28][28] = 0
cellular_automaton[0][28][29] = 1
cellular_automaton[0][28][30] = 2
cellular_automaton[0][29][28] = 3
cellular_automaton[0][30][28] = 4
cellular_automaton[0][30][29] = 5
cellular_automaton[0][30][30] = 6
cellular_automaton[0][31][28] = 7
cellular_automaton[0][32][28] = 8
cellular_automaton[0][32][29] = 9
cellular_automaton[0][32][30] = 10

def activity_rule(n, c, t):
    current_activity = n[1][1]
    return (np.sum(n) - current_activity) % 11

cellular_automaton = cpl.evolve2d(cellular_automaton, timesteps=23,
                                  apply_rule=activity_rule, neighbourhood="von Neumann")

cpl.plot2d_animate(cellular_automaton, interval=350, colormap='viridis')
read_data = pd.read_excel("test_logs/" + test_name + "_results.xlsx")
fitness = list(read_data["max fitness"])

run_chromosome = get_best_chromosome_in_this_excel(fitness, chromosomes)
#run_chromosome = np.array(np.random.randint(2, size=50 * 50))

# Board conditions
max_timestep = 100
board_width = int(math.sqrt(len(run_chromosome)))
board_height = int(math.sqrt(len(run_chromosome)))

full_board = np.zeros((board_width*2, board_height*2))
# shape from list to array
initial_board = run_chromosome.reshape((board_height, board_width))
# Needs to be this format for the cpl package

full_board[int(board_width/2):int(board_width*1.5), int(board_height/2):int(board_height*1.5)] = initial_board
cellular_automaton = np.array([full_board])


# evolve the cellular automaton
cellular_automaton = cpl.evolve2d(
    cellular_automaton,
    timesteps=max_timestep,
    neighbourhood="Moore",
    apply_rule=cpl.game_of_life_rule,
)

cpl.plot2d_animate(cellular_automaton)
コード例 #20
0
def evolve_configuration(configuration, steps=1):
    return cpl.evolve2d(configuration,
                        timesteps=steps,
                        neighbourhood='Moore',
                        apply_rule=cpl.game_of_life_rule)