Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 19th, 2013  |  syntax: Scala  |  size: 2.59 KB  |  views: 59  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package simulations
  2.  
  3. import math.random
  4.  
  5. class EpidemySimulator extends Simulator {
  6.  
  7.   def randomBelow(i: Int) = (random * i).toInt
  8.   def randomTo(i: Int) = (random * (i+1)).toInt
  9.  
  10.   protected[simulations] object SimConfig {
  11.     val population: Int = 300
  12.     val roomRows: Int = 8      
  13.     val roomColumns: Int = 8
  14.  
  15.     // to complete: additional parameters of simulation
  16.    
  17.     val incubationTime = 6
  18.     val dieTime = 14
  19.     val immuneTime = 16
  20.     val healTime = 18
  21.     val prevalenceRate = 0.01
  22.     val transRate = 0.4
  23.     val dieRate = 0.25
  24.   }
  25.  
  26.   import SimConfig._
  27.  
  28.   val persons: List[Person] =
  29.     for {
  30.     i <- (0 until population).toList
  31.   } yield {
  32.     val p = new Person(i)
  33.     if (i < population * prevalenceRate)
  34.       p.setInfected()
  35.     p.mode()
  36.     p
  37.   }
  38.  
  39.   class Person(val id: Int) {
  40.     var infected = false
  41.     var sick = false
  42.     var immune = false
  43.     var dead = false
  44.  
  45.     // demonstrates random number generation
  46.     var row: Int = randomBelow(roomRows)
  47.     var col: Int = randomBelow(roomColumns)
  48.  
  49.     def setInfected() {
  50.       infected = true
  51.       afterDelay(incubationTime)(setSick)
  52.       afterDelay(dieTime)(setDead)
  53.       afterDelay(immuneTime)(setImmune)
  54.       afterDelay(healTime)(setHealthy)
  55.     }
  56.  
  57.     def setSick() {
  58.       sick = true
  59.     }
  60.  
  61.     def setDead() {
  62.       if (random < dieRate)
  63.         dead = true
  64.     }
  65.  
  66.     def setImmune() {
  67.       if (dead) return
  68.       sick = false
  69.       immune = true
  70.     }
  71.  
  72.     def setHealthy() {
  73.       if (dead) return
  74.       immune = false
  75.       infected = false
  76.     }
  77.  
  78.     def mode() {
  79.       val moveDelay = randomTo(5)
  80.       afterDelay(moveDelay)(move)
  81.     }
  82.  
  83.     def move() {
  84.       if (dead) return
  85.       // Move to a neighbor room
  86.       val neighbors = List(((row - 1 + roomRows) % roomRows, col),
  87.         ((row + 1) % roomRows, col),
  88.         (row, (col - 1 + roomColumns) % roomColumns),
  89.         (row, (col + 1) % roomColumns))
  90.        
  91.       def isHealthy(room: (Int, Int)): Boolean = room match {
  92.         case (r, c) => (persons.find { p => p.row == r && p.col == c && (p.sick || p.dead) }).isEmpty
  93.       }
  94.       val candidates = neighbors filter isHealthy
  95.       if (!candidates.isEmpty) {
  96.         val candidate: (Int, Int) = candidates(randomBelow(candidates.length))
  97.         candidate match {
  98.           case (a, b) => row = a; col = b
  99.         }
  100.       }
  101.       if (!immune && !infected)
  102.         if (random < transRate)
  103.           if (!(persons.find { p => p.row == row && p.col == col && p.infected }).isEmpty)
  104.             setInfected()
  105.  
  106.       mode()
  107.     }
  108.   }
  109. }