使用 Jenetics 库
Using the Jenetics Library
假设我想使用 Jenetics
库来解决问题,并且我打算说 20 代。但是我想为前 10 代应用 Elitism
并为其余几代禁用它。我该怎么做?
您可以使用 io.jenetics.ext
模块中提供的 ConcatEngine
来完成此操作。
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);
}
}
假设我想使用 Jenetics
库来解决问题,并且我打算说 20 代。但是我想为前 10 代应用 Elitism
并为其余几代禁用它。我该怎么做?
您可以使用 io.jenetics.ext
模块中提供的 ConcatEngine
来完成此操作。
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);
}
}