Advertisement
DoomProg

MonteCarloPercolation

Jan 31st, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.48 KB | None | 0 0
  1. // Monte Carlo Simulation
  2.  
  3. /*
  4. -> The program creates a n*n grid filled with zeros
  5. -> It starts puting 1's in random places
  6. -> When there is a path of 1's connecting top and bottom it stops
  7.  
  8. */
  9. object monteCarloPercolation{
  10.  
  11.     class MonteCarloFiller(val n:Int) {
  12.  
  13.  
  14.        
  15.         val rnd = new scala.util.Random
  16.         val monteAr = Array.fill[String](n,n)("0")
  17.         val boxes = Array.fill[Int](n*n)(-1)
  18.  
  19.         // CoordsToOneDimension
  20.         def ctod(i : Int , j : Int) : Int = n*i + j
  21.  
  22.         def addRanomPiece() = {
  23.             val i = rnd.nextInt(n)
  24.             val j = rnd.nextInt(n)
  25.             monteAr(i)(j) = "1"
  26.             (i,j)
  27.         }
  28.  
  29.         def union(p:Int , q:Int) {
  30.             val pboxes = boxes(p)
  31.             val qboxes = boxes(q)
  32.             (0 until boxes.length).foreach{ i =>
  33.                 if ( boxes(i) == pboxes ) boxes(i) = qboxes
  34.             }
  35.         }      
  36.  
  37.         def hasPercolated() : (Boolean , Int) = {  
  38.             // check if the virtual top side is  
  39.             // connected to the virtual bottom side
  40.             for (i<-0 until n){
  41.                 for (j<-n*(n-1) until n*n){
  42.                     if ( boxes(i) == boxes(j) && boxes(i) != -1 ) {
  43.                         return (true , i)
  44.                     }
  45.                 }
  46.             }
  47.             (false , -1)           
  48.         }
  49.  
  50.         def getNeighbors( i : Int , j : Int ) = {
  51.             var neighbors: List[Int]  = List.empty
  52.             try{
  53.                 if ( monteAr( i - 1 )(j) != "0")
  54.                     neighbors = ctod( i - 1 , j) :: neighbors
  55.             }
  56.             catch {case e:Exception =>}
  57.  
  58.             try {
  59.                 if ( monteAr( i + 1 )(j) != "0")
  60.                         neighbors = ctod( i + 1 , j) :: neighbors
  61.             }catch{case e:Exception =>}
  62.  
  63.             try {
  64.                 if ( monteAr( i )(j - 1) != "0")
  65.                         neighbors = ctod( i , j - 1) :: neighbors
  66.             }catch{case e:Exception =>}
  67.  
  68.             try {
  69.                 if ( monteAr( i )(j + 1) != "0")
  70.                         neighbors = ctod( i , j + 1) :: neighbors
  71.             }catch{ case e:Exception => }
  72.             neighbors
  73.         }
  74.        
  75.         def doUntilPercolation() : Int = {
  76.             def inner(times:Int) : Int =  {
  77.                 val per = hasPercolated()
  78.                 if ( per._1  ) return times
  79.                 else {
  80.                     val coords = addRanomPiece()
  81.                     val sumindex = ctod( coords._1 , coords._2 )
  82.                     val neighbors = getNeighbors( coords._1 , coords._2)
  83.                     boxes(sumindex) = sumindex
  84.                     for ( i<-0 until neighbors.length ) {
  85.                         union(sumindex , neighbors(i) )
  86.                     }  
  87.                 }
  88.                 inner(times+1)
  89.             }
  90.             inner(0)
  91.         }
  92.  
  93.         def showArray() {
  94.             println("-"*20)
  95.             for (i<-0 until n){
  96.                 for (j<-0 until n){
  97.                     print(monteAr(i)(j) + " ")
  98.                 }
  99.                 println()
  100.             }
  101.             println("-"*20)
  102.         }
  103.     }
  104.  
  105.  
  106.     def main(args: Array[String]): Unit = {  
  107.       val mc = new MonteCarloFiller(10)
  108.       mc.doUntilPercolation()
  109.       mc.showArray()
  110.     }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement