Skip to content

Instantly share code, notes, and snippets.

@timriffe
Created February 24, 2012 10:10
Show Gist options
  • Save timriffe/1899921 to your computer and use it in GitHub Desktop.
Save timriffe/1899921 to your computer and use it in GitHub Desktop.
Game of Life Experiment: Exponential Decay
# this code sources 2 functions from Vadim Vinichenko for the game of life.
# His functions were more convenient because they scale well. I'll make a 1000x1000 starting matrix, with
# 30% of cells initially on, remove the plotting and just get this one diagnostic:
# this line loads the 2 functions from an abdridged github gist:
source("http://raw.github.com/gist/1899814/881e5f92c9413a5e1800c8a274d177982d796a8c/life2.r")
# set parameters:
n_rows <- 1000
n_cols <- 1000
n_cells <- n_rows * n_cols
n_cycles <- 100
sleep_time <- 0.1
clrs <- c("#f0f0f0", "#2f81c1") #colors for empty squares and cells
rnd_threshold <- 0.3 # 0 - empty board; 1 - all squares are filled
# make starting conditions:
set.seed(1)
board <- matrix(0, nrow = n_rows, ncol = n_cols)
board[runif(n_cells, 0, 1) < rnd_threshold] <- 1
# this is our diagnostic vector:
prop_on <- vector(length = n_cycles+1)
prop_on[1] <- sum(board)/n_cells
# here we iterate n_cycles times:
for (i in (1:n_cycles)) {
board <- life_cycle(board)
prop_on[i+1] <- sum(board)/n_cells
}
# let's take a look:
plot(0:100,prop_on,type='l',main="% of cells switched on over time\nIt appears to exponentially decay")
text(20,.3,"1,000,000 cells, initially 30% 'alive'. We iterate forward\n100 times using game of life birth/death rules and save\nthe percent living after each iteration.",pos=4)
points(10,prop_on[11],pch=19,col="blue")
arrows(25,.25,10,prop_on[11])
text(25,.25,paste("t = 10\np =",round(prop_on[11],digits=4)),pos=4)
points(100,prop_on[101],pch=19,col="blue")
arrows(90,.25,100,prop_on[101])
text(70,.25,paste("t = 100\np =",round(prop_on[101],digits=4)),pos=4)
# where prop_on[11] is the proportion living at time 10,
# and prop[101] is the proportion living at time 100
# and 90 time units have passed (iterations
(half_life <- 90/log(prop_on[11]/prop_on[101],base=2))
# meaning that lambda, our decay constant
(lambda <- log(2)/half_life)
text(20,.2,paste("Assuming that this is really exponential decay,\nwe get a population 1/2 life of",round(half_life,4),"\nand a decay constant of",round(lambda,4)),pos=4)
# these quantities refer to the 'population', not to cells: they switch on and off rather
# frequently.
# The BIG QUESTION is: will the cells die off asymptotically, or can we predict a proportion
# that will remain alive ad infinitum???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment