MATH-1335

Use new RNG API.
This commit is contained in:
Gilles 2016-05-11 13:54:06 +02:00
parent 6fc152e417
commit 0c9af5f450
4 changed files with 15 additions and 15 deletions

View File

@ -18,8 +18,8 @@ package org.apache.commons.math4.genetics;
import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.OutOfRangeException;
import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.exception.util.LocalizedFormats;
import org.apache.commons.math4.random.JDKRandomGenerator; import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.random.RandomGenerator; import org.apache.commons.math4.rng.UniformRandomProvider;
/** /**
* Implementation of a genetic algorithm. All factors that govern the operation * 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 { public class GeneticAlgorithm {
/** /**
* Static random number generator shared by GA implementation classes. Set the randomGenerator seed to get * Static random number generator shared by GA implementation classes.
* reproducible results. Use {@link #setRandomGenerator(RandomGenerator)} to supply an alternative to the default * Use {@link #setRandomGenerator(UniformRandomProvider)} to supply an
* JDK-provided PRNG. * alternative to the default PRNG, and/or select a specific seed.
*/ */
//@GuardedBy("this") //@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. */ /** the crossover policy used by the algorithm. */
private final CrossoverPolicy crossoverPolicy; private final CrossoverPolicy crossoverPolicy;
@ -90,7 +90,7 @@ public class GeneticAlgorithm {
* *
* @param random random generator * @param random random generator
*/ */
public static synchronized void setRandomGenerator(final RandomGenerator random) { public static synchronized void setRandomGenerator(final UniformRandomProvider random) {
randomGenerator = random; randomGenerator = random;
} }
@ -99,7 +99,7 @@ public class GeneticAlgorithm {
* *
* @return the static random generator shared by GA implementation classes * @return the static random generator shared by GA implementation classes
*/ */
public static synchronized RandomGenerator getRandomGenerator() { public static synchronized UniformRandomProvider getRandomGenerator() {
return randomGenerator; return randomGenerator;
} }
@ -148,7 +148,7 @@ public class GeneticAlgorithm {
public Population nextGeneration(final Population current) { public Population nextGeneration(final Population current) {
Population nextGeneration = current.nextGeneration(); Population nextGeneration = current.nextGeneration();
RandomGenerator randGen = getRandomGenerator(); UniformRandomProvider randGen = getRandomGenerator();
while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) {
// select parent chromosomes // select parent chromosomes

View File

@ -24,7 +24,7 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.util.LocalizedFormats; 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 * 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> child1Rep = new ArrayList<T>(length);
final List<T> child2Rep = 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> c1 = child1Rep;
List<T> c2 = child2Rep; List<T> c2 = child2Rep;

View File

@ -25,7 +25,7 @@ import java.util.Set;
import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.util.LocalizedFormats; 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; 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> child1Set = new HashSet<T>(length);
final Set<T> child2Set = 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. // choose random points, making sure that lb < ub.
int a = random.nextInt(length); int a = random.nextInt(length);
int b; int b;

View File

@ -23,7 +23,7 @@ import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.OutOfRangeException;
import org.apache.commons.math4.exception.util.LocalizedFormats; 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 * 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> child1Rep = new ArrayList<T>(length);
final List<T> child2Rep = 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++) { for (int index = 0; index < length; index++) {