-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGOL_Lattice.py
395 lines (272 loc) · 9.26 KB
/
GOL_Lattice.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import random
import numpy as np
import matplotlib.pyplot as plt
class GOL_Lattice(object):
"""
Class for a Game of Life Lattice object
"""
def __init__(self, N, dynamics = "GOL", lattice = None):
"""
Initialisation function for Lattice class.
Parameters
----------
N : int
determines size of square lattce (NxN)
dynamics : str
Dynamics to use within the simulation, options are "GOL"
lattice : numpy array or str
An option to input starting lattice. Must be of size (NxN).
Can also input string with options: "uniform" "blinker" "glider" (see functions for specifics)
Returns
-------
"""
#initialise variables
self.N = N
self.num_activ_sites = []
self.sweep_list = []
#N**2 is the required length of time between visualisations as in the lecture notes
#self.sweep_size = self.N**2
#this isnt necessary anymore, but no harm
if dynamics == "GOL":
self.dynamics = self.game_of_life
else:
print("here becaus eyou havent done this yet")
#set innactive stop (for if the sim should stop if innactive)
#set lattice
self.glider_meas = False
if type(lattice) == np.ndarray:#'numpy.ndarray':
if lattice.shape == (N,N):
#print("yeye")
self.lattice = lattice
self.innactive_stop = True
else:
print("input lattice must be correct shape!!")
elif lattice is None:
#assume uniform
self.innactive_stop = True
self.lattice = self.uniform_generate()
elif lattice == "uniform":
self.innactive_stop = True
self.lattice = self.uniform_generate()
elif lattice == "glider":
self.innactive_stop = False
self.glider_meas = True
self.lattice = self.glider_generate()
elif lattice == "blinker":
self.innactive_stop = False
self.lattice = self.blinker_generate()
else:
print("your lattice input is wrong")
#vectorise update function
self.vec_update_point_gol = np.vectorize(self.update_point_gol)
def blinker_generate(self):
"""
Generates "blinker" lattice, where the lattice has been layed out speciffically to show one blinker
Parameters
----------
Returns
-------
"""
blinker_lattice = np.zeros((self.N,self.N))
if self.N<3:
print("lattice is too small")
blinker_lattice[0,1] = 1
blinker_lattice[1,1] = 1
blinker_lattice[2,1] = 1
return blinker_lattice
def glider_generate(self):
"""
Generates "blinker" lattice, where the lattice has been layed out specifically to show one glider,
starting at the origin and moving diagonally
Parameters
----------
Returns
-------
"""
glider_lattice = np.zeros((self.N, self.N))
if self.N < 3:
print("lattice is too small")
glider_lattice[0, 1] = 1
glider_lattice[1, 2] = 1
glider_lattice[2, 1] = 1
glider_lattice[2, 0] = 1
glider_lattice[2, 2] = 1
return glider_lattice
def game_of_life(self):
"""
GOL dynamics function
Parameters
----------
Returns
-------
"""
#assumes dead = 0 and alive = 1
#sums nearest enighbours of each point
NN_sum = self.find_sum_NN()
#updates according to NN
new_lattice = self.update_gol(NN_sum)
#finds numbe rof active sites (for possible termination)
self.num_activ_sites.append(self.N**2-np.sum(np.isclose(self.lattice,new_lattice)))
#update lattice
self.lattice = new_lattice
def update_gol(self, NN_sum):
"""
updates according to GOL rules
Parameters
----------
NN_sum : array
lattice of NN neighbour sums
Returns
-------
"""
next_step = self.vec_update_point_gol(self.lattice, NN_sum)
return next_step
def update_point_gol(self, bef, sums):
"""
lays out update rules will be vectorized
Parameters
----------
bef: int
alive or dead (binary)
sum: int
numbe rof NN
Returns
-------
"""
if bef == 0:
#cell is dead
if sums == 3:
#becomes alive
aft = 1
else:
#stays dead
aft = 0
elif bef ==1:
if sums <2:
#dies
aft = 0
elif sums >3:
#dies
aft = 0
elif sums ==2 or sums == 3:
#stays alive
aft = 1
else:
print("you have fucked something else up")
else:
print("you have fucked something up")
return aft
def find_sum_NN(self):
"""
finds how many nearest neightbours for the entire lattice
Parameters
----------
Returns
-------
"""
#zero array
NN_sum = np.zeros_like(self.lattice)
#use rolls to find NN sums
NN_sum += np.roll(self.lattice, 1, axis=0)
NN_sum += np.roll(self.lattice, -1, axis=0)
NN_sum += np.roll(self.lattice, 1, axis=1)
NN_sum += np.roll(self.lattice, -1, axis=1)
NN_sum += np.roll(self.lattice, (1, 1), axis=(0,1))
NN_sum += np.roll(self.lattice, (1, -1), axis=(0, 1))
NN_sum += np.roll(self.lattice, (-1, 1), axis=(0, 1))
NN_sum += np.roll(self.lattice, (-1, -1), axis=(0, 1))
return NN_sum
def uniform_generate(self):
"""
Generates "uniform" lattice, where each point on the lattice has equal probability of being -1 or 1
Parameters
----------
Returns
-------
a : numpy array
uniform (NxN) array
"""
a = np.random.choice((1,0), size=[self.N,self.N], replace=True, p=[0.5,0.5])
return a
def ground_generate(self):
"""
Generates "ground" lattice, where every point on the lattice is set to 1,
not used, or properly coded at th moment
Parameters
----------
Returns
-------
a : numpy array
uniform (NxN) array
"""
#a = np.rint(np.random.uniform(0,1,(self.N,self.N))).astype(int)
#a = np.where(a == 1,a,-1)
a = np.ones((self.N,self.N))
return a
def find_glider_pos(self):
#this is extremely bad coding
pos_list = []
for i in range(self.N):
for j in range(self.N):
if self.lattice[i,j] == 1:
if i%self.N >1 and j%self.N >1:
pos_list.append(np.sqrt(i**2+j**2))
else:
#if boundary condition
return None
r_cm = np.sum(pos_list)/len(pos_list)
return r_cm
def plot_lattice(self):
"""
Function to plot the lattice
This function uses pyplot not gnuplot or other.
It does not write to file to simplify the procedure.
It plots directly from self.lattice.
Parameters
----------
Returns
-------
"""
plt.cla()
plt.imshow(self.lattice, animated=True)
plt.draw()
plt.pause(0.00001)
def run(self,wait_sweeps = 100, num_tot_sweeps = 1000, plot_anim = True):
"""
Function to run GOL simulation
Parameters
----------
wait_sweeps : int
number of sweeps to wait before starting measurements
num_total_sweeps : int
number of total sweeps to run
plot_anim : bool
whether or not to animate as it runs. Saves time on long simulations to not animate.
Returns
-------
"""
if self.glider_meas:
self.glider_pos = []
#loop for required number of sweeps
for i in range(num_tot_sweeps):
#print(i)
#if it is time to take a measurement
if i%1 == 0 and i >= wait_sweeps:
self.sweep_list.append(i)
#plot animation if required
if plot_anim:
#print(i)
self.plot_lattice()
if self.glider_meas:
self.glider_pos.append(self.find_glider_pos())
#print every 100 sweeps to check the sim progress
if i%100 == 0:
pass
print(i)
#run dynamics
self.dynamics()
if len(self.sweep_list)>3:
if self.innactive_stop and len(self.num_activ_sites)>10:
if np.all(np.isclose(self.num_activ_sites[-9:], self.num_activ_sites[-10])):
print("its done now so stop!!")
return