|
| 1 | +# z-includes.R |
| 2 | +# Include file for Main script |
| 3 | +# |
| 4 | +# jason bailey |
| 5 | +# 20200120 first commit |
| 6 | + |
| 7 | + |
| 8 | +# test data |
| 9 | +fn_initial_conditions<-function(){ |
| 10 | + |
| 11 | + #iterations =15 #rows =10 #cols =10 #cells <- rows*cols |
| 12 | + # numberOFZombies to start with #zombieStart =1 |
| 13 | + # number of Immune #immuneStart =250 |
| 14 | + a1<-c(15,10,10,1,1) |
| 15 | + a2<-c(15,10,10,1,1) |
| 16 | + a3<-c(15,10,10,1,1) |
| 17 | + a4<-c(15,10,10,1,1) |
| 18 | + a5<-c(15,10,10,1,1) |
| 19 | + a6<-c(15,10,10,1,1) |
| 20 | + a7<-c(15,10,10,1,1) |
| 21 | + a8<-c(15,10,10,1,1) |
| 22 | + a9<-c(15,10,10,1,1) |
| 23 | + a10<-c(15,10,10,1,1) |
| 24 | + a12<-c(15,10,10,1,1) |
| 25 | + a11<-c(15,10,10,1,1) |
| 26 | + a13<-c(15,10,10,1,1) |
| 27 | + a14<-c(15,10,10,1,1) |
| 28 | + a15<-c(15,10,10,1,1) |
| 29 | + a16<-c(15,10,10,1,250) |
| 30 | + a17<-c(15,10,10,1,250) |
| 31 | + a18<-c(15,10,10,1,250) |
| 32 | + a19<-c(15,10,10,1,250) |
| 33 | + a20<-c(15,10,10,1,250) |
| 34 | + a21<-c(15,10,10,1,299) |
| 35 | + a22<-c(15,10,10,1,299) |
| 36 | + a23<-c(15,10,10,1,299) |
| 37 | + a24<-c(15,10,10,1,299) |
| 38 | + a25<-c(15,10,10,1,299) |
| 39 | + a26<-c(15,10,10,1,299) |
| 40 | + a27<-c(15,10,10,1,250) |
| 41 | + a28<-c(15,10,10,1,250) |
| 42 | + a29<-c(15,10,10,1,250) |
| 43 | + a30<-c(15,10,10,1,250) |
| 44 | + a31<-c(15,10,10,1,50) |
| 45 | + a32<-c(15,10,10,1,50) |
| 46 | + a33<-c(15,10,10,1,50) |
| 47 | + a34<-c(15,10,10,1,50) |
| 48 | + a35<-c(15,10,10,1,60) |
| 49 | + a36<-c(15,10,10,1,70) |
| 50 | + a37<-c(15,10,10,1,80) |
| 51 | + a38<-c(15,10,10,1,80) |
| 52 | + a39<-c(15,10,10,1,70) |
| 53 | + a40<-c(15,10,10,1,60) |
| 54 | + a41<-c(25,100,100,1,1) |
| 55 | + a42<-c(25,100,100,1,1) |
| 56 | + a43<-c(25,100,100,1,1) |
| 57 | + a44<-c(25,100,100,1,1) |
| 58 | + a45<-c(25,100,100,1,1) |
| 59 | + a46<-c(25,100,100,1,1000) |
| 60 | + a47<-c(25,100,100,1,2000) |
| 61 | + a48<-c(25,100,100,1,3000) |
| 62 | + a49<-c(25,100,100,1,5000) |
| 63 | + a50<-c(25,100,100,1,6000) |
| 64 | + a51<-c(25,100,100,1,35000) |
| 65 | + a52<-c(25,100,100,1,35000) |
| 66 | + a53<-c(25,100,100,1,35000) |
| 67 | + a54<-c(25,100,100,1,35000) |
| 68 | + a55<-c(25,100,100,1,35000) |
| 69 | + |
| 70 | + d<-as.matrix( rbind(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55)) |
| 71 | + #d<-as.matrix( rbind(a1)) |
| 72 | + #matrix( c(1,2,3,), c(4,5,6), nrow = 2, byrow = TRUE) |
| 73 | + return(d) |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +########### |
| 79 | +# Functions |
| 80 | +########### |
| 81 | +fn_doIterations <-function (testName,popMatrix, numRows,numCols){ |
| 82 | + cells = numRows*numCols |
| 83 | + #print(popMatrix) |
| 84 | + fn_plotZombie(testName,popMatrix,0, numRows, numCols) |
| 85 | + |
| 86 | + #dfnamesR<-c( "test","step", "rows","cols","human","zombie","immune") |
| 87 | + #iters0<-data.frame(testName0,0,0,0,0,0,0) |
| 88 | + iters = fn_iterationStore(testName,popMatrix, 0, numRows,numCols) |
| 89 | + infectionMatrix<-fn_resetInfections(0, cells, numRows) |
| 90 | + for(step in 1:(iterations+1)){ |
| 91 | + # iterate over cells |
| 92 | + for (rw in 1:(rows)){ |
| 93 | + for (col in 1:(cols)){ |
| 94 | + xy<-fn_checkPopulationCells(popMatrix[rw,col]) |
| 95 | + if(xy[1] ==FALSE){ |
| 96 | + #infectionMatrix[xy[1],xy[2]]<-1 |
| 97 | + } else { |
| 98 | + #print(xy) |
| 99 | + infectionMatrix[xy[1],xy[2]]<-1 |
| 100 | + } |
| 101 | + } # end col |
| 102 | + } # end row |
| 103 | + |
| 104 | + #infect humans! |
| 105 | + popMatrix<-fn_infectPopulation(popMatrix, infectionMatrix) |
| 106 | + fn_plotZombie(testName,popMatrix,step, numRows, numCols) |
| 107 | + iters2 = fn_iterationStore(testName,popMatrix, step, numRows,numCols) |
| 108 | + iters<-rbind(iters,iters2) |
| 109 | + #print(popMatrix) |
| 110 | + # reset matrix after iteration |
| 111 | + infectionMatrix<-fn_resetInfections(0, cells, rows) |
| 112 | + } |
| 113 | + #print(iters) |
| 114 | + #return(popMatrix) |
| 115 | + return(iters) |
| 116 | +} |
| 117 | + |
| 118 | +fn_plotZombie<-function(testName,population,step, numRows, numCols){ |
| 119 | + #dev.off() |
| 120 | + #print(as.vector(population)) |
| 121 | + |
| 122 | + Is<-table(population) |
| 123 | + numI<-Is[names(Is)=='I'] |
| 124 | + perI<-(numI/(numRows*numCols))*100 |
| 125 | + |
| 126 | + gs <- lapply(as.vector(population), function(zhi){ |
| 127 | + if(zhi=="H"){ |
| 128 | + colr ="green" |
| 129 | + } else if(zhi=="Z") { |
| 130 | + colr="red" |
| 131 | + } else{ |
| 132 | + colr="blue" |
| 133 | + } |
| 134 | + grobTree(rectGrob(gp=gpar(fill=colr, alpha=0.5)), textGrob(zhi, gp=gpar(fontsize=12,font=8) )) |
| 135 | + |
| 136 | + } |
| 137 | + ) |
| 138 | + #grid.arrange(grobs=gs, ncol=numCols, |
| 139 | + # top="top label", bottom="bottom\nlabel", |
| 140 | + # left="left label", right="right label") |
| 141 | + #top=textGrob("Title", gp=gpar(fontsize=15,font=8) |
| 142 | + title =paste(testName,"- ","Percentage Immune ",perI,"% - Step=", step,sep="") |
| 143 | + |
| 144 | + folder<-"/Users/jason/Source/RCode/zombie/media/" |
| 145 | + pad<-sprintf("%02d", step) |
| 146 | + path<-paste(folder,testName,"-",pad,".png", sep="") |
| 147 | + png(filename=path) |
| 148 | + grid.arrange(grobs=gs, ncol=numCols, |
| 149 | + top=textGrob(title, gp=gpar(fontsize=15,font=8) )) |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + dev.off() |
| 154 | + |
| 155 | + |
| 156 | +} |
| 157 | + |
| 158 | +fn_randomXY<-function(){ |
| 159 | + |
| 160 | + randomRow <- sample(1:rows, 1) |
| 161 | + randomCol <- sample(1:cols, 1) |
| 162 | + xy <- c(randomRow, randomCol) |
| 163 | + #xy <- c(1, 2) |
| 164 | + return(xy) |
| 165 | +} |
| 166 | + |
| 167 | +fn_checkPopulationCells<-function(x){ |
| 168 | + isZombie<-fn_isZombie(x) |
| 169 | + if (isZombie =="Y"){ |
| 170 | + randomXY<-fn_randomXY() |
| 171 | + } else{ |
| 172 | + randomXY<-c(FALSE, FALSE) |
| 173 | + } |
| 174 | + |
| 175 | + return(randomXY) |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | +fn_isZombie<-function(x){ |
| 182 | + if(x==zombie){ |
| 183 | + return("Y") |
| 184 | + } else{ |
| 185 | + return ("N") |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +fn_isHuman<-function(x){ |
| 190 | + if(x==human){ |
| 191 | + return("Y") |
| 192 | + } else{ |
| 193 | + return ("N") |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | +fn_infectPopulation<-function(popM, infM){ |
| 199 | + for (rw in 1:(rows)){ |
| 200 | + for (col in 1:(cols)){ |
| 201 | + isHuman <-fn_isHuman(popM[rw, col]) |
| 202 | + infection <-infM[rw, col] |
| 203 | + if(isHuman == "Y" && infection ==1){ |
| 204 | + popM[rw, col] <-zombie # turn human into zombie |
| 205 | + } |
| 206 | + } # end col |
| 207 | + } # end row |
| 208 | + return(popM) |
| 209 | +} |
| 210 | + |
| 211 | +# Create zombies and immunue |
| 212 | +fn_zombies_and_immune<-function(popData, immuneStart, zombieStart ){ |
| 213 | + # add some random imumune |
| 214 | + for (iStep in 1:(immuneStart)){ |
| 215 | + xy =fn_randomXY() |
| 216 | + popData[xy[1],xy[2]] <-immune |
| 217 | + } |
| 218 | + # add some random zombies |
| 219 | + for (zStep in 1:(zombieStart)){ |
| 220 | + xy =fn_randomXY() |
| 221 | + popData[xy[1],xy[2]] <-zombie |
| 222 | + |
| 223 | + } |
| 224 | + |
| 225 | + return(popData) |
| 226 | +} |
| 227 | + |
| 228 | + |
| 229 | +# reset infectionMatrix |
| 230 | +fn_resetInfections<-function(resetInfection, numcells, numrows){ |
| 231 | + infMatrix<-matrix( rep( resetInfection, len=numcells), nrow = numrows) |
| 232 | + return(infMatrix) |
| 233 | +} |
| 234 | + |
| 235 | +# get numbers over 1 iteration |
| 236 | +fn_iterationStore<-function(testName,popMatrix, step, numRows,numCols){ |
| 237 | + |
| 238 | + popData=table(popMatrix) |
| 239 | + #print(postpopData[names(postpopData)=='H']) |
| 240 | + if (length(popData[names(popData)=='H'])>0){ |
| 241 | + numH<-popData[names(popData)=='H'] |
| 242 | + } else{ |
| 243 | + numH<-0 |
| 244 | + } |
| 245 | + |
| 246 | + if (length(popData[names(popData)=='Z'])>0){ |
| 247 | + numZ<-popData[names(popData)=='Z'] |
| 248 | + } else{ |
| 249 | + numZ<-0 |
| 250 | + } |
| 251 | + |
| 252 | + if (length(popData[names(popData)=='I'])>0){ |
| 253 | + numI<-popData[names(popData)=='I'] |
| 254 | + } else{ |
| 255 | + numI<-0 |
| 256 | + } |
| 257 | + #row1<-data.frame(iterations,rows,cols,prepopData[names(prepopData)=='Z'],prepopData[names(prepopData)=='H'],prepopData[names(prepopData)=='I'],postpopData[names(postpopData)=='Z'],postpopData[names(postpopData)=='H'],postpopData[names(postpopData)=='I']) |
| 258 | + |
| 259 | + dfnamesR<-c( "test","step", "rows","cols","human","zombie","immune") |
| 260 | + rowStep<-data.frame(testName,step, numRows,numCols, numH, numZ, numI) |
| 261 | + names(rowStep)<-dfnamesR |
| 262 | + return(rowStep) |
| 263 | + |
| 264 | +} |
| 265 | + |
| 266 | +# Test function -is include OK |
| 267 | +fn_test<-function(x){ |
| 268 | + return ("hello") |
| 269 | +} |
0 commit comments