commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r967288 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/optimization/ main/java/org/apache/commons/math/optimization/general/ site/xdoc/
Date Fri, 23 Jul 2010 22:35:09 GMT
Author: erans
Date: Fri Jul 23 22:35:09 2010
New Revision: 967288

URL: http://svn.apache.org/viewvc?rev=967288&view=rev
Log:
MATH-389
Duplicate code in "MultivariateRealOptimizer" and
"DifferentiableMultivariateRealOptimizer" moved to new interface
"BaseMultivariateRealOptimizer".
New "AbstractScalarOptimizer" superclass allow implementations that do not
use derivatives.
Deprecated "protected" fields.
"NonLinearConjugateGradientOptimizer" changed to make use of the newly
added accessors instead of the "protected" fields.

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
  (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarOptimizer.java
  (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java
  (with props)
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java?rev=967288&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
Fri Jul 23 22:35:09 2010
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.optimization;
+
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.analysis.MultivariateRealFunction;
+
+/**
+ * Optimization algorithms find the input point set that either {@link GoalType
+ * maximize or minimize} an objective function.
+ * This interface is mainly intended to enforce the internal coherence of
+ * Commons-Math. Users of the API are advised to base their code on
+ * {@link MultivariateRealOptimizer} or on
+ * {@link DifferentiableMultivariateRealOptimizer}.
+ *
+ * @see MultivariateRealOptimizer
+ * @see DifferentiableMultivariateRealOptimizer
+ * @version $Revision$ $Date$
+ * @since 2.2
+ */
+public interface BaseMultivariateRealOptimizer<T extends MultivariateRealFunction>
{
+    /**
+     * 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 the 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 the maximal number of functions evaluations.
+     */
+    int getMaxEvaluations();
+
+    /**
+     * Get the number of iterations realized by the algorithm.
+     * 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.
+     *
+     * @return the number of iterations.
+     */
+    int getIterations();
+
+    /**
+     * Get the number of evaluations of the objective function.
+     *
+     * The number of evaluations corresponds to the last call to the
+     * {@link #optimize(T, GoalType, double[]) optimize}
+     * method. It is 0 if the method has not been called yet.
+     *
+     * @return the 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 the object used to check for convergence.
+     */
+    RealConvergenceChecker getConvergenceChecker();
+
+    /**
+     * Optimize an objective function.
+     *     
+     * @param f Objective function.
+     * @param goalType Type of optimization goal: either {@link GoalType#MAXIMIZE}
+     * or {@link GoalType#MINIMIZE}.
+     * @param startPoint Start point for optimization.
+     * @return the point/value pair giving the optimal value for objective function.
+     * @throws FunctionEvaluationException if the objective function throws one during
+     * the search.
+     * @throws OptimizationException if the algorithm failed to converge.
+     * @throws IllegalArgumentException if the start point dimension is wrong.
+     */
+    RealPointValuePair optimize(T f, GoalType goalType, double[] startPoint)
+        throws FunctionEvaluationException, OptimizationException;
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java?rev=967288&r1=967287&r2=967288&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.java
Fri Jul 23 22:35:09 2010
@@ -30,82 +30,15 @@ import org.apache.commons.math.analysis.
  * @version $Revision$ $Date$
  * @since 2.0
  */
-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
-     * {@link #optimize(DifferentiableMultivariateRealFunction, GoalType, double[]) optimize}
-     * method. It is 0 if the method has not been called yet.
-     * </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>
+public interface DifferentiableMultivariateRealOptimizer
+    extends BaseMultivariateRealOptimizer<DifferentiableMultivariateRealFunction> {
+    /**
+     * Get the number of evaluations of the objective function gradient.
      * 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
+     *
+     * @return the 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/trunk/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java?rev=967288&r1=967287&r2=967288&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultivariateRealOptimizer.java
Fri Jul 23 22:35:09 2010
@@ -30,72 +30,5 @@ import org.apache.commons.math.analysis.
  * @version $Revision$ $Date$
  * @since 2.0
  */
-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;
-
-}
+public interface MultivariateRealOptimizer
+    extends BaseMultivariateRealOptimizer<MultivariateRealFunction> {}

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java?rev=967288&r1=967287&r2=967288&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
Fri Jul 23 22:35:09 2010
@@ -30,89 +30,55 @@ import org.apache.commons.math.optimizat
 import org.apache.commons.math.optimization.SimpleScalarValueChecker;
 
 /**
- * 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>
+ * Base class for implementing optimizers for multivariate scalar
+ * differentiable functions.
+ * It contains boiler-plate code for dealing with gradient evaluation.
+ *
  * @version $Revision$ $Date$
  * @since 2.0
  */
 public abstract class AbstractScalarDifferentiableOptimizer
+    extends BaseAbstractScalarOptimizer<DifferentiableMultivariateRealFunction>
     implements DifferentiableMultivariateRealOptimizer {
+    /** Number of gradient evaluations. */
+    private int gradientEvaluations;
+    /** Objective function gradient. */
+    private MultivariateVectorialFunction gradient;
 
-    /** Default maximal number of iterations allowed. */
-    public static final int DEFAULT_MAX_ITERATIONS = 100;
-
-    /** Convergence checker. */
+    /** Convergence checker.
+     * @deprecated in 2.2 (to be removed in 3.0). Please use the accessor
+     * {@link BaseAbstractScalarOptimizer#getConvergenceChecker()} instead.
+     */
     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.
      */
     protected GoalType goal;
-
-    /** Current point set. */
+    /** Current point set.
+     * @deprecated in 2.2 (to be removed in 3.0).
+     */
     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.
-     * <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;
+    /**
+     * Simple constructor with default settings.
+     * The convergence check is set to a {@link SimpleScalarValueChecker},
+     * the allowed number of iterations and evaluations are set to their
+     * default values.
+     */
+    protected AbstractScalarDifferentiableOptimizer() {}
+    /**
+     * @param convergenceChecker 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).
     }
 
     /** {@inheritDoc} */
@@ -120,27 +86,6 @@ 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
@@ -153,52 +98,21 @@ public abstract class AbstractScalarDiff
         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} */
     public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
-                                         final GoalType goalType,
-                                         final double[] startPoint)
-        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException
{
-
+                                       final GoalType goalType,
+                                       final double[] startPoint)
+        throws FunctionEvaluationException,
+               OptimizationException {
         // reset counters
-        iterations          = 0;
-        evaluations         = 0;
         gradientEvaluations = 0;
 
         // store optimization problem characteristics
-        function = f;
         gradient = f.gradient();
-        goal     = goalType;
-        point    = startPoint.clone();
 
-        return doOptimize();
+        goal = goalType; // Do not use (deprecated).
+        point = startPoint.clone(); // Do not use (deprecated).
 
+        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;
-
 }

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarOptimizer.java?rev=967288&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarOptimizer.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarOptimizer.java
Fri Jul 23 22:35:09 2010
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.MultivariateRealFunction;
+import org.apache.commons.math.analysis.MultivariateVectorialFunction;
+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.BaseMultivariateRealOptimizer;
+import org.apache.commons.math.optimization.MultivariateRealOptimizer;
+import org.apache.commons.math.optimization.RealPointValuePair;
+import org.apache.commons.math.optimization.SimpleScalarValueChecker;
+
+/**
+ * Base class for implementing optimizers for multivariate (not necessarily
+ * differentiable) scalar functions.
+ *
+ * @version $Revision$ $Date$
+ * @since 2.2
+ */
+public abstract class AbstractScalarOptimizer
+    extends BaseAbstractScalarOptimizer<MultivariateRealFunction>
+    implements MultivariateRealOptimizer {
+    /**
+     * Simple constructor with default settings.
+     * The convergence check is set to a {@link SimpleScalarValueChecker},
+     * the allowed number of iterations and evaluations are set to their
+     * default values.
+     */
+    protected AbstractScalarOptimizer() {}
+    /**
+     * @param convergenceChecker Convergence checker.
+     * @param maxIterations Maximum number of iterations.
+     * @param maxEvaluations Maximum number of evaluations.
+     */
+    protected AbstractScalarOptimizer(RealConvergenceChecker checker,
+                                      int maxIterations,
+                                      int maxEvaluations) {
+        super(checker, maxIterations, maxEvaluations);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarOptimizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java?rev=967288&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java
Fri Jul 23 22:35:09 2010
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.MultivariateRealFunction;
+import org.apache.commons.math.analysis.MultivariateVectorialFunction;
+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.BaseMultivariateRealOptimizer;
+import org.apache.commons.math.optimization.RealPointValuePair;
+import org.apache.commons.math.optimization.SimpleScalarValueChecker;
+
+/**
+ * Base class for implementing optimizers for multivariate scalar functions.
+ * This base class handles the boiler-plate methods associated to thresholds
+ * settings, iterations and evaluations counting.
+ * This class is mainly intended to enforce the internal coherence of
+ * Commons-Math.
+ * A class that implements an optimization algorithm should inherit from
+ * {@link AbstractScalarOptimizer} or from
+ * {@link AbstractScalarDifferentiableOptimizer}.
+ *
+ * @version $Revision$ $Date$
+ * @since 2.2
+ */
+public abstract class BaseAbstractScalarOptimizer<T extends MultivariateRealFunction>
+    implements BaseMultivariateRealOptimizer<T> {
+    /** Default maximal number of iterations allowed ({@value}). */
+    public static final int DEFAULT_MAX_ITERATIONS = 1000;
+    /** Default maximal number of iterations allowed ({@value}). */
+    public static final int DEFAULT_MAX_EVALUATIONS = 10000;
+
+    /** Convergence checker. */
+    private RealConvergenceChecker checker;
+    /** Type of optimization. */
+    private GoalType goal;
+    /** Initial guess. */
+    private double[] start;
+    /** 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;
+    /** Objective function. */
+    private MultivariateRealFunction function;
+
+    /**
+     * Simple constructor with default settings.
+     * The convergence check is set to a {@link SimpleScalarValueChecker},
+     * the allowed number of iterations and evaluations are set to their
+     * default values.
+     */
+    protected BaseAbstractScalarOptimizer() {
+        this(new SimpleScalarValueChecker(),
+             DEFAULT_MAX_ITERATIONS,
+             DEFAULT_MAX_EVALUATIONS);
+    }
+    /**
+     * @param convergenceChecker Convergence checker.
+     * @param maxIterations Maximum number of iterations.
+     * @param maxEvaluations Maximum number of evaluations.
+     */
+    protected BaseAbstractScalarOptimizer(RealConvergenceChecker checker,
+                                          int maxIterations,
+                                          int maxEvaluations) {
+        this.checker = checker;
+        this.maxIterations = maxIterations;
+        this.maxEvaluations = maxEvaluations;
+    }
+
+    /** {@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} */
+    public void setConvergenceChecker(RealConvergenceChecker checker) {
+        this.checker = checker;
+    }
+
+    /** {@inheritDoc} */
+    public RealConvergenceChecker getConvergenceChecker() {
+        return checker;
+    }
+
+    /**
+     * Increment the iterations counter by 1.
+     * @throws 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 objective function value.
+     * @param evaluationPoint point at which the objective function must be evaluated
+     * @return objective function value at specified point
+     * @throws 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(double[] evaluationPoint)
+        throws FunctionEvaluationException {
+        if (++evaluations > maxEvaluations) {
+            throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
+                                                  evaluationPoint);
+        }
+        return function.value(evaluationPoint);
+    }
+
+    /** {@inheritDoc} */
+    public RealPointValuePair optimize(T f,
+                                       GoalType goalType,
+                                       double[] startPoint)
+        throws FunctionEvaluationException, OptimizationException {
+
+        // reset counters
+        iterations = 0;
+        evaluations = 0;
+
+        // store optimization problem characteristics
+        function = f;
+        goal = goalType;
+        start = startPoint.clone();
+
+        return doOptimize();
+    }
+
+    /**
+     * @return the optimization type.
+     */
+    public GoalType getGoalType() {
+        return goal;
+    }
+
+    /**
+     * @return the initial guess.
+     */
+    public double[] getStartPoint() {
+        return start.clone();
+    }
+
+    /**
+     * Perform the bulk of optimization algorithm.
+     * @return the point/value pair giving the optimal value for objective function
+     * @throws FunctionEvaluationException if the objective function throws one during
+     * the search
+     * @throws OptimizationException if the algorithm failed to converge
+     * @throws IllegalArgumentException if the start point dimension is wrong
+     */
+    protected abstract RealPointValuePair doOptimize()
+        throws FunctionEvaluationException, OptimizationException;
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java?rev=967288&r1=967287&r2=967288&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
Fri Jul 23 22:35:09 2010
@@ -42,20 +42,17 @@ import org.apache.commons.math.util.Loca
  */
 
 public class NonLinearConjugateGradientOptimizer
-    extends AbstractScalarDifferentiableOptimizer
-    implements DifferentiableMultivariateRealOptimizer {
-
+    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
@@ -115,7 +112,6 @@ public class NonLinearConjugateGradientO
     protected RealPointValuePair doOptimize()
         throws FunctionEvaluationException, OptimizationException, IllegalArgumentException
{
         try {
-
             // initialization
             if (preconditioner == null) {
                 preconditioner = new IdentityPreconditioner();
@@ -123,6 +119,8 @@ 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) {
@@ -147,7 +145,7 @@ public class NonLinearConjugateGradientO
                 RealPointValuePair previous = current;
                 current = new RealPointValuePair(point, objective);
                 if (previous != null) {
-                    if (checker.converged(getIterations(), previous, current)) {
+                    if (getConvergenceChecker().converged(getIterations(), previous, current))
{
                         // we have found an optimum
                         return current;
                     }

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=967288&r1=967287&r2=967288&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Jul 23 22:35:09 2010
@@ -52,6 +52,17 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="2.2" date="TBD" description="TBD">
+      <action dev="erans" type="update" issue="MATH-389">
+        Refactoring of some classes in package "optimization.general" to remove
+        code duplication and to allow the implementation of optimization algorithms
+        that do not use derivatives.
+        Created interface "BaseMultivariateRealOptimizer".
+        Created classes "BaseAbstractScalarOptimizer" and "AbstractScalarOptimizer".
+        "MultivariateRealOptimizer" and "DifferentiableMultivariateRealOptimizer"
+        both extend "BaseMultivariateRealOptimizer".
+        "AbstractScalarOptimizer" extends "BaseAbstractScalarOptimizer" and
+        "AbstractScalarDifferentiableOptimizer" inherits from "AbstractScalarOptimizer".
+      </action>
       <action dev="luc" type="add" issue="MATH-388">
         Added a feature allowing error estimation to be computed only on a subset of
         Ordinary Differential Equations, considered as the main set, the remaining equations



Mime
View raw message