0

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.

  • What does getFittest do? What the error is telling you is that you have an infinite recursion in your code. So i would guess that getFittest never returns a value smaller than or equal to 10. – MissingSemiColon Jul 17 '20 at 11:13
  • getFittest runs the current population of chromosomes through the maze and returns a fitness integer of the best chromosome, the maze is a 20x20 multidimensional array, and the fitness function is the Manhattan distance between the player's position upon the execution of the chromosome and the finishing point. – Jack Morrill Jul 17 '20 at 11:21

1 Answers1

2

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;
Joni
  • 108,737
  • 14
  • 143
  • 193