Module io.jenetics.base
Package io.jenetics.engine
Interface EvolutionStream<G extends Gene<?,G>,C extends Comparable<? super C>>
- All Superinterfaces:
AutoCloseable
,BaseStream<EvolutionResult<G,
,C>, Stream<EvolutionResult<G, C>>> Stream<EvolutionResult<G,
C>>
- All Known Implementing Classes:
EvolutionStreamImpl
public interface EvolutionStream<G extends Gene<?,G>,C extends Comparable<? super C>>
extends Stream<EvolutionResult<G,C>>
The
EvolutionStream
class extends the Java Stream
and adds a
method for limiting the evolution by a given predicate.- Since:
- 3.0
- Version:
- 6.0
- See Also:
- Implementation Note:
- Collecting an empty
EvolutionStream
will returnnull
.final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(0) .collect(toBestEvolutionResult()); assert result == null;
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object>
-
Method Summary
Modifier and TypeMethodDescriptionlimit
(Predicate<? super EvolutionResult<G, C>> proceed) Returns a stream consisting of the elements of this stream, truncated when the givenproceed
predicate returnsfalse
.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
EvolutionStream<G,C> ofAdjustableEvolution
(Supplier<EvolutionStart<G, C>> start, Function<? super EvolutionStart<G, C>, ? extends Evolution<G, C>> evolution) Create a new evolution stream with an adjustable evolution function.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
EvolutionStream<G,C> ofEvolution
(Supplier<EvolutionStart<G, C>> start, Evolution<G, C> evolution) Create a newEvolutionStream
from the givenstart
population andevolution
function.Methods inherited from interface java.util.stream.BaseStream
close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered
Methods inherited from interface java.util.stream.Stream
allMatch, anyMatch, collect, collect, count, distinct, dropWhile, filter, findAny, findFirst, flatMap, flatMapToDouble, flatMapToInt, flatMapToLong, forEach, forEachOrdered, limit, map, mapMulti, mapMultiToDouble, mapMultiToInt, mapMultiToLong, mapToDouble, mapToInt, mapToLong, max, min, noneMatch, peek, reduce, reduce, reduce, skip, sorted, sorted, takeWhile, toArray, toArray, toList
-
Method Details
-
limit
Returns a stream consisting of the elements of this stream, truncated when the givenproceed
predicate returnsfalse
.General usage example:
Note: The evolution result may befinal Phenotype<DoubleGene, Double> result = engine.stream() // Truncate the evolution stream after 5 "steady" generations. .limit(bySteadyFitness(5)) // The evolution will stop after maximal 100 generations. .limit(100) .collect(toBestPhenotype());
null
, if your truncation predicate returnsfalse
for the initial population.final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(er -> false) .collect(toBestEvolutionResult()); assert result == null;
- Parameters:
proceed
- the predicate which determines whether the stream is truncated or not. If the predicate returnsfalse
, the evolution stream is truncated.- Returns:
- the new stream
- Throws:
NullPointerException
- if the given predicate isnull
.- See Also:
-
ofEvolution
static <G extends Gene<?,G>, EvolutionStream<G,C extends Comparable<? super C>> C> ofEvolution(Supplier<EvolutionStart<G, C>> start, Evolution<G, C> evolution) Create a newEvolutionStream
from the givenstart
population andevolution
function. The main purpose of this factory method is to simplify the creation of anEvolutionStream
from an own evolution (GA) engine.A more complete example for would look like as:final Supplier<EvolutionStart<DoubleGene, Double>> start = ... final EvolutionStream<DoubleGene, Double> stream = EvolutionStream.of(start, new MySpecialEngine());
public final class SpecialEngine { // The fitness function. private static Double fitness(final Genotype<DoubleGene> gt) { return gt.gene().allele(); } // Create new evolution start object. private static EvolutionStart<DoubleGene, Double> start(final int populationSize, final long generation) { final Population<DoubleGene, Double> population = Genotype.of(DoubleChromosome.of(0, 1)).instances() .map(gt -> Phenotype.of(gt, generation, SpecialEngine::fitness)) .limit(populationSize) .collect(Population.toPopulation()); return EvolutionStart.of(population, generation); } // The special evolution function. private static EvolutionResult<DoubleGene, Double> evolve(final EvolutionStart<DoubleGene, Double> start) { // Your special evolution implementation comes here! return null; } public static void main(final String[] args) { final Genotype<DoubleGene> best = EvolutionStream .ofEvolution(() -> start(50, 0), SpecialEngine::evolve) .limit(Limits.bySteadyFitness(10)) .limit(1000) .collect(EvolutionResult.toBestGenotype()); System.out.println(String.format("Best Genotype: %s", best)); } }
- Type Parameters:
G
- the gene typeC
- the fitness type- Parameters:
start
- the evolution startevolution
- the evolution function- Returns:
- a new
EvolutionStream
with the givenstart
andevolution
function - Throws:
NullPointerException
- if one of the arguments isnull
- Since:
- 5.1
- See Also:
-
ofAdjustableEvolution
static <G extends Gene<?,G>, EvolutionStream<G,C extends Comparable<? super C>> C> ofAdjustableEvolution(Supplier<EvolutionStart<G, C>> start, Function<? super EvolutionStart<G, C>, ? extends Evolution<G, C>> evolution) Create a new evolution stream with an adjustable evolution function.public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); // Engine builder template. final Engine.Builder<DoubleGene, Double> builder = Engine .builder(problem) .minimizing(); // Evolution used for low fitness variance. final Evolution<DoubleGene, Double> lowVar = builder.copy() .alterers(new Mutator<>(0.5)) .selector(new MonteCarloSelector<>()) .build(); // Evolution used for high fitness variance. final Evolution<DoubleGene, Double> highVar = builder.copy() .alterers( new Mutator<>(0.05), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build(); final EvolutionStream<DoubleGene, Double> stream = EvolutionStream.ofAdjustableEvolution( EvolutionStart::empty, er -> var(er) < 0.2 ? lowVar : highVar ); final Genotype<DoubleGene> result = stream .limit(Limits.bySteadyFitness(50)) .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); } private static double var(final EvolutionStart<DoubleGene, Double> result) { return result != null ? result.getPopulation().stream() .map(Phenotype::fitness) .collect(DoubleMoments.toDoubleMoments()) .variance() : 0.0; }
- Type Parameters:
G
- the gene typeC
- the fitness type- Parameters:
start
- the evolution start objectevolution
- the adaptable evolution function- Returns:
- a new
EvolutionStream
with the givenstart
andevolution
function - Throws:
NullPointerException
- if one of the arguments isnull
- See Also:
-