I am attempting to generate all closed curves in a finite region of the simple hexagonal lattice. This isn't too important, it is just a finite set of points a distance of 1 away from each other. My code however, will generate closed curves for a while, and then my program will quit working and get stuck in a infinite loop? I have tried forcing java to garbage collect by command, but the same code stops at different points. As far as I can tell, where it stops is random. Sphere is an array storing all the points in the region under consideration
private static Sphere Sphere1 = new Sphere();
private static double [][] vectors = {{0, 0, 1},{0, 0, -1},{1, 0, 0},{-1,0,0},{.5, (Math.sqrt(3))/2, 0},{-.5, -(Math.sqrt(3))/2, 0},{.5, -(Math.sqrt(3))/2, 0},{-.5, (Math.sqrt(3))/2, 0}};
private static StringBuilder loopString = new StringBuilder("r");
private static String posPoints = "abcdefghijklmnopqrstuvwxyz1234567";
private static int garbage = 0;
private static boolean once = false;
private static PrintWriter output = null;
public static void main (String [] args){
try {
output = new PrintWriter(new FileOutputStream("closedLoops.txt"));
} catch (FileNotFoundException e){
System.out.println("error");
}
System.out.println("start");
KnotPoint loopPoint = new KnotPoint(0,0,1);
addVector(loopPoint, vectors);
}
public static void addVector(KnotPoint loopPoint, double [][] vectors){
garbage ++;
if (garbage == 200){
System.gc();
garbage = 0;
}
if (loopString.length() > 19
&& (loopString.charAt(loopString.length()-1) == 'z' ||
loopString.charAt(loopString.length()-1) == '3' ||
loopString.charAt(loopString.length()-1) == 'u' ||
loopString.charAt(loopString.length()-1) == 'o' ||
loopString.charAt(loopString.length()-1) == 'g' ||
loopString.charAt(loopString.length()-1) == 'j' ||
loopString.charAt(loopString.length()-1) == 'q'))
{
System.out.println(loopString);
output.println(loopString);
once = true;
return;
}
for (int i = 0; i < 8 ; i ++){
if (validAdd(loopPoint, vectors[i], loopString, posPoints)){
loopPoint.addNext(vectors[i]);
addVector(loopPoint, vectors);
//System.gc();
loopString.deleteCharAt(loopString.length()-1);
loopPoint.subtractLast(vectors[i]);
if(loopString.toString().equals("r") ){
output.println("you did it");
output.close();
System.out.println("you did it good job");
System.exit(0);
}
}
}
return;
}
public static boolean validAdd(KnotPoint loopPoint, double [] vector, StringBuilder loopString, String posPoints){
KnotPoint testAdd = new KnotPoint();
testAdd.set(loopPoint);
testAdd.addNext(vector);
int pointIndex = 0;
boolean pointCheck = false;
char point = '.';
for (int i = 0; i < 33; i ++){
if( testAdd.equals(Sphere1.getKnotPoint(i))){
pointIndex = i;
pointCheck = true;
point = posPoints.charAt(i);
}
}
if (pointCheck && !loopString.toString().contains(posPoints.substring(pointIndex, pointIndex + 1))&& point != 'r'){// added not r check
loopString.append(point);
return true;
} else {
return false;
}
}