@Test public void cycle1() { final EvolutionStreamable<DoubleGene, Double> engine = CyclicEngine.of( _engine1.limit(10), _engine2.limit(10), _engine3.limit(10) ); final AtomicLong count = new AtomicLong(0); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .peek(r -> count.incrementAndGet()) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(count.intValue(), 100); Assert.assertEquals(result.getTotalGenerations(), 100); }
private Supplier<EvolutionStart<G, C>> evolutionStart(final Supplier<EvolutionStart<G, C>> start) { return () -> { final EvolutionStart<G, C> es = start.get(); final ISeq<Phenotype<G, C>> population = es.getPopulation(); final long generation = es.getGeneration(); final Stream<Phenotype<G, C>> stream = Stream.concat( population.stream().map(this::toFixedPhenotype), Stream.generate(() -> newPhenotype(generation)) ); final ISeq<Phenotype<G, C>> pop = stream .limit(getPopulationSize()) .collect(ISeq.toISeq()); return EvolutionStart.of(pop, generation); }; }
@Override public EvolutionStream<G, C> stream(final EvolutionInit<G> init) { return stream(evolutionStart(init)); }
public static void main(final String[] args) { final Knapsack knapsack = Knapsack.of(15, new Random(123)); // The base engine tries to approximate to good solution in current // environment. final Engine<BitGene, Double> baseEngine = Engine.builder(knapsack) .populationSize(500) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build(); // The 'diversity' engine tries to broaden the search space again. final Engine<BitGene, Double> diversityEngine = baseEngine.builder() .alterers(new Mutator<>(0.5)) .build(); // Concatenates the two engines into one cyclic engine. final EvolutionStreamable<BitGene, Double> engine = CyclicEngine.of( // This engine stops the evolution after 10 non-improving // generations and hands over to the diversity engine. baseEngine.limit(() -> Limits.bySteadyFitness(10)), // The higher mutation rate of this engine broadens the search // space for 15 generations and hands over to the base engine. diversityEngine.limit(15) ); final EvolutionResult<BitGene, Double> best = engine.stream() // The evolution is stopped after 50 non-improving generations. .limit(bySteadyFitness(50)) .collect(toBestEvolutionResult()); System.out.println(best.getTotalGenerations()); System.out.println(best.getBestPhenotype()); }
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()); }
/** * Create a new evolution {@code Engine.Builder} for the given * {@link Problem}. * * @since 3.4 * * @param problem the problem to be solved by the evolution {@code Engine} * @param <T> the (<i>native</i>) argument type of the problem fitness function * @param <G> the gene type the evolution engine is working with * @param <C> the result type of the fitness function * @return Create a new evolution {@code Engine.Builder} */ public static <T, G extends Gene<?, G>, C extends Comparable<? super C>> Builder<G, C> builder(final Problem<T, G, C> problem) { return builder(problem.fitness(), problem.codec()); }
.builder(fitnessFunction, genotypeFactory) .offspringSelector(offspringSelector) .survivorsSelector(survivorsSelector) .build(); Assert.assertEquals(engine.getFitnessFunction(), fitnessFunction); Assert.assertEquals(engine.getOffspringSelector(), offspringSelector); Assert.assertEquals(engine.getSurvivorsSelector(), survivorsSelector); Assert.assertEquals(engine.getAlterer(), alterer); Assert.assertEquals(engine.getOptimize(), optimize); Assert.assertEquals(engine.getOffspringCount(), (int)(offspringFraction*populationSize)); Assert.assertEquals(engine.getOffspringCount() + engine.getSurvivorsCount(), populationSize); Assert.assertEquals(engine.getPopulationSize(), populationSize); Assert.assertEquals(engine.getMaximalPhenotypeAge(), phenotypeAge);
@Test public void survivorsFraction() { 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) .survivorsFraction(0.3) .build(); Assert.assertEquals(engine.getSurvivorsCount(), 15); Assert.assertEquals(engine.getOffspringCount(), 35); }
/** * 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); }
/** * Builds an new {@code Engine} instance from the set properties. * * @return an new {@code Engine} instance from the set properties */ public Engine<G, C> build() { return new Engine<>( _fitnessFunction, _genotypeFactory, _fitnessScaler, _survivorsSelector, _offspringSelector, _alterer, _validator, _optimize, getOffspringCount(), getSurvivorsCount(), _maximalPhenotypeAge, _executor, _evaluator != null ? _evaluator : new ConcurrentEvaluator<>(_executor), _clock, _individualCreationRetries, _mapper ); }
@Test(dataProvider = "generations") public void engineGenerationLimit1(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 .limit(() -> Limits.byFixedGeneration(generations)) .stream() .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
protected double[] fitness(final P param) { final Predicate<? super EvolutionResult<G, N>> terminator = _terminator.apply(param); final long start = System.currentTimeMillis(); final EvolutionResult<G, N> result = _engine.apply(param).stream() .limit(terminator) .collect(EvolutionResult.toBestEvolutionResult()); final long end = System.currentTimeMillis(); return new double[] { result.getTotalGenerations(), result.getBestFitness() != null ? result.getBestFitness().doubleValue() : Double.NEGATIVE_INFINITY, end - start }; }
/** * Create a new evolution {@code Engine.Builder} with the given fitness * function and problem {@code codec}. * * @since 3.2 * * @param ff the fitness function * @param codec the problem codec * @param <T> the fitness function input type * @param <C> the fitness function result type * @param <G> the gene type * @return a new engine builder * @throws java.lang.NullPointerException if one of the arguments is * {@code null}. */ public static <T, G extends Gene<?, G>, C extends Comparable<? super C>> Builder<G, C> builder( final Function<? super T, ? extends C> ff, final Codec<T, G> codec ) { return builder(ff.compose(codec.decoder()), codec.encoding()); }
@Test public void offspringSize() { 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) .offspringSize(35) .build(); Assert.assertEquals(engine.getSurvivorsCount(), 15); Assert.assertEquals(engine.getOffspringCount(), 35); }
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); }
@Test(dataProvider = "generations") public void engineGenerationLimit2(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 .limit(() -> Limits.byFixedGeneration(generations)) .limit(() -> Limits.byFixedGeneration(Math.min(generations, 5))) .stream() .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(Math.min(generations, 5), result.getTotalGenerations()); }
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) throws InterruptedException { final Runner2<Long, BitGene, Double> runner = Runner2.of( KNAPSACK.builder() .selector(new MonteCarloSelector<>()) .build(), Limits::byFixedGeneration, KNAPSACK, Limits::byFixedGeneration, TRIAL_METER, Writer.text().map(Object::toString), Reader.text().map(Long::parseLong), args ); runner.start(); runner.join(); }
@Test public void survivorsSize() { 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) .survivorsSize(15) .build(); Assert.assertEquals(engine.getSurvivorsCount(), 15); Assert.assertEquals(engine.getOffspringCount(), 35); }