-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellular_Automaton_color.py
153 lines (117 loc) · 3.42 KB
/
Cellular_Automaton_color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 5 10:36:18 2016
@author: zhoulinn
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Cellular Automaton trial 1
with rule
Created on Fri Dec 2 18:25:55 2016
@author: zhoulinn
"""
import numpy as np
import matplotlib.pyplot as plt
import cv2
import time
import random
import matplotlib.animation as animation
import sys
rows = 50
columns = 50
board = np.zeros((rows,columns))
#board = np.zeros((10,10))
choice = input ('2 random starting point? [Y/N]')
if (choice == 'Y'):
#start with two random points on board
x1 = np.random.randint(0,rows-1)
y1 = np.random.randint(0,columns-1)
x2 = np.random.randint(0,rows-1)
y2 = np.random.randint(0,columns-1)
elif (choice == 'N'):
x1 = int (input ('first x (integer 0 - ' + str(rows-1) + '):'))
y1 = int (input ('first y (integer 0 - ' + str(columns-1) + '):'))
x2 = int (input ('second x (integer 0 - ' + str(rows-1) + '):'))
y2 = int (input ('second y (integer 0 - ' + str(columns-1) + '):'))
else:
print ('error input!')
board [x1][y1] = 1
board [x2][y2] = 1
#cycles = 20
cycles = input ('And how many cycles would u like to see? (default = 30)')
if (cycles == ''):
cycles = 30
else:
pass
cycles = int (cycles)
def rule2d (left, right, up, down):
if (left + right + up + down == 0):
return 0
elif (left + right + up + down == 1):
return 1
elif (left + right + up + down == 2):
return 0
elif (left + right + up + down == 3):
return 0
elif (left + right + up + down == 4):
return 0
else:
return 1
#print ('hey there s sth wrong with the code..')
# print the board according to values
count = 1
fig = plt.figure()
User = input('Enter to start the program!')
while (count < cycles):
for i in range (rows-1):
for j in range (columns-1):
left = board [i-1][j]
right = board [i+1][j]
up = board [i][j-1]
down = board [i][j+1]
middle = board [i][j]
board[i][j] += rule2d (left, right, up, down)
if (board[i][j] >= 10):
board[i][j] -= 13
else:
pass
#board[i][j][color] = eRule126 (left, middle, right)
color = random.randint(0,2)
print (count)
#plt.matshow(board)
img = plt.matshow(board, cmap='spectral')
#img = plt.matshow(count * board, cmap='spectral')
plt.colorbar(img, orientation='vertical')
img.set_clim(vmin=0, vmax=10)
if (count < 10):
plt.savefig('images/img00'+str(count)+'.jpg')
elif (10 <= count < 100):
plt.savefig('images/img0'+str(count)+'.jpg')
elif (100 <= count < 1000):
plt.savefig('images/img'+str(count)+'.jpg')
else:
pass
plt.show()
time.sleep(0.05) # delays for 0.5 seconds
count += 1
# let user to exit the program
#if User == 'e':
#break
'''
ani = animation.FuncAnimation(fig,300,interval=30)
writer = animation.writers['ffmpeg'](fps=30)
ani.save('demo.mp4',writer=writer,dpi=dpi)
#return ani
#mencoder
'''
'''
problems (solved):
1. displaying array with color: RGB --> spectrum & setting limits
2. input datatype: str --> int
3.
(unsolved)
1. more realistic rules (e.g. considering previous events)
2. export as video
'''