Beispiel #1
0
def simulation(size, steps):
    np.random.seed(None)

    grow = ChangeStateRule(from_state=0, to_state=1, p_change=0.0025)
    lightning = ChangeStateRule(
        from_state=1,
        to_state=2,
        p_change=1e-5,
    )
    burn = SlowBurnRule()

    forest = CellularAutomaton(
        shape=size,
        rules=[grow, lightning, burn],
    )
    recorder = AutomataRecorder(automaton=forest)

    forest.start()
    for i in range(steps):
        forest.step()

    palette = np.zeros((256, 3), dtype='uint8')
    palette[1] = [0, 255, 0]
    palette[2] = [255, 0, 0]
    palette[3] = [255, 255, 255]

    save_image_sequence(recorder, 'test.gif', palette, 100)
Beispiel #2
0
def simulation(p_mold, size, steps, transform=None):
    np.random.seed(None)

    grow = ChangeStateRule(from_state=0, to_state=1, p_change=0.0025)
    # lightning = ChangeStateRule(
    #     from_state=1,
    #     to_state=2,
    #     p_change=1e-5,
    # )
    # burn = SlowBurnRule()
    burn_groves = BurnGrovesRule()
    mold = MoldRule(dead_state=3, p_mold=p_mold)
    mold_die = ChangeStateRule(from_state=3, to_state=0, p_change=1.0)
    fire_out = ChangeStateRule(from_state=2, to_state=0, p_change=1.0)

    forest = CellularAutomaton(
        shape=size,
        rules=[mold_die, fire_out, grow, burn_groves, mold],
    )
    recorder = AutomataRecorder(automaton=forest, transform=transform)

    forest.start()
    for i in range(steps):
        forest.step()

    return recorder.as_array()[:, 1:4].T
def simulation(p_mold, size, steps):
    """ Perform a simulation of a moldy forest, returning statistics.

    Parameters
    ----------
    p_mold : probability
        The probability that a crowded tree dies of mold.
    size : size tuple
        The number of cells in each direction for the simulation.
    steps : int
        The number of ticks to run the simulation for.

    Returns
    -------
    counts : array
        Array with shape (4, steps) of counts of each state at
        each tick.
    """
    np.random.seed(None)

    # trees grow
    grow = ChangeStateRule(from_state=EMPTY, to_state=TREE, p_change=0.0025)

    # fires are started, and all connected trees burn
    burn_groves = BurnGrovesRule()

    # crowded trees have a chance to be infected with mold
    mold = MoldRule(dead_state=MOLD, p_mold=p_mold)

    # trees which are infected with mold die
    mold_die = ChangeStateRule(from_state=MOLD, to_state=EMPTY, p_change=1.0)

    # fires are extinguished
    fire_out = ChangeStateRule(from_state=FIRE, to_state=EMPTY, p_change=1.0)

    forest = CellularAutomaton(
        shape=size,
        rules=[mold_die, fire_out, grow, burn_groves, mold],
    )

    # record the number of each state
    recorder = AutomataRecorder(automaton=forest, transform=count_states)

    forest.start()
    for i in range(steps):
        forest.step()

    return recorder.as_array()
def main(rule, size, pattern, boundary, ticks):
    """ Run an elementary 1D cellular automaton. """
    pattern = [(0 if char == ' ' else 1) for char in pattern]

    # setup simulation
    rule = Elementary1DRule(rule_number=rule, boundary=boundary)
    pattern_overlay = PatternOverlay(pattern=pattern)
    world = CellularAutomaton(shape=(size, ),
                              initializers=[pattern_overlay],
                              rules=[rule])

    # initialize and display initial state
    world.start()
    print(automaton_to_text(world))

    # run
    for i in range(ticks):
        world.step()
        print(automaton_to_text(world))
Beispiel #5
0
def simulation(size, steps):
    """ Perform a simulation of a forest fire, outputting a GIF.

    Parameters
    ----------
    size : size tuple
        The number of cells in each direction for the simulation.
    steps : int
        The number of ticks to run the simulation for.
    """
    np.random.seed(None)

    grow = ChangeStateRule(
        from_state=EMPTY,
        to_state=TREE,
        p_change=0.0025
    )
    lightning = ChangeStateRule(
        from_state=TREE,
        to_state=FIRE,
        p_change=1e-5,
    )
    burn = SlowBurnRule()

    forest = CellularAutomaton(
        shape=size,
        rules=[grow, lightning, burn],
    )
    recorder = AutomataRecorder(automaton=forest)

    forest.start()
    for i in range(steps):
        forest.step()

    palette = np.zeros((256, 3), dtype='uint8')
    palette[1] = GREEN
    palette[2] = RED

    save_image_sequence(recorder, 'test.gif', palette, 100)