Search code examples
javastack-overflowgenetic-algorithmtail-recursion

Tail Recursive Genetic Algorithm throwing "AWT-EventQueue-0" java.lang.StackOverflowError


I have created a project that runs a genetic algorithm to solve a maze, the optimal chromosome is calculated using a tail recursive function to prevent a StackOverflowError.

The error produced by the console is:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.TimSort.mergeLo(Unknown Source)
at java.util.TimSort.mergeAt(Unknown Source)
at java.util.TimSort.mergeCollapse(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.stream.SortedOps$SizedRefSortingSink.end(Unknown Source)
at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.collect(Unknown Source)
at GeneticAlgorithm.searchOptimalMovesTR(GeneticAlgorithm.java:168)
at GeneticAlgorithm.searchOptimalMovesTR(GeneticAlgorithm.java:184)
at GeneticAlgorithm.searchOptimalMovesTR(GeneticAlgorithm.java:184)

The searchOptimalMovesTR and searchOptimalMoves functions are as follows:

//Optimal moves wrapper
    private int[][] searchOptimalMoves(int[][] population) {
        return searchOptimalMovesTR(population, 999);
    }
    
    //search optimal moves using tail recursion
    private int[][] searchOptimalMovesTR(int[][] population, int acceptableScore) {
        runCount++;
        System.out.println(runCount);
        if(acceptableScore <= 10) {
            System.out.println("HIT ACCEPTABLE SCORE");
            return population;
        } 
        else {
            //populate hashmap of chromosome(key), fitness(value)  
            HashMap<Integer[], Integer> fitness = new HashMap<Integer[], Integer>();
            for(int i = 0; i < population.length; i++) {
                Integer score = new Integer(calcFitness(population[i]));
                Integer[] chromosome = new Integer[population[i].length];

                for(int j = 0; j < population[i].length; j++) {
                    chromosome[j] = Integer.valueOf(population[i][j]);
                }
                fitness.put(chromosome, score);
            }
            
            //sort hashmap by value REF THIS https://www.javacodegeeks.com/2017/09/java-8-sorting-hashmap-values-ascending-descending-order.html
            HashMap<Integer[], Integer> sortedFitness = fitness.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(
                    toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
                                
            //turn sorted fitness keys into an array and take half of the length
            Integer[][] arrayFitness = new Integer[sortedFitness.keySet().size()][sortedFitness.size()]; 
            arrayFitness = sortedFitness.keySet().toArray(arrayFitness);
            int[][] halfFitness = new int[arrayFitness.length / 2][arrayFitness[0].length];

            //convert to int primitive type
            for(int i = 0; i < halfFitness.length; i++) {
                for(int j = 0; j < halfFitness[i].length; j++) {
                    halfFitness[i][j] = arrayFitness[i][j].intValue();
                }
            }
            
            //create new population 
            int[][] newpopulation = new int[arrayFitness.length / 2][arrayFitness[0].length];
            newpopulation = crossover(halfFitness, arrayFitness.length, 0.5);
            return searchOptimalMovesTR(newpopulation, getFittest(newpopulation));
        }           
    }

The console line at GeneticAlgorithm.searchOptimalMovesTR(GeneticAlgorithm.java:168) highlights this line of code:

HashMap<Integer[], Integer> sortedFitness = fitness.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect( toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));

I am using this line to sort a hashmap by the value instead of the key to obtain the most effective chromosome according to the fitness value. My initial thoughts were that the use of a hashmap is effecting the stack somehow and causing the program to not be tail recursive, yet I am unfamiliar with another way to sort a hashmap by value as opposed to the key.


Solution

  • Java does not implement the tail recursion optimisation: Tail Call Optimisation in Java You have to replace the recursion with a loop manually. Luckily it's easy:

        while (acceptableScore > 10) {
            runCount++;
            System.out.println(runCount);
            //populate hashmap of chromosome(key), fitness(value)  
            HashMap<Integer[], Integer> fitness = new HashMap<Integer[], Integer>();
            for(int i = 0; i < population.length; i++) {
                Integer score = new Integer(calcFitness(population[i]));
                Integer[] chromosome = new Integer[population[i].length];
    
                for(int j = 0; j < population[i].length; j++) {
                    chromosome[j] = Integer.valueOf(population[i][j]);
                }
                fitness.put(chromosome, score);
            }
            
            //sort hashmap by value REF THIS https://www.javacodegeeks.com/2017/09/java-8-sorting-hashmap-values-ascending-descending-order.html
            HashMap<Integer[], Integer> sortedFitness = fitness.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(
                    toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
                                
            //turn sorted fitness keys into an array and take half of the length
            Integer[][] arrayFitness = new Integer[sortedFitness.keySet().size()][sortedFitness.size()]; 
            arrayFitness = sortedFitness.keySet().toArray(arrayFitness);
            int[][] halfFitness = new int[arrayFitness.length / 2][arrayFitness[0].length];
    
            //convert to int primitive type
            for(int i = 0; i < halfFitness.length; i++) {
                for(int j = 0; j < halfFitness[i].length; j++) {
                    halfFitness[i][j] = arrayFitness[i][j].intValue();
                }
            }
            
            //create new population 
            int[][] newpopulation = new int[arrayFitness.length / 2][arrayFitness[0].length];
            newpopulation = crossover(halfFitness, arrayFitness.length, 0.5);
            population = newpopulation;
            acceptableScore = getFittest(newpopulation));
        }           
        System.out.println("HIT ACCEPTABLE SCORE");
        return population;