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 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()); }
@Test public void populationEvaluator() { final int populationSize = 100; final AtomicInteger count = new AtomicInteger(); final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .populationSize(populationSize) .evaluator((gt, ff) -> { count.compareAndSet(0, gt.length()); return gt.stream().map(ff).collect(ISeq.toISeq()); }) .build(); engine.stream() .limit(1) .collect(EvolutionResult.toBestGenotype()); Assert.assertEquals(count.get(), populationSize); }
/** * Create a new builder, with the current configuration. * * @since 3.1 * * @return a new builder, with the current configuration */ @Override public Builder<G, C> copy() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .executor(_executor) .evaluator(_evaluator) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction(_offspringFraction) .offspringSelector(_offspringSelector) .phenotypeValidator(_validator) .optimize(_optimize) .populationSize(_populationSize) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }
/** * Create a new evolution {@code Engine.Builder} initialized with the values * of the current evolution {@code Engine}. With this method, the evolution * engine can serve as a template for a new one. * * @return a new engine builder */ public Builder<G, C> builder() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .evaluator(_evaluator) .executor(_executor.get()) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction((double)_offspringCount/(double)getPopulationSize()) .offspringSelector(_offspringSelector) .optimize(_optimize) .phenotypeValidator(_validator) .populationSize(getPopulationSize()) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }
public static void main(String[] args) throws IOException { final Knapsack knapsack = Knapsack.of(15, new Random(123)); final Engine<BitGene, Double> engine = Engine.builder(knapsack) .populationSize(500) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .evaluator(BatchEvalKnapsack::batchEval) .evaluator(pop -> { pop.forEach(Phenotype::evaluate); return pop.asISeq(); }) .build(); final Phenotype<BitGene, Double> best = engine.stream() .limit(bySteadyFitness(20)) .collect(toBestPhenotype()); System.out.println(best); }