commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r758054 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/optimization/direct/ test/org/apache/commons/math/optimization/direct/
Date Tue, 24 Mar 2009 22:13:27 GMT
Author: luc
Date: Tue Mar 24 22:13:27 2009
New Revision: 758054

URL: http://svn.apache.org/viewvc?rev=758054&view=rev
Log:
updated direct algorithms with latest interfaces definitions

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/MultiDirectional.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/NelderMead.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/NelderMeadTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java?rev=758054&r1=758053&r2=758054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java
Tue Mar 24 22:13:27 2009
@@ -20,15 +20,15 @@
 import java.util.Arrays;
 import java.util.Comparator;
 
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.MathRuntimeException;
 import org.apache.commons.math.MaxIterationsExceededException;
-import org.apache.commons.math.optimization.ScalarConvergenceChecker;
+import org.apache.commons.math.analysis.MultivariateRealFunction;
 import org.apache.commons.math.optimization.GoalType;
-import org.apache.commons.math.optimization.ObjectiveException;
-import org.apache.commons.math.optimization.ScalarObjectiveFunction;
 import org.apache.commons.math.optimization.OptimizationException;
-import org.apache.commons.math.optimization.ScalarOptimizer;
-import org.apache.commons.math.optimization.ScalarPointValuePair;
+import org.apache.commons.math.optimization.RealConvergenceChecker;
+import org.apache.commons.math.optimization.MultivariateRealOptimizer;
+import org.apache.commons.math.optimization.RealPointValuePair;
 import org.apache.commons.math.optimization.SimpleScalarValueChecker;
 
 /** 
@@ -58,14 +58,14 @@
  * before optimization is attempted, an explicit call to the first method
  * with all steps set to +1 is triggered, thus building a default
  * configuration from a unit hypercube. Each call to {@link
- * #optimize(ScalarObjectiveFunction, GoalType, double[]) optimize} will reuse
+ * #optimize(MultivariateRealFunction, GoalType, double[]) optimize} will reuse
  * the current start configuration and move it such that its first vertex
  * is at the provided start point of the optimization. If the same optimizer
  * is used to solve different problems and the number of parameters change,
  * the start configuration <em>must</em> be reset or a dimension mismatch
  * will occur.</p>
  *
- * <p>If {@link #setConvergenceChecker(ScalarConvergenceChecker)} is not called,
+ * <p>If {@link #setConvergenceChecker(RealConvergenceChecker)} is not called,
  * a default {@link SimpleScalarValueChecker} is used.</p>
  *
  * <p>Convergence is checked by providing the <em>worst</em> points of
@@ -76,25 +76,25 @@
  * performed by the derived classes according to the implemented
  * algorithms.</p>
  *
- * @see ScalarObjectiveFunction
+ * @see MultivariateRealFunction
  * @see NelderMead
  * @see MultiDirectional
  * @version $Revision$ $Date$
  * @since 1.2
  */
-public abstract class DirectSearchOptimizer implements ScalarOptimizer {
+public abstract class DirectSearchOptimizer implements MultivariateRealOptimizer {
 
     /** Serializable version identifier. */
     private static final long serialVersionUID = 4299910390345933369L;
 
     /** Simplex. */
-    protected ScalarPointValuePair[] simplex;
+    protected RealPointValuePair[] simplex;
 
     /** Objective function. */
-    private ScalarObjectiveFunction f;
+    private MultivariateRealFunction f;
 
     /** Convergence checker. */
-    private ScalarConvergenceChecker checker;
+    private RealConvergenceChecker checker;
 
     /** Maximal number of iterations allowed. */
     private int maxIterations;
@@ -232,19 +232,21 @@
     }
 
     /** {@inheritDoc} */
-    public void setConvergenceChecker(ScalarConvergenceChecker checker) {
+    public void setConvergenceChecker(RealConvergenceChecker checker) {
         this.checker = checker;
     }
 
     /** {@inheritDoc} */
-    public ScalarConvergenceChecker getConvergenceChecker() {
+    public RealConvergenceChecker getConvergenceChecker() {
         return checker;
     }
 
     /** {@inheritDoc} */
-    public ScalarPointValuePair optimize(final ScalarObjectiveFunction f, final GoalType
goalType,
+    public RealPointValuePair optimize(final MultivariateRealFunction f,
+                                         final GoalType goalType,
                                          final double[] startPoint)
-        throws ObjectiveException, OptimizationException, IllegalArgumentException {
+        throws FunctionEvaluationException, OptimizationException,
+        IllegalArgumentException {
 
         if (startConfiguration == null) {
             // no initial configuration has been set up for simplex
@@ -255,14 +257,16 @@
         }
 
         this.f = f;
-        final Comparator<ScalarPointValuePair> comparator = new Comparator<ScalarPointValuePair>()
{
-            public int compare(final ScalarPointValuePair o1, final ScalarPointValuePair
o2) {
-                final double v1 = o1.getValue();
-                final double v2 = o2.getValue();
-                return (goalType == GoalType.MINIMIZE) ?
-                        Double.compare(v1, v2) : Double.compare(v2, v1);
-            }
-        };
+        final Comparator<RealPointValuePair> comparator =
+            new Comparator<RealPointValuePair>() {
+                public int compare(final RealPointValuePair o1,
+                                   final RealPointValuePair o2) {
+                    final double v1 = o1.getValue();
+                    final double v2 = o2.getValue();
+                    return (goalType == GoalType.MINIMIZE) ?
+                            Double.compare(v1, v2) : Double.compare(v2, v1);
+                }
+            };
 
         // initialize search
         iterations  = 0;
@@ -270,7 +274,7 @@
         buildSimplex(startPoint);
         evaluateSimplex(comparator);
 
-        ScalarPointValuePair[] previous = new ScalarPointValuePair[simplex.length];
+        RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
         while (true) {
 
             if (iterations > 0) {
@@ -305,26 +309,26 @@
 
     /** Compute the next simplex of the algorithm.
      * @param comparator comparator to use to sort simplex vertices from best to worst
-     * @exception ObjectiveException if the function cannot be evaluated at
+     * @exception FunctionEvaluationException if the function cannot be evaluated at
      * some point
      * @exception OptimizationException if the algorithm fails to converge
      * @exception IllegalArgumentException if the start point dimension is wrong
      */
-    protected abstract void iterateSimplex(final Comparator<ScalarPointValuePair> comparator)
-        throws ObjectiveException, OptimizationException, IllegalArgumentException;
+    protected abstract void iterateSimplex(final Comparator<RealPointValuePair> comparator)
+        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
 
     /** Evaluate the objective function on one point.
      * <p>A side effect of this method is to count the number of
      * function evaluations</p>
      * @param x point on which the objective function should be evaluated
      * @return objective function value at the given point
-     * @exception ObjectiveException if no value can be computed for the parameters
+     * @exception FunctionEvaluationException if no value can be computed for the parameters
      * @exception IllegalArgumentException if the start point dimension is wrong
      */
     protected double evaluate(final double[] x)
-        throws ObjectiveException, IllegalArgumentException {
+        throws FunctionEvaluationException, IllegalArgumentException {
         evaluations++;
-        return f.objective(x);
+        return f.value(x);
     }
 
     /** Build an initial simplex.
@@ -343,8 +347,8 @@
         }
 
         // set first vertex
-        simplex = new ScalarPointValuePair[n + 1];
-        simplex[0] = new ScalarPointValuePair(startPoint, Double.NaN);
+        simplex = new RealPointValuePair[n + 1];
+        simplex[0] = new RealPointValuePair(startPoint, Double.NaN);
 
         // set remaining vertices
         for (int i = 0; i < n; ++i) {
@@ -353,24 +357,24 @@
             for (int k = 0; k < n; ++k) {
                 vertexI[k] = startPoint[k] + confI[k];
             }
-            simplex[i + 1] = new ScalarPointValuePair(vertexI, Double.NaN);
+            simplex[i + 1] = new RealPointValuePair(vertexI, Double.NaN);
         }
 
     }
 
     /** Evaluate all the non-evaluated points of the simplex.
      * @param comparator comparator to use to sort simplex vertices from best to worst
-     * @exception ObjectiveException if no value can be computed for the parameters
+     * @exception FunctionEvaluationException if no value can be computed for the parameters
      */
-    protected void evaluateSimplex(final Comparator<ScalarPointValuePair> comparator)
-        throws ObjectiveException {
+    protected void evaluateSimplex(final Comparator<RealPointValuePair> comparator)
+        throws FunctionEvaluationException {
 
         // evaluate the objective function at all non-evaluated simplex points
         for (int i = 0; i < simplex.length; ++i) {
-            final ScalarPointValuePair vertex = simplex[i];
+            final RealPointValuePair vertex = simplex[i];
             final double[] point = vertex.getPointRef();
             if (Double.isNaN(vertex.getValue())) {
-                simplex[i] = new ScalarPointValuePair(point, evaluate(point), false);
+                simplex[i] = new RealPointValuePair(point, evaluate(point), false);
             }
         }
 
@@ -383,12 +387,12 @@
      * @param pointValuePair point to insert
      * @param comparator comparator to use to sort simplex vertices from best to worst
      */
-    protected void replaceWorstPoint(ScalarPointValuePair pointValuePair,
-                                     final Comparator<ScalarPointValuePair> comparator)
{
+    protected void replaceWorstPoint(RealPointValuePair pointValuePair,
+                                     final Comparator<RealPointValuePair> comparator)
{
         int n = simplex.length - 1;
         for (int i = 0; i < n; ++i) {
             if (comparator.compare(simplex[i], pointValuePair) > 0) {
-                ScalarPointValuePair tmp = simplex[i];
+                RealPointValuePair tmp = simplex[i];
                 simplex[i]         = pointValuePair;
                 pointValuePair     = tmp;
             }

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/MultiDirectional.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/MultiDirectional.java?rev=758054&r1=758053&r2=758054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/MultiDirectional.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/MultiDirectional.java
Tue Mar 24 22:13:27 2009
@@ -19,9 +19,9 @@
 
 import java.util.Comparator;
 
-import org.apache.commons.math.optimization.ObjectiveException;
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.optimization.OptimizationException;
-import org.apache.commons.math.optimization.ScalarPointValuePair;
+import org.apache.commons.math.optimization.RealPointValuePair;
 
 /** 
  * This class implements the multi-directional direct search method.
@@ -59,24 +59,24 @@
     }
 
     /** {@inheritDoc} */
-    protected void iterateSimplex(final Comparator<ScalarPointValuePair> comparator)
-        throws ObjectiveException, OptimizationException, IllegalArgumentException {
+    protected void iterateSimplex(final Comparator<RealPointValuePair> comparator)
+        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException
{
 
         while (true) {
 
             incrementIterationsCounter();
 
             // save the original vertex
-            final ScalarPointValuePair[] original = simplex;
-            final ScalarPointValuePair best = original[0];
+            final RealPointValuePair[] original = simplex;
+            final RealPointValuePair best = original[0];
 
             // perform a reflection step
-            final ScalarPointValuePair reflected = evaluateNewSimplex(original, 1.0, comparator);
+            final RealPointValuePair reflected = evaluateNewSimplex(original, 1.0, comparator);
             if (comparator.compare(reflected, best) < 0) {
 
                 // compute the expanded simplex
-                final ScalarPointValuePair[] reflectedSimplex = simplex;
-                final ScalarPointValuePair expanded = evaluateNewSimplex(original, khi, comparator);
+                final RealPointValuePair[] reflectedSimplex = simplex;
+                final RealPointValuePair expanded = evaluateNewSimplex(original, khi, comparator);
                 if (comparator.compare(reflected, expanded) <= 0) {
                     // accept the reflected simplex
                     simplex = reflectedSimplex;
@@ -87,7 +87,7 @@
             }
 
             // compute the contracted simplex
-            final ScalarPointValuePair contracted = evaluateNewSimplex(original, gamma, comparator);
+            final RealPointValuePair contracted = evaluateNewSimplex(original, gamma, comparator);
             if (comparator.compare(contracted, best) < 0) {
                 // accept the contracted simplex
                 return;
@@ -102,19 +102,19 @@
      * @param coeff linear coefficient
      * @param comparator comparator to use to sort simplex vertices from best to poorest
      * @return best point in the transformed simplex
-     * @exception ObjectiveException if the function cannot be evaluated at
+     * @exception FunctionEvaluationException if the function cannot be evaluated at
      * some point
      */
-    private ScalarPointValuePair evaluateNewSimplex(final ScalarPointValuePair[] original,
+    private RealPointValuePair evaluateNewSimplex(final RealPointValuePair[] original,
                                               final double coeff,
-                                              final Comparator<ScalarPointValuePair>
comparator)
-        throws ObjectiveException {
+                                              final Comparator<RealPointValuePair>
comparator)
+        throws FunctionEvaluationException {
 
         final double[] xSmallest = original[0].getPointRef();
         final int n = xSmallest.length;
 
         // create the linearly transformed simplex
-        simplex = new ScalarPointValuePair[n + 1];
+        simplex = new RealPointValuePair[n + 1];
         simplex[0] = original[0];
         for (int i = 1; i <= n; ++i) {
             final double[] xOriginal    = original[i].getPointRef();
@@ -122,7 +122,7 @@
             for (int j = 0; j < n; ++j) {
                 xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
             }
-            simplex[i] = new ScalarPointValuePair(xTransformed, Double.NaN, false);
+            simplex[i] = new RealPointValuePair(xTransformed, Double.NaN, false);
         }
 
         // evaluate it

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/NelderMead.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/NelderMead.java?rev=758054&r1=758053&r2=758054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/NelderMead.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/direct/NelderMead.java
Tue Mar 24 22:13:27 2009
@@ -19,9 +19,9 @@
 
 import java.util.Comparator;
 
-import org.apache.commons.math.optimization.ObjectiveException;
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.optimization.OptimizationException;
-import org.apache.commons.math.optimization.ScalarPointValuePair;
+import org.apache.commons.math.optimization.RealPointValuePair;
 
 /** 
  * This class implements the Nelder-Mead direct search method.
@@ -73,8 +73,8 @@
     }
 
     /** {@inheritDoc} */
-    protected void iterateSimplex(final Comparator<ScalarPointValuePair> comparator)
-        throws ObjectiveException, OptimizationException {
+    protected void iterateSimplex(final Comparator<RealPointValuePair> comparator)
+        throws FunctionEvaluationException, OptimizationException {
 
         incrementIterationsCounter();
 
@@ -82,9 +82,9 @@
         final int n = simplex.length - 1;
 
         // interesting values
-        final ScalarPointValuePair best       = simplex[0];
-        final ScalarPointValuePair secondBest = simplex[n-1];
-        final ScalarPointValuePair worst      = simplex[n];
+        final RealPointValuePair best       = simplex[0];
+        final RealPointValuePair secondBest = simplex[n-1];
+        final RealPointValuePair worst      = simplex[n];
         final double[] xWorst = worst.getPointRef();
 
         // compute the centroid of the best vertices
@@ -106,7 +106,7 @@
         for (int j = 0; j < n; ++j) {
             xR[j] = centroid[j] + rho * (centroid[j] - xWorst[j]);
         }
-        final ScalarPointValuePair reflected = new ScalarPointValuePair(xR, evaluate(xR),
false);
+        final RealPointValuePair reflected = new RealPointValuePair(xR, evaluate(xR), false);
 
         if ((comparator.compare(best, reflected) <= 0) &&
             (comparator.compare(reflected, secondBest) < 0)) {
@@ -121,7 +121,7 @@
             for (int j = 0; j < n; ++j) {
                 xE[j] = centroid[j] + khi * (xR[j] - centroid[j]);
             }
-            final ScalarPointValuePair expanded = new ScalarPointValuePair(xE, evaluate(xE),
false);
+            final RealPointValuePair expanded = new RealPointValuePair(xE, evaluate(xE),
false);
 
             if (comparator.compare(expanded, reflected) < 0) {
                 // accept the expansion point
@@ -140,7 +140,7 @@
                 for (int j = 0; j < n; ++j) {
                     xC[j] = centroid[j] + gamma * (xR[j] - centroid[j]);
                 }
-                final ScalarPointValuePair outContracted = new ScalarPointValuePair(xC, evaluate(xC),
false);
+                final RealPointValuePair outContracted = new RealPointValuePair(xC, evaluate(xC),
false);
 
                 if (comparator.compare(outContracted, reflected) <= 0) {
                     // accept the contraction point
@@ -155,7 +155,7 @@
                 for (int j = 0; j < n; ++j) {
                     xC[j] = centroid[j] - gamma * (centroid[j] - xWorst[j]);
                 }
-                final ScalarPointValuePair inContracted = new ScalarPointValuePair(xC, evaluate(xC),
false);
+                final RealPointValuePair inContracted = new RealPointValuePair(xC, evaluate(xC),
false);
 
                 if (comparator.compare(inContracted, worst) < 0) {
                     // accept the contraction point
@@ -172,7 +172,7 @@
                 for (int j = 0; j < n; ++j) {
                     x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]);
                 }
-                simplex[i] = new ScalarPointValuePair(x, Double.NaN, false);
+                simplex[i] = new RealPointValuePair(x, Double.NaN, false);
             }
             evaluateSimplex(comparator);
 

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java?rev=758054&r1=758053&r2=758054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java
Tue Mar 24 22:13:27 2009
@@ -22,11 +22,11 @@
 import junit.framework.TestSuite;
 
 import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.analysis.MultivariateRealFunction;
 import org.apache.commons.math.linear.decomposition.NotPositiveDefiniteMatrixException;
 import org.apache.commons.math.optimization.GoalType;
-import org.apache.commons.math.optimization.ObjectiveException;
-import org.apache.commons.math.optimization.ScalarObjectiveFunction;
-import org.apache.commons.math.optimization.ScalarPointValuePair;
+import org.apache.commons.math.optimization.RealPointValuePair;
 import org.apache.commons.math.optimization.SimpleScalarValueChecker;
 
 public class MultiDirectionalTest
@@ -36,15 +36,15 @@
     super(name);
   }
 
-  public void testObjectiveExceptions() throws ConvergenceException {
-      ScalarObjectiveFunction wrong =
-          new ScalarObjectiveFunction() {
+  public void testFunctionEvaluationExceptions() throws ConvergenceException {
+      MultivariateRealFunction wrong =
+          new MultivariateRealFunction() {
             private static final long serialVersionUID = 4751314470965489371L;
-            public double objective(double[] x) throws ObjectiveException {
+            public double value(double[] x) throws FunctionEvaluationException {
                 if (x[0] < 0) {
-                    throw new ObjectiveException("{0}", "oops");
+                    throw new FunctionEvaluationException(x, "{0}", "oops");
                 } else if (x[0] > 1) {
-                    throw new ObjectiveException(new RuntimeException("oops"));
+                    throw new FunctionEvaluationException(new RuntimeException("oops"), x);
                 } else {
                     return x[0] * (1 - x[0]);
                 }
@@ -54,7 +54,7 @@
           MultiDirectional optimizer = new MultiDirectional(0.9, 1.9);
           optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { -1.0 });
           fail("an exception should have been thrown");
-      } catch (ObjectiveException ce) {
+      } catch (FunctionEvaluationException ce) {
           // expected behavior
           assertNull(ce.getCause());
       } catch (Exception e) {
@@ -64,7 +64,7 @@
           MultiDirectional optimizer = new MultiDirectional(0.9, 1.9);
           optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { +2.0 });
           fail("an exception should have been thrown");
-      } catch (ObjectiveException ce) {
+      } catch (FunctionEvaluationException ce) {
           // expected behavior
           assertNotNull(ce.getCause());
       } catch (Exception e) {
@@ -73,7 +73,7 @@
   }
 
   public void testMinimizeMaximize()
-      throws ObjectiveException, ConvergenceException, NotPositiveDefiniteMatrixException
{
+      throws FunctionEvaluationException, ConvergenceException, NotPositiveDefiniteMatrixException
{
 
       // the following function has 4 local extrema:
       final double xM        = -3.841947088256863675365;
@@ -84,9 +84,9 @@
       final double valueXmYp = -valueXmYm;                // local  minimum
       final double valueXpYm = -0.7290400707055187115322; // global minimum
       final double valueXpYp = -valueXpYm;                // global maximum
-      ScalarObjectiveFunction fourExtrema = new ScalarObjectiveFunction() {
+      MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
           private static final long serialVersionUID = -7039124064449091152L;
-          public double objective(double[] variables) {
+          public double value(double[] variables) throws FunctionEvaluationException {
               final double x = variables[0];
               final double y = variables[1];
               return ((x == 0) || (y == 0)) ? 0 : (Math.atan(x) * Math.atan(x + 2) * Math.atan(y)
* Math.atan(y) / (x * y));
@@ -97,7 +97,7 @@
       optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-10, 1.0e-30));
       optimizer.setMaxIterations(200);
       optimizer.setStartConfiguration(new double[] { 0.2, 0.2 });
-      ScalarPointValuePair optimum;
+      RealPointValuePair optimum;
 
       // minimization
       optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { -3.0, 0
});
@@ -132,12 +132,12 @@
   }
 
   public void testRosenbrock()
-    throws ObjectiveException, ConvergenceException {
+    throws FunctionEvaluationException, ConvergenceException {
 
-    ScalarObjectiveFunction rosenbrock =
-      new ScalarObjectiveFunction() {
+    MultivariateRealFunction rosenbrock =
+      new MultivariateRealFunction() {
         private static final long serialVersionUID = -9044950469615237490L;
-        public double objective(double[] x) {
+        public double value(double[] x) throws FunctionEvaluationException {
           ++count;
           double a = x[1] - x[0] * x[0];
           double b = 1.0 - x[0];
@@ -152,7 +152,7 @@
     optimizer.setStartConfiguration(new double[][] {
             { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
     });
-    ScalarPointValuePair optimum =
+    RealPointValuePair optimum =
         optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });
 
     assertEquals(count, optimizer.getEvaluations());
@@ -163,12 +163,12 @@
   }
 
   public void testPowell()
-    throws ObjectiveException, ConvergenceException {
+    throws FunctionEvaluationException, ConvergenceException {
 
-    ScalarObjectiveFunction powell =
-      new ScalarObjectiveFunction() {
+    MultivariateRealFunction powell =
+      new MultivariateRealFunction() {
         private static final long serialVersionUID = -832162886102041840L;
-        public double objective(double[] x) {
+        public double value(double[] x) throws FunctionEvaluationException {
           ++count;
           double a = x[0] + 10 * x[1];
           double b = x[2] - x[3];
@@ -182,7 +182,7 @@
     MultiDirectional optimizer = new MultiDirectional();
     optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
     optimizer.setMaxIterations(1000);
-    ScalarPointValuePair optimum =
+    RealPointValuePair optimum =
       optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
     assertEquals(count, optimizer.getEvaluations());
     assertTrue(optimizer.getEvaluations() > 800);

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/NelderMeadTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/NelderMeadTest.java?rev=758054&r1=758053&r2=758054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/NelderMeadTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/direct/NelderMeadTest.java
Tue Mar 24 22:13:27 2009
@@ -22,11 +22,11 @@
 import junit.framework.TestSuite;
 
 import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.analysis.MultivariateRealFunction;
 import org.apache.commons.math.linear.decomposition.NotPositiveDefiniteMatrixException;
 import org.apache.commons.math.optimization.GoalType;
-import org.apache.commons.math.optimization.ObjectiveException;
-import org.apache.commons.math.optimization.ScalarObjectiveFunction;
-import org.apache.commons.math.optimization.ScalarPointValuePair;
+import org.apache.commons.math.optimization.RealPointValuePair;
 import org.apache.commons.math.optimization.SimpleScalarValueChecker;
 
 public class NelderMeadTest
@@ -36,15 +36,15 @@
     super(name);
   }
 
-  public void testObjectiveExceptions() throws ConvergenceException {
-      ScalarObjectiveFunction wrong =
-          new ScalarObjectiveFunction() {
+  public void testFunctionEvaluationExceptions() throws ConvergenceException {
+      MultivariateRealFunction wrong =
+          new MultivariateRealFunction() {
             private static final long serialVersionUID = 4751314470965489371L;
-            public double objective(double[] x) throws ObjectiveException {
+            public double value(double[] x) throws FunctionEvaluationException {
                 if (x[0] < 0) {
-                    throw new ObjectiveException("{0}", "oops");
+                    throw new FunctionEvaluationException(x, "{0}", "oops");
                 } else if (x[0] > 1) {
-                    throw new ObjectiveException(new RuntimeException("oops"));
+                    throw new FunctionEvaluationException(new RuntimeException("oops"), x);
                 } else {
                     return x[0] * (1 - x[0]);
                 }
@@ -54,7 +54,7 @@
           NelderMead optimizer = new NelderMead(0.9, 1.9, 0.4, 0.6);
           optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { -1.0 });
           fail("an exception should have been thrown");
-      } catch (ObjectiveException ce) {
+      } catch (FunctionEvaluationException ce) {
           // expected behavior
           assertNull(ce.getCause());
       } catch (Exception e) {
@@ -64,7 +64,7 @@
           NelderMead optimizer = new NelderMead(0.9, 1.9, 0.4, 0.6);
           optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { +2.0 });
           fail("an exception should have been thrown");
-      } catch (ObjectiveException ce) {
+      } catch (FunctionEvaluationException ce) {
           // expected behavior
           assertNotNull(ce.getCause());
       } catch (Exception e) {
@@ -73,7 +73,7 @@
   }
 
   public void testMinimizeMaximize()
-      throws ObjectiveException, ConvergenceException, NotPositiveDefiniteMatrixException
{
+      throws FunctionEvaluationException, ConvergenceException, NotPositiveDefiniteMatrixException
{
 
       // the following function has 4 local extrema:
       final double xM        = -3.841947088256863675365;
@@ -84,9 +84,9 @@
       final double valueXmYp = -valueXmYm;                // local  minimum
       final double valueXpYm = -0.7290400707055187115322; // global minimum
       final double valueXpYp = -valueXpYm;                // global maximum
-      ScalarObjectiveFunction fourExtrema = new ScalarObjectiveFunction() {
+      MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
           private static final long serialVersionUID = -7039124064449091152L;
-          public double objective(double[] variables) {
+          public double value(double[] variables) throws FunctionEvaluationException {
               final double x = variables[0];
               final double y = variables[1];
               return ((x == 0) || (y == 0)) ? 0 : (Math.atan(x) * Math.atan(x + 2) * Math.atan(y)
* Math.atan(y) / (x * y));
@@ -97,7 +97,7 @@
       optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-10, 1.0e-30));
       optimizer.setMaxIterations(100);
       optimizer.setStartConfiguration(new double[] { 0.2, 0.2 });
-      ScalarPointValuePair optimum;
+      RealPointValuePair optimum;
 
       // minimization
       optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { -3.0, 0
});
@@ -132,12 +132,12 @@
   }
 
   public void testRosenbrock()
-    throws ObjectiveException, ConvergenceException {
+    throws FunctionEvaluationException, ConvergenceException {
 
-    ScalarObjectiveFunction rosenbrock =
-      new ScalarObjectiveFunction() {
+    MultivariateRealFunction rosenbrock =
+      new MultivariateRealFunction() {
         private static final long serialVersionUID = -9044950469615237490L;
-        public double objective(double[] x) {
+        public double value(double[] x) throws FunctionEvaluationException {
           ++count;
           double a = x[1] - x[0] * x[0];
           double b = 1.0 - x[0];
@@ -152,7 +152,7 @@
     optimizer.setStartConfiguration(new double[][] {
             { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
     });
-    ScalarPointValuePair optimum =
+    RealPointValuePair optimum =
         optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });
 
     assertEquals(count, optimizer.getEvaluations());
@@ -163,12 +163,12 @@
   }
 
   public void testPowell()
-    throws ObjectiveException, ConvergenceException {
+    throws FunctionEvaluationException, ConvergenceException {
 
-    ScalarObjectiveFunction powell =
-      new ScalarObjectiveFunction() {
+    MultivariateRealFunction powell =
+      new MultivariateRealFunction() {
         private static final long serialVersionUID = -832162886102041840L;
-        public double objective(double[] x) {
+        public double value(double[] x) throws FunctionEvaluationException {
           ++count;
           double a = x[0] + 10 * x[1];
           double b = x[2] - x[3];
@@ -182,7 +182,7 @@
     NelderMead optimizer = new NelderMead();
     optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
     optimizer.setMaxIterations(200);
-    ScalarPointValuePair optimum =
+    RealPointValuePair optimum =
       optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
     assertEquals(count, optimizer.getEvaluations());
     assertTrue(optimizer.getEvaluations() > 110);



Mime
View raw message