MATH-1335

Use new RNG API.
This commit is contained in:
Gilles 2016-05-11 14:06:15 +02:00
parent 0c9af5f450
commit 11409e628b
2 changed files with 14 additions and 12 deletions

View File

@ -35,7 +35,9 @@ import org.apache.commons.math4.optim.OptimizationData;
import org.apache.commons.math4.optim.PointValuePair;
import org.apache.commons.math4.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.optim.nonlinear.scalar.MultivariateOptimizer;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.rng.UniformRandomProvider;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.distribution.NormalDistribution;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathArrays;
@ -195,8 +197,8 @@ public class CMAESOptimizer
/** Size of history queue of best values. */
private int historySize;
/** Random generator. */
private final RandomGenerator random;
/** Gaussian sampler. */
private final RealDistribution.Sampler random;
/** History of sigma values. */
private final List<Double> statisticsSigmaHistory = new ArrayList<Double>();
@ -216,7 +218,7 @@ public class CMAESOptimizer
* remains diagonal.
* @param checkFeasableCount Determines how often new random objective variables are
* generated in case they are out of bounds.
* @param random Random generator.
* @param rng Random generator.
* @param generateStatistics Whether statistic data is collected.
* @param checker Convergence checker.
*
@ -227,7 +229,7 @@ public class CMAESOptimizer
boolean isActiveCMA,
int diagonalOnly,
int checkFeasableCount,
RandomGenerator random,
UniformRandomProvider rng,
boolean generateStatistics,
ConvergenceChecker<PointValuePair> checker) {
super(checker);
@ -236,7 +238,7 @@ public class CMAESOptimizer
this.isActiveCMA = isActiveCMA;
this.diagonalOnly = diagonalOnly;
this.checkFeasableCount = checkFeasableCount;
this.random = random;
this.random = new NormalDistribution().createSampler(rng);
this.generateStatistics = generateStatistics;
}
@ -1335,7 +1337,7 @@ public class CMAESOptimizer
private double[] randn(int size) {
final double[] randn = new double[size];
for (int i = 0; i < size; i++) {
randn[i] = random.nextGaussian();
randn[i] = random.sample();
}
return randn;
}
@ -1349,7 +1351,7 @@ public class CMAESOptimizer
final double[][] d = new double[size][popSize];
for (int r = 0; r < size; r++) {
for (int c = 0; c < popSize; c++) {
d[r][c] = random.nextGaussian();
d[r][c] = random.sample();
}
}
return new Array2DRowRealMatrix(d, false);

View File

@ -34,7 +34,7 @@ import org.apache.commons.math4.optim.SimpleBounds;
import org.apache.commons.math4.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math4.optim.nonlinear.scalar.noderiv.CMAESOptimizer;
import org.apache.commons.math4.random.MersenneTwister;
import org.apache.commons.math4.rng.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@ -368,7 +368,7 @@ public class CMAESOptimizerTest {
public void testMath864() {
final CMAESOptimizer optimizer
= new CMAESOptimizer(30000, 0, true, 10,
0, new MersenneTwister(), false, null);
0, RandomSource.create(RandomSource.MT_64), false, null);
final MultivariateFunction fitnessFunction = new MultivariateFunction() {
public double value(double[] parameters) {
final double target = 1;
@ -399,7 +399,7 @@ public class CMAESOptimizerTest {
public void testFitAccuracyDependsOnBoundary() {
final CMAESOptimizer optimizer
= new CMAESOptimizer(30000, 0, true, 10,
0, new MersenneTwister(), false, null);
0, RandomSource.create(RandomSource.MT_64), false, null);
final MultivariateFunction fitnessFunction = new MultivariateFunction() {
public double value(double[] parameters) {
final double target = 11.1;
@ -486,7 +486,7 @@ public class CMAESOptimizerTest {
int dim = startPoint.length;
// test diagonalOnly = 0 - slow but normally fewer feval#
CMAESOptimizer optim = new CMAESOptimizer(30000, stopValue, isActive, diagonalOnly,
0, new MersenneTwister(), false, null);
0, RandomSource.create(RandomSource.MT_64), false, null);
PointValuePair result = boundaries == null ?
optim.optimize(new MaxEval(maxEvaluations),
new ObjectiveFunction(func),