public interface EvolutionStream<G extends Gene<?,G>,C extends Comparable<? super C>> extends Stream<EvolutionResult<G,C>>
EvolutionStream
class extends the Java Stream
and adds a
method for limiting the evolution by a given predicate.Stream
,
Engine
Stream.Builder<T>
Modifier and Type | Method and Description |
---|---|
EvolutionStream<G,C> |
limit(Predicate<? super EvolutionResult<G,C>> proceed)
Returns a stream consisting of the elements of this stream, truncated
when the given
proceed predicate returns false . |
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Supplier<EvolutionStart<G,C>> start,
Function<? super EvolutionStart<G,C>,EvolutionResult<G,C>> evolution)
Create a new
EvolutionStream from the given start
population and evolution function. |
allMatch, anyMatch, builder, collect, collect, concat, count, distinct, empty, filter, findAny, findFirst, flatMap, flatMapToDouble, flatMapToInt, flatMapToLong, forEach, forEachOrdered, generate, iterate, limit, map, mapToDouble, mapToInt, mapToLong, max, min, noneMatch, of, of, peek, reduce, reduce, reduce, skip, sorted, sorted, toArray, toArray
close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered
EvolutionStream<G,C> limit(Predicate<? super EvolutionResult<G,C>> proceed)
proceed
predicate returns false
.
General usage example:
final 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());
proceed
- the predicate which determines whether the stream is
truncated or not. If the predicate returns false
, the
evolution stream is truncated.NullPointerException
- if the given predicate is null
.limit
static <G extends Gene<?,G>,C extends Comparable<? super C>> EvolutionStream<G,C> of(Supplier<EvolutionStart<G,C>> start, Function<? super EvolutionStart<G,C>,EvolutionResult<G,C>> evolution)
EvolutionStream
from the given start
population and evolution
function. The main purpose of this
factory method is to simplify the creation of an EvolutionStream
from an own evolution (GA) engine.
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.getGene().getAllele();
}
// 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
.of(() -> start(50, 0), SpecialEngine::evolve)
.limit(limit.bySteadyFitness(10))
.limit(1000)
.collect(EvolutionResult.toBestGenotype());
System.out.println(String.format("Best Genotype: %s", best));
}
}
G
- the gene typeC
- the fitness typestart
- the evolution startevolution
- the evolution functionEvolutionStream
with the given start
and
evolution
functionNullPointerException
- if one of the arguments is
null
© 2007-2017 Franz Wilhelmstötter (2017-04-28 16:50)