Search code examples
javagenetic-algorithmjenetics

How to plot the fitness of different generations in my GA using jenetics


In general how would you print out or plot the fitness scores of each generation created using the jenetics library?

More specific on my own code:

private static double clashes(final Genotype<EnumGene<Integer>> gt) {
        // Calculate the path distance.



                final int[] intTriplets=gt.getChromosome().stream().mapToInt(EnumGene<Integer>::getAllele).toArray();
                ArrayList<Triplet> triplets=new ArrayList<Triplet>();
                for(int i=0;i<intTriplets.length;i++)
                {
                    Triplet e=intToTriplet.get(intTriplets[i]);
                    triplets.add(e);
                }
                double clashes=returnScore(triplets);

        return (clashes);



public static void main(String[] args) {
        final Engine<EnumGene<Integer>, Double> engine = Engine
            .builder(
                GA::clashes,
                PermutationChromosome.ofInteger(REQUIREMENTS))
            .optimize(Optimize.MINIMUM)
                        .offspringFraction(0.75)//0.6 standaard
                        .survivorsSelector (new TournamentSelector <>(7) )  //standaard new TournamentSelector <>(3)
                        .offspringSelector (new RouletteWheelSelector <>() )  //standaard new TournamentSelector <>(3)
            .maximalPhenotypeAge(40)//standaard 70
            .populationSize(1000)

                       //.selector(new TournamentSelector<>(5))
            .alterers(
                new SwapMutator<>(0.02),
                new PartiallyMatchedCrossover<>(0.7))
            .build();

I noticed that there might be premature convergence, as i am only getting a limited amount of generations despite setting the steady state fitness limit at 200. So i'd like to figure out around what generation the change in fitness score gets close to 0, as well as printing the fitness score for the best element in each generation.


Solution

  • You can print out information during evolution process with the Stream.peek method: java EvolutionResult<EnumGene<Integer>, Double> stream = engine.stream() .limit(100) .peek(er -> System.out.println(er.getBestPhenotype())) .collect(EvolutionResult.toBestEvolutionResult()); This will print the best phenotype for each generation.