Skip to content

Instantly share code, notes, and snippets.

Avatar

Ram Narasimhan Ram-N

View GitHub Profile
View gist:5019048
# RING of R-Rows C-cols in the center of width w cells
seedCenterRing <- function(r, c, wide){
for(x in as.integer((areaW-c)/2): as.integer((areaW + c)/2)){
for(y in as.integer((areaH-r)/2): as.integer((areaH + r)/2)){
area_df[x,y] <<- 1
}
}
#scoop out the inner ring
for(x in as.integer((areaW-c)/2 + wide): as.integer((areaW + c)/2 - wide)){
for(y in as.integer((areaH-r)/2+wide): as.integer((areaH + r)/2- wide )){
View gist:5019066
# R-Rows C-cols in the center
seedAreaCenter <- function(r, c){
for(x in as.integer((areaW-c)/2): as.integer((areaW + c)/2)){
for(y in as.integer((areaH-r)/2): as.integer((areaH + r)/2)){
area_df[x,y] <<- 1
}
}
}
#Seed a central area and let it grow
View gist:5019092
# w-cols to the R and L of center column
seedColumns <- function(c, w){
startLCol = as.integer( (areaW-c) / 2 )
startRCol = as.integer( (areaW+c) / 2 )
for(y in 1:areaH) {
clist = c((startLCol-w):startLCol, startRCol:(startRCol+w))
lapply(clist, function(x) area_df[x,y] <<- 1)
}
}
@Ram-N
Ram-N / populationSimulation.R
Last active Dec 14, 2015
Simulating City Population Growth patterns with R
View populationSimulation.R
# Original Code by Matt Asher for statisticsblog.com
# Feel free to modify and redistribute, but please keep this notice
#Modifications by Ram Narasimhan
# @ramnarasimhan
source("~/RStats/simFunctions.R")
library(ggplot2)
library(plyr)
View gist:5067912
store_iteration_stats<- function(kNumSettlers, found.home, max.look.around) {
#how many settlers found homes
print(c(found.home," settled out of ", kNumSettlers, (found.home/kNumSettlers)*100, "%" ) )
#avg # of steps per new settler
print(c("num of look arounds max'ed:", max.look.around))
}
View gist:5068453
# Are any of the 4 direct N E W S adjacent cells occupied?
isAnyOfNEWSCellsOccupied <- function(m,n) {
canOccupy <- FALSE
NEWSdir = c(2,4,5,7)
#traverse the vector of 4 elements
for(k in 1:4) {
xCheck = m + adjacells[NEWSdir[k],1]
yCheck = n + adjacells[NEWSdir[k],2]
if(!(outOfBounds(xCheck, yCheck))) {
if(area_df[xCheck, yCheck] > 0) {
View gist:5068470
# Are any of the 4 diagonally adjacent cells occupied?
isAnyDiagNeighboringCellOccupied <- function(m,n) {
canOccupy <- FALSE
diag.dir = c(1,3,6,8)
#traverse the vector of 4 elements
for(k in 1:4) {
xCheck = m + adjacells[diag.dir[k],1]
yCheck = n + adjacells[diag.dir[k],2]
if(!(outOfBounds(xCheck, yCheck))) {
View gist:5068922
# Make multiple runs (Replication of simulation) and take the average of stats
st <- data.frame()
st_row<- vector()
for(i in 1:kNumReplications) {
area_df <- resetIteration()
seedAreaWithPioneers(numPioneers,seeding.opt)
simstats <- accommodateSettlers(kNumSettlers, settling.option)
found.home <- simstats[1]
View gist:5111903
store_iteration_stats<- function(iter, kNumSettlers, found.home, max.look.around) {
#how many settlers found homes
perSet <- (found.home/kNumSettlers) * 100
print(c(found.home," settled out of ", kNumSettlers, perSet, "%" ) )
#avg # of steps per new settler
print(c("num of Settlers who hit max look around:", max.look.around))
return(c(iter, found.home, kNumSettlers, perSet))
}
View gist:5136633
# Genotype AA = 1, Aa = 2 and aa = 3
# Gamete A =1 and a = 0
#Uniformly distribute AA, Aa and aa
start.pop <- sample(1:3, kStartPop, replace=TRUE)