commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r786466 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/optimization/ java/org/apache/commons/math/optimization/direct/ java/org/apache/commons/math/optimization/general/ java/org/apache/commons/math/optimization/linear/ j...
Date Fri, 19 Jun 2009 12:03:15 GMT
Author: luc
Date: Fri Jun 19 12:03:14 2009
New Revision: 786466

URL: http://svn.apache.org/viewvc?rev=786466&view=rev
Log:
added protection against infinite loops by setting a maximal number of evaluations
(some classes were forgotten in the first pass to fix this problem)

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java
    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/general/AbstractLeastSquaresOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/AbstractLinearOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.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/DifferentiableMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -52,6 +52,16 @@
      */
     int getIterations();
 
+    /** Set the maximal number of functions evaluations.
+     * @param maxEvaluations maximal number of function evaluations
+     */
+    void setMaxEvaluations(int maxEvaluations);
+
+    /** Get the maximal number of functions evaluations.
+     * @return maximal number of functions evaluations
+     */
+    int getMaxEvaluations();
+
     /** Get the number of evaluations of the objective function.
      * <p>
      * The number of evaluations corresponds to the last call to the

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -48,6 +48,16 @@
     */
    int getIterations();
 
+   /** Set the maximal number of functions evaluations.
+    * @param maxEvaluations maximal number of function evaluations
+    */
+   void setMaxEvaluations(int maxEvaluations);
+
+   /** Get the maximal number of functions evaluations.
+    * @return maximal number of functions evaluations
+    */
+   int getMaxEvaluations();
+
     /** Get the number of evaluations of the objective function.
      * <p>
      * The number of evaluation correspond to the last call to the

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -53,6 +53,9 @@
     /** Number of iterations already performed for all starts. */
     private int totalIterations;
 
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
     /** Number of evaluations already performed for all starts. */
     private int totalEvaluations;
 
@@ -80,13 +83,14 @@
                                                              final int starts,
                                                              final RandomVectorGenerator
generator) {
         this.optimizer                = optimizer;
-        this.maxIterations            = Integer.MAX_VALUE;
         this.totalIterations          = 0;
         this.totalEvaluations         = 0;
         this.totalGradientEvaluations = 0;
         this.starts                   = starts;
         this.generator                = generator;
         this.optima                   = null;
+        setMaxIterations(Integer.MAX_VALUE);
+        setMaxEvaluations(Integer.MAX_VALUE);
     }
 
     /** Get all the optima found during the last call to {@link
@@ -139,6 +143,16 @@
     }
 
     /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
     public int getEvaluations() {
         return totalEvaluations;
     }
@@ -174,6 +188,7 @@
 
             try {
                 optimizer.setMaxIterations(maxIterations - totalIterations);
+                optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations);
                 optima[i] = optimizer.optimize(f, goalType,
                                                (i == 0) ? startPoint : generator.nextVector());
             } catch (FunctionEvaluationException fee) {

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -52,6 +52,9 @@
     /** Number of iterations already performed for all starts. */
     private int totalIterations;
 
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
     /** Number of evaluations already performed for all starts. */
     private int totalEvaluations;
 
@@ -80,13 +83,14 @@
                 final int starts,
                 final RandomVectorGenerator generator) {
         this.optimizer                = optimizer;
-        this.maxIterations            = Integer.MAX_VALUE;
         this.totalIterations          = 0;
         this.totalEvaluations         = 0;
         this.totalJacobianEvaluations = 0;
         this.starts                   = starts;
         this.generator                = generator;
         this.optima                   = null;
+        setMaxIterations(Integer.MAX_VALUE);
+        setMaxEvaluations(Integer.MAX_VALUE);
     }
 
     /** Get all the optima found during the last call to {@link
@@ -139,6 +143,16 @@
     }
 
     /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
     public int getEvaluations() {
         return totalEvaluations;
     }
@@ -174,6 +188,7 @@
 
             try {
                 optimizer.setMaxIterations(maxIterations - totalIterations);
+                optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations);
                 optima[i] = optimizer.optimize(f, target, weights,
                                                (i == 0) ? startPoint : generator.nextVector());
             } catch (FunctionEvaluationException fee) {

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -81,12 +81,13 @@
                                                final int starts,
                                                final RandomVectorGenerator generator) {
         this.optimizer        = optimizer;
-        this.maxIterations    = Integer.MAX_VALUE;
         this.totalIterations  = 0;
         this.totalEvaluations = 0;
         this.starts           = starts;
         this.generator        = generator;
         this.optima           = null;
+        setMaxIterations(Integer.MAX_VALUE);
+        setMaxEvaluations(Integer.MAX_VALUE);
     }
 
     /** Get all the optima found during the last call to {@link

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -47,9 +47,15 @@
     /** Maximal number of iterations allowed. */
     private int maxIterations;
 
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
     /** Number of iterations already performed for all starts. */
     private int totalIterations;
 
+    /** Number of evaluations already performed for all starts. */
+    private int totalEvaluations;
+
     /** Number of starts to go. */
     private int starts;
 
@@ -71,11 +77,12 @@
                                              final int starts,
                                              final RandomGenerator generator) {
         this.optimizer        = optimizer;
-        this.maxIterations    = Integer.MAX_VALUE;
         this.totalIterations  = 0;
         this.starts           = starts;
         this.generator        = generator;
         this.optima           = null;
+        setMaximalIterationCount(Integer.MAX_VALUE);
+        setMaxEvaluations(Integer.MAX_VALUE);
     }
 
     /** {@inheritDoc} */
@@ -104,6 +111,16 @@
     }
 
     /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getEvaluations() {
+        return totalEvaluations;
+    }
+
+    /** {@inheritDoc} */
     public double getRelativeAccuracy() {
         return optimizer.getRelativeAccuracy();
     }
@@ -134,6 +151,11 @@
     }
 
     /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
     public void setRelativeAccuracy(double accuracy) {
         optimizer.setRelativeAccuracy(accuracy);
     }
@@ -184,14 +206,16 @@
                            final double min, final double max, final double startValue)
             throws ConvergenceException, FunctionEvaluationException {
 
-        optima          = new double[starts];
-        totalIterations = 0;
+        optima           = new double[starts];
+        totalIterations  = 0;
+        totalEvaluations = 0;
 
         // multi-start loop
         for (int i = 0; i < starts; ++i) {
 
             try {
                 optimizer.setMaximalIterationCount(maxIterations - totalIterations);
+                optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations);
                 optima[i] = optimizer.optimize(f, goalType, min, max,
                                                (i == 0) ? startValue : generator.nextDouble()
* (max - min));
             } catch (FunctionEvaluationException fee) {
@@ -201,6 +225,7 @@
             }
 
             totalIterations  += optimizer.getIterationCount();
+            totalEvaluations += optimizer.getEvaluations();
 
         }
 

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -30,6 +30,26 @@
  */
 public interface UnivariateRealOptimizer extends ConvergingAlgorithm {
 
+    /** Set the maximal number of functions evaluations.
+     * @param maxEvaluations maximal number of function evaluations
+     */
+    void setMaxEvaluations(int maxEvaluations);
+
+    /** Get the maximal number of functions evaluations.
+     * @return maximal number of functions evaluations
+     */
+    int getMaxEvaluations();
+
+    /** Get the number of evaluations of the objective function.
+     * <p>
+     * The number of evaluations corresponds to the last call to the
+     * {@link #optimize(UnivariateRealFunction, GoalType, double, double) optimize}
+     * method. It is 0 if the method has not been called yet.
+     * </p>
+     * @return number of evaluations of the objective function
+     */
+    int getEvaluations();
+
     /**
      * Find an optimum in the given interval.
      * <p>

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=786466&r1=786465&r2=786466&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
Fri Jun 19 12:03:14 2009
@@ -99,12 +99,12 @@
     /** Maximal number of iterations allowed. */
     private int maxIterations;
 
-    /** Maximal number of evaluations allowed. */
-    private int maxEvaluations;
-
     /** Number of iterations already performed. */
     private int iterations;
 
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
     /** Number of evaluations already performed. */
     private int evaluations;
 
@@ -336,14 +336,15 @@
      * function evaluations</p>
      * @param x point on which the objective function should be evaluated
      * @return objective function value at the given point
-     * @exception FunctionEvaluationException if no value can be computed for the parameters
+     * @exception FunctionEvaluationException if no value can be computed for the
+     * parameters or if the maximal number of evaluations is exceeded
      * @exception IllegalArgumentException if the start point dimension is wrong
-     * @exception OptimizationException if the maximal number of evaluations is exceeded
      */
     protected double evaluate(final double[] x)
-        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException
{
+        throws FunctionEvaluationException, IllegalArgumentException {
         if (++evaluations > maxEvaluations) {
-            throw new OptimizationException(new MaxEvaluationsExceededException(maxEvaluations));
+            throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
+                                                  x);
         }
         return f.value(x);
     }
@@ -360,7 +361,7 @@
         if (n != startConfiguration.length) {
             throw MathRuntimeException.createIllegalArgumentException(
                     "dimension mismatch {0} != {1}",
-                    n, simplex.length);
+                    n, startConfiguration.length);
         }
 
         // set first vertex

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -18,6 +18,7 @@
 package org.apache.commons.math.optimization.general;
 
 import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MaxEvaluationsExceededException;
 import org.apache.commons.math.MaxIterationsExceededException;
 import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
 import org.apache.commons.math.analysis.MultivariateMatrixFunction;
@@ -50,6 +51,9 @@
     /** Number of iterations already performed. */
     private int iterations;
 
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
     /** Number of evaluations already performed. */
     private int objectiveEvaluations;
 
@@ -105,6 +109,7 @@
     protected AbstractLeastSquaresOptimizer() {
         setConvergenceChecker(new SimpleVectorialValueChecker());
         setMaxIterations(DEFAULT_MAX_ITERATIONS);
+        setMaxEvaluations(Integer.MAX_VALUE);
     }
 
     /** {@inheritDoc} */
@@ -123,6 +128,16 @@
     }
 
     /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
     public int getEvaluations() {
         return objectiveEvaluations;
     }
@@ -149,9 +164,7 @@
     protected void incrementIterationsCounter()
         throws OptimizationException {
         if (++iterations > maxIterations) {
-            if (++iterations > maxIterations) {
-                throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
-            }
+            throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
         }
     }
 
@@ -179,12 +192,16 @@
     /** 
      * Update the residuals array and cost function value.
      * @exception FunctionEvaluationException if the function cannot be evaluated
-     * or its dimension doesn't match problem dimension
+     * or its dimension doesn't match problem dimension or maximal number of
+     * of evaluations is exceeded
      */
     protected void updateResidualsAndCost()
         throws FunctionEvaluationException {
 
-        ++objectiveEvaluations;
+        if (++objectiveEvaluations > maxEvaluations) {
+            throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
+                                                  point);
+        }
         objective = f.value(point);
         if (objective.length != rows) {
             throw new FunctionEvaluationException(point, "dimension mismatch {0} != {1}",

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -18,6 +18,7 @@
 package org.apache.commons.math.optimization.general;
 
 import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MaxEvaluationsExceededException;
 import org.apache.commons.math.MaxIterationsExceededException;
 import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
 import org.apache.commons.math.analysis.MultivariateVectorialFunction;
@@ -36,7 +37,7 @@
  * @since 2.0
  */
 public abstract class AbstractScalarDifferentiableOptimizer
-    implements DifferentiableMultivariateRealOptimizer{
+    implements DifferentiableMultivariateRealOptimizer {
 
     /** Default maximal number of iterations allowed. */
     public static final int DEFAULT_MAX_ITERATIONS = 100;
@@ -47,6 +48,9 @@
     /** Number of iterations already performed. */
     private int iterations;
 
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
     /** Number of evaluations already performed. */
     private int evaluations;
 
@@ -75,6 +79,7 @@
     protected AbstractScalarDifferentiableOptimizer() {
         setConvergenceChecker(new SimpleScalarValueChecker());
         setMaxIterations(DEFAULT_MAX_ITERATIONS);
+        setMaxEvaluations(Integer.MAX_VALUE);
     }
 
     /** {@inheritDoc} */
@@ -93,6 +98,16 @@
     }
 
     /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
     public int getEvaluations() {
         return evaluations;
     }
@@ -119,9 +134,7 @@
     protected void incrementIterationsCounter()
         throws OptimizationException {
         if (++iterations > maxIterations) {
-            if (++iterations > maxIterations) {
-                throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
-            }
+            throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
         }
     }
 
@@ -142,11 +155,15 @@
      * @param point point at which the objective function must be evaluated
      * @return objective function value at specified point
      * @exception FunctionEvaluationException if the function cannot be evaluated
-     * or its dimension doesn't match problem dimension
+     * or its dimension doesn't match problem dimension or the maximal number
+     * of iterations is exceeded
      */
     protected double computeObjectiveValue(final double[] point)
         throws FunctionEvaluationException {
-        ++evaluations;
+        if (++evaluations > maxEvaluations) {
+            throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
+                                                  point);
+        }
         return f.value(point);
     }
 

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/AbstractLinearOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/AbstractLinearOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/AbstractLinearOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/AbstractLinearOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -84,9 +84,7 @@
     protected void incrementIterationsCounter()
         throws OptimizationException {
         if (++iterations > maxIterations) {
-            if (++iterations > maxIterations) {
-                throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
-            }
+            throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
         }
     }
 
@@ -102,7 +100,7 @@
         this.goalType              = goalType;
         this.restrictToNonNegative = restrictToNonNegative;
 
-        iterations = 0;
+        iterations  = 0;
 
         // solve the problem
         return doOptimize();

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -18,7 +18,10 @@
 package org.apache.commons.math.optimization.univariate;
 
 import org.apache.commons.math.ConvergingAlgorithmImpl;
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.MathRuntimeException;
+import org.apache.commons.math.MaxEvaluationsExceededException;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
 import org.apache.commons.math.optimization.UnivariateRealOptimizer;
 
 /**
@@ -43,9 +46,15 @@
     /** Value of the function at the last computed result. */
     protected double functionValue;
 
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
+    /** Number of evaluations already performed. */
+    private int evaluations;
+
     /**
      * Construct a solver with given iteration count and accuracy.
-     * 
+     * FunctionEvaluationExceptionFunctionEvaluationException
      * @param defaultAbsoluteAccuracy maximum absolute error
      * @param defaultMaximalIterationCount maximum number of iterations
      * @throws IllegalArgumentException if f is null or the 
@@ -55,6 +64,7 @@
                                               final double defaultAbsoluteAccuracy) {
         super(defaultMaximalIterationCount, defaultAbsoluteAccuracy);
         resultComputed = false;
+        setMaxEvaluations(Integer.MAX_VALUE);
     }
 
     /** Check if a result has been computed.
@@ -112,4 +122,36 @@
         this.resultComputed = false;
     }
 
+    /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getEvaluations() {
+        return evaluations;
+    }
+
+    /** 
+     * Compute the objective function value.
+     * @param point point at which the objective function must be evaluated
+     * @return objective function value at specified point
+     * @exception FunctionEvaluationException if the function cannot be evaluated
+     * or the maximal number of iterations is exceeded
+     */
+    protected double computeObjectiveValue(final UnivariateRealFunction f,
+                                           final double point)
+        throws FunctionEvaluationException {
+        if (++evaluations > maxEvaluations) {
+            throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
+                                                  point);
+        }
+        return f.value(point);
+    }
+
 }

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java?rev=786466&r1=786465&r2=786466&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java
Fri Jun 19 12:03:14 2009
@@ -92,7 +92,7 @@
         double v = x;
         double w = x;
         double e = 0;
-        double fx = f.value(x);
+        double fx = computeObjectiveValue(f, x);
         if (goalType == GoalType.MAXIMIZE) {
             fx = -fx;
         }
@@ -145,7 +145,7 @@
 
                 // f must not be evaluated too close to a or b.
                 u = x + ((Math.abs(d) > tol) ? d : ((d > 0) ? tol : -tol));
-                double fu = f.value(u);
+                double fu = computeObjectiveValue(f, u);
                 if (goalType == GoalType.MAXIMIZE) {
                     fu = -fu;
                 }

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=786466&r1=786465&r2=786466&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
Fri Jun 19 12:03:14 2009
@@ -196,11 +196,11 @@
           optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
           optimizer.setMaxEvaluations(20);
           optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0
});
-      } catch (OptimizationException oe) {
-          if (oe.getCause() instanceof ConvergenceException) {
-              throw (ConvergenceException) oe.getCause();
+      } catch (FunctionEvaluationException fee) {
+          if (fee.getCause() instanceof ConvergenceException) {
+              throw (ConvergenceException) fee.getCause();
           }
-          throw oe;
+          throw fee;
       }
   }
 



Mime
View raw message