Package io.jenetics.util
Class RandomRegistry
- java.lang.Object
-
- io.jenetics.util.RandomRegistry
-
public final class RandomRegistry extends Object
This class holds theRandom
engine used for the GA. TheRandomRegistry
is thread safe. The registry is initialized with theThreadLocalRandom
PRNG, which has a much better performance behavior than an instance of theRandom
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.random(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.random(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()); } }
- Since:
- 1.0
- Version:
- 3.0
- See Also:
Random
,ThreadLocalRandom
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Random
random()
Return the globalRandom
object.static void
random(ThreadLocal<? extends Random> random)
Set the new globalRandom
object for the GA.static void
random(Random random)
Set the new globalRandom
object for the GA.static void
reset()
Set the random object to it's default value.static <R extends Random>
voidusing(ThreadLocal<R> random, Consumer<? super R> consumer)
Executes the consumer code using the givenrandom
engine.static <R extends Random>
voidusing(R random, Consumer<? super R> consumer)
Executes the consumer code using the givenrandom
engine.static <R extends Random,T>
Twith(ThreadLocal<R> random, Function<? super R,? extends T> function)
Opens a newScope
with the given random engine and executes the given function within it.static <R extends Random,T>
Twith(R random, Function<? super R,? extends T> function)
Opens a newScope
with the given random engine and executes the given function within it.
-
-
-
Method Detail
-
random
public static Random random()
Return the globalRandom
object.- Returns:
- the global
Random
object.
-
random
public static void random(Random random)
Set the new globalRandom
object for the GA. The givenRandom
must be thread safe, which is the case for the default JavaRandom
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.- Parameters:
random
- the new globalRandom
object for the GA.- Throws:
NullPointerException
- if therandom
object isnull
.- See Also:
random(ThreadLocal)
-
random
public static void random(ThreadLocal<? extends Random> random)
Set the new globalRandom
object for the GA. The givenRandom
don't have be thread safe, because the givenThreadLocal
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.- Parameters:
random
- the thread-local random engine to use.- Throws:
NullPointerException
- if therandom
object isnull
.
-
reset
public static void reset()
Set the random object to it's default value. The default used PRNG is theThreadLocalRandom
PRNG.
-
using
public static <R extends Random> void using(R random, Consumer<? super R> consumer)
Executes the consumer code using the givenrandom
engine.The example above shuffles the given integerfinal MSeq<Integer> seq = ... using(new Random(123), r -> { seq.shuffle(); });
seq
using the givenRandom(123)
engine.- Type Parameters:
R
- the type of the random engine- Parameters:
random
- the PRNG used within the consumerconsumer
- the consumer which is executed with the scope of the givenrandom
engine.- Throws:
NullPointerException
- if one of the arguments isnull
- Since:
- 3.0
-
using
public static <R extends Random> void using(ThreadLocal<R> random, Consumer<? super R> consumer)
Executes the consumer code using the givenrandom
engine.The example above shuffles the given integerfinal MSeq<Integer> seq = ... using(new LCG64ShiftRandom.ThreadLocal(), r -> { seq.shuffle(); });
seq
using the givenLCG64ShiftRandom.ThreadLocal()
engine.- Type Parameters:
R
- the type of the random engine- Parameters:
random
- the PRNG used within the consumerconsumer
- the consumer which is executed with the scope of the givenrandom
engine.- Throws:
NullPointerException
- if one of the arguments isnull
- Since:
- 3.0
-
with
public static <R extends Random,T> T with(R random, Function<? super R,? extends T> function)
Opens a newScope
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()) );
- Type Parameters:
R
- the type of the random engineT
- the function return type- Parameters:
random
- the PRNG used for the opened scopefunction
- the function to apply within the random scope- Returns:
- the object returned by the given function
- Throws:
NullPointerException
- if one of the arguments isnull
- Since:
- 3.0
-
with
public static <R extends Random,T> T with(ThreadLocal<R> random, Function<? super R,? extends T> function)
Opens a newScope
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()) );
- Type Parameters:
R
- the type of the random engineT
- the function return type- Parameters:
random
- the PRNG used for the opened scopefunction
- the function to apply within the random scope- Returns:
- the object returned by the given function
- Throws:
NullPointerException
- if one of the arguments isnull
.- Since:
- 3.0
-
-