Quatch

Untitled

May 14th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.75 KB | None | 0 0
  1. #http://www.reddit.com/r/askscience/comments/35uljq/if_i_wanted_to_randomly_find_someone_in_an/cr89r0o
  2.  
  3. #options:
  4.  
  5. options(error=debug)
  6. #initialize:
  7.  
  8.  
  9.  
  10.  
  11. simulate=function(move2=F,verbose=F,moveA="random"){
  12.     die=function(x=1){floor(runif(x,1,11))}
  13.     coords=function(who){pos=grep(who,park);y=ceiling(pos/10);x=pos%%10;if(x==0)x=10;return(c(x,y))}
  14.     gridAstatus="placeholder" # this var is used to remember what we're doing when we're walking a grid
  15.    
  16.     park= matrix(data="0",nrow=10,ncol=10,byrow=T)
  17.    
  18.     #place A
  19.     posA=die(2)
  20.     park[posA[1],posA[2]]="A0"
  21.  
  22.     #place B
  23.     overlap=T
  24.     while(overlap){
  25.         posB=die(2)
  26.         if(! all(posB==posA)){overlap=F}
  27.         }
  28.     park[posB[1],posB[2]]="B0"
  29.  
  30.  
  31.     #plotpark=function(){for(i in 1:10){for(j in 1:10){cat(park[park$x==i & park$y==j,"who"], " ") };print("")}}#print()
  32.     plotpark=function(){print(park)}
  33.     if(verbose)plotpark()
  34.  
  35.     found=FALSE
  36.     counter=0
  37.     if(all(posB==posA)){
  38.         warning("+++ REDO FROM START +++               >> collision")
  39.         found=T
  40.         }
  41.  
  42.     while(!found){
  43.         counter=counter+1
  44.         if(verbose==2)cat("\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nCounter: ",counter,"\n")
  45.        
  46.         # ####  A
  47.         #get pos:
  48.         pos=coords("A")
  49.         x=pos[1]
  50.         y=pos[2]
  51.         if(verbose==2)cat("A: ",x,y,"\n")
  52.         park[x,y]=as.numeric(gsub("([a-zA-Z])([[:digit:]]*)","\\2",park[pos[1],pos[2]] ))+1 #or counter
  53.        
  54.         #valid moves:
  55.         if(moveA=="random"){
  56.         dirs=c("U","UR","R","DR","D","DL","L","UL")
  57.         if(y==1)dirs=grep("[^L]$",dirs,value=T)
  58.         if(y==10)dirs=grep("[^R]$",dirs,value=T)
  59.         if(x==1)dirs=grep("^[^U]",dirs,value=T)
  60.         if(x==10)dirs=grep("^[^D]",dirs,value=T)
  61.        
  62.         if(verbose==2)print(dirs)
  63.        
  64.         dirs=sample(dirs,1)
  65.         if(verbose==2)print(dirs)
  66.        
  67.         if(length(grep("D",dirs))==1) x=x+1
  68.         if(length(grep("U",dirs))==1) x=x-1
  69.         if(length(grep("R",dirs))==1) y=y+1
  70.         if(length(grep("L",dirs))==1) y=y-1
  71.         }
  72.         if(moveA=="grid"){
  73.         if(verbose==2)cat("A: grid: ",gridAstatus,"\n")
  74.             switch(gridAstatus,
  75.                 down={
  76.                 if(x<10)
  77.                     x=x+1
  78.                     else {
  79.                     if(y<10){
  80.                         y=y+1
  81.                         gridAstatus="up"
  82.                         }else{
  83.                         gridAstatus="left"
  84.                         }
  85.                     }
  86.                 }, # end "right"
  87.                 up={
  88.                     if(x>1)
  89.                     x=x-1
  90.                     else {
  91.                         if(y<10){
  92.                         y=y+1
  93.                         gridAstatus="down"
  94.                         }else{
  95.                         gridAstatus="left"
  96.                         }
  97.                     }
  98.                 }, #end "left"
  99.                 left={
  100.                     if(y>1) y=y-1
  101.                     else{
  102.                         if(x==10){ #we are at top left
  103.                             x=x-1
  104.                             gridAstatus="down"
  105.                             }
  106.                         else{ #we are at top right
  107.                             x=x+1
  108.                             gridAstatus="up"
  109.                             }
  110.                     }
  111.                 }, #end "up"
  112.                 { #DEFAULT: we are just starting out, gotta pick a direction.
  113.                     if(x<10){
  114.                         x=x+1
  115.                         gridAstatus="up"
  116.                         }
  117.                     else{
  118.                         x=x-1
  119.                         gridAstatus="down"
  120.                         }
  121.                     }# end default selection
  122.                 )#end switch(gridAstatus)
  123.             if(verbose==2)cat("A: grid: ",gridAstatus,"\n")
  124.             }#end if move=grid
  125.        
  126.  
  127.        
  128.         if(verbose==2)cat("A: ",x,y,"\n")
  129.        
  130.         newA=c(x,y)
  131.        
  132.         # ####  B
  133.         #get pos:
  134.         pos=coords("B")
  135.         x=pos[1]
  136.         y=pos[2]
  137.         if(verbose==2)cat("B: ",x,y,"\n")
  138.         park[x,y]=as.numeric(gsub("([a-zA-Z])([[:digit:]]*)","\\2",park[pos[1],pos[2]]))+1 #or counter
  139.        
  140.        
  141.         #valid moves:
  142.         dirs=c("U","UR","R","DR","D","DL","L","UL")
  143.         if(y==1)dirs=grep("[^L]$",dirs,value=T)
  144.         if(y==10)dirs=grep("[^R]$",dirs,value=T)
  145.         if(x==1)dirs=grep("^[^U]",dirs,value=T)
  146.         if(x==10)dirs=grep("^[^D]",dirs,value=T)
  147.        
  148.         if(verbose==2)print(dirs)
  149.        
  150.         dirs=sample(dirs,1)
  151.        
  152.         if(verbose==2)print(dirs)
  153.        
  154.         if(length(grep("D",dirs))==1) x=x+1
  155.         if(length(grep("U",dirs))==1) x=x-1
  156.         if(length(grep("R",dirs))==1) y=y+1
  157.         if(length(grep("L",dirs))==1) y=y-1
  158.        
  159.         if(verbose==2)cat(x,y,"\n")
  160.        
  161.         newB=c(x,y)
  162.        
  163.        
  164.         if(all(newA==newB))
  165.             {
  166.             found=T
  167.             #park[x,y]="X"
  168.             }
  169.             else{
  170.             park[newA[1],newA[2]]=paste("A",park[newA[1],newA[2]],sep="")
  171.             park[newB[1],newB[2]]=paste("B",park[newB[1],newB[2]],sep="")
  172.             }
  173.        
  174.         #Search nieghbouring squares
  175.        
  176.         if(verbose==2)plotpark()
  177.     }
  178.     if(verbose)plotpark()
  179.     if(verbose)cat("\n\nFound in: ",counter)
  180. return(c(counter,summary(as.numeric(park[park!=0]))))
  181. }
  182.  
  183. #simulate()
  184. #simulate(verbose=T)
  185. simulate(moveA="grid",verbose=2)
  186.  
  187. nruns=1e5
  188.  
  189. if(F){
  190. pb=txtProgressBar(min=0,max=nruns,style=3)
  191.  
  192. a=rep(0,nruns)
  193. dataA=data.frame(steps=a,min=a,q1=a,md=a,me=a,q3=a,max=a)
  194. dataAB=data.frame(steps=a,min=a,q1=a,md=a,me=a,q3=a,max=a)
  195. dataAgB=data.frame(steps=a,min=a,q1=a,md=a,me=a,q3=a,max=a)
  196. for(i in 1:nruns){dataA[i,]=simulate(F); setTxtProgressBar(pb, i)}
  197. for(i in 1:nruns){dataAB[i,]=simulate(T); setTxtProgressBar(pb, i)}
  198. for(i in 1:nruns){dataAgB[i,]=simulate(T,moveA="grid"); if(!(x%%1000))setTxtProgressBar(pb, i)}
  199.  
  200. print(summary(dataA));print(sd(dataA$steps))
  201. print(summary(dataAB));print(sd(dataAB$steps))
  202. print(summary(dataAgB));print(sd(dataAgB$steps))
  203. }
Add Comment
Please, Sign In to add comment