2

假设我想用这个Jenetics库来解决一个问题,我打算说 20 代。但是,我想申请Elitism前 10 代并为剩余的代禁用它。我该怎么做?

4

1 回答 1

1

您可以使用模块ConcatEngine中的 ,来完成此操作io.jenetics.ext

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);
    }
}
于 2018-12-02T21:16:20.727 回答