/** * Return the current evolution result object as an {@code EvolutionStart} * object with the current population and current total generation. * * @since 4.1 * * @return the current result as evolution start */ public EvolutionStart<G, C> toEvolutionStart() { return EvolutionStart.of(_population, _totalGenerations); }
/** * Return the next evolution start object with the current population and * the incremented generation. * * @since 4.1 * * @return the next evolution start object */ public EvolutionStart<G, C> next() { return EvolutionStart.of(_population, _totalGenerations + 1); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * initial population. If an empty {@code Population} is given, the engines * genotype factory is used for creating the population. The given population * might be the result of an other engine and this method allows to start the * evolution with the outcome of an different engine. The fitness function * and the fitness scaler are replaced by the one defined for this engine. * * @param population the initial individuals used for the evolution stream. * Missing individuals are created and individuals not needed are * skipped. * @return a new evolution stream. * @throws java.lang.NullPointerException if the given {@code population} is * {@code null}. */ public default EvolutionStream<G, C> stream(final ISeq<Phenotype<G, C>> population) { return stream(EvolutionStart.of(population, 1)); }
/** * Create a new <b>infinite</b> evolution iterator with the given initial * population. If an empty {@code Population} is given, the engines genotype * factory is used for creating the population. The given population might * be the result of an other engine and this method allows to start the * evolution with the outcome of an different engine. The fitness function * and the fitness scaler are replaced by the one defined for this engine. * * @deprecated Marked for removal in the next major version. * @param population the initial individuals used for the evolution iterator. * Missing individuals are created and individuals not needed are * skipped. * @return a new <b>infinite</b> evolution iterator * @throws java.lang.NullPointerException if the given {@code population} is * {@code null}. */ @Deprecated public default Iterator<EvolutionResult<G, C>> iterator(final ISeq<Phenotype<G, C>> population) { return iterator(EvolutionStart.of(population, 1)); }
/** * Create a new <b>infinite</b> evolution iterator with a newly created * population. This is an alternative way for evolution. It lets the user * start, stop and resume the evolution process whenever desired. * * @deprecated Marked for removal in the next major version. * @return a new <b>infinite</b> evolution iterator */ @Deprecated public default Iterator<EvolutionResult<G, C>> iterator() { return iterator(EvolutionStart.of(ISeq.empty(), 1)); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with the given * initial population. If an empty {@code Population} is given, the engines * genotype factory is used for creating the population. The given population * might be the result of an other engine and this method allows to start the * evolution with the outcome of an different engine. The fitness function * and the fitness scaler are replaced by the one defined for this engine. * * @param population the initial individuals used for the evolution stream. * Missing individuals are created and individuals not needed are * skipped. * @param generation the generation the stream starts from; must be greater * than zero. * @return a new evolution stream. * @throws java.lang.NullPointerException if the given {@code population} is * {@code null}. * @throws IllegalArgumentException if the given {@code generation} is * smaller then one */ public default EvolutionStream<G, C> stream( final ISeq<Phenotype<G, C>> population, final long generation ) { return stream(EvolutionStart.of(population, generation)); }
/** * Perform one evolution step with the given {@code population} and * {@code generation}. New phenotypes are created with the fitness function * and fitness scaler defined by this <em>engine</em> * <p> * <em>This method is thread-safe.</em> * * @see #evolve(EvolutionStart) * * @param population the population to evolve * @param generation the current generation; used for calculating the * phenotype age. * @return the evolution result * @throws java.lang.NullPointerException if the given {@code population} is * {@code null} * @throws IllegalArgumentException if the given {@code generation} is * smaller then one */ public EvolutionResult<G, C> evolve( final ISeq<Phenotype<G, C>> population, final long generation ) { return evolve(EvolutionStart.of(population, generation)); }
/** * Create a new <b>infinite</b> evolution iterator with the given initial * population. If an empty {@code Population} is given, the engines genotype * factory is used for creating the population. The given population might * be the result of an other engine and this method allows to start the * evolution with the outcome of an different engine. The fitness function * and the fitness scaler are replaced by the one defined for this engine. * * @deprecated Marked for removal in the next major version. * @param population the initial individuals used for the evolution iterator. * Missing individuals are created and individuals not needed are * skipped. * @param generation the generation the iterator starts from; must be greater * than zero. * @return a new <b>infinite</b> evolution iterator * @throws java.lang.NullPointerException if the given {@code population} is * {@code null}. * @throws IllegalArgumentException if the given {@code generation} is smaller * then one */ @Deprecated public default Iterator<EvolutionResult<G, C>> iterator( final ISeq<Phenotype<G, C>> population, final long generation ) { return iterator(EvolutionStart.of(population, generation)); }
/** * Create a new, possibly <em>infinite</em>, evolution stream with a newly * created population. This method is a shortcut for * <pre>{@code * final EvolutionStream<G, C> stream = streamable * .stream(() -> EvolutionStart.of(ISeq.empty(), 1)); * }</pre> * * @return a new evolution stream. */ public default EvolutionStream<G, C> stream() { return stream(() -> EvolutionStart.of(ISeq.empty(), 1)); }
/** * Create a new <b>infinite</b> evolution iterator starting with a * previously evolved {@link EvolutionResult}. The iterator is initialized * with the population of the given {@code result} and its total generation * {@link EvolutionResult#getTotalGenerations()}. * * @deprecated Marked for removal in the next major version. * @param result the previously evolved {@code EvolutionResult} * @return a new evolution stream, which continues a previous one * @throws NullPointerException if the given evolution {@code result} is * {@code null} */ @Deprecated public default Iterator<EvolutionResult<G, C>> iterator(final EvolutionResult<G, C> result) { return iterator(EvolutionStart.of( result.getPopulation(), result.getGeneration() )); }
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); }; }
return stream(EvolutionStart.of( result.getPopulation(), result.getGeneration()
private static EvolutionStart<DoubleGene, Double> start(final int populationSize, final long generation) { final ISeq<Phenotype<DoubleGene, Double>> population = Genotype.of(DoubleChromosome.of(0, 1)).instances() .map(gt -> Phenotype.of(gt, generation, SpecialEngine::fitness)) .limit(populationSize) .collect(ISeq.toISeq()); return EvolutionStart.of(population, generation); }
private Supplier<EvolutionStart<G, C>> evolutionStart(final EvolutionInit<G> init) { return evolutionStart(() -> EvolutionStart.of( init.getPopulation() .map(gt -> Phenotype.of( gt, init.getGeneration(), _fitnessFunction, _fitnessScaler) ), init.getGeneration()) ); }
private static EvolutionStream<DoubleGene, Double> stream() { final Function<EvolutionStart<DoubleGene, Double>, EvolutionResult<DoubleGene, Double>> ff = s -> EvolutionResult.of( Optimize.MAXIMUM, ISeq.empty(), 1, EvolutionDurations.ZERO, 0, 0, 0 ); return new EvolutionStreamImpl<DoubleGene, Double>( () -> EvolutionStart.of(ISeq.empty(), 1), ff ); }