commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1065481 - in /commons/proper/math/branches/MATH_2_X/src: main/java/org/apache/commons/math/optimization/ main/java/org/apache/commons/math/optimization/direct/ main/java/org/apache/commons/math/optimization/general/ test/java/org/apache/co...
Date Mon, 31 Jan 2011 05:31:41 GMT
Author: psteitz
Date: Mon Jan 31 05:31:41 2011
New Revision: 1065481

URL: http://svn.apache.org/viewvc?rev=1065481&view=rev
Log:
Reverted incompatible changes made in r967288, JIRA: MATH-389.

Removed:
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java
Modified:
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
    commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java
    commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java
    commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/direct/PowellOptimizerTest.java
    commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
Mon Jan 31 05:31:41 2011
@@ -17,6 +17,7 @@
 
 package org.apache.commons.math.optimization;
 
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
 
 /**
@@ -31,15 +32,82 @@ import org.apache.commons.math.analysis.
  * @version $Revision$ $Date$
  * @since 2.0
  */
-public interface DifferentiableMultivariateRealOptimizer
-    extends BaseMultivariateRealOptimizer<DifferentiableMultivariateRealFunction> {
-    /**
-     * Get the number of evaluations of the objective function gradient.
+public interface DifferentiableMultivariateRealOptimizer {
+
+    /** Set the maximal number of iterations of the algorithm.
+     * @param maxIterations maximal number of function calls
+     */
+    void setMaxIterations(int maxIterations);
+
+    /** Get the maximal number of iterations of the algorithm.
+     * @return maximal number of iterations
+     */
+    int getMaxIterations();
+
+    /** Get the number of iterations realized by the algorithm.
+     * <p>
      * The number of evaluations corresponds to the last call to the
      * {@code optimize} method (see {@link BaseMultivariateRealOptimizer}).
      * It is 0 if the method has not been called yet.
-     *
-     * @return the number of evaluations of the objective function gradient.
+     * </p>
+     * @return number of iterations
+     */
+    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
+     * {@link #optimize(DifferentiableMultivariateRealFunction, GoalType, 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();
+
+    /** Get the number of evaluations of the objective function gradient.
+     * <p>
+     * The number of evaluations corresponds to the last call to the
+     * {@link #optimize(DifferentiableMultivariateRealFunction, GoalType, double[]) optimize}
+     * method. It is 0 if the method has not been called yet.
+     * </p>
+     * @return number of evaluations of the objective function gradient
      */
     int getGradientEvaluations();
+
+    /** Set the convergence checker.
+     * @param checker object to use to check for convergence
+     */
+    void setConvergenceChecker(RealConvergenceChecker checker);
+
+    /** Get the convergence checker.
+     * @return object used to check for convergence
+     */
+    RealConvergenceChecker getConvergenceChecker();
+
+    /** Optimizes an objective function.
+     * @param f objective function
+     * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
+     * or {@link GoalType#MINIMIZE}
+     * @param startPoint the start point for optimization
+     * @return the point/value pair giving the optimal value for objective function
+     * @exception FunctionEvaluationException if the objective function throws one during
+     * the search
+     * @exception OptimizationException if the algorithm failed to converge
+     * @exception IllegalArgumentException if the start point dimension is wrong
+     */
+    RealPointValuePair optimize(DifferentiableMultivariateRealFunction f,
+                                  GoalType goalType,
+                                  double[] startPoint)
+        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
+
 }

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
Mon Jan 31 05:31:41 2011
@@ -20,6 +20,7 @@ package org.apache.commons.math.optimiza
 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.analysis.DifferentiableMultivariateRealFunction;
 import org.apache.commons.math.exception.MathUserException;
@@ -172,7 +173,7 @@ public class MultiStartDifferentiableMul
     public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
                                          final GoalType goalType,
                                          double[] startPoint)
