Advertisement
Quatch

Untitled

May 14th, 2015
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.13 KB | None | 0 0
  1.  
  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.    
  15.     park= matrix(data="0",nrow=10,ncol=10,byrow=T)
  16.    
  17.     #place A
  18.     posA=die(2)
  19.     park[posA[1],posA[2]]="A0"
  20.  
  21.     #place B
  22.     overlap=T
  23.     while(overlap){
  24.         posB=die(2)
  25.         if(! all(posB==posA)){overlap=F}
  26.         }
  27.     park[posB[1],posB[2]]="B0"
  28.  
  29.  
  30.     #plotpark=function(){for(i in 1:10){for(j in 1:10){cat(park[park$x==i & park$y==j,"who"], " ") };print("")}}#print()
  31.     plotpark=function(){print(park)}
  32.     if(verbose)plotpark()
  33.  
  34.     found=FALSE
  35.     counter=0
  36.     if(all(posB==posA)){
  37.         warning("+++ REDO FROM START +++               >> collision")
  38.         found=T
  39.         }
  40.  
  41.     while(!found){
  42.         counter=counter+1
  43.        
  44.         # ####  A
  45.         #get pos:
  46.         pos=coords("A")
  47.         x=pos[1]
  48.         y=pos[2]
  49.         if(verbose==2)cat("A: ",x,y,"\n")
  50.         park[x,y]=as.numeric(gsub("([a-zA-Z])([[:digit:]]*)","\\2",park[pos[1],pos[2]] ))+1 #or counter
  51.        
  52.         #valid moves:
  53.         if(moveA=="random"){
  54.         dirs=c("U","UR","R","DR","D","DL","L","UL")
  55.         if(y==1)dirs=grep("[^L]$",dirs,value=T)
  56.         if(y==10)dirs=grep("[^R]$",dirs,value=T)
  57.         if(x==1)dirs=grep("^[^U]",dirs,value=T)
  58.         if(x==10)dirs=grep("^[^D]",dirs,value=T)
  59.        
  60.         if(verbose==2)print(dirs)
  61.        
  62.         dirs=sample(dirs,1)
  63.         }
  64.         if(moveA=="grid"){
  65.         if(x<10)x=x+1
  66.             else {x=1;y=y+1}
  67.             }
  68.        
  69.         if(verbose==2)print(dirs)
  70.        
  71.         if(length(grep("D",dirs))==1) x=x+1
  72.         if(length(grep("U",dirs))==1) x=x-1
  73.         if(length(grep("R",dirs))==1) y=y+1
  74.         if(length(grep("L",dirs))==1) y=y-1
  75.        
  76.         if(verbose==2)cat(x,y,"\n")
  77.        
  78.         newA=c(x,y)
  79.        
  80.         # ####  B
  81.         #get pos:
  82.         pos=coords("B")
  83.         x=pos[1]
  84.         y=pos[2]
  85.         if(verbose==2)cat("B: ",x,y,"\n")
  86.         park[x,y]=as.numeric(gsub("([a-zA-Z])([[:digit:]]*)","\\2",park[pos[1],pos[2]]))+1 #or counter
  87.        
  88.        
  89.         #valid moves:
  90.         dirs=c("U","UR","R","DR","D","DL","L","UL")
  91.         if(y==1)dirs=grep("[^L]$",dirs,value=T)
  92.         if(y==10)dirs=grep("[^R]$",dirs,value=T)
  93.         if(x==1)dirs=grep("^[^U]",dirs,value=T)
  94.         if(x==10)dirs=grep("^[^D]",dirs,value=T)
  95.        
  96.         if(verbose==2)print(dirs)
  97.        
  98.         dirs=sample(dirs,1)
  99.        
  100.         if(verbose==2)print(dirs)
  101.        
  102.         if(length(grep("D",dirs))==1) x=x+1
  103.         if(length(grep("U",dirs))==1) x=x-1
  104.         if(length(grep("R",dirs))==1) y=y+1
  105.         if(length(grep("L",dirs))==1) y=y-1
  106.        
  107.         if(verbose==2)cat(x,y,"\n")
  108.        
  109.         newB=c(x,y)
  110.        
  111.        
  112.         if(all(newA==newB))
  113.             {
  114.             found=T
  115.             #park[x,y]="X"
  116.             }
  117.             else{
  118.             park[newA[1],newA[2]]=paste("A",park[newA[1],newA[2]],sep="")
  119.             park[newB[1],newB[2]]=paste("B",park[newB[1],newB[2]],sep="")
  120.             }
  121.        
  122.         #Search nieghbouring squares
  123.        
  124.         if(verbose==2)plotpark()
  125.     }
  126.     if(verbose)plotpark()
  127.     if(verbose)cat("\n\nFound in: ",counter)
  128. return(c(counter,summary(as.numeric(park[park!=0]))))
  129. }
  130.  
  131. simulate()
  132. simulate(verbose=T)
  133.  
  134. nruns=1e5
  135.  
  136. if(F){
  137. a=rep(0,nruns)
  138. dataA=data.frame(steps=a,min=a,q1=a,md=a,me=a,q3=a,max=a)
  139. dataAB=data.frame(steps=a,min=a,q1=a,md=a,me=a,q3=a,max=a)
  140. for(i in 1:nruns)dataA[i,]=simulate(F)
  141. for(i in 1:nruns)dataAB[i,]=simulate(T)
  142.  
  143.  
  144. print(summary(dataA))
  145. print(summary(dataAB))
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement