MATH-1335 Use new RNG API.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/0c9af5f4 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/0c9af5f4 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/0c9af5f4 Branch: refs/heads/develop Commit: 0c9af5f450ce73da9b791051f861cb9a6420e0f1 Parents: 6fc152e Author: Gilles <er...@apache.org> Authored: Wed May 11 13:54:06 2016 +0200 Committer: Gilles <er...@apache.org> Committed: Tue May 17 15:30:23 2016 +0200 ---------------------------------------------------------------------- .../commons/math4/genetics/GeneticAlgorithm.java | 18 +++++++++--------- .../commons/math4/genetics/NPointCrossover.java | 4 ++-- .../commons/math4/genetics/OrderedCrossover.java | 4 ++-- .../commons/math4/genetics/UniformCrossover.java | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/0c9af5f4/src/main/java/org/apache/commons/math4/genetics/GeneticAlgorithm.java ---------------------------------------------------------------------- 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 bf77606..d76fe93 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 http://git-wip-us.apache.org/repos/asf/commons-math/blob/0c9af5f4/src/main/java/org/apache/commons/math4/genetics/NPointCrossover.java ---------------------------------------------------------------------- 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 5c79ef1..847e9bc 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<T> implements CrossoverPolicy { final List<T> child1Rep = new ArrayList<T>(length); final List<T> child2Rep = new ArrayList<T>(length); - final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); + final UniformRandomProvider random = GeneticAlgorithm.getRandomGenerator(); List<T> c1 = child1Rep; List<T> c2 = child2Rep; http://git-wip-us.apache.org/repos/asf/commons-math/blob/0c9af5f4/src/main/java/org/apache/commons/math4/genetics/OrderedCrossover.java ---------------------------------------------------------------------- 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 b5aecc8..6b6502c 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<T> implements CrossoverPolicy { final Set<T> child1Set = new HashSet<T>(length); final Set<T> child2Set = new HashSet<T>(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; http://git-wip-us.apache.org/repos/asf/commons-math/blob/0c9af5f4/src/main/java/org/apache/commons/math4/genetics/UniformCrossover.java ---------------------------------------------------------------------- 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 3da1c87..df5b929 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<T> implements CrossoverPolicy { final List<T> child1Rep = new ArrayList<T>(length); final List<T> child2Rep = new ArrayList<T>(length); - final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); + final UniformRandomProvider random = GeneticAlgorithm.getRandomGenerator(); for (int index = 0; index < length; index++) {