====== Temporal LIFE ======
# https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
# 1. Any live cell with fewer than two live neighbours dies,
# as if caused by underpopulation.
# 2. Any live cell with two or three live neighbours lives on
# to the next generation.
# 3. Any live cell with more than three live neighbours dies,
# as if by overpopulation.
# 4. Any dead cell with exactly three live neighbours becomes
# a live cell, as if by reproduction.
from Nets import Network
def makeLifeNet(n,m):
L = Network()
L._nodes = { (r+1,c+1): [{},{},{},{'active': False,'s':[]}] \
for r in range(n) for c in range(m) }
for r in range(1,n):
for c in range(1,m):
id = L.addEdge((r,c),(r,c+1)); id = L.addEdge((r,c),(r+1,c))
id = L.addEdge((r,c),(r+1,c+1)); id = L.addEdge((r+1,c),(r,c+1))
return L
def setActive(L,A):
for v in A: L.setNode(v,'active',True)
def show(Active):
m1 = min([r for r,c in Active]); M1 = max([r for r,c in Active])
m2 = min([c for r,c in Active]); M2 = max([c for r,c in Active])
print(m1,m2,M1,M2)
P = [['□'] * (M2-m2+1) for i in range(M1-m1+1)]
for r,c in Active: P[r-m1][c-m2] = '■'
for l in P: print(' '.join(l))
L = makeLifeNet(20,20)
glider = [(1,2),(2,3),(3,1),(3,2),(3,3)]
setActive(L,glider); nStep = 5; Cand = glider
# pentadecathlon = [(5,7),(5,12),(6,5),(6,6),(6,8),(6,9),(6,10),(6,11),(6,13),
# (6,14),(7,7),(7,12)]
# setActive(L,pentadecathlon); nStep = 16; Cand = pentadecathlon
for step in range(nStep+1):
# display current state
Active = { v for v in Cand if L.getNode(v,'active') }
print('\nStep',step); show(Active)
for v in Active: L.setNode(v,'s',L.getNode(v,'s')+[step])
if step>=nStep: break
# determine Candidates for change
Cand = Active
for v in Active: Cand = Cand | L.neighbors(v)
# prepare change
S = [ (u, sum([ L.getNode(v,'active',False) for v in {u}|L.neighbors(u) ])) \
for u in Cand ]
# make change
# To avoid decisions and branches -if the sum of all nine fields:
# - is 3, the inner field state for the next generation will be life
# - is 4, the inner field retains its current state
# - every other sum sets the inner field to death.
for v,s in S:
if s==3: L.setNode(v,'active',True)
elif s!=4: L.setNode(v,'active',False)
print("\nNode activity")
for v in L.nodes():
s = L.getNode(v,'s')
if s!=[]: print(v,"=",s)
======= RESTART: C:/Users/batagelj/work/Python/graph/Nets/lifeTime.py =======
Step 0
1 1 3 3
□ ■ □
□ □ ■
■ ■ ■
Step 1
2 1 4 3
■ □ ■
□ ■ ■
□ ■ □
Step 2
2 1 4 3
□ □ ■
■ □ ■
□ ■ ■
Step 3
2 2 4 4
■ □ □
□ ■ ■
■ ■ □
Step 4
2 2 4 4
□ ■ □
□ □ ■
■ ■ ■
Step 5
3 2 5 4
■ □ ■
□ ■ ■
□ ■ □
Node activity
(1, 2) = [0]
(2, 1) = [1]
(2, 2) = [3]
(2, 3) = [0, 1, 2, 4]
(3, 1) = [0, 2]
(3, 2) = [0, 1, 5]
(3, 3) = [0, 1, 2, 3]
(3, 4) = [3, 4, 5]
(4, 2) = [1, 2, 3, 4]
(4, 3) = [2, 3, 4, 5]
(4, 4) = [4, 5]
(5, 3) = [5]
>>>