Beispiel #1
0
    
    Each of the 120 nodes represents a body that can contain some amount of heat. Reproduces the plot at the top of 
    Wolfram's NKS, page 163. 
    
    See: https://www.wolframscience.com/nks/p163--partial-differential-equations/
    See: http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html
    """

    space = np.linspace(25, -25, 120)
    initial_conditions = [np.exp(-x**2) for x in space]

    adjacency_matrix = ntm.network.cellular_automaton(120)

    a = 0.25
    dt = .5
    dx = .5
    F = a * dt / dx**2

    def activity_rule(ctx):
        current = ctx.current_activity
        left = ctx.activities[0]
        right = ctx.activities[2]
        return current + F * (right - 2 * current + left)

    activities, _ = ntm.evolve(initial_conditions,
                               adjacency_matrix,
                               activity_rule,
                               timesteps=75)

    ntm.plot_grid(activities)
        HEAD['q6']: {
            CELL[' ']: [HEAD['q6'], CELL[' '], TuringMachine.STAY],
            CELL['a']: [HEAD['q6'], CELL['a'], TuringMachine.STAY],
            CELL['b']: [HEAD['q6'], CELL['b'], TuringMachine.STAY],
            CELL['c']: [HEAD['q6'], CELL['c'], TuringMachine.STAY],
            CELL['x']: [HEAD['q6'], CELL['x'], TuringMachine.STAY],
            CELL['y']: [HEAD['q6'], CELL['y'], TuringMachine.STAY],
            CELL['z']: [HEAD['q6'], CELL['z'], TuringMachine.STAY]
        }
    }

    tape = "  aabbcc  "

    tm = HeadCentricTuringMachine(tape=[CELL[t] for t in tape],
                                  rule_table=rule_table,
                                  initial_head_state=HEAD['q0'],
                                  initial_head_position=2,
                                  terminating_state=HEAD['q6'],
                                  max_timesteps=50)

    activities, _ = ntm.evolve(tm.initial_conditions,
                               tm.adjacency_matrix,
                               activity_rule=tm.activity_rule,
                               input=tm.input_function)

    tape_history, head_activities = tm.activities_for_plotting(activities)

    ntm.plot_grid(tape_history,
                  node_annotations=head_activities,
                  show_grid=True)
                      0, 0, 1, 1]
    initial_conditions = [1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0,
                          0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
                          1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
                          1, 1, 1, 0, 1, 1, 1]

    r = ntm.ReversibleRule(lambda ctx: ntm.rules.nks_ca_rule(ctx, 122))
    activities, _ = ntm.evolve(initial_conditions, adjacency_matrix, timesteps=1002, activity_rule=r.activity_rule,
                               past_conditions=[previous_state])

    timestep = []
    average_node_entropies = []

    for i, c in enumerate(activities):
        timestep.append(i)
        bit_string = ''.join([str(x) for x in c])
        average_node_entropies.append(ntm.average_node_entropy(activities[:i+1]))
        print("%s, %s" % (i, average_node_entropies[-1]))

    plt.subplot(3, 1, (1, 2))
    plt.title("Avg. Node (Shannon) Entropy")
    plt.gca().set_xlim(0, 1002)
    plt.gca().axes.xaxis.set_ticks([])
    plt.plot(timestep, average_node_entropies)

    plt.subplot(3, 1, 3)
    plt.gca().axes.yaxis.set_ticks([])
    ntm.plot_grid(np.array(activities).T.tolist())


    HEAD = {"up": 1, "down": 2}
    CELL = {"on": 1, "off": 0}

    rule_table = {
        HEAD['up']: {
            CELL['on']: [HEAD['up'], CELL['off'], TuringMachine.RIGHT],
            CELL['off']: [HEAD['down'], CELL['on'], TuringMachine.RIGHT]
        },
        HEAD['down']: {
            CELL['on']: [HEAD['up'], CELL['on'], TuringMachine.LEFT],
            CELL['off']: [HEAD['down'], CELL['on'], TuringMachine.LEFT]
        }
    }

    tm = TapeCentricTuringMachine(n=21,
                                  rule_table=rule_table,
                                  initial_head_state=HEAD['up'],
                                  initial_head_position=3)

    initial_conditions = [0] * 21

    activities, _ = ntm.evolve(initial_conditions,
                               tm.adjacency_matrix,
                               activity_rule=tm.activity_rule,
                               timesteps=61)

    ntm.plot_grid(activities,
                  node_annotations=tm.head_activities(activities),
                  show_grid=True)
import netomaton as ntm

if __name__ == '__main__':

    adjacency_matrix = ntm.network.cellular_automaton2d(rows=60,
                                                        cols=60,
                                                        r=1,
                                                        neighbourhood='Moore')

    initial_conditions = ntm.init_simple2d(60, 60)

    activities, _ = ntm.evolve(
        initial_conditions,
        adjacency_matrix,
        timesteps=30,
        activity_rule=lambda ctx: ntm.rules.totalistic_ca(ctx, k=2, rule=126))

    ntm.plot_grid(activities, shape=(60, 60))
import netomaton as ntm

if __name__ == '__main__':
    adjacency_matrix = ntm.network.cellular_automaton(n=21)

    # implements the rule 60 sequential automaton from the NKS Notes on
    #   Chapter 9, section 10: "Sequential cellular automata"
    #   http://www.wolframscience.com/nks/notes-9-10--sequential-cellular-automata/
    initial_conditions = [0] * 10 + [1] + [0] * 10

    r = ntm.AsynchronousRule(
        activity_rule=lambda ctx: ntm.rules.nks_ca_rule(ctx, 60),
        update_order=range(1, 20))

    activities, adjacencies = ntm.evolve(initial_conditions,
                                         adjacency_matrix,
                                         timesteps=19 * 20,
                                         activity_rule=r.activity_rule)

    # plot every 19th row, including the first, as a cycle is completed every 19 rows
    ntm.plot_grid(activities[::19])