Advertisement
jules0707

PercolationStats

Nov 16th, 2017
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.  *  Author:       Jules Martel (julesmartel@gmail.com)
  4.  *  Date:         Tuesday 9th Feb. 2016
  5.  *  Task: http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
  6.  *  This program estimates the means Standard deviation and percolation threshold
  7.  *  value of a size N grid.
  8.  *  It reads 2 parameters from command line times is the number of experiment
  9.  *  and N the grid Size
  10.  * ------------------------------TEST RESULTS----------------------------------------
  11.                     //------- with Percolation.java ---------
  12.  * input (n,T) =500 500
  13.  * with WeightedQuickUnionUF through Percolation.java implementation elapsed time of MonteCarlo(n, T)=
  14.  * (500,  * 500): 9.354
  15.  * mean:                   = 0.5928887759999997
  16.  * stddev                  = 0.005109796339884218
  17.  * 95% confidence interval = [0.592440882602887, 0.5933366693971124 ]
  18.  * Process finished with exit code 0
  19.  
  20.  * input (n,T) = 150 100
  21.  * with QuickUnionUF through Percolation.java implementation elapsed time of MonteCarlo(n, T)= (150, 100):      
  22.  * 29.345
  23.  * mean:                   = 0.5935008888888889
  24.  * stddev                  = 0.011125106974212842
  25.  * 95% confidence interval = [0.5913203679219431, 0.5956814098558346 ]
  26.  * Process finished with exit code 0
  27.  
  28.                     //------- with PercoScala.scala ---------
  29.  * input (n,T) = 150 100
  30.  * with QuickUnionUF through PercoScala implementation elapsed time of MonteCarlo(n, T)= (150, 100): 11.305
  31.  * mean:                   = 0.5932506666666664
  32.  * stddev                  = 0.011555774874893586
  33.  * 95% confidence interval = [0.5909857347911872, 0.5955155985421455 ]
  34.  * Process finished with exit code 0
  35.  
  36.  * input (n,T) = 500 500
  37.  * with WeightedQuickUnionUF through PercoScala implementation elapsed time of MonteCarlo(n, T)= (500,
  38.  * 500): 9.917
  39.  * mean:                   = 0.5927400480000005
  40.  * stddev                  = 0.005282813982419002
  41.  * 95% confidence interval = [0.5922769889378552, 0.5932031070621457 ]
  42.  * Process finished with exit code 0
  43.  ******************************************************************************/
  44.  
  45. import edu.princeton.cs.algs4.StdIn;
  46. import edu.princeton.cs.algs4.StdRandom;
  47. import edu.princeton.cs.algs4.StdStats;
  48.  
  49. public class PercolationStats {
  50.     private static final double CONFIDENCE_95 = 1.96;
  51.     private final int times;
  52.     private final double[] thresholds;
  53.     private final double stddev;
  54.     private final double mean;
  55.  
  56.     public PercolationStats(int n, int times) {
  57.         this.times = times;
  58.         if (n <= 0 || times <= 0)
  59.             throw new java.lang.IllegalArgumentException("not valid indices");
  60.  
  61.         thresholds = new double[times];
  62.         // runs times times the monteCarlo simulation
  63.         for (int i = 0; i < times; i++) {
  64.             thresholds[i] = monteCarlo(n);
  65.         }
  66.         mean = StdStats.mean(thresholds);
  67.         stddev = StdStats.stddev(thresholds);
  68.     }
  69.  
  70.     public double mean() {
  71.         return mean;
  72.     }
  73.  
  74.     public double stddev() {
  75.         return stddev;
  76.     }
  77.  
  78.     public double confidenceLo() {
  79.         return (mean - ((CONFIDENCE_95 * stddev) / Math.sqrt(times)));
  80.     }
  81.  
  82.     public double confidenceHi() {
  83.         return (mean + ((CONFIDENCE_95 * stddev) / Math.sqrt(times)));
  84.     }
  85.  
  86.     private double monteCarlo(int n) {
  87.         Percolation percolation = new Percolation(n);
  88.         int p;
  89.         int q;
  90.         int opened = 0;
  91.         // as long as the system does not percolate whe open a new random site
  92.         while (!percolation.percolates()) {
  93.             p = StdRandom.uniform(1, n + 1); // a random site index in a n size grid + 1 as last item is excluded
  94.             q = StdRandom.uniform(1, n + 1); // an other random site
  95.             if (!percolation.isOpen(p, q)) {
  96.                 percolation.open(p, q);
  97.                 opened++;
  98.             }
  99.         }
  100.         return opened * 1.0 / (n * n);
  101.     }
  102.  
  103.     public static void main(String[] args) {
  104.         int n = StdIn.readInt();
  105.         int t = StdIn.readInt();
  106.         PercolationStats stats = new PercolationStats(n, t);
  107.         System.out.println("mean:                   = " + stats.mean);
  108.         System.out.println("stddev                  = " + stats.stddev);
  109.         System.out.println("95% confidence interval = [" + stats.confidenceLo()
  110.                 + ", " + stats.confidenceHi() + " ]");
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement