public static void main(final String[] args) { final Engine<AnyGene<LocalDate>, Integer> engine = Engine .builder(LastMonday::fitness, CODEC) .offspringSelector(new RouletteWheelSelector<>()) .build(); final Phenotype<AnyGene<LocalDate>, Integer> best = engine.stream() .limit(50) .collect(EvolutionResult.toBestPhenotype()); System.out.println(best); }
@Test(dataProvider = "expectedDistribution", groups = {"statistics"}) public void selectDistribution(final Named<double[]> expected, final Optimize opt) { retry(3, () -> { final int loops = 50; final int npopulation = POPULATION_COUNT; final Random random = new Random(); using(random, r -> { final Histogram<Double> distribution = SelectorTester.distribution( new RouletteWheelSelector<>(), opt, npopulation, loops ); assertDistribution(distribution, expected.value, 0.001, 5); }); }); }
public static void main(final String[] args) { final Knapsack knapsack = Knapsack.of(15, new Random(123)); // Configure and build the evolution engine. final Engine<BitGene, Double> engine = Engine.builder(knapsack) .populationSize(500) .survivorsSelector(new TournamentSelector<>(5)) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build(); // Create evolution statistics consumer. final EvolutionStatistics<Double, ?> statistics = EvolutionStatistics.ofNumber(); final Phenotype<BitGene, Double> best = engine.stream() // Truncate the evolution stream after 7 "steady" // generations. .limit(bySteadyFitness(7)) // The evolution will stop after maximal 100 // generations. .limit(100) // Update the evaluation statistics after // each generation .peek(statistics) // Collect (reduce) the evolution stream to // its best phenotype. .collect(toBestPhenotype()); System.out.println(statistics); System.out.println(best); }
BitChromosome.of(20, 0.15)) .populationSize(500) .selector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.55),
@Test public void minimize() { using(new Random(7345), r -> { final Function<Genotype<IntegerGene>, Integer> ff = g -> g.getChromosome().getGene().getAllele(); final Factory<Phenotype<IntegerGene, Integer>> ptf = () -> Phenotype.of(Genotype.of(IntegerChromosome.of(0, 100)), 1, ff); final ISeq<Phenotype<IntegerGene, Integer>> population = IntStream.range(0, 1000) .mapToObj(i -> ptf.newInstance()) .collect(ISeq.toISeq()); final RouletteWheelSelector<IntegerGene, Integer> selector = new RouletteWheelSelector<>(); final double[] p = selector.probabilities(population, 100, Optimize.MINIMUM); Assert.assertTrue(RouletteWheelSelector.sum2one(p), Arrays.toString(p) + " != 1"); }); }
@Test public void maximize() { using(new Random(7345), r -> { final Function<Genotype<IntegerGene>, Integer> ff = g -> g.getChromosome().getGene().getAllele(); final Factory<Phenotype<IntegerGene, Integer>> ptf = () -> Phenotype.of(Genotype.of(IntegerChromosome.of(0, 100)), 1, ff); final ISeq<Phenotype<IntegerGene, Integer>> population = IntStream.range(0, 1000) .mapToObj(i -> ptf.newInstance()) .collect(ISeq.toISeq()); final RouletteWheelSelector<IntegerGene, Integer> selector = new RouletteWheelSelector<>(); final double[] p = selector.probabilities(population, 100, Optimize.MAXIMUM); Assert.assertTrue(RouletteWheelSelector.sum2one(p), Arrays.toString(p) + " != 1"); }); }
.populationSize(500) .survivorsSelector(new TournamentSelector<>(5)) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.115),
@Test public void streamWithSerializedPopulation() throws IOException { // Problem definition. final Problem<Double, DoubleGene, Double> problem = Problem.of( x -> cos(0.5 + sin(x))*cos(x), Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI)) ); // Define the GA engine. final Engine<DoubleGene, Double> engine = Engine.builder(problem) .optimize(Optimize.MINIMUM) .offspringSelector(new RouletteWheelSelector<>()) .build(); final EvolutionResult<DoubleGene, Double> interimResult = engine.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); final ByteArrayOutputStream out = new ByteArrayOutputStream(); IO.object.write(interimResult, out); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); @SuppressWarnings("unchecked") final EvolutionResult<DoubleGene, Double> loadedResult = (EvolutionResult<DoubleGene, Double>)IO.object.read(in); final EvolutionResult<DoubleGene, Double> result = engine .stream(loadedResult) .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }
/** * Create a new {@link Engine} for solving the {@link Knapsack} problem. The * engine is used for testing purpose. * * @see Knapsack#of(int, Random) * * @param populationSize the population size of the created engine * @param random the random engine used for creating the {@link Knapsack} * problem instance * @return a new {@link Knapsack} solving evolution {@link Engine} */ public static Engine<BitGene, Double> knapsack( final int populationSize, final Random random ) { // Search space fo 2^250 ~ 10^75. final Knapsack knapsack = Knapsack.of(250, random); // Configure and build the evolution engine. return Engine.builder(knapsack) .populationSize(populationSize) .survivorsSelector(new TournamentSelector<>(5)) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.03), new SinglePointCrossover<>(0.125)) .build(); }
public static void main(final String[] args) { final RectFill problem = new RectFill(new Rect(0, 100, 0, 100)); final Engine<AnyGene<Rect>, Double> engine = Engine.builder(problem) .individualCreationRetries(10) .genotypeValidator(gt -> true) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new SwapMutator<>(), new SinglePointCrossover<>()) .build(); final ISeq<Rect> best = problem.codec().decode( engine.stream() .limit(byFixedGeneration(10)) .collect(EvolutionResult.toBestGenotype()) ); System.out.println(best); }
@Test public void initialResult() { // Problem definition. final Problem<Double, DoubleGene, Double> problem = Problem.of( x -> cos(0.5 + sin(x))*cos(x), Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI)) ); // Define the GA engine. final Engine<DoubleGene, Double> engine = Engine.builder(problem) .optimize(Optimize.MINIMUM) .offspringSelector(new RouletteWheelSelector<>()) .build(); final EvolutionResult<DoubleGene, Double> interimResult = engine.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); engine.builder() .alterers(new Mutator<>()).build() .stream(interimResult); }
static Engine<EnumGene<Integer>, Integer> buildEngine(Codec<ISeq<Integer>, EnumGene<Integer>> codec) { return Engine.builder(E305::fitness, codec) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new SwapMutator<>(), new PartiallyMatchedCrossover<>(0.9) ) .populationSize(20) .optimize(Optimize.MINIMUM) .build(); }
final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Selector<DoubleGene, Double> survivorsSelector = new RouletteWheelSelector<>(); final Selector<DoubleGene, Double> offspringSelector = new BoltzmannSelector<>(); final Alterer<DoubleGene, Double> alterer = new GaussianMutator<>();
new RouletteWheelSelector<>() }, new RouletteWheelSelector<>() }, new RouletteWheelSelector<>() }, new RouletteWheelSelector<>() },
public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); final Engine<DoubleGene, Double> engine1 = Engine.builder(problem) .minimizing() .alterers(new Mutator<>(0.2)) .selector(new MonteCarloSelector<>()) .build(); final Engine<DoubleGene, Double> engine2 = Engine.builder(problem) .minimizing() .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build(); final Genotype<DoubleGene> result = ConcatEngine.of( engine1.limit(50), engine2.limit(() -> Limits.bySteadyFitness(30))) .stream() .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); }
public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); final Engine<DoubleGene, Double> engine1 = Engine.builder(problem) .minimizing() .alterers(new Mutator<>(0.2)) .selector(new MonteCarloSelector<>()) .build(); final Engine<DoubleGene, Double> engine2 = Engine.builder(problem) .minimizing() .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build(); final Genotype<DoubleGene> result = CyclicEngine.of( engine1.limit(50), engine2.limit(() -> Limits.bySteadyFitness(30))) .stream() .limit(Limits.bySteadyFitness(1000)) .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); }
private static EvolutionStreamable<DoubleGene, Double> engine( final EvolutionResult<DoubleGene, Double> result, final Engine.Builder<DoubleGene, Double> builder ) { return var(result) < 0.2 ? builder.copy() .alterers(new Mutator<>(0.5)) .selector(new MonteCarloSelector<>()) .build() .limit(5) : builder.copy() .alterers( new Mutator<>(0.05), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build() .limit(15); }