From 678377a1e84797e74daf1f5eb835882af9b172dc Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Fri, 28 Sep 2012 14:06:19 +0000 Subject: [PATCH] MATH-867 Unit test showing the problem. It is disabled (with the "@Ignore" annotation) because it fails with the current code. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1391477 13f79535-47bb-0310-9956-ffa450edef68 --- .../direct/CMAESOptimizerTest.java | 67 ++++++++++++++++--- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java b/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java index aec40b053..9d43909c2 100644 --- a/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java +++ b/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java @@ -35,6 +35,7 @@ import org.apache.commons.math3.random.MersenneTwister; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; +import org.junit.Ignore; import org.junit.runner.RunWith; /** @@ -349,9 +350,9 @@ public class CMAESOptimizerTest { @Test public void testConstrainedRosen() { - double[] startPoint = point(DIM,0.1); - double[] insigma = point(DIM,0.1); - double[][] boundaries = boundaries(DIM,-1,2); + double[] startPoint = point(DIM, 0.1); + double[] insigma = point(DIM, 1); + double[][] boundaries = boundaries(DIM, -1, 2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, @@ -387,7 +388,7 @@ public class CMAESOptimizerTest { final double[] start = { 0 }; final double[] lower = { -1e6 }; - final double[] upper = { 0.5 }; + final double[] upper = { 1.5 }; final double[] result = optimizer.optimize(10000, fitnessFunction, GoalType.MINIMIZE, start, lower, upper).getPoint(); Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")", @@ -423,6 +424,51 @@ public class CMAESOptimizerTest { start, lower, upper).getPoint(); } + /** + * Cf. MATH-867 + */ + @Ignore@Test + public void testFitAccuracyDependsOnBoundary() { + final CMAESOptimizer optimizer = new CMAESOptimizer(); + final MultivariateFunction fitnessFunction = new MultivariateFunction() { + public double value(double[] parameters) { + final double target = 11.1; + final double error = target - parameters[0]; + return error * error; + } + }; + + final double[] start = { 1 }; + + // No bounds. + PointValuePair result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE, + start); + final double resNoBound = result.getPoint()[0]; + + // Optimum is near the lower bound. + final double[] lower = { -20 }; + final double[] upper = { 5e16 }; + result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE, + start, lower, upper); + final double resNearLo = result.getPoint()[0]; + + // Optimum is near the upper bound. + lower[0] = -5e16; + upper[0] = 20; + result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE, + start, lower, upper); + final double resNearHi = result.getPoint()[0]; + + // System.out.println("resNoBound=" + resNoBound + + // " resNearLo=" + resNearLo + + // " resNearHi=" + resNearHi); + + // The two values currently differ by a substantial amount, indicating that + // the bounds definition can prevent reaching the optimum. + Assert.assertEquals(resNoBound, resNearLo, 1e-3); + Assert.assertEquals(resNoBound, resNearHi, 1e-3); + } + /** * @param func Function to optimize. * @param startPoint Starting point. @@ -453,17 +499,16 @@ public class CMAESOptimizerTest { PointValuePair expected) { int dim = startPoint.length; // test diagonalOnly = 0 - slow but normally fewer feval# - CMAESOptimizer optim = new CMAESOptimizer( lambda, inSigma, 30000, - stopValue, isActive, diagonalOnly, - 0, new MersenneTwister(), false); + CMAESOptimizer optim = new CMAESOptimizer(lambda, inSigma, 30000, + stopValue, isActive, diagonalOnly, + 0, new MersenneTwister(), false); final double[] lB = boundaries == null ? null : boundaries[0]; final double[] uB = boundaries == null ? null : boundaries[1]; PointValuePair result = optim.optimize(maxEvaluations, func, goal, startPoint, lB, uB); - Assert.assertEquals(expected.getValue(), - result.getValue(), fTol); + // System.out.println("sol=" + Arrays.toString(result.getPoint())); + Assert.assertEquals(expected.getValue(), result.getValue(), fTol); for (int i = 0; i < dim; i++) { - Assert.assertEquals(expected.getPoint()[i], - result.getPoint()[i], pointTol); + Assert.assertEquals(expected.getPoint()[i], result.getPoint()[i], pointTol); } }