Game of Life

Initial position

An example of cellular automata in the field of organism modelling was explored by John Conway more than half a century ago, and it is one of the simpler examples of simulations with given simple rules for each cell. The process is not stochastic; instead, it prescribes deterministic behaviour for each cell, comprising the following three rules:

  1. If a cell has exactly two live neighbours, it keeps its state (nothing changes).

  2. If a cell has exactly three live neighbours, it becomes alive as well.

  3. Otherwise it is dead, or becomes dead.

The neighbours of a cell are all the nearest cells (8 neighbours, including diagonals). At the edges, periodicity, i.e. connectivity of the edges, can be maintained. The transition to the next generation happens instantaneously (tick). The changing of cell states is called life.

Write a program that simulates this life. Any kind of rendering may be used. ASCII output (* or X) is possible for smaller domains. For larger ones, it is better to use the PGM/PBM format. Also print the number of live cells in the system and the number of state changes (died, born). The initial state should be a cross of cells across the middle. Parallelise the program by domain (matrix) decomposition.

## -*- coding: utf-8 -*- conway.py
import numpy as np
import pylab as pl

def update(board):
    # Število sosedov celice
    neighbours = np.zeros(board.shape)
    neighbours[1:, 1:] += board[:-1, :-1]
    neighbours[1:, :-1] += board[:-1, 1:]
    neighbours[:-1, 1:] += board[1:, :-1]
    neighbours[:-1, :-1] += board[1:, 1:]
    neighbours[:-1, :] += board[1:, :]
    neighbours[1:, :] += board[:-1, :]
    neighbours[:, :-1] += board[:, 1:]
    neighbours[:, 1:] += board[:, :-1]

    new_board = np.where(neighbours < 2, 0, board)
    new_board = np.where(neighbours > 3, 0, new_board)
    new_board = np.where(neighbours == 3, 1, new_board)

    return new_board

## inicializacija deske
board = np.zeros((32, 32), int)
board[15,:] = 1
board[:,15] = 1

#board[:] = np.random.rand(32,32).round(0)

pl.ion()
pl.imshow(board, cmap = pl.cm.prism)
pl.savefig('conway_initial.png')
for iter in range(30):
    board = update(board)
    pl.imshow(board, cmap = pl.cm.prism)
    pl.draw()

pl.ioff()
pl.show()

Igra življenja

Začetni položaj

Primer celičnih avtomatov na področju modeliranja organizmov je preizkušal John Conway že pred več kot pol stoletja in predstavlja enega preprostejših primerov simulacij s podanimi enostavnimi ppravili za vsako celico. Proces ni stohastičen ampak predpisuje za vsako celico determinističen način obnašanja, ki vključuje naslednja tri pravila za vsako celico:

  1. Če ima celica natanko dva živa soseda, potem ohranja svoje stanje (se nič ne spremeni)

  2. Če ima celica natanko tri žive sosede potem tudi ona postane živa.

  3. Sicer je mrtva, oziroma postane mrtva.

Sosedi celice so vse najbližje celice (8 sosedov, tudi v diagonalah). Na robovih lahko ohranimo periodičnost oziroma povezanost robov. Prestop v naslednjo generavijo se zgodi v trenutku (tick). Spreminjanje stanja celic imenujemo življenje.

Napišite program, ki simulira to življenje. Izris lahko uporabite poljuben. ASCII izpis (* ali X)  je možen za manjša območja. Za večja pa je bolje uporabiti PGM/PBM format. Izpisujte tudi število živih celic v sistemu ter število sprememb posameznega stanja (umrlih, rojenih). Začetno stanje naj bo križ iz celic preko sredine. Program paralelizirajte s porazdelitvijo prostora (matrike).

## -*- coding: utf-8 -*- conway.py
import numpy as np
import pylab as pl

def update(board):
    # Število sosedov celice
    neighbours = np.zeros(board.shape)
    neighbours[1:, 1:] += board[:-1, :-1]
    neighbours[1:, :-1] += board[:-1, 1:]
    neighbours[:-1, 1:] += board[1:, :-1]
    neighbours[:-1, :-1] += board[1:, 1:]
    neighbours[:-1, :] += board[1:, :]
    neighbours[1:, :] += board[:-1, :]
    neighbours[:, :-1] += board[:, 1:]
    neighbours[:, 1:] += board[:, :-1]

    new_board = np.where(neighbours < 2, 0, board)
    new_board = np.where(neighbours > 3, 0, new_board)
    new_board = np.where(neighbours == 3, 1, new_board)

    return new_board

## inicializacija deske
board = np.zeros((32, 32), int)
board[15,:] = 1
board[:,15] = 1

#board[:] = np.random.rand(32,32).round(0)

pl.ion()
pl.imshow(board, cmap = pl.cm.prism)
pl.savefig('conway_initial.png')
for iter in range(30):
    board = update(board)
    pl.imshow(board, cmap = pl.cm.prism)
    pl.draw()

pl.ioff()
pl.show()