1 year ago
#76393
silicongolem
How can I find the average dead-end length and escape length of a self avoiding walk?
I'm building a program that takes command-line arguments n and trials and computes trials self-avoiding walks in an n-by-n lattice. For each walk, it creates a boolean array, starts the walk in the center, and continues until either a dead end or a boundary is reached. The result of the computation is the percentage of dead ends. Increasing the number of experiments increases the precision.
I have been able to simulate the self-avoiding walk and calculate the average length of the walk, but I am trying to find the average dead-end length and escape length of the simulation. I have tried to find the dead-end length by first using this statement within the dead-end if statement:
deadEndLength += length;
After the if statement:
System.out.println("Average dead-end length" + deadEndLength/deadEnds);
This statement results in a length that is incorrect and too large. Now, I am trying to make deadEndLength an array that is currently present in the code below.
I have also tried implementing Math.abs() for the x and y values to calculate the length, but it doesn't seem to help me find a solution.
n: lattice size
trials: number of trials
deadEnds: number of trials resulting in a dead-end
escapes: number of trials resulting in an escape
length: length of all trials in terms of distance
deadEndLength: length of trial resulting in a dead-end
escapeLength: length of trial resulting in an escape
a[][]: intersections visited
x, y: current position
r: random number in (0, 1)
public class SelfAvoidingWalkMod
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
int deadEnds = 0;
int escapes = 0;
int length = 0;
int escapeLength = 0;
double[] deadEndLengths = new double[trials];
for (int t = 0; t < trials; t++)
{ // Do trials random self-avoiding walks in an n-by-n lattice.
boolean[][] a = new boolean[n][n];
int x = n/2;
int y = n/2;
while (x > 0 && x < n - 1 && y > 0 && y < n - 1)
{ // Check for dead end and make a random move.
a[x][y] = true;
if (a[x-1][y] && a[x+1][y] && a[x][y-1] && a[x][y+1])
{
deadEnds++;
for (int i = deadEnds; i < deadEnds+1; i++)
{
deadEndLengths[i] = length;
}
break;
}
double r = Math.random();
if (r < 0.25) { if (!a[x+1][y]) x++; length++; } // If the value of random is less than 0.25, then if the coordinate at a[x+1][y] is false, add 1 to the x coordinate and add one to length
else if (r < 0.50) { if (!a[x-1][y]) x--; length++; }
else if (r < 0.75) { if (!a[x][y+1]) y++; length++; }
else if (r < 1.00) { if (!a[x][y-1]) y--; length++; }
}
}
escapes = trials - deadEnds;
System.out.println(100 * deadEnds/trials + "% dead ends");
System.out.println("Dead-ends: " + deadEnds);
System.out.println("Length: " + length);
System.out.println("Average length: " + length/trials);
System.out.println("Average dead-end length: ");
System.out.println("Average escape length: ");
}
}
EDIT:
for (int t = 0; t < trials; t++)
{ // Do trials random self-avoiding walks in an n-by-n lattice.
boolean[][] a = new boolean[n][n];
int x = n/2;
int y = n/2;
length = 0;
while (x > 0 && x < n - 1 && y > 0 && y < n - 1)
{ // Check for dead end and make a random move.
a[x][y] = true;
if (a[x-1][y] && a[x+1][y] && a[x][y-1] && a[x][y+1])
{
deadEnds++;
deadEndLength += length;
break;
}
double r = Math.random();
if (r < 0.25) { if (!a[x+1][y]) x++; length++; } // If the value of random is less than 0.25, then if the coordinate at a[x+1][y] is false, add 1 to the x coordinate and add one to length
else if (r < 0.50) { if (!a[x-1][y]) x--; length++; }
else if (r < 0.75) { if (!a[x][y+1]) y++; length++; }
else if (r < 1.00) { if (!a[x][y-1]) y--; length++; }
}
totalLength += length;
}
escapes = trials - deadEnds;
escapeLength = totalLength - deadEndLength;
System.out.println(100 * deadEnds/trials + "% dead ends");
System.out.println("Dead-ends: " + deadEnds);
System.out.println("Total length: " + totalLength);
System.out.println("Average length: " + totalLength/trials);
System.out.println("Total dead-end length: " + deadEndLength);
System.out.println("Average dead-end length: " + deadEndLength/deadEnds);
System.out.println("Total escape length: " + escapeLength);
System.out.println("Average escape length: " + escapeLength/escapes);
}
}
java
arrays
conditional-statements
simulation
random-walk
0 Answers
Your Answer