Created
February 21, 2012 14:03
-
-
Save timriffe/1876710 to your computer and use it in GitHub Desktop.
Model Thinking- Aggregation- Game of Life- Experiments in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define a set of functions for iterating through the Game of Life, as described (err, as I understood it) by Scott Page in the 2012 Model Thinking course: | |
# queenNeighbors() | |
# given the status of the cell located and index 'ind' | |
# in matrix 'M' with 'n' columns, what are the statuses | |
# of its neighbors | |
# *neighborhoods to NOT wrap left and right but they DO wrap up and down... | |
queenNeighbors <- function(ind,M,n){ | |
inds <- ind+outer(c(-1,0,1),c(-n,0,n),"+") | |
M[inds[inds > 0 & inds <= length(M) & inds != ind]] | |
} | |
# onOff() | |
# given the status of the cell located and index 'ind' | |
# in matrix 'M' with 'n' columns, does it live or die? | |
onOff <- function(ind,M,n){ | |
status <- M[ind] | |
Ons <- sum(queenNeighbors(ind,M,n)) | |
ifelse(status,ifelse(status & (Ons == 2 | Ons == 3),1,0),ifelse(Ons == 3,1,0)) | |
} | |
# gameOfLifeCycle() | |
# given a matrix M with n columns (n given as a parameter to remove call to ncol()) | |
# what will be the ending state (starting state of next iteration)? | |
gameOfLifeCycle <- function(M,n){ | |
matrix(sapply(1:length(M),onOff,M=M,n=n),ncol=n) | |
} | |
# gameOfLife() | |
# takes a starting matrix, M0 and iterates N times | |
gameOfLife <- function(M0,N=100){ | |
n <- ncol(M0) | |
Mi <- M0 | |
MiArray <- array(dim=c(dim(M0),N+1)) | |
MiArray[,,1] <- M0 | |
for (i in 1:N){ | |
MiArray[,,i+1] <- gameOfLifeCycle(MiArray[,,i],n=n) | |
} | |
return(MiArray) | |
} | |
# an experiment. Setting the random number seed makes it so that the same | |
# results will be produced. remove that line to make a different experiment | |
# each time | |
set.seed(1) | |
# 'GOLresults' is a 3 dimensional array | |
GOLresults <- gameOfLife(matrix(sample(c(1,0),100,replace=T),ncol=10)) | |
# iterate through as image()s and save as multipage pdf: | |
pdf("GameofLife.pdf") | |
for (i in 1:101){ | |
image(t(GOLresults[,,i]),axes=FALSE,main=i-1) | |
} | |
dev.off() # save and close device. | |
cat("GameofLife.pdf is located here:",getwd()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to be able to help very well, I'd need to know what code you've tried, and the characteristics of the data (e.g. comma separated, etc). As far as I recall, none of the code that I've posed here requires reading in data, so I guess your question isn't related to Model Thinking. Consider reading a beginner's tutorial, such as this one: http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CEwQFjAE&url=http%3A%2F%2Fwww.springer.com%2Fcda%2Fcontent%2Fdocument%2Fcda_downloaddocument%2F9780387938363-c2.pdf%3FSGWID%3D0-0-45-772114-p173894303&ei=IFx7T_W3E9PqggfNsOWBAw&usg=AFQjCNF4Sc602efZTzF1ESAHAG7KQF7hcA&sig2=kBsLZbKd3LaawTc7QvAs1Q