public PercolationExperiment(int N, int numberOfTimes) { if (N <= 0 || numberOfTimes <= 0) { throw new ArgumentException("parameters must be positive"); } times = numberOfTimes; size = N; p = new double[times]; for (int i = 0; i < times; i++) { double count = 0.0; // number of opened site Percolation percolation = new Percolation(size); // create the grid ready for percolation Random random = new Random(); while (!percolation.CheckIfPercolates()) { var x = random.Next(size) + 1; var y = random.Next(size) + 1; if (!percolation.IsOpen(x, y)) { percolation.OpenByIndex(x, y); count++; } } p[i] = count / (size * size); } }
public PercolationStats(int n, int tries) { if (n < 0 && tries < 0) { throw new ArgumentException("Number of tries and size of the grid must be more than 0"); } _means = new double[tries]; _tries = tries; Random rnd = new Random(); for (int i = 0; i < tries; i++) { Percolation percolation = new Percolation(n); while (!percolation.Percolates()) { int row = rnd.Next(1, n + 1); int col = rnd.Next(1, n + 1); percolation.Open(row, col); } int openSites = percolation.NumberOfOpenSites(); _means[i] = (double)openSites / (n * n); } }
public void TestInputs() { for (var i = 1; i <= 4; i++) { var inputData = File.ReadAllText(string.Format("TestData/input{0}.txt", i)); var inputArray = inputData.Split(' '); var size = int.Parse(inputArray[0]); var percolation = new Percolation(size); Assert.IsFalse(percolation.CheckIfPercolates()); for (var index = 1; index < inputArray.Length; index += 2) { var rowValue = int.Parse(inputArray[index]); var columnValue = int.Parse(inputArray[index + 1]); Assert.IsFalse(percolation.IsOpen(rowValue, columnValue)); percolation.OpenByIndex(rowValue, columnValue); Assert.IsTrue(percolation.IsOpen(rowValue, columnValue)); } Assert.IsTrue(percolation.CheckIfPercolates()); } }
// perform T independent computational experiments on an N-by-N grid public PercolationStats(int N, int T) { Percolation perc = new Percolation(N); for (int i = 0; i < T; i++) { while (!perc.percolates()) { Random rnd = new Random(); int x = rnd.Next(N); int y = rnd.Next(N); perc.open(x, y); } } }