I have a problem relating to the percolation model with the latest version of Java
My project includes three file a illegalargumentexception.java file (use to handle illegal arguments), a percolation.java file (the datatype), and the PercolationStats.java file (use to calculate and generate the statistic)
In my expectation, the code should generate the probability of the percolated model (after a specific time of trials)
And here is my code:
the percolation datatype:
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class percolation {
public static boolean[] grid;
public static WeightedQuickUnionUF gridMap ;
public static int openCell;
public static int top = 0;
private static int current_index;
public int bottom;
private int n;
public percolation(int n) {
}
// creates n-by-n grid, with all sites initially blocked
public int Percolation (int n) {
gridMap = new WeightedQuickUnionUF(n*n + 2);
grid = new boolean[n*n + 2];
this.n =n;
openCell = 0;
top = 0;
bottom = n*n +1;
if (n <=0) {
throw new IllegalArgumentException("Grid's number can not negative or equal to zero");
}
return n ;
}
// opens the site (row, col) if it is not open already
public void open( int row, int col) {
current_index = (row-1)*n + (col-1);
grid[percolation.current_index] = true;
percolation.openCell++;
if (row == 1)
gridMap.union(percolation.current_index, top);
if(row == this.n)
gridMap.union(current_index, col);
if (row > this.n || col > this.n)
throw new IllegalArgumentException("Out of bound");
if (row <1 || col <1)
throw new IllegalArgumentException("The index must be greater than zero");
if(row >1 && isOpen(row-1, col)) // above cell
{
assert(current_index > n);
gridMap.union(current_index,current_index-n);
}
if (row < this.n && isOpen(row + 1 , col)) // below cell
{
assert(current_index + n < n * n);
gridMap.union(current_index , current_index + n);
}
if(col > 1 && isOpen(row, col - 1)) //left cell
{
gridMap.union(current_index,current_index- 1);
}
if(col < this.n && isOpen(row, col + 1)) //right cell
{
gridMap.union(current_index,current_index + 1);
}
return ;
}
// is the site (row, col) open?
public boolean isOpen(int row, int col) {
return grid[current_index];
}
// is the site (row, col) full?
public boolean isFull(int row, int col)
{
if(!isOpen(row,col)) return false;
return gridMap.find(current_index) == gridMap.find(top);
}
public int numberOfOpenSites() // returns the number of open sites
{
return percolation.openCell;
}
// does the system percolate?
public boolean percolates() {
return gridMap.find(top) == gridMap.find(bottom);
}
// test client (optional)
public static void main(String[] args) {
}
}
the PercolationStats.java file
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
public class PercolationStats {
public static double[] result;
public static int trials;
// perform independent trials on an n-by-n grid
public PercolationStats(int n, int trials) {
double[] result = new double[trials];
int test_row ;
int test_col ;
for (int i=0; i <trials; i++) {
percolation per = new percolation(n);
while (!per.percolates()) {
test_row = (int)((StdRandom.uniformInt(n)*n) +1 );
test_col = (int)((StdRandom.uniformInt(n)*n) +1 );
if( !per.isOpen(test_row,test_col) )
per.open(test_row,test_col);
}
result[i] = (double)(per.numberOfOpenSites()) / (n * n);
}
if(n <= 0 || trials <= 0) throw new IllegalArgumentException("Must greater than zero");
}
// sample mean of percolation threshold
public double mean() {
return StdStats.mean(result);
}
// sample standard deviation of percolation threshold
public double stddev() {
return StdStats.stddev(result) ;
}
// low endpoint of 95% confidence interval
public double Low_confidence() {
return (mean() - (1.96 * stddev())/ Math.sqrt(trials));
}
// high endpoint of 95% confidence interval
public double High_confidenceHi() {
return mean() + (1.96 * stddev())/ Math.sqrt(trials);
}
// test client (see below)
public static void main(String[] args) {
PercolationStats s = new PercolationStats(50,10000);
System.out.println("Mean:" + s.mean());
System.out.println("Standard Deviation:"+ s.stddev());
System.out.println("High Confidence" + s.High_confidenceHi());
System.out.println("Low Confidence" + s.Low_confidence());
}
}
My expectation is when I compile the PercolationStats file it will generate the result of the percolating probability threshold like this: (the result if you give the input 200 by 200 grid and try the simulation 100 times
~/Desktop/percolation> java-algs4 PercolationStats 200 100
mean = 0.5929934999999997
stddev = 0.00876990421552567
95% confidence interval = [0.5912745987737567, 0.5947124012262428]
but my result is a bunch of errors that:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "edu.princeton.cs.algs4.WeightedQuickUnionUF.find(int)" because "percolation.gridMap" is null
at percolation.percolates(percolation.java:93)
at PercolationStats.<init>(PercolationStats.java:17)
at PercolationStats.main(PercolationStats.java:58)\
I believe this is to be caused by the percolate datatype, and to be more specific is the line:
public boolean percolates() {
return gridMap.find(top) == gridMap.find(bottom);
}
It states that the gridMap variable of Union-Find is null - I don't know why this happens, I speculate this can relate to the scope of the "gridMap" variable, which is not applicable to all methods in the datatype. That is my hypothesis It could be wrong.
Can anyone give me a clue for why this happens and what should I fix for the successful compilation (I means to generate the probability)
Thanks in advance!
You have not set gridMap
, though it is mentioned in a method called public int Percolation (int n)
. Perhaps you intended that Percolation
is called from body of the constructor which you declared as public percolation(int n)
.
The fix should be simple: move the public int Percolation (int n)
code into the constructor or make constructor public percolation(int n)
call Percolation(n)
.
The class should follow conventions and renamed to Percolation
.