0

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;
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
izaak berg
  • 11
  • 1
  • why are you calling `System.gc()`? Are you running out of memory at some point? Anyway, `System.gc()` is just a hint is generally a really bad practice in Java. If you have a memory leak it won't do anything for you anyway. – Martin Serrano May 15 '15 at 03:38
  • I suggest removing the gc code - it's very unlikely to help. Perhaps investigate how deep your recursion is going - it's possible it's a runaway which could cause random failures. – sprinter May 15 '15 at 05:47
  • I tried test the maximum depth on my system and checked that against maximum depth of my code. The maximum of my code is 33 stacks, and my system max is between 4000 and 6000 according to tests that I ran. Is that what you mean by runaway recursion? – izaak berg May 21 '15 at 16:39

0 Answers0