-        throws MathUserException, OptimizationException {
+        throws MathUserException, OptimizationException, FunctionEvaluationException {
 
         optima                   = new RealPointValuePair[starts];
         totalIterations          = 0;

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
Mon Jan 31 05:31:41 2011
@@ -20,6 +20,7 @@ package org.apache.commons.math.optimiza
 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.analysis.MultivariateRealFunction;
 import org.apache.commons.math.exception.MathUserException;
@@ -162,7 +163,7 @@ public class MultiStartMultivariateRealO
     public RealPointValuePair optimize(final MultivariateRealFunction f,
                                          final GoalType goalType,
                                          double[] startPoint)
-        throws MathUserException, OptimizationException {
+        throws MathUserException, OptimizationException, FunctionEvaluationException {
 
         optima           = new RealPointValuePair[starts];
         totalIterations  = 0;

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
Mon Jan 31 05:31:41 2011
@@ -17,6 +17,7 @@
 
 package org.apache.commons.math.optimization;
 
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.analysis.MultivariateRealFunction;
 
 /**
@@ -29,5 +30,72 @@ import org.apache.commons.math.analysis.
  * @version $Revision$ $Date$
  * @since 2.0
  */
-public interface MultivariateRealOptimizer
-    extends BaseMultivariateRealOptimizer<MultivariateRealFunction> {}
+public interface MultivariateRealOptimizer {
+
+    /** Set the maximal number of iterations of the algorithm.
+     * @param maxIterations maximal number of algorithm iterations
+     */
+    void setMaxIterations(int maxIterations);
+
+    /** Get the maximal number of iterations of the algorithm.
+     * @return maximal number of iterations
+     */
+    int getMaxIterations();
+
+    /** 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 iterations realized by the algorithm.
+     * <p>
+     * The number of evaluations corresponds to the last call to the
+     * {@link #optimize(MultivariateRealFunction, GoalType, double[]) optimize}
+     * method. It is 0 if the method has not been called yet.
+     * </p>
+     * @return number of iterations
+     */
+    int getIterations();
+
+    /** Get the number of evaluations of the objective function.
+     * <p>
+     * The number of evaluations corresponds to the last call to the
+     * {@link #optimize(MultivariateRealFunction, GoalType, 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();
+
+    /** Set the convergence checker.
+     * @param checker object to use to check for convergence
+     */
+    void setConvergenceChecker(RealConvergenceChecker checker);
+
+    /** Get the convergence checker.
+     * @return object used to check for convergence
+     */
+    RealConvergenceChecker getConvergenceChecker();
+
+    /** Optimizes an objective function.
+     * @param f objective function
+     * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
+     * or {@link GoalType#MINIMIZE}
+     * @param startPoint the start point for optimization
+     * @return the point/value pair giving the optimal value for objective function
+     * @exception FunctionEvaluationException if the objective function throws one during
+     * the search
+     * @exception OptimizationException if the algorithm failed to converge
+     * @exception IllegalArgumentException if the start point dimension is wrong
+     */
+    RealPointValuePair optimize(MultivariateRealFunction f,
+                                  GoalType goalType,
+                                  double[] startPoint)
+        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
+
+}

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java
Mon Jan 31 05:31:41 2011
@@ -19,11 +19,12 @@ package org.apache.commons.math.optimiza
 
 import org.apache.commons.math.MaxIterationsExceededException;
 import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.exception.MathUserException;
 import org.apache.commons.math.optimization.GoalType;
 import org.apache.commons.math.optimization.OptimizationException;
 import org.apache.commons.math.optimization.RealPointValuePair;
-import org.apache.commons.math.optimization.general.AbstractScalarOptimizer;
+import org.apache.commons.math.optimization.general.AbstractScalarDifferentiableOptimizer;
 import org.apache.commons.math.optimization.univariate.AbstractUnivariateRealOptimizer;
 import org.apache.commons.math.optimization.univariate.BracketFinder;
 import org.apache.commons.math.optimization.univariate.BrentOptimizer;
@@ -38,7 +39,7 @@ import org.apache.commons.math.optimizat
  * @since 2.2
  */
 public class PowellOptimizer
-    extends AbstractScalarOptimizer {
+    extends AbstractScalarDifferentiableOptimizer {
     /**
      * Default relative tolerance for line search ({@value}).
      */
@@ -88,10 +89,9 @@ public class PowellOptimizer
     /** {@inheritDoc} */
     @Override
     protected RealPointValuePair doOptimize()
-        throws MathUserException,
+        throws FunctionEvaluationException,
                OptimizationException {
-        final GoalType goal = getGoalType();
-        final double[] guess = getStartPoint();
+        final double[] guess = point;
         final int n = guess.length;
 
         final double[][] direc = new double[n][n];
@@ -236,7 +236,7 @@ public class PowellOptimizer
          * @throws OptimizationException if algorithm fails to converge
          */
         public void search(final double[] p, final double[] d)
-            throws MathUserException, OptimizationException {
+            throws FunctionEvaluationException, OptimizationException {
 
             // Reset.
             optimum = Double.NaN;
@@ -252,12 +252,16 @@ public class PowellOptimizer
                             for (int i = 0; i < n; i++) {
                                 x[i] = p[i] + alpha * d[i];
                             }
-                            final double obj = computeObjectiveValue(x);
+                            final double obj;
+                            try {
+                                obj = computeObjectiveValue(x);
+                            } catch (FunctionEvaluationException ex) {
+                                throw new MathUserException(ex);
+                            }
                             return obj;
                         }
                     };
 
-                final GoalType goal = getGoalType();
                 bracket.search(f, goal, 0, 1);
                 optimum = optim.optimize(f, goal,
                                          bracket.getLo(),

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
Mon Jan 31 05:31:41 2011
@@ -19,68 +19,103 @@ package org.apache.commons.math.optimiza
 
 import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
 import org.apache.commons.math.analysis.MultivariateVectorialFunction;
-import org.apache.commons.math.exception.MathUserException;
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.MaxEvaluationsExceededException;
+import org.apache.commons.math.MaxIterationsExceededException;
 import org.apache.commons.math.optimization.DifferentiableMultivariateRealOptimizer;
 import org.apache.commons.math.optimization.GoalType;
 import org.apache.commons.math.optimization.OptimizationException;
 import org.apache.commons.math.optimization.RealConvergenceChecker;
 import org.apache.commons.math.optimization.RealPointValuePair;
+import org.apache.commons.math.optimization.SimpleScalarValueChecker;
 
 /**
- * Base class for implementing optimizers for multivariate scalar
- * differentiable functions.
- * It contains boiler-plate code for dealing with gradient evaluation.
- *
+ * Base class for implementing optimizers for multivariate scalar functions.
+ * <p>This base class handles the boilerplate methods associated to thresholds
+ * settings, iterations and evaluations counting.</p>
  * @version $Revision$ $Date$
  * @since 2.0
  */
 public abstract class AbstractScalarDifferentiableOptimizer
-    extends BaseAbstractScalarOptimizer<DifferentiableMultivariateRealFunction>
     implements DifferentiableMultivariateRealOptimizer {
 
-    /** Convergence checker.
-     * @deprecated in 2.2 (to be removed in 3.0). Please use the accessor
-     * {@link BaseAbstractScalarOptimizer#getConvergenceChecker()} instead.
-     */
+    /** Default maximal number of iterations allowed. */
+    public static final int DEFAULT_MAX_ITERATIONS = 100;
+
+    /** Convergence checker. */
     @Deprecated
     protected RealConvergenceChecker checker;
+
     /**
      * Type of optimization.
      * @since 2.1
-     * @deprecated in 2.2 (to be removed in 3.0). Please use the accessor
-     * {@link BaseAbstractScalarOptimizer#getGoalType()} instead.
      */
     @Deprecated
     protected GoalType goal;
-    /** Current point set.
-     * @deprecated in 2.2 (to be removed in 3.0).
-     */
+
+    /** Current point set. */
     @Deprecated
     protected double[] point;
 
+    /** Maximal number of iterations allowed. */
+    private int maxIterations;
+
+    /** Number of iterations already performed. */
+    private int iterations;
+
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+
+    /** Number of evaluations already performed. */
+    private int evaluations;
+
     /** Number of gradient evaluations. */
     private int gradientEvaluations;
 
+    /** Objective function. */
+    private DifferentiableMultivariateRealFunction function;
+
     /** Objective function gradient. */
     private MultivariateVectorialFunction gradient;
 
-    /**
-     * Simple constructor with default settings.
-     * The convergence check is set to a {@link org.apache.commons.math.optimization.SimpleScalarValueChecker},
-     * the allowed number of iterations and evaluations are set to their
-     * default values.
-     */
-    protected AbstractScalarDifferentiableOptimizer() {}
-    /**
-     * @param checker Convergence checker.
-     * @param maxIterations Maximum number of iterations.
-     * @param maxEvaluations Maximum number of evaluations.
-     */
-    protected AbstractScalarDifferentiableOptimizer(RealConvergenceChecker checker,
-                                                    int maxIterations,
-                                                    int maxEvaluations) {
-        super(checker, maxIterations, maxEvaluations);
-        this.checker = checker; // Do not use (deprecated).
+    /** Simple constructor with default settings.
+     * <p>The convergence check is set to a {@link SimpleScalarValueChecker}
+     * and the maximal number of evaluation is set to its default value.</p>
+     */
+    protected AbstractScalarDifferentiableOptimizer() {
+        setConvergenceChecker(new SimpleScalarValueChecker());
+        setMaxIterations(DEFAULT_MAX_ITERATIONS);
+        setMaxEvaluations(Integer.MAX_VALUE);
+    }
+
+    /** {@inheritDoc} */
+    public void setMaxIterations(int maxIterations) {
+        this.maxIterations = maxIterations;
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxIterations() {
+        return maxIterations;
+    }
+
+    /** {@inheritDoc} */
+    public int getIterations() {
+        return iterations;
+    }
+
+    /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getEvaluations() {
+        return evaluations;
     }
 
     /** {@inheritDoc} */
@@ -88,6 +123,27 @@ public abstract class AbstractScalarDiff
         return gradientEvaluations;
     }
 
+    /** {@inheritDoc} */
+    public void setConvergenceChecker(RealConvergenceChecker convergenceChecker) {
+        this.checker = convergenceChecker;
+    }
+
+    /** {@inheritDoc} */
+    public RealConvergenceChecker getConvergenceChecker() {
+        return checker;
+    }
+
+    /** Increment the iterations counter by 1.
+     * @exception OptimizationException if the maximal number
+     * of iterations is exceeded
+     */
+    protected void incrementIterationsCounter()
+        throws OptimizationException {
+        if (++iterations > maxIterations) {
+            throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
+        }
+    }
+
     /**
      * Compute the gradient vector.
      * @param evaluationPoint point at which the gradient must be evaluated
@@ -95,26 +151,57 @@ public abstract class AbstractScalarDiff
      * @exception MathUserException if the function gradient
      */
     protected double[] computeObjectiveGradient(final double[] evaluationPoint)
-        throws MathUserException {
+        throws FunctionEvaluationException {
         ++gradientEvaluations;
         return gradient.value(evaluationPoint);
     }
 
+    /**
+     * Compute the objective function value.
+     * @param evaluationPoint 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 the maximal number
+     * of iterations is exceeded
+     */
+    protected double computeObjectiveValue(final double[] evaluationPoint)
+        throws FunctionEvaluationException {
+        if (++evaluations > maxEvaluations) {
+            throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
+                                                  evaluationPoint);
+        }
+        return function.value(evaluationPoint);
+    }
+
     /** {@inheritDoc} */
-    @Override
     public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
-                                       final GoalType goalType,
-                                       final double[] startPoint)
-        throws MathUserException, OptimizationException {
+                                         final GoalType goalType,
+                                         final double[] startPoint)
+        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException
{
+
         // reset counters
+        iterations          = 0;
+        evaluations         = 0;
         gradientEvaluations = 0;
 
         // store optimization problem characteristics
+        function = f;
         gradient = f.gradient();
+        goal     = goalType;
+        point    = startPoint.clone();
 
-        goal = goalType; // Do not use (deprecated).
-        point = startPoint.clone(); // Do not use (deprecated).
+        return doOptimize();
 
-        return super.optimize(f, goalType, startPoint);
     }
+
+    /** Perform the bulk of optimization algorithm.
+     * @return the point/value pair giving the optimal value for objective function
+     * @exception FunctionEvaluationException if the objective function throws one during
+     * the search
+     * @exception OptimizationException if the algorithm failed to converge
+     * @exception IllegalArgumentException if the start point dimension is wrong
+     */
+    protected abstract RealPointValuePair doOptimize()
+        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
+
 }

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
Mon Jan 31 05:31:41 2011
@@ -18,6 +18,7 @@
 package org.apache.commons.math.optimization.general;
 
 import org.apache.commons.math.ConvergenceException;
+import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.analysis.UnivariateRealFunction;
 import org.apache.commons.math.analysis.solvers.BrentSolver;
 import org.apache.commons.math.analysis.solvers.UnivariateRealSolver;
@@ -43,16 +44,18 @@ import org.apache.commons.math.util.Fast
 
 public class NonLinearConjugateGradientOptimizer
     extends AbstractScalarDifferentiableOptimizer {
+
     /** Update formula for the beta parameter. */
     private final ConjugateGradientFormula updateFormula;
+
     /** Preconditioner (may be null). */
     private Preconditioner preconditioner;
+
     /** solver to use in the line search (may be null). */
     private UnivariateRealSolver solver;
+
     /** Initial step used to bracket the optimum in line search. */
     private double initialStep;
-    /** Current point. */
-    private double[] point;
 
     /** Simple constructor with default settings.
      * <p>The convergence check is set to a {@link
@@ -110,8 +113,9 @@ public class NonLinearConjugateGradientO
     /** {@inheritDoc} */
     @Override
     protected RealPointValuePair doOptimize()
-        throws MathUserException, OptimizationException, IllegalArgumentException {
+        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException
{
         try {
+
             // initialization
             if (preconditioner == null) {
                 preconditioner = new IdentityPreconditioner();
@@ -119,8 +123,6 @@ public class NonLinearConjugateGradientO
             if (solver == null) {
                 solver = new BrentSolver();
             }
-            point = getStartPoint();
-            final GoalType goal = getGoalType();
             final int n = point.length;
             double[] r = computeObjectiveGradient(point);
             if (goal == GoalType.MINIMIZE) {
@@ -145,7 +147,7 @@ public class NonLinearConjugateGradientO
                 RealPointValuePair previous = current;
                 current = new RealPointValuePair(point, objective);
                 if (previous != null) {
-                    if (getConvergenceChecker().converged(getIterations(), previous, current))
{
+                    if (checker.converged(getIterations(), previous, current)) {
                         // we have found an optimum
                         return current;
                     }
@@ -275,7 +277,12 @@ public class NonLinearConjugateGradientO
             }
 
             // gradient of the objective function
-            final double[] gradient = computeObjectiveGradient(shiftedPoint);
+            final double[] gradient;
+            try {
+                gradient = computeObjectiveGradient(shiftedPoint);
+            } catch (FunctionEvaluationException ex) {
+                throw new MathUserException(ex);
+            }
 
             // dot product with the search direction
             double dotProduct = 0;

Modified: commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java
Mon Jan 31 05:31:41 2011
@@ -39,7 +39,7 @@ import org.junit.Test;
 public class MultiStartDifferentiableMultivariateRealOptimizerTest {
 
     @Test
-    public void testCircleFitting() throws MathUserException, OptimizationException {
+    public void testCircleFitting() throws Exception {
         Circle circle = new Circle();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);

Modified: commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java
Mon Jan 31 05:31:41 2011
@@ -33,8 +33,7 @@ import org.junit.Test;
 public class MultiStartMultivariateRealOptimizerTest {
 
   @Test
-  public void testRosenbrock()
-    throws MathUserException, ConvergenceException {
+  public void testRosenbrock() throws Exception {
 
     Rosenbrock rosenbrock = new Rosenbrock();
     NelderMead underlying = new NelderMead();

Modified: commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/direct/PowellOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/direct/PowellOptimizerTest.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/direct/PowellOptimizerTest.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/direct/PowellOptimizerTest.java
Mon Jan 31 05:31:41 2011
@@ -17,7 +17,9 @@
 package org.apache.commons.math.optimization.direct;
 
 import org.apache.commons.math.MathException;
+import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
 import org.apache.commons.math.analysis.MultivariateRealFunction;
+import org.apache.commons.math.analysis.MultivariateVectorialFunction;
 import org.apache.commons.math.analysis.SumSincFunction;
 import org.apache.commons.math.optimization.GoalType;
 import org.apache.commons.math.optimization.MultivariateRealOptimizer;
@@ -33,7 +35,7 @@ public class PowellOptimizerTest {
 
     @Test
     public void testSumSinc() throws MathException {
-        final MultivariateRealFunction func = new SumSincFunction(-1);
+        final DifferentiableMultivariateRealFunction func = new SumSincFunction(-1);
 
         int dim = 2;
         final double[] minPoint = new double[dim];
@@ -58,12 +60,18 @@ public class PowellOptimizerTest {
 
     @Test
     public void testQuadratic() throws MathException {
-        final MultivariateRealFunction func = new MultivariateRealFunction() {
+        final DifferentiableMultivariateRealFunction func = new DifferentiableMultivariateRealFunction()
{
                 public double value(double[] x) {
                     final double a = x[0] - 1;
                     final double b = x[1] - 1;
                     return a * a + b * b + 1;
                 }
+                public MultivariateRealFunction partialDerivative(int k) {
+                    return null; // Not used
+                }
+                public MultivariateVectorialFunction gradient() {
+                    return null; // Not used
+                }
             };
 
         int dim = 2;
@@ -89,12 +97,18 @@ public class PowellOptimizerTest {
 
     @Test
     public void testMaximizeQuadratic() throws MathException {
-        final MultivariateRealFunction func = new MultivariateRealFunction() {
+        final DifferentiableMultivariateRealFunction func = new DifferentiableMultivariateRealFunction()
{
                 public double value(double[] x) {
                     final double a = x[0] - 1;
                     final double b = x[1] - 1;
                     return -a * a - b * b + 1;
                 }
+                public MultivariateRealFunction partialDerivative(int k) {
+                    return null;  // Not used
+                }
+                public MultivariateVectorialFunction gradient() {
+                    return null;  // Not used
+                }
             };
 
         int dim = 2;
@@ -129,7 +143,7 @@ public class PowellOptimizerTest {
      * "Powell" algorithm.
      * @param pointTol Tolerance for checking that the optimum is correct.
      */
-    private void doTest(MultivariateRealFunction func,
+    private void doTest(DifferentiableMultivariateRealFunction func,
                         double[] optimum,
                         double[] init,
                         GoalType goal,
@@ -137,7 +151,7 @@ public class PowellOptimizerTest {
                         double fTol,
                         double pointTol)
         throws MathException {
-        final MultivariateRealOptimizer optim = new PowellOptimizer(xTol);
+        final PowellOptimizer optim = new PowellOptimizer(xTol);
         optim.setConvergenceChecker(new SimpleScalarValueChecker(fTol, -1));
 
         final RealPointValuePair result = optim.optimize(func, goal, init);

Modified: commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java?rev=1065481&r1=1065480&r2=1065481&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
Mon Jan 31 05:31:41 2011
@@ -104,7 +104,7 @@ extends TestCase {
         super(name);
     }
 
-    public void testTrivial() throws MathUserException, OptimizationException {
+    public void testTrivial() throws Exception {
         LinearProblem problem =
             new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
         NonLinearConjugateGradientOptimizer optimizer =
@@ -117,7 +117,7 @@ extends TestCase {
         assertEquals(0.0, optimum.getValue(), 1.0e-10);
     }
 
-    public void testColumnsPermutation() throws MathUserException, OptimizationException
{
+    public void testColumnsPermutation() throws Exception {
 
         LinearProblem problem =
             new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 }
},
@@ -135,7 +135,7 @@ extends TestCase {
 
     }
 
-    public void testNoDependency() throws MathUserException, OptimizationException {
+    public void testNoDependency() throws Exception {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 2, 0, 0, 0, 0, 0 },
                 { 0, 2, 0, 0, 0, 0 },
@@ -155,7 +155,7 @@ extends TestCase {
         }
     }
 
-    public void testOneSet() throws MathUserException, OptimizationException {
+    public void testOneSet() throws Exception {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1,  0, 0 },
@@ -174,7 +174,7 @@ extends TestCase {
 
     }
 
-    public void testTwoSets() throws MathUserException, OptimizationException {
+    public void testTwoSets() throws Exception {
         final double epsilon = 1.0e-7;
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  2,  1,   0,  4,       0, 0 },
@@ -213,7 +213,7 @@ extends TestCase {
 
     }
 
-    public void testNonInversible() throws MathUserException, OptimizationException {
+    public void testNonInversible() throws Exception {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1, 2, -3 },
@@ -229,7 +229,7 @@ extends TestCase {
         assertTrue(optimum.getValue() > 0.5);
     }
 
-    public void testIllConditioned() throws MathUserException, OptimizationException {
+    public void testIllConditioned() throws Exception {
         LinearProblem problem1 = new LinearProblem(new double[][] {
                 { 10.0, 7.0,  8.0,  7.0 },
                 {  7.0, 5.0,  6.0,  5.0 },
@@ -266,8 +266,7 @@ extends TestCase {
 
     }
 
-    public void testMoreEstimatedParametersSimple()
-        throws MathUserException, OptimizationException {
+    public void testMoreEstimatedParametersSimple() throws Exception {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 3.0, 2.0,  0.0, 0.0 },
@@ -285,8 +284,7 @@ extends TestCase {
 
     }
 
-    public void testMoreEstimatedParametersUnsorted()
-        throws MathUserException, OptimizationException {
+    public void testMoreEstimatedParametersUnsorted() throws Exception {
         LinearProblem problem = new LinearProblem(new double[][] {
                  { 1.0, 1.0,  0.0,  0.0, 0.0,  0.0 },
                  { 0.0, 0.0,  1.0,  1.0, 1.0,  0.0 },
@@ -303,7 +301,7 @@ extends TestCase {
         assertEquals(0, optimum.getValue(), 1.0e-10);
     }
 
-    public void testRedundantEquations() throws MathUserException, OptimizationException
{
+    public void testRedundantEquations() throws Exception {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -321,7 +319,7 @@ extends TestCase {
 
     }
 
-    public void testInconsistentEquations() throws MathUserException, OptimizationException
{
+    public void testInconsistentEquations() throws Exception {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -338,7 +336,7 @@ extends TestCase {
 
     }
 
-    public void testCircleFitting() throws MathUserException, OptimizationException {
+    public void testCircleFitting() throws Exception {
         Circle circle = new Circle();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);



Mime
View raw message