Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * Author: Jules Martel (julesmartel@gmail.com)
- * Date: Tuesday 9th Feb. 2016
- * Task: http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
- * This program estimates the means Standard deviation and percolation threshold
- * value of a size N grid.
- * It reads 2 parameters from command line times is the number of experiment
- * and N the grid Size
- * ------------------------------TEST RESULTS----------------------------------------
- //------- with Percolation.java ---------
- * input (n,T) =500 500
- * with WeightedQuickUnionUF through Percolation.java implementation elapsed time of MonteCarlo(n, T)=
- * (500, * 500): 9.354
- * mean: = 0.5928887759999997
- * stddev = 0.005109796339884218
- * 95% confidence interval = [0.592440882602887, 0.5933366693971124 ]
- * Process finished with exit code 0
- * input (n,T) = 150 100
- * with QuickUnionUF through Percolation.java implementation elapsed time of MonteCarlo(n, T)= (150, 100):
- * 29.345
- * mean: = 0.5935008888888889
- * stddev = 0.011125106974212842
- * 95% confidence interval = [0.5913203679219431, 0.5956814098558346 ]
- * Process finished with exit code 0
- //------- with PercoScala.scala ---------
- * input (n,T) = 150 100
- * with QuickUnionUF through PercoScala implementation elapsed time of MonteCarlo(n, T)= (150, 100): 11.305
- * mean: = 0.5932506666666664
- * stddev = 0.011555774874893586
- * 95% confidence interval = [0.5909857347911872, 0.5955155985421455 ]
- * Process finished with exit code 0
- * input (n,T) = 500 500
- * with WeightedQuickUnionUF through PercoScala implementation elapsed time of MonteCarlo(n, T)= (500,
- * 500): 9.917
- * mean: = 0.5927400480000005
- * stddev = 0.005282813982419002
- * 95% confidence interval = [0.5922769889378552, 0.5932031070621457 ]
- * Process finished with exit code 0
- ******************************************************************************/
- import edu.princeton.cs.algs4.StdIn;
- import edu.princeton.cs.algs4.StdRandom;
- import edu.princeton.cs.algs4.StdStats;
- public class PercolationStats {
- private static final double CONFIDENCE_95 = 1.96;
- private final int times;
- private final double[] thresholds;
- private final double stddev;
- private final double mean;
- public PercolationStats(int n, int times) {
- this.times = times;
- if (n <= 0 || times <= 0)
- throw new java.lang.IllegalArgumentException("not valid indices");
- thresholds = new double[times];
- // runs times times the monteCarlo simulation
- for (int i = 0; i < times; i++) {
- thresholds[i] = monteCarlo(n);
- }
- mean = StdStats.mean(thresholds);
- stddev = StdStats.stddev(thresholds);
- }
- public double mean() {
- return mean;
- }
- public double stddev() {
- return stddev;
- }
- public double confidenceLo() {
- return (mean - ((CONFIDENCE_95 * stddev) / Math.sqrt(times)));
- }
- public double confidenceHi() {
- return (mean + ((CONFIDENCE_95 * stddev) / Math.sqrt(times)));
- }
- private double monteCarlo(int n) {
- Percolation percolation = new Percolation(n);
- int p;
- int q;
- int opened = 0;
- // as long as the system does not percolate whe open a new random site
- while (!percolation.percolates()) {
- p = StdRandom.uniform(1, n + 1); // a random site index in a n size grid + 1 as last item is excluded
- q = StdRandom.uniform(1, n + 1); // an other random site
- if (!percolation.isOpen(p, q)) {
- percolation.open(p, q);
- opened++;
- }
- }
- return opened * 1.0 / (n * n);
- }
- public static void main(String[] args) {
- int n = StdIn.readInt();
- int t = StdIn.readInt();
- PercolationStats stats = new PercolationStats(n, t);
- System.out.println("mean: = " + stats.mean);
- System.out.println("stddev = " + stats.stddev);
- System.out.println("95% confidence interval = [" + stats.confidenceLo()
- + ", " + stats.confidenceHi() + " ]");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement