@Override public EvolutionStream<G, C> stream(final EvolutionInit<G> init) { return stream(evolutionStart(init)); }
private EvolutionResult<BitGene, Double> run( final EvolutionResult<BitGene, Double> last, final AtomicBoolean proceed ) { System.out.println("Starting evolution with existing result."); return (last != null ? ENGINE.stream(last) : ENGINE.stream()) .limit(r -> proceed.get()) .collect(EvolutionResult.toBestEvolutionResult()); }
public static void main(final String[] args) { final Engine engine = Engine .builder(MixedGenotype::fitness, ENCODING) .build(); final Phenotype best = (Phenotype)engine.stream() .limit(10) .collect(EvolutionResult.toBestPhenotype()); System.out.println(best); }
public static void main(final String[] args) { final Engine<AnyGene<Dimension>, Double> engine = Engine .builder(Squares::area, Codecs.ofScalar(Squares::nextDimension)) .build(); final Phenotype<AnyGene<Dimension>, Double> pt = engine.stream() .limit(50) .collect(EvolutionResult.toBestPhenotype()); System.out.println(pt); }
public static void main(final String[] args) { final ISeq<Vec<double[]>> front = ENGINE.stream() .limit(2500) .collect(MOEA.toParetoSet(IntRange.of(1000, 1100))) .map(Phenotype::getFitness); }
public static void main(final String[] args) { final OnesCounting problem = new OnesCounting(15, 0.13); final Engine<BitGene, Integer> engine = Engine.builder(problem).build(); final ISeq<BitGene> result = problem.codec().decoder().apply( engine.stream() .limit(10) .collect(EvolutionResult.toBestGenotype()) ); System.out.println(result); }
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); }
public static void main(final String[] args) throws IOException { final EvolutionResult<DoubleGene, Double> rescue = ENGINE.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); final Path path = Paths.get("result.bin"); IO.object.write(rescue, path); @SuppressWarnings("unchecked") final EvolutionResult<DoubleGene, Double> result = ENGINE .stream((EvolutionResult<DoubleGene, Double>)IO.object.read(path)) .limit(Limits.bySteadyFitness(20)) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result); }
public static void main(final String[] args) { final Engine<DoubleGene, Double> engine = Engine .builder(DynamicGenotype::fitness, ENCODING) .alterers(new DynamicMutator<>(0.25)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(20) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestFitness()); }
public static void main(final String[] args) { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(/*Dummy fitness function*/gt -> Double.NaN, gtf) .evaluator(new RxEvaluator<>(RxEvaluator::fitness)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestPhenotype()); }
public static void main(final String[] args) { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(/*Dummy fitness function*/gt -> Double.NaN, gtf) .evaluator(new FutureEvaluator<>(FutureEvaluator::fitness)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestPhenotype()); }
public static void main(final String[] args) { final Engine<ProgramGene<Double>, Double> engine = Engine .builder(SymbolicRegression::error, CODEC) .minimizing() .alterers( new SingleNodeCrossover<>(), new Mutator<>()) .build(); final ProgramGene<Double> program = engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) .getGene(); System.out.println(program.toParenthesesString()); }
@Test(dataProvider = "generations") public void generationCount(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(generations) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@Test public void emptyStreamCollectEvolutionResult() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNull(result); }
public static void main(final String[] args) { final ISeq<Integer> orders = buildOrders(); final Codec<ISeq<Integer>, EnumGene<Integer>> codec = buildCodec(orders); final Engine<EnumGene<Integer>, Integer> engine = buildEngine(codec); final Genotype<EnumGene<Integer>> collect = engine.stream() .limit(1000) .collect(EvolutionResult.toBestGenotype()); final ISeq<Integer> best = codec.decode(collect); System.out.println(best); System.out.println(fitness(best)); }
@Test public void emptyStreamCollectGenotype() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final Genotype<DoubleGene> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestGenotype()); Assert.assertNull(result); }
@Test public void emptyStreamCollectPhenotype() { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final Phenotype<DoubleGene, Double> result = engine.stream() .limit(0) .collect(EvolutionResult.toBestPhenotype()); Assert.assertNull(result); }
@Test(dataProvider = "generations") public void generationLimit(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFixedGeneration(generations)) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@Test public void phenotypeValidator() { final int populationSize = 100; final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .phenotypeValidator(pt -> false) .populationSize(populationSize) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(result.getInvalidCount(), populationSize); }
@Test public void offspringFractionZero() { final Function<Genotype<DoubleGene>, Double> fitnessFunction = gt -> gt.getGene().getAllele(); final Factory<Genotype<DoubleGene>> genotypeFactory = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(fitnessFunction, genotypeFactory) .offspringFraction(0) .build(); engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }