commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1388517 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java
Date Fri, 21 Sep 2012 14:17:38 GMT
Author: erans
Date: Fri Sep 21 14:17:37 2012
New Revision: 1388517

URL: http://svn.apache.org/viewvc?rev=1388517&view=rev
Log:
MATH-864
Fixed missing "repair" of a point that lies outside the boundaries. Thanks
to Frank Hessen for the report and for pinpointing the cause of the problem.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1388517&r1=1388516&r2=1388517&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Fri Sep 21 14:17:37 2012
@@ -52,6 +52,10 @@ If the output is not quite correct, chec
   <body>
     <release version="3.1" date="TBD" description="
 ">
+      <action dev="erans" type="fix" issue="MATH-864" due-to="Frank Hess">
+        "CMAESOptimizer": Solution was not constrained to lie within the
+        provided boundaries.
+      </action>
       <action dev="erans" type="add" issue="MATH-863" due-to="Julien Anxionnat">
         New "Quaternion" class (package "o.a.c.m.complex").
       </action>

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java?rev=1388517&r1=1388516&r2=1388517&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java
Fri Sep 21 14:17:37 2012
@@ -24,9 +24,11 @@ import java.util.List;
 import org.apache.commons.math3.analysis.MultivariateFunction;
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.MathUnsupportedOperationException;
+import org.apache.commons.math3.exception.MathIllegalStateException;
 import org.apache.commons.math3.exception.NotPositiveException;
 import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.TooManyEvaluationsException;
+import org.apache.commons.math3.exception.util.LocalizedFormats;
 import org.apache.commons.math3.linear.Array2DRowRealMatrix;
 import org.apache.commons.math3.linear.EigenDecomposition;
 import org.apache.commons.math3.linear.MatrixUtils;
@@ -414,7 +416,7 @@ public class CMAESOptimizer
                     bestValue = bestFitness;
                     lastResult = optimum;
                     optimum = new PointValuePair(
-                            fitfun.decode(bestArx.getColumn(0)),
+                            fitfun.repairAndDecode(bestArx.getColumn(0)),
                             isMinimize ? bestFitness : -bestFitness);
                     if (getConvergenceChecker() != null && lastResult != null) {
                         if (getConvergenceChecker().converged(iterations, optimum, lastResult))
{
@@ -913,6 +915,16 @@ public class CMAESOptimizer
 
         /**
          * @param x Normalized objective variables.
+         * @return the original objective variables, possibly repaired.
+         */
+        public double[] repairAndDecode(final double[] x) {
+            return boundaries != null && isRepairMode ?
+                decode(repair(x)) :
+                decode(x);
+        }
+
+        /**
+         * @param x Normalized objective variables.
          * @return the original objective variables.
          */
         public double[] decode(final double[] x) {

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java?rev=1388517&r1=1388516&r2=1388517&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.java
Fri Sep 21 14:17:37 2012
@@ -372,6 +372,27 @@ public class CMAESOptimizerTest {
                 1e-10, 1e-4, 1000000, expected);
      }
 
+    @Test
+    public void testMath864() {
+        final CMAESOptimizer optimizer = new CMAESOptimizer();
+        final MultivariateFunction fitnessFunction = new MultivariateFunction() {
+                @Override
+                public double value(double[] parameters) {
+                    final double target = 1;
+                    final double error = target - parameters[0];
+                    return error * error;
+                }
+            };
+
+        final double[] start = { 0 };
+        final double[] lower = { -1e6 };
+        final double[] upper = { 0.5 };
+        final double[] result = optimizer.optimize(10000, fitnessFunction, GoalType.MINIMIZE,
+                                                   start, lower, upper).getPoint();
+        Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
+                          result[0] <= upper[0]);
+    }
+
     /**
      * @param func Function to optimize.
      * @param startPoint Starting point.



Mime
View raw message