├── LICENSE ├── README.md ├── Results ├── RULE126.png ├── RULE126_SMALLWORLD.png ├── RULE129.png ├── RULE129_SMALLWORLD.png ├── Variation_RULE109_1.png ├── Variation_RULE109_2.png ├── Variation_RULE109_SMALLWORLD.png ├── Variation_RULE129.png ├── Variation_RULE129_2.png └── Variation_RULE129_SMALLWORLD.png ├── SmallWorldCA.py └── main.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Aditya Milind Deshpande 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SmallWorldNetCA 2 | 3 | **1D Cellular Automata (CA) for any rule in the range of 0 to 255 can be drawn using this code.** 4 | 5 | ### Prameters 6 | * `p` - probability of CA-world to follow given CA rule 7 | * `q` - probability of choosing **_on_** or **_off_** state of the cell randomly when `p>0` 8 | * `f` - fraction of cells in CA-world which will choose neighbors randomly 9 | * `config` - _random_ or _uniform_ 10 | 11 | ## Along with well known 1d cellular automata rules, there are implementations of few interesting variations described below 12 | 13 | ### Introduction of NOISE in 1D Cellular Automata 14 | One can analyze the effect of noise in the CA-world. It is interesting to observe how a simple rule starting in a uniform CA-world can also result in a chaotic behavior with beautiful patterns. 15 | 16 | I have shown **Rule 129** and **Rule 161** as an example. Both the rules show same fractal pattern of **_Sierpinski triangle_** but both result in completely different result when noise is inserted. 17 | 18 | ### Small World Network 19 | For this to be implemented, a small change in neighborhood of CA-cell in each rule is made. The fraction `f` of cells are selected randomly and neighbors are assigned randomly to these selected cells. Based on neighborhood changes, if one uses a deterministic CA rule (without any noise `p=0`), the results show variation of patterns. Even with small value of `f`, huge changes can be observed. 20 | 21 | # Sample Results 22 | ![RULE 126](Results/RULE126.png) 23 | ![RULE 126 - SMALL WORLD NETWORK](Results/RULE126_SMALLWORLD.png) 24 | 25 | ![RULE 129](Results/RULE129.png) 26 | ![RULE 129 - SMALL WORLD NETWORK](Results/RULE126_SMALLWORLD.png) 27 | 28 | ![RULE 129 - PARAMETER VARIATIONS](Results/Variation_RULE129.png) 29 | ![RULE 129 - PARAMETER VARIATIONS](Results/Variation_RULE129_2.png) 30 | ![RULE 129 SMALL WORLD NETWORK - PARAMETER VARIATIONS](Results/Variation_RULE129_SMALLWORLD.png) 31 | 32 | ![RULE 109 - PARAMETER VARIATIONS](Results/Variation_RULE109_1.png) 33 | ![RULE 109 - PARAMETER VARIATIONS](Results/Variation_RULE109_2.png) 34 | ![RULE 109 SMALL WORLD NETWORK - PARAMETER VARIATIONS](Results/Variation_RULE109_SMALLWORLD.png) 35 | -------------------------------------------------------------------------------- /Results/RULE126.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/RULE126.png -------------------------------------------------------------------------------- /Results/RULE126_SMALLWORLD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/RULE126_SMALLWORLD.png -------------------------------------------------------------------------------- /Results/RULE129.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/RULE129.png -------------------------------------------------------------------------------- /Results/RULE129_SMALLWORLD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/RULE129_SMALLWORLD.png -------------------------------------------------------------------------------- /Results/Variation_RULE109_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/Variation_RULE109_1.png -------------------------------------------------------------------------------- /Results/Variation_RULE109_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/Variation_RULE109_2.png -------------------------------------------------------------------------------- /Results/Variation_RULE109_SMALLWORLD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/Variation_RULE109_SMALLWORLD.png -------------------------------------------------------------------------------- /Results/Variation_RULE129.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/Variation_RULE129.png -------------------------------------------------------------------------------- /Results/Variation_RULE129_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/Variation_RULE129_2.png -------------------------------------------------------------------------------- /Results/Variation_RULE129_SMALLWORLD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adipandas/SmallWorldNetCA/dca03cf87a06539f236a9d301298a86d4d4ec6b1/Results/Variation_RULE129_SMALLWORLD.png -------------------------------------------------------------------------------- /SmallWorldCA.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Feb 3, 2018 3 | 4 | @author: aditya 5 | 6 | This class can be used to generate spatio temporal patterns of 1D cellular automata 7 | ''' 8 | import numpy as np 9 | class CA_SmallWorld: 10 | def __init__(self, rule, p = 1, q = 0.1, world_size = 1080, lifespan = 1920, config = 'regular'): 11 | self.rule = np.array([int(i) for i in '{:08b}'.format(rule)]) 12 | self.height_img = world_size 13 | self.width_img = lifespan 14 | self.X = np.zeros((world_size, lifespan), dtype = int) 15 | self.gates = np.zeros(world_size, dtype=int) 16 | self.p = p 17 | self.q = q 18 | 19 | if config not in ['regular', 'random']: 20 | raise ValueError('Configurations not present in available options:[\'regular\', \'random\', \'smallworld\']') 21 | self.config = config 22 | 23 | # neighbors for periodic boundary conditions 24 | self.left = [world_size-1]+[i for i in range(world_size-1)] 25 | self.right = [i for i in range(1, world_size)]+[0] 26 | 27 | self.setup() 28 | 29 | def resetworld(self): 30 | self.X = np.zeros((self.height_img, self.width_img), dtype = int) 31 | self.setup() 32 | 33 | def setup(self): 34 | if self.config=='regular': 35 | start_life = [i for i in range(int(self.height_img/2-len(self.rule)/2),int(self.height_img/2+len(self.rule)/2))] 36 | self.X[start_life,0]=1 37 | else: 38 | p = np.random.rand(self.height_img) 39 | self.X[p>0.5,0] = 1 40 | 41 | def simulation(self): 42 | # This method takes care of cellular automata with given rule and randomness 43 | # Main Logic 44 | for t in range(self.width_img): 45 | world = self.X[:, t] 46 | 47 | p_t, q_t = np.random.rand(self.height_img), np.random.rand(self.height_img) 48 | 49 | self.gates[p_t<=self.p] = 1 50 | 51 | world_future_1 = 4*world[self.left] + 2*world + world[self.right] 52 | world_future_1 = self.rule[world_future_1.astype(int)] 53 | world_future_1 = np.multiply(world_future_1, self.gates) 54 | 55 | world_future_2 = np.zeros(self.height_img, dtype=int) 56 | world_future_2[q_t<=self.q]=1 57 | world_future_2 = np.multiply(world_future_2, np.logical_not(self.gates)) 58 | 59 | world = world_future_1 + world_future_2 60 | 61 | if t+1>=self.width_img: break 62 | 63 | self.X[:,t+1] = world 64 | 65 | image = np.multiply(self.X,255) 66 | image = image.astype(np.uint8) 67 | 68 | self.resetworld() 69 | return image 70 | 71 | def smallworld(self, f = 0.1): 72 | # This method takes care of small world simulation 73 | left, right = self.setneighbor_smallworld(f) 74 | 75 | # Main Logic 76 | for t in range(self.width_img): 77 | world = self.X[:, t] 78 | 79 | p_t, q_t = np.random.rand(self.height_img), np.random.rand(self.height_img) 80 | 81 | self.gates[p_t<=self.p] = 1 82 | 83 | world_future_1 = 4*world[left] + 2*world + world[right] 84 | world_future_1 = self.rule[world_future_1.astype(int)] 85 | world_future_1 = np.multiply(world_future_1, self.gates) 86 | 87 | world_future_2 = np.zeros(self.height_img, dtype=int) 88 | world_future_2[q_t<=self.q]=1 89 | world_future_2 = np.multiply(world_future_2, np.logical_not(self.gates)) 90 | 91 | world = world_future_1 + world_future_2 92 | 93 | if t+1>=self.width_img: break 94 | 95 | self.X[:,t+1] = world 96 | 97 | image = np.multiply(self.X,255) 98 | image = image.astype(np.uint8) 99 | 100 | self.resetworld() 101 | return image 102 | 103 | def setneighbor_smallworld(self, f): 104 | # randomizing the neighborhood for small world 105 | left = self.left 106 | right = self.right 107 | for i in range(self.height_img): 108 | rand = np.random.rand(2) 109 | if rand[0]0.5: 112 | right[i] = np.random.randint(self.height_img) 113 | return left, right 114 | 115 | if __name__=='__main__': 116 | 117 | import matplotlib.pyplot as plt 118 | RULE = 129 119 | CA = CA_SmallWorld(RULE) 120 | 121 | image1 = CA.simulation() 122 | im = plt.imshow(image1) 123 | plt.xticks([]), plt.yticks([]) 124 | plt.title('Rule {}'.format(RULE)) 125 | plt.show() 126 | 127 | image2 = CA.smallworld() 128 | im = plt.imshow(image2) 129 | plt.xticks([]), plt.yticks([]) 130 | plt.title('Small World Network - Rule {}'.format(RULE)) 131 | plt.show() 132 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Feb 3, 2018 3 | 4 | @author: aditya 5 | ''' 6 | 7 | from SmallWorldCA import CA_SmallWorld as CAS 8 | import matplotlib.pyplot as plt 9 | 10 | if __name__=='__main__': 11 | RULE = 129 12 | subplot_h, subplot_v = 3, 2 13 | 14 | p = [1, 0.999, 0.99, 0.9, 0.5, 0] 15 | q = [0.5, 0.1] 16 | for j in range(len(q)): # col 17 | fig = plt.figure() 18 | fig.suptitle("RULE {}".format(RULE), fontsize="x-large") 19 | for i in range(len(p)): # row 20 | CA = CAS(RULE, p[i], q[j], world_size=400, lifespan=400) 21 | image1 = CA.simulation() 22 | plt.subplot(subplot_h, subplot_v, i+1) 23 | plt.imshow(image1) 24 | plt.xticks([]), plt.yticks([]) 25 | plt.title('p = {0}, q = {1}'.format(p[i], q[j]) ) 26 | plt.tight_layout() 27 | plt.show() 28 | 29 | f = [0 , 0.001, 0.01, 0.1, 0.5, 1] 30 | fig = plt.figure() 31 | fig.suptitle("SMALL WORLD NETWORK - RULE {}".format(RULE), fontsize="x-large") 32 | for j in range(len(f)): 33 | CA = CAS(RULE, world_size=400, lifespan=400) 34 | image2 = CA.smallworld(f[j]) 35 | plt.subplot(subplot_h, subplot_v, j+1) 36 | im = plt.imshow(image2) 37 | plt.xticks([]), plt.yticks([]) 38 | plt.title('p = {0}, f = {1}'.format(1, f[j]) ) 39 | plt.tight_layout() 40 | plt.show() 41 | --------------------------------------------------------------------------------