Advertisement
jules0707

PercoStats

Nov 28th, 2017
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.17 KB | None | 0 0
  1. /**
  2.   * Benchmark WeightedQuickUnionUF vs QuickUnionUF
  3.   * with Warmer config from Scalameter see below. WQUF can scale (linear) not QUF (exponential)
  4.   * -------------------------------------------------------------------------------------                          
  5.   * double n            Time in ms             
  6.   * (n,t)                (50, 50)   (100, 50)   (200, 50)   (400, 50)   Trend line
  7.   * QuickUnionUF              50        914         12082       179841      T(N) = 3.8 N^4
  8.   * WeightedQuickUnionUF      6         22          90          395         T(N)= 124 N – 181
  9.   * --------------------------------------------------------------------------------------
  10.   * double t            Time in ms             
  11.   * (n,t)                (50, 10)   (50, 20)    (50, 40)    (50, 80)    Trend line
  12.   * QuickUnionUF              10        19          41          83          T(N) = 1.03 N (proportional)
  13.   * WeightedQuickUnionUF      1         2           4           9           T(N) = 0.11 N
  14.   */
  15.  
  16. /** System:
  17.   *8-core 3.1 GHz i7-4770S Linux Centos 2.6.32, java version "1.8.0_121"
  18.   *Java SE Runtime Environment (build 1.8.0_121-b13)
  19.   *Java HotSpot 64-Bit Server VM (build 25.121-b13, mixed mode)
  20.   *Scala 2.12.0-M1
  21. */
  22.  
  23. import edu.princeton.cs.algs4.StdStats
  24. import org.scalameter._
  25. import scala.util.Random
  26. import scala.io.StdIn._
  27.  
  28.  
  29. class PercoStats(n: Int, times: Int) {
  30.  
  31.   val CONFIDENCE_95 = 1.96
  32.  
  33.   // we build an immutable collection of percolation thresholds
  34.   // a List[Double]
  35.   val thresholds: List[Double] = (
  36.     for (_ <- 1 until times)
  37.       yield monteCarlo(n)
  38.     ).toList
  39.  
  40.  
  41.   // call of the Princeton API with Scala List to Java Array conversion
  42.   val mean = StdStats.mean(thresholds.toArray)
  43.   val stddev = StdStats.stddev(thresholds.toArray)
  44.   val confidenceLo = mean - ((CONFIDENCE_95 * stddev) / Math.sqrt(times))
  45.   val confidenceHi = mean + ((CONFIDENCE_95 * stddev) / Math.sqrt(times))
  46.  
  47.  
  48.   def monteCarlo(n: Int): Double = {
  49.     val perc: PercoScala = new PercoScala(n)
  50.     var opend = 0.0
  51.     while (!perc.percolates) {
  52.       val p = math.abs(Random.nextInt(n)) + 1
  53.       val q = math.abs(Random.nextInt(n)) + 1
  54.       if (!perc.isOpen(p, q)) {
  55.         perc.open(p, q)
  56.         opend += 1
  57.       }
  58.     }
  59.     opend / (n * n)
  60.   }
  61. }
  62.  
  63. // we use scalameter to test MonteCarlo method using WQUF vs. QUF implementations
  64. object PercoStats extends Bench.LocalTime {
  65.  
  66.   override def main(args: Array[String]) = {
  67.     val Array(n, t) = readLine.split(" ").map(_.toInt)
  68.  
  69.     /* configuration */
  70.     val stdConfig = config(
  71.       Key.exec.minWarmupRuns -> 50,
  72.       Key.exec.maxWarmupRuns -> 100,
  73.       Key.verbose -> true,
  74.       Key.exec.independentSamples -> 16,
  75.       Key.exec.warmupCovThreshold -> 0.0001
  76.     ) withWarmer (new Warmer.Default) withMeasurer (new Measurer.IgnoringGC)
  77.  
  78.  
  79.     /* tests */
  80.     val time: Quantity[Double] = stdConfig measure {
  81.       val stats: PercoStats = new PercoStats(n, t)
  82.       /*println("mean:                   = " + stats.mean)
  83.       println("stddev                  = " + stats.stddev)
  84.       println("95% confidence interval = [" + stats.confidenceLo
  85.         + ", " + stats.confidenceHi + " ]")*/
  86.     }
  87.  
  88.     println(s"With WeightedQuickUnionUF through Percolation.java implementation" +
  89.       s" elapsed time of MonteCarlo(n, T)= ($n,  $t): $time"
  90.     )
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement