Fixed failing test.

As functions evaluations were not counted properly, which has been fixed
a few days ago, the multi-start test in fact did not performed all
expected restarts. This commit fixes this (by increasing the max
evaluations), and fixes the result checks accordingly.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1573523 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2014-03-03 11:11:46 +00:00
parent 3f0a000fef
commit 5929846a20
1 changed files with 19 additions and 12 deletions

View File

@ -18,8 +18,8 @@ package org.apache.commons.math3.optim.nonlinear.scalar;
import org.apache.commons.math3.analysis.MultivariateFunction;
import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
import org.apache.commons.math3.optim.MaxEval;
import org.apache.commons.math3.optim.InitialGuess;
import org.apache.commons.math3.optim.MaxEval;
import org.apache.commons.math3.optim.PointValuePair;
import org.apache.commons.math3.optim.SimpleValueChecker;
import org.apache.commons.math3.optim.nonlinear.scalar.gradient.CircleScalar;
@ -54,26 +54,31 @@ public class MultiStartMultivariateOptimizerTest {
= new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 },
new double[] { 10, 10 },
new GaussianRandomGenerator(g));
int nbStarts = 10;
MultiStartMultivariateOptimizer optimizer
= new MultiStartMultivariateOptimizer(underlying, 10, generator);
= new MultiStartMultivariateOptimizer(underlying, nbStarts, generator);
PointValuePair optimum
= optimizer.optimize(new MaxEval(200),
= optimizer.optimize(new MaxEval(1000),
circle.getObjectiveFunction(),
circle.getObjectiveFunctionGradient(),
GoalType.MINIMIZE,
new InitialGuess(new double[] { 98.680, 47.345 }));
Assert.assertEquals(200, optimizer.getMaxEvaluations());
Assert.assertEquals(1000, optimizer.getMaxEvaluations());
PointValuePair[] optima = optimizer.getOptima();
Assert.assertEquals(nbStarts, optima.length);
for (PointValuePair o : optima) {
// we check the results of all intermediate restarts here (there are 10 such results)
Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]);
Assert.assertEquals(69.9601, circle.getRadius(center), 1e-4);
Assert.assertEquals(96.075, center.getX(), 1e-3);
Assert.assertEquals(48.13516, center.getY(), 1e-5);
Assert.assertTrue(69.9592 < circle.getRadius(center));
Assert.assertTrue(69.9602 > circle.getRadius(center));
Assert.assertTrue(96.0745 < center.getX());
Assert.assertTrue(96.0762 > center.getX());
Assert.assertTrue(48.1344 < center.getY());
Assert.assertTrue(48.1354 > center.getY());
}
Assert.assertTrue(optimizer.getEvaluations() > 590);
Assert.assertTrue(optimizer.getEvaluations() < 610);
Assert.assertTrue(optimizer.getEvaluations() > 850);
Assert.assertTrue(optimizer.getEvaluations() < 900);
Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8);
}
@ -92,19 +97,21 @@ public class MultiStartMultivariateOptimizerTest {
g.setSeed(16069223052l);
RandomVectorGenerator generator
= new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
int nbStarts = 10;
MultiStartMultivariateOptimizer optimizer
= new MultiStartMultivariateOptimizer(underlying, 10, generator);
= new MultiStartMultivariateOptimizer(underlying, nbStarts, generator);
PointValuePair optimum
= optimizer.optimize(new MaxEval(1100),
new ObjectiveFunction(rosenbrock),
GoalType.MINIMIZE,
simplex,
new InitialGuess(new double[] { -1.2, 1.0 }));
Assert.assertEquals(nbStarts, optimizer.getOptima().length);
Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
Assert.assertTrue(optimizer.getEvaluations() > 900);
Assert.assertTrue(optimizer.getEvaluations() < 1200);
Assert.assertTrue(optimum.getValue() < 8e-4);
Assert.assertTrue(optimum.getValue() < 5e-5);
}
private static class Rosenbrock implements MultivariateFunction {