public static void main(final String[] args) { final ISeq<Genotype<DoubleGene>> population = RandomRegistry.with(new Random(123), r -> CODEC.encoding().instances() .limit(10) .collect(ISeq.toISeq()) ); final Engine<DoubleGene, Double> engine = Engine.builder(Function.identity(), CODEC) .executor(Runnable::run) .build(); final EvolutionResult<DoubleGene, Double> result = RandomRegistry.with(new Random(456), r -> engine.stream(population) .limit(100) .collect(EvolutionResult.toBestEvolutionResult()) ); System.out.println(result.getBestPhenotype()); }
@Test // https://github.com/jenetics/jenetics/issues/318 public void initialGeneConvergence() { RandomRegistry.using(Random32.of(() -> 234), random -> { final Problem<Double, DoubleGene, Double> problem = Problem.of( d -> 1.0, Codecs.ofScalar(DoubleRange.of(0, 1)) ); final Engine<DoubleGene, Double> engine = Engine .builder(problem) .executor(Runnable::run) .build(); final AtomicInteger count = new AtomicInteger(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byGeneConvergence(0.03, 0.03)) .peek(er -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertNotNull(result); Assert.assertEquals(count.get(), 1); Assert.assertEquals(result.getTotalGenerations(), 1); Assert.assertEquals(result.getGeneration(), 1); }); }
public static void main(final String[] args) { //final ExecutorService executor = Executors.newFixedThreadPool(2); final Engine<DoubleGene, Double> engine = Engine .builder(EngineExecutorTest::f, DoubleChromosome.of(0, 1)) .executor(new ForkJoinPool(10)) .build(); for (int i = 0; i < 1000; ++i) { final Double result = engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) .getGene().getAllele(); System.out.println("Gen: " + i + ": " + result); } //executor.shutdown(); System.out.println("READY"); }
/** * 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); }
@Test(dataProvider = "executors", timeOut = 2_000L) public void executorDeadLock(final Executor executor) { try { final Engine<DoubleGene, Double> engine = Engine .builder(gt -> gt.getGene().doubleValue(), DoubleChromosome.of(0, 1)) .executor(executor) .populationSize(10) .build(); engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); } finally { if (executor instanceof ExecutorService) { ((ExecutorService)executor).shutdown(); } } }