def main():
    vsa = VirusSpreadAutomata(100, 0.5573631610109397, 0.45333239503416245)
    vsa.evolve(30)

    cpl.plot2d_animate(vsa.sandbox)

    plt.plot(list(vsa.get_sir()))
    plt.legend(('Susceptible', 'Infectious', 'Recovered'))
    plt.show()
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)
Esempio n. 3
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)
Esempio n. 4
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"]))
Esempio n. 5
0
    0, 0, 1, 0, 0,
    0, 0, 1, 0, 0,
    0, 0, 1, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0]

half_two = [
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 1, 1, 0, 0,
    1, 0, 0, 0, 0,
    1, 1, 1, 1, 1,
    0, 0, 0, 0, 0]
half_zero = [-1 if x == 0 else x for x in half_zero]
half_one = [-1 if x == 0 else x for x in half_one]
half_two = [-1 if x == 0 else x for x in half_two]

cellular_automaton = np.array([half_two])

hopfield_net = cpl.HopfieldNet(num_cells=35)

hopfield_net.train(P)

cellular_automaton = cpl.evolve(cellular_automaton, timesteps=155,
                                apply_rule=hopfield_net.apply_rule, r=hopfield_net.r)

cpl.plot(hopfield_net.W)
cpl.plot2d_animate(np.reshape(cellular_automaton, (155, 7, 5)))
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')
Esempio n. 7
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)
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="von Neumann")

cpl.plot2d_animate(cellular_automaton, interval=350)
Esempio n. 9
0
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):
		#cellular_automaton[0, row, col] = round(cellular_automaton[0, row, col], 5)
		val_string += str(cellular_automaton[:,0, row, col]) + " "
	print (val_string + "\n")
	val_string = ""

#Prints out state set values
for r in range(ca_rows):
	for c in range(ca_cols):
		if cellular_automaton[1, r, c] == 1.0: