genetic-algorithmjenetics

Using the Jenetics Library


Assuming I want to use the Jenetics library for a problem and I intend to have say 20 generations. However I want to apply Elitism for the first 10 generations and disable it for the remaining generations. How do I go about it?


Solution

  • You can do this by using the ConcatEngine, available in the io.jenetics.ext module.

    import io.jenetics.DoubleGene;
    import io.jenetics.EliteSelector;
    import io.jenetics.MeanAlterer;
    import io.jenetics.Mutator;
    import io.jenetics.RouletteWheelSelector;
    import io.jenetics.engine.Engine;
    import io.jenetics.engine.EvolutionResult;
    import io.jenetics.engine.EvolutionStreamable;
    import io.jenetics.engine.Limits;
    import io.jenetics.engine.Problem;
    
    import io.jenetics.ext.engine.ConcatEngine;
    
    public class Elite {
        public static void main(final String[] args) {
            // Your problem definition.
            final Problem<double[], DoubleGene, Double> problem = null;
    
            // Evolution Engine with elitism selector.
            final Engine<DoubleGene, Double> eliteEngine = Engine.builder(problem)
                .minimizing()
                .selector(new EliteSelector<>())
                .build();
    
            // Evolution Engine with RouletteWheelSelector.
            final Engine<DoubleGene, Double> remainingEngine = Engine.builder(problem)
                .minimizing()
                .selector(new RouletteWheelSelector<>())
                .build();
    
            // Concatenation of the two engine.
            final EvolutionStreamable<DoubleGene, Double> engine = ConcatEngine.of(
                eliteEngine.limit(10),
                remainingEngine.limit(() -> Limits.bySteadyFitness(30))
            );
    
            final EvolutionResult<DoubleGene, Double> result = engine.stream()
                .collect(EvolutionResult.toBestEvolutionResult());
    
            System.out.println(result);
        }
    }