From 0c9af5f450ce73da9b791051f861cb9a6420e0f1 Mon Sep 17 00:00:00 2001 From: Gilles Date: Wed, 11 May 2016 13:54:06 +0200 Subject: [PATCH] MATH-1335 Use new RNG API. --- .../math4/genetics/GeneticAlgorithm.java | 18 +++++++++--------- .../math4/genetics/NPointCrossover.java | 4 ++-- .../math4/genetics/OrderedCrossover.java | 4 ++-- .../math4/genetics/UniformCrossover.java | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/apache/commons/math4/genetics/GeneticAlgorithm.java b/src/main/java/org/apache/commons/math4/genetics/GeneticAlgorithm.java index bf7760665..d76fe9399 100644 --- a/src/main/java/org/apache/commons/math4/genetics/GeneticAlgorithm.java +++ b/src/main/java/org/apache/commons/math4/genetics/GeneticAlgorithm.java @@ -18,8 +18,8 @@ package org.apache.commons.math4.genetics; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.JDKRandomGenerator; -import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.rng.RandomSource; +import org.apache.commons.math4.rng.UniformRandomProvider; /** * Implementation of a genetic algorithm. All factors that govern the operation @@ -30,12 +30,12 @@ import org.apache.commons.math4.random.RandomGenerator; public class GeneticAlgorithm { /** - * Static random number generator shared by GA implementation classes. Set the randomGenerator seed to get - * reproducible results. Use {@link #setRandomGenerator(RandomGenerator)} to supply an alternative to the default - * JDK-provided PRNG. + * Static random number generator shared by GA implementation classes. + * Use {@link #setRandomGenerator(UniformRandomProvider)} to supply an + * alternative to the default PRNG, and/or select a specific seed. */ //@GuardedBy("this") - private static RandomGenerator randomGenerator = new JDKRandomGenerator(); + private static UniformRandomProvider randomGenerator = RandomSource.create(RandomSource.WELL_19937_C); /** the crossover policy used by the algorithm. */ private final CrossoverPolicy crossoverPolicy; @@ -90,7 +90,7 @@ public class GeneticAlgorithm { * * @param random random generator */ - public static synchronized void setRandomGenerator(final RandomGenerator random) { + public static synchronized void setRandomGenerator(final UniformRandomProvider random) { randomGenerator = random; } @@ -99,7 +99,7 @@ public class GeneticAlgorithm { * * @return the static random generator shared by GA implementation classes */ - public static synchronized RandomGenerator getRandomGenerator() { + public static synchronized UniformRandomProvider getRandomGenerator() { return randomGenerator; } @@ -148,7 +148,7 @@ public class GeneticAlgorithm { public Population nextGeneration(final Population current) { Population nextGeneration = current.nextGeneration(); - RandomGenerator randGen = getRandomGenerator(); + UniformRandomProvider randGen = getRandomGenerator(); while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { // select parent chromosomes diff --git a/src/main/java/org/apache/commons/math4/genetics/NPointCrossover.java b/src/main/java/org/apache/commons/math4/genetics/NPointCrossover.java index 5c79ef1f4..847e9bca5 100644 --- a/src/main/java/org/apache/commons/math4/genetics/NPointCrossover.java +++ b/src/main/java/org/apache/commons/math4/genetics/NPointCrossover.java @@ -24,7 +24,7 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.rng.UniformRandomProvider; /** * N-point crossover policy. For each iteration a random crossover point is @@ -142,7 +142,7 @@ public class NPointCrossover implements CrossoverPolicy { final List child1Rep = new ArrayList(length); final List child2Rep = new ArrayList(length); - final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); + final UniformRandomProvider random = GeneticAlgorithm.getRandomGenerator(); List c1 = child1Rep; List c2 = child2Rep; diff --git a/src/main/java/org/apache/commons/math4/genetics/OrderedCrossover.java b/src/main/java/org/apache/commons/math4/genetics/OrderedCrossover.java index b5aecc8f8..6b6502cf2 100644 --- a/src/main/java/org/apache/commons/math4/genetics/OrderedCrossover.java +++ b/src/main/java/org/apache/commons/math4/genetics/OrderedCrossover.java @@ -25,7 +25,7 @@ import java.util.Set; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.FastMath; /** @@ -103,7 +103,7 @@ public class OrderedCrossover implements CrossoverPolicy { final Set child1Set = new HashSet(length); final Set child2Set = new HashSet(length); - final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); + final UniformRandomProvider random = GeneticAlgorithm.getRandomGenerator(); // choose random points, making sure that lb < ub. int a = random.nextInt(length); int b; diff --git a/src/main/java/org/apache/commons/math4/genetics/UniformCrossover.java b/src/main/java/org/apache/commons/math4/genetics/UniformCrossover.java index 3da1c8760..df5b929b4 100644 --- a/src/main/java/org/apache/commons/math4/genetics/UniformCrossover.java +++ b/src/main/java/org/apache/commons/math4/genetics/UniformCrossover.java @@ -23,7 +23,7 @@ import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.rng.UniformRandomProvider; /** * Perform Uniform Crossover [UX] on the specified chromosomes. A fixed mixing @@ -115,7 +115,7 @@ public class UniformCrossover implements CrossoverPolicy { final List child1Rep = new ArrayList(length); final List child2Rep = new ArrayList(length); - final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); + final UniformRandomProvider random = GeneticAlgorithm.getRandomGenerator(); for (int index = 0; index < length; index++) {