假设我想使用Genetics
库来解决问题,并且我打算说20代。但是,我想在前10代应用Elitism
,并在其余的几代禁用它。我该怎么办?
答案 0 :(得分:0)
您可以使用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);
}
}