public final class RandomRegistry extends Object
Random engine used for the GA. The
RandomRegistry is thread safe. The registry is initialized with the
ThreadLocalRandom PRNG, which has a much better performance behavior
than an instance of the Random class. Alternatively, you can
initialize the registry with one of the PRNG, which are being part of the
library.
Setup of a global PRNG
public class GA {
public static void main(final String[] args) {
// Initialize the registry with a ThreadLocal instance of the PRGN.
// This is the preferred way setting a new PRGN.
RandomRegistry.setRandom(new LCG64ShiftRandom.ThreadLocal());
// Using a thread safe variant of the PRGN. Leads to slower PRN
// generation, but gives you the possibility to set a PRNG seed.
RandomRegistry.setRandom(new LCG64ShiftRandom.ThreadSafe(1234));
...
final EvolutionResult<DoubleGene, Double> result = stream
.limit(100)
.collect(toBestEvolutionResult());
}
}
Setup of a local PRNG
You can temporarily (and locally) change the implementation of the PRNG. E.g.
for initialize the engine stream with the same initial population.
public class GA {
public static void main(final String[] args) {
// Create a reproducible list of genotypes.
final List<Genotype<DoubleGene>> genotypes =
with(new LCG64ShiftRandom(123), r ->
Genotype.of(DoubleChromosome.of(0, 10)).instances()
.limit(50)
.collect(toList())
);
final Engine<DoubleGene, Double> engine = ...;
final EvolutionResult<DoubleGene, Double> result = engine
// Initialize the evolution stream with the given genotypes.
.stream(genotypes)
.limit(100)
.collect(toBestEvolutionResult());
}
}Random,
ThreadLocalRandom,
LCG64ShiftRandom| Modifier and Type | Method and Description |
|---|---|
static Random |
getRandom()
Return the global
Random object. |
static void |
reset()
Set the random object to it's default value.
|
static void |
setRandom(Random random)
Set the new global
Random object for the GA. |
static void |
setRandom(ThreadLocal<? extends Random> random)
Set the new global
Random object for the GA. |
static <R extends Random> |
using(R random,
Consumer<? super R> consumer)
Executes the consumer code using the given
random engine. |
static <R extends Random> |
using(ThreadLocal<R> random,
Consumer<? super R> consumer)
Executes the consumer code using the given
random engine. |
static <R extends Random,T> |
with(R random,
Function<? super R,? extends T> function)
Opens a new
Scope with the given random engine and executes the
given function within it. |
static <R extends Random,T> |
with(ThreadLocal<R> random,
Function<? super R,? extends T> function)
Opens a new
Scope with the given random engine and executes the
given function within it. |
public static Random getRandom()
Random object.Random object.public static void setRandom(Random random)
Random object for the GA. The given
Random must be thread safe, which is the case for the
default Java Random implementation.
Setting a thread-local random object leads, in general, to a faster
PRN generation, because the given Random engine don't have to be
thread-safe.
random - the new global Random object for the GA.NullPointerException - if the random object is null.setRandom(ThreadLocal)public static void setRandom(ThreadLocal<? extends Random> random)
Random object for the GA. The given
Random don't have be thread safe, because the given
ThreadLocal wrapper guarantees thread safety. Setting a
thread-local random object leads, in general, to a faster
PRN generation, when using a non-blocking PRNG. This is the preferred
way for changing the PRNG.random - the thread-local random engine to use.NullPointerException - if the random object is null.public static void reset()
ThreadLocalRandom PRNG.public static <R extends Random> void using(R random, Consumer<? super R> consumer)
random engine.
final MSeq<Integer> seq = ...
using(new Random(123), r -> {
seq.shuffle();
});seq using the
given Random(123) engine.R - the type of the random enginerandom - the PRNG used within the consumerconsumer - the consumer which is executed with the scope of
the given random engine.NullPointerException - if one of the arguments is nullpublic static <R extends Random> void using(ThreadLocal<R> random, Consumer<? super R> consumer)
random engine.
final MSeq<Integer> seq = ...
using(new LCG64ShiftRandom.ThreadLocal(), r -> {
seq.shuffle();
});seq using the
given LCG64ShiftRandom.ThreadLocal() engine.R - the type of the random enginerandom - the PRNG used within the consumerconsumer - the consumer which is executed with the scope of
the given random engine.NullPointerException - if one of the arguments is nullpublic static <R extends Random,T> T with(R random, Function<? super R,? extends T> function)
Scope with the given random engine and executes the
given function within it. The following example shows how to create a
reproducible list of genotypes:
final List<Genotype<DoubleGene>> genotypes =
with(new LCG64ShiftRandom(123), r ->
Genotype.of(DoubleChromosome.of(0, 10)).instances()
.limit(50)
.collect(toList())
);R - the type of the random engineT - the function return typerandom - the PRNG used for the opened scopefunction - the function to apply within the random scopeNullPointerException - if one of the arguments is nullpublic static <R extends Random,T> T with(ThreadLocal<R> random, Function<? super R,? extends T> function)
Scope with the given random engine and executes the
given function within it. The following example shows how to create a
reproducible list of genotypes:
final List<Genotype<DoubleGene>> genotypes =
with(new LCG64ShiftRandom.ThreadLocal(), random ->
Genotype.of(DoubleChromosome.of(0, 10)).instances()
.limit(50)
.collect(toList())
);R - the type of the random engineT - the function return typerandom - the PRNG used for the opened scopefunction - the function to apply within the random scopeNullPointerException - if one of the arguments is null.© 2007-2017 Franz Wilhelmstötter (2017-04-28 16:50)