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
,
EvolutionStreamable
EvolutionStream
will return
null
.
final EvolutionResult<DoubleGene, Double> result = engine.stream()
.limit(0)
.collect(toBestEvolutionResult());
assert result == null;
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)
Deprecated.
Will be removed, use
ofEvolution(Supplier, Evolution)
instead |
static <G extends Gene<?,G>,C extends Comparable<? super 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>> |
ofEvolution(Supplier<EvolutionStart<G,C>> start,
Evolution<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());
null
, if your truncation
predicate returns false
for the initial population.
final EvolutionResult<DoubleGene, Double> result = engine.stream()
.limit(er -> false)
.collect(toBestEvolutionResult());
assert result == null;
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
.Limits
@Deprecated 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)
ofEvolution(Supplier, Evolution)
insteadEvolutionStream
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.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
ofEvolution(Supplier, Evolution)
static <G extends Gene<?,G>,C extends Comparable<? super C>> EvolutionStream<G,C> ofEvolution(Supplier<EvolutionStart<G,C>> start, Evolution<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.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));
}
}
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
ofAdjustableEvolution(Supplier, Function)
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)
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;
}
G
- the gene typeC
- the fitness typestart
- the evolution start objectevolution
- the adaptable evolution functionEvolutionStream
with the given start
and
evolution
functionNullPointerException
- if one of the arguments is
null
ofEvolution(Supplier, Evolution)
© 2007-2020 Franz Wilhelmstötter (2020-02-18 20:01)