diff --git a/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java b/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java index f6a4f2820..866c8fdf8 100644 --- a/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java +++ b/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java @@ -585,8 +585,8 @@ public class CMAESOptimizer // initialize sigma double[][] sigmaArray = new double[guess.length][1]; for (int i = 0; i < guess.length; i++) { - final double range = (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i]; - sigmaArray[i][0] = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range; + final double range = (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i]; + sigmaArray[i][0] = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range; } RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false); sigma = max(insigma); // overall standard deviation @@ -929,7 +929,7 @@ public class CMAESOptimizer double[] res = new double[x.length]; for (int i = 0; i < x.length; i++) { double diff = boundaries[1][i] - boundaries[0][i]; - res[i] = (x[i] - boundaries[0][i]) / diff; + res[i] = x[i] / diff; } return res; } @@ -955,7 +955,7 @@ public class CMAESOptimizer double[] res = new double[x.length]; for (int i = 0; i < x.length; i++) { double diff = boundaries[1][i] - boundaries[0][i]; - res[i] = diff * x[i] + boundaries[0][i]; + res[i] = diff * x[i]; } return res; } @@ -986,11 +986,15 @@ public class CMAESOptimizer if (boundaries == null) { return true; } + + final double[] bLoEnc = encode(boundaries[0]); + final double[] bHiEnc = encode(boundaries[1]); + for (int i = 0; i < x.length; i++) { - if (x[i] < 0) { + if (x[i] < bLoEnc[i]) { return false; } - if (x[i] > 1.0) { + if (x[i] > bHiEnc[i]) { return false; } } 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 9d43909c2..4ebd1b0c3 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 @@ -350,8 +350,8 @@ public class CMAESOptimizerTest { @Test public void testConstrainedRosen() { - double[] startPoint = point(DIM, 0.1); - double[] insigma = point(DIM, 1); + double[] startPoint = point(DIM, 0.7); + double[] insigma = point(DIM, 0.1); double[][] boundaries = boundaries(DIM, -1, 2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); @@ -427,7 +427,7 @@ public class CMAESOptimizerTest { /** * Cf. MATH-867 */ - @Ignore@Test + @Test public void testFitAccuracyDependsOnBoundary() { final CMAESOptimizer optimizer = new CMAESOptimizer(); final MultivariateFunction fitnessFunction = new MultivariateFunction() { @@ -439,7 +439,7 @@ public class CMAESOptimizerTest { }; final double[] start = { 1 }; - + // No bounds. PointValuePair result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE, start);