MATH-1335
Use new RNG API. Unrelated code comments removed in unit test.
This commit is contained in:
parent
11409e628b
commit
63a9488f29
|
@ -26,7 +26,7 @@ import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||||
import org.apache.commons.math4.optim.MaxEval;
|
import org.apache.commons.math4.optim.MaxEval;
|
||||||
import org.apache.commons.math4.optim.OptimizationData;
|
import org.apache.commons.math4.optim.OptimizationData;
|
||||||
import org.apache.commons.math4.optim.nonlinear.scalar.GoalType;
|
import org.apache.commons.math4.optim.nonlinear.scalar.GoalType;
|
||||||
import org.apache.commons.math4.random.RandomGenerator;
|
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Special implementation of the {@link UnivariateOptimizer} interface
|
* Special implementation of the {@link UnivariateOptimizer} interface
|
||||||
|
@ -47,7 +47,7 @@ public class MultiStartUnivariateOptimizer
|
||||||
/** Number of starts to go. */
|
/** Number of starts to go. */
|
||||||
private final int starts;
|
private final int starts;
|
||||||
/** Random generator for multi-start. */
|
/** Random generator for multi-start. */
|
||||||
private final RandomGenerator generator;
|
private final UniformRandomProvider generator;
|
||||||
/** Found optima. */
|
/** Found optima. */
|
||||||
private UnivariatePointValuePair[] optima;
|
private UnivariatePointValuePair[] optima;
|
||||||
/** Optimization data. */
|
/** Optimization data. */
|
||||||
|
@ -75,7 +75,7 @@ public class MultiStartUnivariateOptimizer
|
||||||
*/
|
*/
|
||||||
public MultiStartUnivariateOptimizer(final UnivariateOptimizer optimizer,
|
public MultiStartUnivariateOptimizer(final UnivariateOptimizer optimizer,
|
||||||
final int starts,
|
final int starts,
|
||||||
final RandomGenerator generator) {
|
final UniformRandomProvider generator) {
|
||||||
super(optimizer.getConvergenceChecker());
|
super(optimizer.getConvergenceChecker());
|
||||||
|
|
||||||
if (starts < 1) {
|
if (starts < 1) {
|
||||||
|
|
|
@ -28,7 +28,8 @@ import org.apache.commons.math4.optim.univariate.SearchInterval;
|
||||||
import org.apache.commons.math4.optim.univariate.UnivariateObjectiveFunction;
|
import org.apache.commons.math4.optim.univariate.UnivariateObjectiveFunction;
|
||||||
import org.apache.commons.math4.optim.univariate.UnivariateOptimizer;
|
import org.apache.commons.math4.optim.univariate.UnivariateOptimizer;
|
||||||
import org.apache.commons.math4.optim.univariate.UnivariatePointValuePair;
|
import org.apache.commons.math4.optim.univariate.UnivariatePointValuePair;
|
||||||
import org.apache.commons.math4.random.JDKRandomGenerator;
|
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||||
|
import org.apache.commons.math4.rng.RandomSource;
|
||||||
import org.apache.commons.math4.util.FastMath;
|
import org.apache.commons.math4.util.FastMath;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -37,8 +38,7 @@ public class MultiStartUnivariateOptimizerTest {
|
||||||
@Test(expected=MathIllegalStateException.class)
|
@Test(expected=MathIllegalStateException.class)
|
||||||
public void testMissingMaxEval() {
|
public void testMissingMaxEval() {
|
||||||
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
||||||
JDKRandomGenerator g = new JDKRandomGenerator();
|
UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 44428400075l);
|
||||||
g.setSeed(44428400075l);
|
|
||||||
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
|
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
|
||||||
optimizer.optimize(new UnivariateObjectiveFunction(new Sin()),
|
optimizer.optimize(new UnivariateObjectiveFunction(new Sin()),
|
||||||
GoalType.MINIMIZE,
|
GoalType.MINIMIZE,
|
||||||
|
@ -47,8 +47,7 @@ public class MultiStartUnivariateOptimizerTest {
|
||||||
@Test(expected=MathIllegalStateException.class)
|
@Test(expected=MathIllegalStateException.class)
|
||||||
public void testMissingSearchInterval() {
|
public void testMissingSearchInterval() {
|
||||||
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
||||||
JDKRandomGenerator g = new JDKRandomGenerator();
|
UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 44428400075l);
|
||||||
g.setSeed(44428400075l);
|
|
||||||
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
|
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
|
||||||
optimizer.optimize(new MaxEval(300),
|
optimizer.optimize(new MaxEval(300),
|
||||||
new UnivariateObjectiveFunction(new Sin()),
|
new UnivariateObjectiveFunction(new Sin()),
|
||||||
|
@ -59,8 +58,7 @@ public class MultiStartUnivariateOptimizerTest {
|
||||||
public void testSinMin() {
|
public void testSinMin() {
|
||||||
UnivariateFunction f = new Sin();
|
UnivariateFunction f = new Sin();
|
||||||
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
||||||
JDKRandomGenerator g = new JDKRandomGenerator();
|
UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 44428400075l);
|
||||||
g.setSeed(44428400075l);
|
|
||||||
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
|
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
|
||||||
optimizer.optimize(new MaxEval(300),
|
optimizer.optimize(new MaxEval(300),
|
||||||
new UnivariateObjectiveFunction(f),
|
new UnivariateObjectiveFunction(f),
|
||||||
|
@ -79,12 +77,9 @@ public class MultiStartUnivariateOptimizerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuinticMin() {
|
public void testQuinticMin() {
|
||||||
// The quintic function has zeros at 0, +-0.5 and +-1.
|
|
||||||
// The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
|
|
||||||
UnivariateFunction f = new QuinticFunction();
|
UnivariateFunction f = new QuinticFunction();
|
||||||
UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
|
UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
|
||||||
JDKRandomGenerator g = new JDKRandomGenerator();
|
UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 4312000053L);
|
||||||
g.setSeed(4312000053L);
|
|
||||||
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g);
|
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g);
|
||||||
|
|
||||||
UnivariatePointValuePair optimum
|
UnivariatePointValuePair optimum
|
||||||
|
@ -92,7 +87,7 @@ public class MultiStartUnivariateOptimizerTest {
|
||||||
new UnivariateObjectiveFunction(f),
|
new UnivariateObjectiveFunction(f),
|
||||||
GoalType.MINIMIZE,
|
GoalType.MINIMIZE,
|
||||||
new SearchInterval(-0.3, -0.2));
|
new SearchInterval(-0.3, -0.2));
|
||||||
Assert.assertEquals(-0.27195613, optimum.getPoint(), 1e-9);
|
Assert.assertEquals(-0.271956128, optimum.getPoint(), 1e-9);
|
||||||
Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9);
|
Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9);
|
||||||
|
|
||||||
UnivariatePointValuePair[] optima = optimizer.getOptima();
|
UnivariatePointValuePair[] optima = optimizer.getOptima();
|
||||||
|
@ -114,8 +109,7 @@ public class MultiStartUnivariateOptimizerTest {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
|
UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
|
||||||
JDKRandomGenerator g = new JDKRandomGenerator();
|
UniformRandomProvider g = RandomSource.create(RandomSource.TWO_CMRES, 4312000053L);
|
||||||
g.setSeed(4312000053L);
|
|
||||||
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g);
|
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -135,5 +129,4 @@ public class MultiStartUnivariateOptimizerTest {
|
||||||
private static class LocalException extends RuntimeException {
|
private static class LocalException extends RuntimeException {
|
||||||
private static final long serialVersionUID = 1194682757034350629L;
|
private static final long serialVersionUID = 1194682757034350629L;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue