Example #1
0
 def test_init_random_dtype(self):
     arr = cpl.init_random(3, dtype=np.float32)
     self.assertEqual(len(arr), 1)
     self.assertEqual(len(arr[0]), 3)
     self.assertTrue(0.0 <= arr[0][0] < 1.0)
     self.assertTrue(0.0 <= arr[0][1] < 1.0)
     self.assertTrue(0.0 <= arr[0][2] < 1.0)
Example #2
0
 def test_init_random_3_k3_n2(self):
     arr = cpl.init_random(3, k=3, n_randomized=2, empty_value=9)
     self.assertEqual(len(arr), 1)
     self.assertEqual(len(arr[0]), 3)
     self.assertTrue(0 <= arr[0][0] <= 2)
     self.assertTrue(0 <= arr[0][1] <= 2)
     self.assertEqual(arr[0][2], 9)
Example #3
0
 def test_init_random_3_k3_n0(self):
     arr = cpl.init_random(3, k=3, n_randomized=0, empty_value=9)
     self.assertEqual(len(arr), 1)
     self.assertEqual(len(arr[0]), 3)
     self.assertEqual(arr[0][0], 9)
     self.assertEqual(arr[0][1], 9)
     self.assertEqual(arr[0][2], 9)
Example #4
0
 def test_init_random_3(self):
     arr = cpl.init_random(3, empty_value=9)
     self.assertEqual(len(arr), 1)
     self.assertEqual(len(arr[0]), 3)
     self.assertTrue(0 <= arr[0][0] <= 1)
     self.assertTrue(0 <= arr[0][1] <= 1)
     self.assertTrue(0 <= arr[0][2] <= 1)
# In[ ]:

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
Example #6
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)
Example #7
0
 def test_init_random_1_k3_n0(self):
     arr = cpl.init_random(1, k=3, n_randomized=0, empty_value=9)
     self.assertEqual(len(arr), 1)
     arr = arr[0]
     self.assertEqual(len(arr), 1)
     self.assertEqual(arr[0], 9)
Example #8
0
 def test_init_random_1_k3_n1(self):
     arr = cpl.init_random(1, k=3, n_randomized=1, empty_value=9)
     self.assertEqual(len(arr), 1)
     self.assertEqual(len(arr[0]), 1)
     self.assertTrue(0 <= arr[0][0] <= 2)
Example #9
0
import cellpylib as cpl

rule_table, actual_lambda, quiescent_state = cpl.random_rule_table(
    lambda_val=0.37, k=4, r=2, strong_quiescence=True, isotropic=True)

# cellular_automaton = cpl.init_simple(128, val=1)
cellular_automaton = cpl.init_random(128, k=4, n_randomized=20)

# evolve the cellular automaton for 200 time steps
cellular_automaton = cpl.evolve(
    cellular_automaton,
    timesteps=200,
    apply_rule=lambda n, c, t: cpl.table_rule(n, rule_table),
    r=2)

cpl.plot(cellular_automaton)
Example #10
0
# 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)