====== LIFE ======
August 15-16, 2017
https://en.wikipedia.org/wiki/Conway%27s_Game_of_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}] \
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)
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)
===== Glider =====
========= RESTART: C:/Users/batagelj/work/Python/graph/Nets/life.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
■ □ ■
□ ■ ■
□ ■ □
>>>
===== Pentadecathlon =====
========= RESTART: C:/Users/batagelj/work/Python/graph/Nets/life.py =========
Step 0
5 5 7 14
□ □ ■ □ □ □ □ ■ □ □
■ ■ □ ■ ■ ■ ■ □ ■ ■
□ □ ■ □ □ □ □ ■ □ □
Step 1
5 6 7 13
■ ■ ■ ■ ■ ■ ■ ■
■ □ ■ ■ ■ ■ □ ■
■ ■ ■ ■ ■ ■ ■ ■
Step 2
4 5 8 14
□ □ ■ ■ ■ ■ ■ ■ □ □
□ ■ □ □ □ □ □ □ ■ □
■ □ □ □ □ □ □ □ □ ■
□ ■ □ □ □ □ □ □ ■ □
□ □ ■ ■ ■ ■ ■ ■ □ □
Step 3
3 5 9 14
□ □ □ ■ ■ ■ ■ □ □ □
□ □ ■ ■ ■ ■ ■ ■ □ □
□ ■ ■ ■ ■ ■ ■ ■ ■ □
■ ■ □ □ □ □ □ □ ■ ■
□ ■ ■ ■ ■ ■ ■ ■ ■ □
□ □ ■ ■ ■ ■ ■ ■ □ □
□ □ □ ■ ■ ■ ■ □ □ □
Step 4
2 5 10 14
□ □ □ □ ■ ■ □ □ □ □
□ □ ■ □ □ □ □ ■ □ □
□ ■ □ □ □ □ □ □ ■ □
■ □ □ □ □ □ □ □ □ ■
■ □ □ □ □ □ □ □ □ ■
■ □ □ □ □ □ □ □ □ ■
□ ■ □ □ □ □ □ □ ■ □
□ □ ■ □ □ □ □ ■ □ □
□ □ □ □ ■ ■ □ □ □ □
Step 5
4 4 8 15
□ □ ■ □ □ □ □ □ □ ■ □ □
□ ■ ■ □ □ □ □ □ □ ■ ■ □
■ ■ ■ □ □ □ □ □ □ ■ ■ ■
□ ■ ■ □ □ □ □ □ □ ■ ■ □
□ □ ■ □ □ □ □ □ □ ■ □ □
Step 6
4 4 8 15
□ ■ ■ □ □ □ □ □ □ ■ ■ □
■ □ □ ■ □ □ □ □ ■ □ □ ■
■ □ □ ■ □ □ □ □ ■ □ □ ■
■ □ □ ■ □ □ □ □ ■ □ □ ■
□ ■ ■ □ □ □ □ □ □ ■ ■ □
Step 7
4 3 8 16
□ □ ■ ■ □ □ □ □ □ □ ■ ■ □ □
□ ■ □ □ ■ □ □ □ □ ■ □ □ ■ □
■ ■ ■ ■ ■ ■ □ □ ■ ■ ■ ■ ■ ■
□ ■ □ □ ■ □ □ □ □ ■ □ □ ■ □
□ □ ■ ■ □ □ □ □ □ □ ■ ■ □ □
Step 8
4 3 8 16
□ □ ■ ■ □ □ □ □ □ □ ■ ■ □ □
■ □ □ □ □ ■ □ □ ■ □ □ □ □ ■
■ □ □ □ □ ■ □ □ ■ □ □ □ □ ■
■ □ □ □ □ ■ □ □ ■ □ □ □ □ ■
□ □ ■ ■ □ □ □ □ □ □ ■ ■ □ □
Step 9
5 2 7 17
□ □ ■ □ □ ■ □ □ □ □ ■ □ □ ■ □ □
■ ■ ■ □ □ ■ ■ ■ ■ ■ ■ □ □ ■ ■ ■
□ □ ■ □ □ ■ □ □ □ □ ■ □ □ ■ □ □
Step 10
5 4 7 15
■ □ □ ■ □ ■ ■ □ ■ □ □ ■
■ ■ ■ ■ □ ■ ■ □ ■ ■ ■ ■
■ □ □ ■ □ ■ ■ □ ■ □ □ ■
Step 11
5 3 7 16
□ ■ □ □ ■ □ ■ ■ □ ■ □ □ ■ □
■ ■ □ □ ■ □ □ □ □ ■ □ □ ■ ■
□ ■ □ □ ■ □ ■ ■ □ ■ □ □ ■ □
Step 12
5 3 7 16
■ ■ □ □ □ ■ □ □ ■ □ □ □ ■ ■
■ ■ ■ ■ ■ □ □ □ □ ■ ■ ■ ■ ■
■ ■ □ □ □ ■ □ □ ■ □ □ □ ■ ■
Step 13
5 2 7 17
□ ■ □ □ ■ ■ □ □ □ □ ■ ■ □ □ ■ □
■ □ □ □ ■ ■ ■ □ □ ■ ■ ■ □ □ □ ■
□ ■ □ □ ■ ■ □ □ □ □ ■ ■ □ □ ■ □
Step 14
5 2 7 17
□ □ □ □ ■ □ ■ □ □ ■ □ ■ □ □ □ □
■ ■ □ ■ □ □ ■ □ □ ■ □ □ ■ □ ■ ■
□ □ □ □ ■ □ ■ □ □ ■ □ ■ □ □ □ □
Step 15
5 5 7 14
□ □ ■ □ □ □ □ ■ □ □
■ ■ □ ■ ■ ■ ■ □ ■ ■
□ □ ■ □ □ □ □ ■ □ □
Step 16
5 6 7 13
■ ■ ■ ■ ■ ■ ■ ■
■ □ ■ ■ ■ ■ □ ■
■ ■ ■ ■ ■ ■ ■ ■
>>>
===== To do =====
* generate network (add node and corresponding links) when needed [[.lifp|LIFE plane]]
* display subpictures
* try other rules and networks
* express LIFE as transformations of bipartite graph with edges linking row wit column or directected graph with arcs linking coordinates