def test_binary_rule_powers_of_two_default(self):
        rule_number = 6667021275756174439087127638698866559
        radius = 3
        timesteps = 49
        init = np.array([[
            1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0,
            0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0,
            0, 1, 1, 1, 1
        ]])

        expected = cpl.evolve(
            init,
            timesteps=timesteps,
            apply_rule=lambda n, c, t: cpl.binary_rule(n, rule_number),
            r=radius)

        powers_of_two = 2**np.arange(radius * 2 + 1)[::-1]
        rule = list(map(int, bin(rule_number)[2:]))
        rule_bin_array = np.pad(rule, ((2**(radius * 2 + 1)) - len(rule), 0),
                                'constant')
        actual = cpl.evolve(
            init,
            timesteps=timesteps,
            apply_rule=lambda n, c, t: cpl.binary_rule(
                n, rule_bin_array, powers_of_two=powers_of_two),
            r=radius)

        np.testing.assert_equal(expected.tolist(), actual.tolist())
    def test_binary_rule(self):
        rule_number = 6667021275756174439087127638698866559
        radius = 3
        timesteps = 12
        init = np.array(
            [[1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1]])

        actual = cpl.evolve(
            init,
            timesteps=timesteps,
            apply_rule=lambda n, c, t: cpl.binary_rule(n, rule_number),
            r=radius)

        expected = np.array(
            [[1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1],
             [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1],
             [1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1],
             [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0],
             [0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1],
             [1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0],
             [0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1],
             [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0],
             [0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
             [1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
             [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
             [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
        np.testing.assert_equal(expected.tolist(), actual.tolist())
 def _create_totalistic_ca(self, expected, k, rule):
     rows, _ = expected.shape
     cellular_automaton = expected[0]
     return cpl.evolve(
         cellular_automaton,
         timesteps=rows,
         apply_rule=lambda n, c, t: cpl.totalistic_rule(n, k, rule))
 def _create_reversible_ca(self, expected, rule):
     rows, _ = expected.shape
     cellular_automaton = expected[0]
     r = cpl.ReversibleRule(cellular_automaton.tolist()[0], rule)
     return cpl.evolve(cellular_automaton,
                       timesteps=rows,
                       apply_rule=r.apply_rule)
    def test_evolve_apply_rule_3_steps(self):
        cellular_automaton = np.array([[1, 2, 3, 4, 5]])
        neighbourhoods = []
        cell_identities = []
        timesteps = []

        def apply_rule(n, c, t):
            neighbourhoods.append(n.tolist())
            cell_identities.append(c)
            timesteps.append(t)
            return n[1]

        cpl.evolve(cellular_automaton, timesteps=3, apply_rule=apply_rule)
        np.testing.assert_equal(
            neighbourhoods,
            [[5, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 1], [5, 1, 2],
             [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 1]])
        np.testing.assert_equal(cell_identities,
                                [0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
        np.testing.assert_equal(timesteps, [1, 1, 1, 1, 1, 2, 2, 2, 2, 2])
 def test_sequential_left_to_right(self):
     expected = self._convert_to_numpy_matrix(
         "rule60_sequential_simple_init.ca")
     cellular_automaton = cpl.init_simple(21)
     r = cpl.AsynchronousRule(
         apply_rule=lambda n, c, t: cpl.nks_rule(n, 60),
         update_order=range(1, 20))
     cellular_automaton = cpl.evolve(cellular_automaton,
                                     timesteps=19 * 20,
                                     apply_rule=r.apply_rule)
     np.testing.assert_equal(expected.tolist(),
                             cellular_automaton[::19].tolist())
    def test_binary_rule_powers_of_two_nks(self):
        rule_number = 30
        radius = 1
        size = 149
        timesteps = 149

        expected = cpl.evolve(cpl.init_simple(size=size),
                              timesteps=timesteps,
                              apply_rule=lambda n, c, t: cpl.binary_rule(
                                  n, rule_number, scheme="nks"),
                              r=radius)

        powers_of_two = 2**np.arange(radius * 2 + 1)[::-1]
        rule = list(map(int, bin(rule_number)[2:]))
        rule_bin_array = np.pad(rule, ((2**(radius * 2 + 1)) - len(rule), 0),
                                'constant').tolist()
        actual = cpl.evolve(
            cpl.init_simple(size=size),
            timesteps=timesteps,
            apply_rule=lambda n, c, t: cpl.binary_rule(
                n, rule_bin_array, scheme="nks", powers_of_two=powers_of_two),
            r=radius)

        np.testing.assert_equal(expected.tolist(), actual.tolist())
 def test_sequential_random(self):
     expected = self._convert_to_numpy_matrix(
         "rule90_sequential_simple_init.ca")
     cellular_automaton = cpl.init_simple(21)
     update_order = [
         19, 11, 4, 9, 6, 16, 10, 2, 17, 1, 12, 15, 5, 3, 8, 18, 7, 13, 14
     ]
     r = cpl.AsynchronousRule(
         apply_rule=lambda n, c, t: cpl.nks_rule(n, 90),
         update_order=update_order)
     cellular_automaton = cpl.evolve(cellular_automaton,
                                     timesteps=19 * 20,
                                     apply_rule=r.apply_rule)
     np.testing.assert_equal(expected.tolist(),
                             cellular_automaton[::19].tolist())
 def test_dtype(self):
     cellular_automaton = cpl.init_simple(11, dtype=np.float32)
     cellular_automaton = cpl.evolve(
         cellular_automaton,
         timesteps=6,
         apply_rule=lambda n, c, t: sum(n) / len(n))
     np.testing.assert_almost_equal(
         cellular_automaton.tolist(),
         [[0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
          [0.0, 0.0, 0.0, 0.0, 0.333, 0.333, 0.333, 0.0, 0.0, 0.0, 0.0],
          [0.0, 0.0, 0.0, 0.111, 0.222, 0.333, 0.222, 0.111, 0.0, 0.0, 0.0],
          [
              0.0, 0.0, 0.037, 0.111, 0.222, 0.259, 0.222, 0.111, 0.037,
              0.0, 0.0
          ],
          [
              0.0, 0.012, 0.049, 0.123, 0.198, 0.235, 0.198, 0.123, 0.049,
              0.0123, 0.0
          ],
          [
              0.004, 0.021, 0.062, 0.123, 0.185, 0.210, 0.185, 0.123, 0.062,
              0.021, 0.004
          ]],
         decimal=3)
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)
Exemple #11
0
import cellpylib as cpl

cellular_automaton = cpl.init_simple(200)
# cellular_automaton = cpl.init_random(200)

# evolve the cellular automaton for 100 time steps
cellular_automaton = cpl.evolve(cellular_automaton,
                                timesteps=100,
                                memoize=True,
                                apply_rule=lambda n, c, t: cpl.nks_rule(n, 30))

cpl.plot(cellular_automaton)
Exemple #12
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 time

start = time.time()
cpl.evolve(cpl.init_simple(1000),
           timesteps=500,
           apply_rule=lambda n, c, t: cpl.nks_rule(n, 30),
           memoize=True)

print(f"Elapsed: {time.time() - start:.2f} seconds")
import cellpylib as cpl

# 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/
cellular_automaton = cpl.init_simple(21)

apply_rule = cpl.AsynchronousRule(apply_rule=lambda n, c, t: cpl.nks_rule(n, 60), update_order=range(1, 20))

cellular_automaton = cpl.evolve(cellular_automaton, timesteps=19*20, apply_rule=apply_rule)

# get every 19th row, including the first, as a cycle is completed every 19 rows
cpl.plot(cellular_automaton[::19])
Exemple #15
0
import cellpylib as cpl
import numpy as np

initial = np.array([[17]], dtype=np.int)


def activity_rule(n, c, t):
    n = n[1]
    if n % 2 == 0:
        # number is even
        return n / 2
    else:
        return 3 * n + 1


cellular_automaton = cpl.evolve(initial,
                                apply_rule=activity_rule,
                                timesteps=lambda ca, t: True
                                if ca[-1][0] != 1 else False)

print([i[0] for i in cellular_automaton])
Exemple #16
0
import cellpylib as cpl
import numpy as np

cellular_automaton = cpl.init_random(149)

print("density of 1s: %s" % (np.count_nonzero(cellular_automaton) / 149))

# M. Mitchell et al. discovered this rule using a Genetic Algorithm
rule_number = 6667021275756174439087127638698866559

cellular_automaton = cpl.evolve(cellular_automaton, timesteps=149,
                                apply_rule=lambda n, c, t: cpl.binary_rule(n, rule_number), r=3)

cpl.plot(cellular_automaton)
# Ajustado especificamente para a regra 90 e sua reversão

# plot the resulting CA evolution
try:
  import cellpylib as cpl
except:
  !pip install cellpylib
  import cellpylib as cpl
 
from cellpylib import *

#import cellpylib as cpl

# initialize a CA with 200 cells (a random initialization is also available) 
cellular_automaton = cpl.init_simple(200)

# evolve the CA for 100 time steps, using Rule 30 as defined in NKS
cellular_automaton = cpl.evolve(cellular_automaton, timesteps=100, 
                                apply_rule=lambda n, c, t: cpl.nks_rule(n, 90))

# plot the resulting CA evolution
cpl.plot(cellular_automaton)

cellular_automaton = cpl.init_random(200)
r = cpl.ReversibleRule(cellular_automaton[0], 90)

cellular_automaton = cpl.evolve(cellular_automaton, timesteps=100, 
                                apply_rule=r.apply_rule)

# plot the resulting reverse CA evolution
cpl.plot(cellular_automaton)
Exemple #18
0
import matplotlib.pyplot as plt
import numpy as np

import cellpylib as cpl

# NKS page 442 - Rule 122R
cellular_automaton = np.array([[0] * 40 + [1] * 20 + [0] * 40])
r = cpl.ReversibleRule(cellular_automaton[0], 122)
cellular_automaton = cpl.evolve(cellular_automaton,
                                timesteps=1000,
                                apply_rule=r.apply_rule)

timestep = []
bientropies = []
shannon_entropies = []
average_cell_entropies = []
apentropies = []
for i, c in enumerate(cellular_automaton):
    timestep.append(i)
    bit_string = ''.join([str(x) for x in c])
    bientropies.append(cpl.ktbien(bit_string))
    shannon_entropies.append(cpl.shannon_entropy(bit_string))
    average_cell_entropies.append(
        cpl.average_cell_entropy(cellular_automaton[:i + 1]))
    apentropies.append(cpl.apen(bit_string, m=1, r=0))
    print("%s, %s, %s, %s" %
          (i, bientropies[-1], shannon_entropies[-1], apentropies[-1]))

plt.figure(1)
plt.title("KTBiEn")
plt.plot(timestep, bientropies)
Exemple #19
0
import numpy as np

import cellpylib as cpl

# NKS page 437 - Rule 214R

# run the CA forward for 32 steps to get the initial condition for the next evolution
cellular_automaton = cpl.init_simple(63)
r = cpl.ReversibleRule(cellular_automaton[0], 214)
cellular_automaton = cpl.evolve(cellular_automaton, timesteps=32,
                                apply_rule=r.apply_rule)

# use the last state of the CA as the initial, previous state for this evolution
r = cpl.ReversibleRule(cellular_automaton[-1], 214)
cellular_automaton = np.array([cellular_automaton[-2]])
cellular_automaton = cpl.evolve(cellular_automaton, timesteps=62,
                                apply_rule=r.apply_rule)

cpl.plot(cellular_automaton)
 def test_evolve_apply_rule_1_step(self):
     cellular_automaton = np.array([[1, 2, 3, 4, 5]])
     cellular_automaton = cpl.evolve(cellular_automaton,
                                     timesteps=1,
                                     apply_rule=lambda n, c, t: 1)
     np.testing.assert_equal(cellular_automaton.tolist(), [[1, 2, 3, 4, 5]])
for carule in range(5000):
    print(carule)
    k = 2 + np.random.randint(6)
    rn = np.random.randint(int(k**(3 * k - 2)))

    f = open("data5k/%.06d/rule.txt" % carule, "w")
    # k, r, type, rule
    # type: 0 = General, 1 = Totalistic
    f.write("%d 1 1 %d" % (k, rn))
    f.close()

    for i in range(50):
        ca = cpl.init_random(256)
        ca = cpl.evolve(
            ca,
            timesteps=256,
            apply_rule=lambda n, c, t: cpl.totalistic_rule(n, k=k, rule=rn),
            r=1)
        im = render(ca, k)
        im.save("data5k/%.06d/%.06d.png" % (carule, i))

# In[16]:

counts = [np.sum(ca == i) for i in range(k)]

# In[17]:

counts

# In[ ]:
import cellpylib as cpl

cellular_automaton = cpl.init_simple(200)
# cellular_automaton = cpl.init_random(200, k=3)

# evolve the cellular automaton for 100 time steps
cellular_automaton = cpl.evolve(
    cellular_automaton,
    timesteps=100,
    apply_rule=lambda n, c, t: cpl.totalistic_rule(n, k=3, rule=777))

cpl.plot(cellular_automaton)