commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r990792 [1/5] - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/ main/java/org/apache/commons/math/optimization/ main/java/org/apache/commons/math/optimization/direct/ main/java/org/apache/commons/math/optimization/fitt...
Date Mon, 30 Aug 2010 13:06:24 GMT
Author: erans
Date: Mon Aug 30 13:06:22 2010
New Revision: 990792

URL: http://svn.apache.org/viewvc?rev=990792&view=rev
Log:
MATH-394, MATH-397, MATH-404
Overhaul of the "optimization" package.
Removed lots of duplicate code.
Removed methods referring to the concept of "iteration".
Removed interface methods to access the number of evaluations of the
gradient and Jacobian.
Removed all references to "OptimizationException" (replaced by
"ConvergenceException").
Javadoc comments updated.


Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/AbstractConvergenceChecker.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateVectorialOptimizer.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateVectorialOptimizer.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseOptimizer.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/ConvergenceChecker.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractVectorialOptimizer.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BaseUnivariateRealOptimizer.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizer.java
      - copied, changed from r984406, commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealOptimizer.java
      - copied, changed from r984406, commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealPointValuePair.java   (with props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizerTest.java
      - copied, changed from r985626, commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizerTest.java
Removed:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizerTest.java
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithm.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithmImpl.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
    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/DifferentiableMultivariateVectorialOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.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/RealConvergenceChecker.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleScalarValueChecker.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleVectorialValueChecker.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/DirectSearchOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/MultiDirectional.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/NelderMead.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/fitting/CurveFitter.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/fitting/PolynomialFitter.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.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/AbstractScalarOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/BaseAbstractScalarOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/PowellOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/package.html
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BracketFinder.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/NelderMeadTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/MinpackTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/PowellOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BracketFinderTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/univariate/BrentOptimizerTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithm.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithm.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithm.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithm.java Mon Aug 30 13:06:22 2010
@@ -26,6 +26,8 @@ package org.apache.commons.math;
  * @see ConvergenceException
  * @version $Revision$ $Date$
  * @since 2.0
+ * @deprecated in 2.2 (to be removed in 3.0). Please use
+ * {@link IterativeAlgorithm} instead.
  */
 public interface ConvergingAlgorithm {
 

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithmImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithmImpl.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithmImpl.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/ConvergingAlgorithmImpl.java Mon Aug 30 13:06:22 2010
@@ -23,6 +23,7 @@ package org.apache.commons.math;
  *
  * @version $Revision$ $Date$
  * @since 2.0
+ * @deprecated in 2.2 (to be removed in 3.0).
  */
 public abstract class ConvergingAlgorithmImpl implements ConvergingAlgorithm {
 

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/AbstractConvergenceChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/AbstractConvergenceChecker.java?rev=990792&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/AbstractConvergenceChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/AbstractConvergenceChecker.java Mon Aug 30 13:06:22 2010
@@ -0,0 +1,85 @@
+/*
+ * 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.util.MathUtils;
+
+/**
+ * Base class for all convergence checker implementations.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public abstract class AbstractConvergenceChecker<T>
+    implements ConvergenceChecker<T> {
+    /**
+     * Default relative threshold.
+     */
+    private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
+    /**
+     * Default absolute threshold.
+     */
+    private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
+    /**
+     * Relative tolerance threshold.
+     */
+    private final double relativeThreshold;
+    /**
+     * Absolute tolerance threshold.
+     */
+    private final double absoluteThreshold;
+
+    /**
+     * Build an instance with default thresholds.
+     */
+    public AbstractConvergenceChecker() {
+        this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD;
+        this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD;
+    }
+
+    /**
+     * Build an instance with a specified thresholds.
+     *
+     * @param relativeThreshold relative tolerance threshold
+     * @param absoluteThreshold absolute tolerance threshold
+     */
+    public AbstractConvergenceChecker(final double relativeThreshold,
+                                      final double absoluteThreshold) {
+        this.relativeThreshold = relativeThreshold;
+        this.absoluteThreshold = absoluteThreshold;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getRelativeThreshold() {
+        return relativeThreshold;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public double getAbsoluteThreshold() {
+        return absoluteThreshold;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public abstract boolean converged(int iteration, T ... points);
+}

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

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java?rev=990792&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java Mon Aug 30 13:06:22 2010
@@ -0,0 +1,195 @@
+/*
+ * 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 java.util.Arrays;
+import java.util.Comparator;
+
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.exception.MathIllegalStateException;
+import org.apache.commons.math.exception.ConvergenceException;
+import org.apache.commons.math.analysis.MultivariateRealFunction;
+import org.apache.commons.math.exception.util.LocalizedFormats;
+import org.apache.commons.math.random.RandomVectorGenerator;
+
+/**
+ * Base class for all implementations of a multi-start optimizer.
+ *
+ * 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 MultiStartMultivariateRealOptimizer} or on
+ * {@link MultiStartDifferentiableMultivariateRealOptimizer}.
+ *
+ * @param <FUNC> Type of the objective function to be optimized.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRealFunction>
+    implements BaseMultivariateRealOptimizer<FUNC> {
+    /** Underlying classical optimizer. */
+    private final BaseMultivariateRealOptimizer<FUNC> optimizer;
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+    /** Number of evaluations already performed for all starts. */
+    private int totalEvaluations;
+    /** Number of starts to go. */
+    private int starts;
+    /** Random generator for multi-start. */
+    private RandomVectorGenerator generator;
+    /** Found optima. */
+    private RealPointValuePair[] optima;
+
+    /**
+     * Create a multi-start optimizer from a single-start optimizer.
+     *
+     * @param optimizer Single-start optimizer to wrap.
+     * @param starts Number of starts to perform (including the
+     * first one), multi-start is disabled if value is less than or
+     * equal to 1.
+     * @param generator Random vector generator to use for restarts.
+     */
+    protected BaseMultiStartMultivariateRealOptimizer(final BaseMultivariateRealOptimizer<FUNC> optimizer,
+                                                      final int starts,
+                                                      final RandomVectorGenerator generator) {
+        this.optimizer = optimizer;
+        this.starts = starts;
+        this.generator = generator;
+    }
+
+    /**
+     * Get all the optima found during the last call to {@link
+     * #optimize(FUNC,GoalType,double[]) optimize}.
+     * The optimizer stores all the optima found during a set of
+     * restarts. The {@link #optimize(FUNC,GoalType,double[])
+     * optimize} method returns the best point only. This method
+     * returns all the points found at the end of each starts,
+     * including the best one already returned by the {@link
+     * #optimize(FUNC,GoalType,double[]) optimize} method.
+     * <br/>
+     * The returned array as one element for each start as specified
+     * in the constructor. It is ordered with the results from the
+     * runs that did converge first, sorted from best to worst
+     * objective value (i.e in ascending order if minimizing and in
+     * descending order if maximizing), followed by and null elements
+     * corresponding to the runs that did not converge. This means all
+     * elements will be null if the {@link #optimize(FUNC,GoalType,double[])
+     * optimize} method did throw a {@link ConvergenceException}).
+     * This also means that if the first element is not {@code null}, it
+     * is the best point found across all starts.
+     *
+     * @return an array containing the optima.
+     * @throws MathIllegalStateException if {@link
+     * #optimize(FUNC,GoalType,double[]) optimize} has not been called.
+     */
+    public RealPointValuePair[] getOptima() {
+        if (optima == null) {
+            throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
+        }
+        return optima.clone();
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getEvaluations() {
+        return totalEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+        optimizer.setMaxEvaluations(maxEvaluations);
+    }
+
+    /** {@inheritDoc} */
+    public void setConvergenceChecker(ConvergenceChecker<RealPointValuePair> checker) {
+        optimizer.setConvergenceChecker(checker);
+    }
+
+    /** {@inheritDoc} */
+    public ConvergenceChecker<RealPointValuePair> getConvergenceChecker() {
+        return optimizer.getConvergenceChecker();
+    }
+
+    /**
+     * @param f Function to optimize.
+     * @param goal Goal type ({@link GoalType#MINIMIZE} or
+     * {@link GoalType#MAXIMIZE}).
+     * @param startPoint Start point.
+     */
+    public RealPointValuePair optimize(final FUNC f,
+                                       final GoalType goal,
+                                       double[] startPoint)
+        throws FunctionEvaluationException {
+        optima = new RealPointValuePair[starts];
+
+        // Multi-start loop.
+        for (int i = 0; i < starts; ++i) {
+
+            try {
+                optima[i] = optimizer.optimize(f, goal,
+                                               (i == 0 ? startPoint :
+                                                generator.nextVector()));
+            } catch (FunctionEvaluationException fee) {
+                optima[i] = null;
+            } catch (ConvergenceException oe) {
+                optima[i] = null;
+            }
+
+            final int usedEvaluations = optimizer.getEvaluations();
+            optimizer.setMaxEvaluations(optimizer.getMaxEvaluations() - usedEvaluations);
+            totalEvaluations += usedEvaluations;
+        }
+
+        sortPairs(goal);
+
+        if (optima[0] == null) {
+            throw new ConvergenceException(LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT,
+                                           starts);
+        }
+
+        // Return the found point given the best objective function value.
+        return optima[0];
+    }
+
+    /**
+     * Sort the optima from best to worst, followed by {@code null} elements.
+     *
+     * @param goal Goal type.
+     */
+    private void sortPairs(final GoalType goal) {
+        Arrays.sort(optima, new Comparator<RealPointValuePair>() {
+                public int compare(final RealPointValuePair o1,
+                                   final RealPointValuePair o2) {
+                    if (o1 == null) {
+                        return (o2 == null) ? 0 : 1;
+                    } else if (o2 == null) {
+                        return -1;
+                    }
+                    final double v1 = o1.getValue();
+                    final double v2 = o2.getValue();
+                    return (goal == GoalType.MINIMIZE) ?
+                        Double.compare(v1, v2) : Double.compare(v2, v1);
+                }
+            });
+    }
+}

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

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateVectorialOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateVectorialOptimizer.java?rev=990792&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateVectorialOptimizer.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateVectorialOptimizer.java Mon Aug 30 13:06:22 2010
@@ -0,0 +1,202 @@
+/*
+ * 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 java.util.Arrays;
+import java.util.Comparator;
+
+import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.exception.MathIllegalStateException;
+import org.apache.commons.math.exception.ConvergenceException;
+import org.apache.commons.math.analysis.MultivariateVectorialFunction;
+import org.apache.commons.math.exception.util.LocalizedFormats;
+import org.apache.commons.math.random.RandomVectorGenerator;
+
+/**
+ * Base class for all implementations of a multi-start optimizer.
+ *
+ * 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 MultiStartDifferentiableMultivariateVectorialOptimizer}.
+ *
+ * @param <FUNC> Type of the objective function to be optimized.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class BaseMultiStartMultivariateVectorialOptimizer<FUNC extends MultivariateVectorialFunction>
+    implements BaseMultivariateVectorialOptimizer<FUNC> {
+    /** Underlying classical optimizer. */
+    private final BaseMultivariateVectorialOptimizer<FUNC> optimizer;
+    /** Maximal number of evaluations allowed. */
+    private int maxEvaluations;
+    /** Number of evaluations already performed for all starts. */
+    private int totalEvaluations;
+    /** Number of starts to go. */
+    private int starts;
+    /** Random generator for multi-start. */
+    private RandomVectorGenerator generator;
+    /** Found optima. */
+    private VectorialPointValuePair[] optima;
+
+    /**
+     * Create a multi-start optimizer from a single-start optimizer.
+     *
+     * @param optimizer Single-start optimizer to wrap.
+     * @param starts Number of starts to perform (including the
+     * first one), multi-start is disabled if value is less than or
+     * equal to 1.
+     * @param generator Random vector generator to use for restarts.
+     */
+    protected BaseMultiStartMultivariateVectorialOptimizer(final BaseMultivariateVectorialOptimizer<FUNC> optimizer,
+                                                           final int starts,
+                                                           final RandomVectorGenerator generator) {
+        this.optimizer = optimizer;
+        this.starts = starts;
+        this.generator = generator;
+    }
+
+    /**
+     * Get all the optima found during the last call to {@link
+     * #optimize(FUNC,double[],double[],double[]) optimize}.
+     * The optimizer stores all the optima found during a set of
+     * restarts. The {@link #optimize(FUNC,double[],double[],double[])
+     * optimize} method returns the best point only. This method
+     * returns all the points found at the end of each starts, including
+     * the best one already returned by the {@link
+     * #optimize(FUNC,double[],double[],double[]) optimize} method.
+     * <br/>
+     * The returned array as one element for each start as specified
+     * in the constructor. It is ordered with the results from the
+     * runs that did converge first, sorted from best to worst
+     * objective value (i.e. in ascending order if minimizing and in
+     * descending order if maximizing), followed by and null elements
+     * corresponding to the runs that did not converge. This means all
+     * elements will be null if the {@link
+     * #optimize(FUNC,double[],double[],double[]) optimize} method did
+     * throw a {@link ConvergenceException}). This also means that if
+     * the first element is not {@code null}, it is the best point found
+     * across all starts.
+     *
+     * @return array containing the optima
+     * @throws MathIllegalStateException if {@link
+     * #optimize(FUNC,double[],double[],double[]) optimize} has not been
+     * called.
+     */
+    public VectorialPointValuePair[] getOptima() {
+        if (optima == null) {
+            throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
+        }
+        return optima.clone();
+    }
+
+    /** {@inheritDoc} */
+    public int getMaxEvaluations() {
+        return maxEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public int getEvaluations() {
+        return totalEvaluations;
+    }
+
+    /** {@inheritDoc} */
+    public void setMaxEvaluations(int maxEvaluations) {
+        this.maxEvaluations = maxEvaluations;
+        optimizer.setMaxEvaluations(maxEvaluations);
+    }
+
+    /** {@inheritDoc} */
+    public void setConvergenceChecker(ConvergenceChecker<VectorialPointValuePair> checker) {
+        optimizer.setConvergenceChecker(checker);
+    }
+
+    /** {@inheritDoc} */
+    public ConvergenceChecker<VectorialPointValuePair> getConvergenceChecker() {
+        return optimizer.getConvergenceChecker();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public VectorialPointValuePair optimize(final FUNC f,
+                                            double[] target, double[] weights,
+                                            double[] startPoint)
+        throws FunctionEvaluationException {
+
+        optima = new VectorialPointValuePair[starts];
+
+        // Multi-start loop.
+        for (int i = 0; i < starts; ++i) {
+
+            try {
+                optima[i] = optimizer.optimize(f, target, weights,
+                                               (i == 0 ? startPoint :
+                                                generator.nextVector()));
+            } catch (FunctionEvaluationException fee) {
+                optima[i] = null;
+            } catch (ConvergenceException oe) {
+                optima[i] = null;
+            }
+
+            final int usedEvaluations = optimizer.getEvaluations();
+            optimizer.setMaxEvaluations(optimizer.getMaxEvaluations() - usedEvaluations);
+            totalEvaluations += usedEvaluations;
+        }
+
+        sortPairs(target, weights);
+
+        if (optima[0] == null) {
+            throw new ConvergenceException(LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT,
+                                           starts);
+        }
+
+        // Return the found point given the best objective function value.
+        return optima[0];
+    }
+
+    /**
+     * Sort the optima from best to worst, followed by {@code null} elements.
+     *
+     * @param target Target value for the objective functions at optimum.
+     * @param weights Weights for the least-squares cost computation.
+     */
+    private void sortPairs(final double[] target,
+                           final double[] weights) {
+        Arrays.sort(optima, new Comparator<VectorialPointValuePair>() {
+                public int compare(final VectorialPointValuePair o1,
+                                   final VectorialPointValuePair o2) {
+                    if (o1 == null) {
+                        return (o2 == null) ? 0 : 1;
+                    } else if (o2 == null) {
+                        return -1;
+                    }
+                    return Double.compare(weightedResidual(o1), weightedResidual(o2));
+                }
+                private double weightedResidual(final VectorialPointValuePair pv) {
+                    final double[] value = pv.getValueRef();
+                    double sum = 0;
+                    for (int i = 0; i < value.length; ++i) {
+                        final double ri = value[i] - target[i];
+                        sum += weights[i] * ri * ri;
+                    }
+                    return sum;
+                }
+            });
+    }
+}

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

Modified: 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=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java Mon Aug 30 13:06:22 2010
@@ -21,95 +21,36 @@ import org.apache.commons.math.FunctionE
 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-FastMath. Users of the API are advised to base their code on
- * {@link MultivariateRealOptimizer} or on
- * {@link DifferentiableMultivariateRealOptimizer}.
- * @param <T> the type of the objective function to be optimized
+ * the following interfaces:
+ * <ul>
+ *  <li>{@link org.apache.commons.math.optimization.MultivariateRealOptimizer}</li>
+ *  <li>{@link org.apache.commons.math.optimization.DifferentiableMultivariateRealOptimizer}</li>
+ * </ul>
+ *
+ * @param <FUNC> Type of the objective function to be optimized.
  *
- * @see MultivariateRealOptimizer
- * @see DifferentiableMultivariateRealOptimizer
  * @version $Revision$ $Date$
- * @since 2.2
+ * @since 3.0
  */
-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 iterations corresponds to the last call to the
-     * {@code 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
-     * {@code 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();
-
+public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunction>
+    extends BaseOptimizer<RealPointValuePair> {
     /**
      * Optimize an objective function.
      *
      * @param f Objective function.
-     * @param goalType Type of optimization goal: either {@link GoalType#MAXIMIZE}
-     * or {@link GoalType#MINIMIZE}.
+     * @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.
+     * @return the point/value pair giving the optimal value for objective
+     * function.
+     * @throws FunctionEvaluationException if the objective function throws one
+     * during the search.
+     * @throws DimensionMismatchException if the start point dimension is wrong.
+     * @throws TooManyEvaluationsException if the maximal number of evaluations is
+     * exceeded.
      */
-    RealPointValuePair optimize(T f, GoalType goalType, double[] startPoint)
-        throws FunctionEvaluationException, OptimizationException;
+    RealPointValuePair optimize(FUNC f, GoalType goalType, double[] startPoint)
+        throws FunctionEvaluationException;
 }

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateVectorialOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateVectorialOptimizer.java?rev=990792&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateVectorialOptimizer.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateVectorialOptimizer.java Mon Aug 30 13:06:22 2010
@@ -0,0 +1,59 @@
+/*
+ * 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.MultivariateVectorialFunction;
+
+/**
+ * This interface is mainly intended to enforce the internal coherence of
+ * Commons-Math. Users of the API are advised to base their code on
+ * the following interfaces:
+ * <ul>
+ *  <li>{@link org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer}</li>
+ * </ul>
+ *
+ * @param <FUNC> Type of the objective function to be optimized.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public interface BaseMultivariateVectorialOptimizer<FUNC extends MultivariateVectorialFunction>
+    extends BaseOptimizer<VectorialPointValuePair> {
+    /**
+     * Optimize an objective function.
+     * Optimization is considered to be a weighted least-squares minimization.
+     * The cost function to be minimized is
+     * <code>&sum;weight<sub>i</sub>(objective<sub>i</sub> - target<sub>i</sub>)<sup>2</sup></code>
+     *
+     * @param f Objective function.
+     * @param target Target value for the objective functions at optimum.
+     * @param weight Weights for the least squares cost computation.
+     * @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 DimensionMismatchException if the start point dimension is wrong.
+     * @throws TooManyEvaluationsException if the maximal number of evaluations is
+     * exceeded.
+     */
+    VectorialPointValuePair optimize(FUNC f, double[] target, double[] weight,
+                                     double[] startPoint)
+        throws FunctionEvaluationException;
+}

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

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseOptimizer.java?rev=990792&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseOptimizer.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseOptimizer.java Mon Aug 30 13:06:22 2010
@@ -0,0 +1,77 @@
+/*
+ * 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;
+
+/**
+ * This interface is mainly intended to enforce the internal coherence of
+ * Commons-Math. Users of the API are advised to base their code on
+ * the following interfaces:
+ * <ul>
+ *  <li>{@link org.apache.commons.math.optimization.MultivariateRealOptimizer}</li>
+ *  <li>{@link org.apache.commons.math.optimization.DifferentiableMultivariateRealOptimizer}</li>
+ *  <li>{@link org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer}</li>
+ *  <li>{@link org.apache.commons.math.optimization.univariate.UnivariateRealOptimizer}</li>
+ * </ul>
+ *
+ * @param <PAIR> Type of the point/objective pair.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public interface BaseOptimizer<PAIR> {
+    /**
+     * Set the maximal number of function evaluations.
+     *
+     * @param maxEvaluations Maximal number of function evaluations.
+     */
+    void setMaxEvaluations(int maxEvaluations);
+
+    /**
+     * Get the maximal number of function evaluations.
+     *
+     * @return the maximal number of function evaluations.
+     */
+    int getMaxEvaluations();
+
+    /**
+     * Get the number of evaluations of the objective function.
+     * The number of evaluations corresponds to the last call to the
+     * {@code 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(ConvergenceChecker<PAIR> checker);
+
+    /**
+     * Get the convergence checker.
+     *
+     * @return the object used to check for convergence.
+     */
+    ConvergenceChecker<PAIR> getConvergenceChecker();
+}

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

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/ConvergenceChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/ConvergenceChecker.java?rev=990792&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/ConvergenceChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/ConvergenceChecker.java Mon Aug 30 13:06:22 2010
@@ -0,0 +1,58 @@
+/*
+ * 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;
+
+/**
+ * This interface specifies how to check if an optimization algorithm has
+ * converged.
+ * Deciding if convergence has been reached is a problem-dependent issue. The
+ * user should provide a class implementing this interface to allow the
+ * optimization algorithm to stop its search according to the problem at hand.
+ *
+ * @param <PAIR> Type of the (point, objective value) pair.
+ *
+ * @see org.apache.commons.math.optimization.SimpleScalarValueChecker
+ * @see org.apache.commons.math.optimization.SimpleRealPointChecker
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public interface ConvergenceChecker<PAIR> {
+    /**
+     * Check if the optimization algorithm has converged.
+     *
+     * @param iteration Current iteration.
+     * @param points Data used for checking the convergence.
+     * @return {@code true} if the algorithm is considered to have converged.
+     */
+    boolean converged(int iteration, PAIR ... points);
+
+    /**
+     * Get the relative tolerance.
+     *
+     * @return the relative threshold.
+     */
+    double getRelativeThreshold();
+
+    /**
+     * Get the absolute tolerance.
+     *
+     * @return the absolute threshold.
+     */
+    double getAbsoluteThreshold();
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/ConvergenceChecker.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=990792&r1=990791&r2=990792&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 Mon Aug 30 13:06:22 2010
@@ -28,18 +28,9 @@ import org.apache.commons.math.analysis.
  *
  * @see MultivariateRealOptimizer
  * @see DifferentiableMultivariateVectorialOptimizer
+ *
  * @version $Revision$ $Date$
  * @since 2.0
  */
 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
-     * {@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.
-     */
-    int getGradientEvaluations();
-}
+    extends BaseMultivariateRealOptimizer<DifferentiableMultivariateRealFunction> {}

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.java Mon Aug 30 13:06:22 2010
@@ -21,94 +21,12 @@ import org.apache.commons.math.FunctionE
 import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
 
 /**
- * This interface represents an optimization algorithm for {@link DifferentiableMultivariateVectorialFunction
- * vectorial differentiable objective functions}.
- * <p>Optimization algorithms find the input point set that either {@link GoalType
- * maximize or minimize} an objective function.</p>
- * @see MultivariateRealOptimizer
- * @see DifferentiableMultivariateRealOptimizer
+ * This interface represents an optimization algorithm for
+ * {@link DifferentiableMultivariateVectorialFunction vectorial differentiable
+ * objective functions}.
+ *
  * @version $Revision$ $Date$
- * @since 2.0
+ * @since 3.0
  */
-public interface DifferentiableMultivariateVectorialOptimizer {
-
-    /** 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.
-     * @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 evaluation correspond to the last call to the
-     * {@link #optimize(DifferentiableMultivariateVectorialFunction,
-     * double[], 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();
-
-    /** Get the number of evaluations of the objective function jacobian .
-     * <p>
-     * The number of evaluation correspond to the last call to the
-     * {@link #optimize(DifferentiableMultivariateVectorialFunction,
-     * double[], double[], double[]) optimize} method. It is 0 if
-     * the method has not been called yet.
-     * </p>
-     * @return number of evaluations of the objective function jacobian
-     */
-    int getJacobianEvaluations();
-
-    /** Set the convergence checker.
-     * @param checker object to use to check for convergence
-     */
-    void setConvergenceChecker(VectorialConvergenceChecker checker);
-
-    /** Get the convergence checker.
-     * @return object used to check for convergence
-     */
-    VectorialConvergenceChecker getConvergenceChecker();
-
-    /** Optimizes an objective function.
-     * <p>
-     * Optimization is considered to be a weighted least-squares minimization.
-     * The cost function to be minimized is
-     * &sum;weight<sub>i</sub>(objective<sub>i</sub>-target<sub>i</sub>)<sup>2</sup>
-     * </p>
-     * @param f objective function
-     * @param target target value for the objective functions at optimum
-     * @param weights weight for the least squares cost computation
-     * @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
-     */
-    VectorialPointValuePair optimize(DifferentiableMultivariateVectorialFunction f,
-                                     double[] target, double[] weights,
-                                     double[] startPoint)
-        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
-
-}
+public interface DifferentiableMultivariateVectorialOptimizer
+    extends BaseMultivariateVectorialOptimizer<DifferentiableMultivariateVectorialFunction> {}

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizer.java Mon Aug 30 13:06:22 2010
@@ -21,208 +21,38 @@ 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.exception.MathIllegalStateException;
+import org.apache.commons.math.exception.ConvergenceException;
 import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
 import org.apache.commons.math.exception.util.LocalizedFormats;
 import org.apache.commons.math.random.RandomVectorGenerator;
 
 /**
- * Special implementation of the {@link DifferentiableMultivariateRealOptimizer} interface adding
- * multi-start features to an existing optimizer.
- * <p>
+ * Special implementation of the {@link DifferentiableMultivariateRealOptimizer}
+ * interface adding multi-start features to an existing optimizer.
+ *
  * This class wraps a classical optimizer to use it several times in
  * turn with different starting points in order to avoid being trapped
  * into a local extremum when looking for a global one.
- * </p>
+ *
  * @version $Revision$ $Date$
  * @since 2.0
  */
 public class MultiStartDifferentiableMultivariateRealOptimizer
+    extends BaseMultiStartMultivariateRealOptimizer<DifferentiableMultivariateRealFunction>
     implements DifferentiableMultivariateRealOptimizer {
-
-    /** Underlying classical optimizer. */
-    private final DifferentiableMultivariateRealOptimizer optimizer;
-
-    /** Maximal number of iterations allowed. */
-    private int maxIterations;
-
-    /** 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;
-
-    /** Number of gradient evaluations already performed for all starts. */
-    private int totalGradientEvaluations;
-
-    /** Number of starts to go. */
-    private int starts;
-
-    /** Random generator for multi-start. */
-    private RandomVectorGenerator generator;
-
-    /** Found optima. */
-    private RealPointValuePair[] optima;
-
     /**
-     * Create a multi-start optimizer from a single-start optimizer
-     * @param optimizer single-start optimizer to wrap
-     * @param starts number of starts to perform (including the
+     * Create a multi-start optimizer from a single-start optimizer.
+     *
+     * @param optimizer Single-start optimizer to wrap.
+     * @param starts Number of starts to perform (including the
      * first one), multi-start is disabled if value is less than or
-     * equal to 1
-     * @param generator random vector generator to use for restarts
+     * equal to 1.
+     * @param generator Random vector generator to use for restarts.
      */
     public MultiStartDifferentiableMultivariateRealOptimizer(final DifferentiableMultivariateRealOptimizer optimizer,
                                                              final int starts,
                                                              final RandomVectorGenerator generator) {
-        this.optimizer                = optimizer;
-        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
-     * #optimize(DifferentiableMultivariateRealFunction, GoalType, double[])
-     * optimize}.
-     * <p>The optimizer stores all the optima found during a set of
-     * restarts. The {@link #optimize(DifferentiableMultivariateRealFunction,
-     * GoalType, double[]) optimize} method returns the best point only. This
-     * method returns all the points found at the end of each starts,
-     * including the best one already returned by the {@link
-     * #optimize(DifferentiableMultivariateRealFunction, GoalType, double[])
-     * optimize} method.
-     * </p>
-     * <p>
-     * The returned array as one element for each start as specified
-     * in the constructor. It is ordered with the results from the
-     * runs that did converge first, sorted from best to worst
-     * objective value (i.e in ascending order if minimizing and in
-     * descending order if maximizing), followed by and null elements
-     * corresponding to the runs that did not converge. This means all
-     * elements will be null if the {@link #optimize(DifferentiableMultivariateRealFunction,
-     * GoalType, double[]) optimize} method did throw a {@link
-     * org.apache.commons.math.ConvergenceException ConvergenceException}).
-     * This also means that if the first element is non null, it is the best
-     * point found across all starts.</p>
-     * @return array containing the optima
-     * @exception IllegalStateException if {@link #optimize(DifferentiableMultivariateRealFunction,
-     * GoalType, double[]) optimize} has not been called
-     */
-    public RealPointValuePair[] getOptima() throws IllegalStateException {
-        if (optima == null) {
-            throw MathRuntimeException.createIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
-        }
-        return optima.clone();
-    }
-
-    /** {@inheritDoc} */
-    public void setMaxIterations(int maxIterations) {
-        this.maxIterations = maxIterations;
-    }
-
-    /** {@inheritDoc} */
-    public int getMaxIterations() {
-        return maxIterations;
+        super(optimizer, starts, generator);
     }
-
-    /** {@inheritDoc} */
-    public int getIterations() {
-        return totalIterations;
-    }
-
-    /** {@inheritDoc} */
-    public void setMaxEvaluations(int maxEvaluations) {
-        this.maxEvaluations = maxEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public int getMaxEvaluations() {
-        return maxEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public int getEvaluations() {
-        return totalEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public int getGradientEvaluations() {
-        return totalGradientEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public void setConvergenceChecker(RealConvergenceChecker checker) {
-        optimizer.setConvergenceChecker(checker);
-    }
-
-    /** {@inheritDoc} */
-    public RealConvergenceChecker getConvergenceChecker() {
-        return optimizer.getConvergenceChecker();
-    }
-
-    /** {@inheritDoc} */
-    public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
-                                         final GoalType goalType,
-                                         double[] startPoint)
-        throws FunctionEvaluationException, OptimizationException {
-
-        optima                   = new RealPointValuePair[starts];
-        totalIterations          = 0;
-        totalEvaluations         = 0;
-        totalGradientEvaluations = 0;
-
-        // multi-start loop
-        for (int i = 0; i < starts; ++i) {
-
-            try {
-                optimizer.setMaxIterations(maxIterations - totalIterations);
-                optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations);
-                optima[i] = optimizer.optimize(f, goalType,
-                                               (i == 0) ? startPoint : generator.nextVector());
-            } catch (FunctionEvaluationException fee) {
-                optima[i] = null;
-            } catch (OptimizationException oe) {
-                optima[i] = null;
-            }
-
-            totalIterations          += optimizer.getIterations();
-            totalEvaluations         += optimizer.getEvaluations();
-            totalGradientEvaluations += optimizer.getGradientEvaluations();
-
-        }
-
-        // sort the optima from best to worst, followed by null elements
-        Arrays.sort(optima, new Comparator<RealPointValuePair>() {
-            public int compare(final RealPointValuePair o1, final RealPointValuePair o2) {
-                if (o1 == null) {
-                    return (o2 == null) ? 0 : +1;
-                } else if (o2 == null) {
-                    return -1;
-                }
-                final double v1 = o1.getValue();
-                final double v2 = o2.getValue();
-                return (goalType == GoalType.MINIMIZE) ?
-                        Double.compare(v1, v2) : Double.compare(v2, v1);
-            }
-        });
-
-        if (optima[0] == null) {
-            throw new OptimizationException(
-                    LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT,
-                    starts);
-        }
-
-        // return the found point given the best objective function value
-        return optima[0];
-
-    }
-
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizer.java Mon Aug 30 13:06:22 2010
@@ -17,222 +17,36 @@
 
 package org.apache.commons.math.optimization;
 
-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.DifferentiableMultivariateVectorialFunction;
-import org.apache.commons.math.exception.util.LocalizedFormats;
 import org.apache.commons.math.random.RandomVectorGenerator;
 
 /**
- * Special implementation of the {@link DifferentiableMultivariateVectorialOptimizer} interface adding
- * multi-start features to an existing optimizer.
- * <p>
+ * Special implementation of the {@link DifferentiableMultivariateVectorialOptimizer}
+ * interface addind multi-start features to an existing optimizer.
+ *
  * This class wraps a classical optimizer to use it several times in
  * turn with different starting points in order to avoid being trapped
  * into a local extremum when looking for a global one.
- * </p>
+ *
  * @version $Revision$ $Date$
  * @since 2.0
  */
 public class MultiStartDifferentiableMultivariateVectorialOptimizer
+    extends BaseMultiStartMultivariateVectorialOptimizer<DifferentiableMultivariateVectorialFunction>
     implements DifferentiableMultivariateVectorialOptimizer {
-
-    /** Serializable version identifier. */
-    private static final long serialVersionUID = 9206382258980561530L;
-
-    /** Underlying classical optimizer. */
-    private final DifferentiableMultivariateVectorialOptimizer optimizer;
-
-    /** Maximal number of iterations allowed. */
-    private int maxIterations;
-
-    /** 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;
-
-    /** Number of jacobian evaluations already performed for all starts. */
-    private int totalJacobianEvaluations;
-
-    /** Number of starts to go. */
-    private int starts;
-
-    /** Random generator for multi-start. */
-    private RandomVectorGenerator generator;
-
-    /** Found optima. */
-    private VectorialPointValuePair[] optima;
-
     /**
-     * Create a multi-start optimizer from a single-start optimizer
-     * @param optimizer single-start optimizer to wrap
-     * @param starts number of starts to perform (including the
+     * Create a multi-start optimizer from a single-start optimizer.
+     *
+     * @param optimizer Single-start optimizer to wrap.
+     * @param starts Number of starts to perform (including the
      * first one), multi-start is disabled if value is less than or
-     * equal to 1
-     * @param generator random vector generator to use for restarts
+     * equal to 1.
+     * @param generator Random vector generator to use for restarts.
      */
     public MultiStartDifferentiableMultivariateVectorialOptimizer(
                 final DifferentiableMultivariateVectorialOptimizer optimizer,
                 final int starts,
                 final RandomVectorGenerator generator) {
-        this.optimizer                = optimizer;
-        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
-     * #optimize(DifferentiableMultivariateVectorialFunction,
-     * double[], double[], double[]) optimize}.
-     * <p>The optimizer stores all the optima found during a set of
-     * restarts. The {@link #optimize(DifferentiableMultivariateVectorialFunction,
-     * double[], double[], double[]) optimize} method returns the
-     * best point only. This method returns all the points found at the
-     * end of each starts, including the best one already returned by the {@link
-     * #optimize(DifferentiableMultivariateVectorialFunction, double[],
-     * double[], double[]) optimize} method.
-     * </p>
-     * <p>
-     * The returned array as one element for each start as specified
-     * in the constructor. It is ordered with the results from the
-     * runs that did converge first, sorted from best to worst
-     * objective value (i.e in ascending order if minimizing and in
-     * descending order if maximizing), followed by and null elements
-     * corresponding to the runs that did not converge. This means all
-     * elements will be null if the {@link #optimize(DifferentiableMultivariateVectorialFunction,
-     * double[], double[], double[]) optimize} method did throw a {@link
-     * org.apache.commons.math.ConvergenceException ConvergenceException}).
-     * This also means that if the first element is non null, it is the best
-     * point found across all starts.</p>
-     * @return array containing the optima
-     * @exception IllegalStateException if {@link #optimize(DifferentiableMultivariateVectorialFunction,
-     * double[], double[], double[]) optimize} has not been called
-     */
-    public VectorialPointValuePair[] getOptima() throws IllegalStateException {
-        if (optima == null) {
-            throw MathRuntimeException.createIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
-        }
-        return optima.clone();
-    }
-
-    /** {@inheritDoc} */
-    public void setMaxIterations(int maxIterations) {
-        this.maxIterations = maxIterations;
-    }
-
-    /** {@inheritDoc} */
-    public int getMaxIterations() {
-        return maxIterations;
-    }
-
-    /** {@inheritDoc} */
-    public int getIterations() {
-        return totalIterations;
-    }
-
-    /** {@inheritDoc} */
-    public void setMaxEvaluations(int maxEvaluations) {
-        this.maxEvaluations = maxEvaluations;
+        super(optimizer, starts, generator);
     }
-
-    /** {@inheritDoc} */
-    public int getMaxEvaluations() {
-        return maxEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public int getEvaluations() {
-        return totalEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public int getJacobianEvaluations() {
-        return totalJacobianEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public void setConvergenceChecker(VectorialConvergenceChecker checker) {
-        optimizer.setConvergenceChecker(checker);
-    }
-
-    /** {@inheritDoc} */
-    public VectorialConvergenceChecker getConvergenceChecker() {
-        return optimizer.getConvergenceChecker();
-    }
-
-    /** {@inheritDoc} */
-    public VectorialPointValuePair optimize(final DifferentiableMultivariateVectorialFunction f,
-                                            final double[] target, final double[] weights,
-                                            final double[] startPoint)
-        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {
-
-        optima                   = new VectorialPointValuePair[starts];
-        totalIterations          = 0;
-        totalEvaluations         = 0;
-        totalJacobianEvaluations = 0;
-
-        // multi-start loop
-        for (int i = 0; i < starts; ++i) {
-
-            try {
-                optimizer.setMaxIterations(maxIterations - totalIterations);
-                optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations);
-                optima[i] = optimizer.optimize(f, target, weights,
-                                               (i == 0) ? startPoint : generator.nextVector());
-            } catch (FunctionEvaluationException fee) {
-                optima[i] = null;
-            } catch (OptimizationException oe) {
-                optima[i] = null;
-            }
-
-            totalIterations          += optimizer.getIterations();
-            totalEvaluations         += optimizer.getEvaluations();
-            totalJacobianEvaluations += optimizer.getJacobianEvaluations();
-
-        }
-
-        // sort the optima from best to worst, followed by null elements
-        Arrays.sort(optima, new Comparator<VectorialPointValuePair>() {
-            public int compare(final VectorialPointValuePair o1, final VectorialPointValuePair o2) {
-                if (o1 == null) {
-                    return (o2 == null) ? 0 : +1;
-                } else if (o2 == null) {
-                    return -1;
-                }
-                return Double.compare(weightedResidual(o1), weightedResidual(o2));
-            }
-            private double weightedResidual(final VectorialPointValuePair pv) {
-                final double[] value = pv.getValueRef();
-                double sum = 0;
-                for (int i = 0; i < value.length; ++i) {
-                    final double ri = value[i] - target[i];
-                    sum += weights[i] * ri * ri;
-                }
-                return sum;
-            }
-        });
-
-        if (optima[0] == null) {
-            throw new OptimizationException(
-                    LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT,
-                    starts);
-        }
-
-        // return the found point given the best objective function value
-        return optima[0];
-
-    }
-
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizer.java Mon Aug 30 13:06:22 2010
@@ -17,200 +17,35 @@
 
 package org.apache.commons.math.optimization;
 
-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.util.LocalizedFormats;
 import org.apache.commons.math.random.RandomVectorGenerator;
 
 /**
  * Special implementation of the {@link MultivariateRealOptimizer} interface adding
  * multi-start features to an existing optimizer.
- * <p>
+ *
  * This class wraps a classical optimizer to use it several times in
  * turn with different starting points in order to avoid being trapped
  * into a local extremum when looking for a global one.
- * </p>
+ *
  * @version $Revision$ $Date$
  * @since 2.0
  */
 public class MultiStartMultivariateRealOptimizer
+    extends BaseMultiStartMultivariateRealOptimizer<MultivariateRealFunction>
     implements MultivariateRealOptimizer {
-
-    /** Underlying classical optimizer. */
-    private final MultivariateRealOptimizer optimizer;
-
-    /** 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;
-
-    /** Random generator for multi-start. */
-    private RandomVectorGenerator generator;
-
-    /** Found optima. */
-    private RealPointValuePair[] optima;
-
     /**
-     * Create a multi-start optimizer from a single-start optimizer
-     * @param optimizer single-start optimizer to wrap
-     * @param starts number of starts to perform (including the
+     * Create a multi-start optimizer from a single-start optimizer.
+     *
+     * @param optimizer Single-start optimizer to wrap.
+     * @param starts Number of starts to perform (including the
      * first one), multi-start is disabled if value is less than or
-     * equal to 1
-     * @param generator random vector generator to use for restarts
+     * equal to 1.
+     * @param generator Random vector generator to use for restarts.
      */
     public MultiStartMultivariateRealOptimizer(final MultivariateRealOptimizer optimizer,
                                                final int starts,
                                                final RandomVectorGenerator generator) {
-        this.optimizer        = optimizer;
-        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
-     * #optimize(MultivariateRealFunction, GoalType, double[]) optimize}.
-     * <p>The optimizer stores all the optima found during a set of
-     * restarts. The {@link #optimize(MultivariateRealFunction, GoalType,
-     * double[]) optimize} method returns the best point only. This
-     * method returns all the points found at the end of each starts,
-     * including the best one already returned by the {@link
-     * #optimize(MultivariateRealFunction, GoalType, double[]) optimize}
-     * method.
-     * </p>
-     * <p>
-     * The returned array as one element for each start as specified
-     * in the constructor. It is ordered with the results from the
-     * runs that did converge first, sorted from best to worst
-     * objective value (i.e in ascending order if minimizing and in
-     * descending order if maximizing), followed by and null elements
-     * corresponding to the runs that did not converge. This means all
-     * elements will be null if the {@link #optimize(MultivariateRealFunction,
-     * GoalType, double[]) optimize} method did throw a {@link
-     * org.apache.commons.math.ConvergenceException ConvergenceException}).
-     * This also means that if the first element is non null, it is the best
-     * point found across all starts.</p>
-     * @return array containing the optima
-     * @exception IllegalStateException if {@link #optimize(MultivariateRealFunction,
-     * GoalType, double[]) optimize} has not been called
-     */
-    public RealPointValuePair[] getOptima() throws IllegalStateException {
-        if (optima == null) {
-            throw MathRuntimeException.createIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
-        }
-        return optima.clone();
-    }
-
-    /** {@inheritDoc} */
-    public void setMaxIterations(int maxIterations) {
-        this.maxIterations = maxIterations;
-    }
-
-    /** {@inheritDoc} */
-    public int getMaxIterations() {
-        return maxIterations;
-    }
-
-    /** {@inheritDoc} */
-    public void setMaxEvaluations(int maxEvaluations) {
-        this.maxEvaluations = maxEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public int getMaxEvaluations() {
-        return maxEvaluations;
+        super(optimizer, starts, generator);
     }
-
-    /** {@inheritDoc} */
-    public int getIterations() {
-        return totalIterations;
-    }
-
-    /** {@inheritDoc} */
-    public int getEvaluations() {
-        return totalEvaluations;
-    }
-
-    /** {@inheritDoc} */
-    public void setConvergenceChecker(RealConvergenceChecker checker) {
-        optimizer.setConvergenceChecker(checker);
-    }
-
-    /** {@inheritDoc} */
-    public RealConvergenceChecker getConvergenceChecker() {
-        return optimizer.getConvergenceChecker();
-    }
-
-    /** {@inheritDoc} */
-    public RealPointValuePair optimize(final MultivariateRealFunction f,
-                                         final GoalType goalType,
-                                         double[] startPoint)
-        throws FunctionEvaluationException, OptimizationException {
-
-        optima           = new RealPointValuePair[starts];
-        totalIterations  = 0;
-        totalEvaluations = 0;
-
-        // multi-start loop
-        for (int i = 0; i < starts; ++i) {
-
-            try {
-                optimizer.setMaxIterations(maxIterations - totalIterations);
-                optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations);
-                optima[i] = optimizer.optimize(f, goalType,
-                                               (i == 0) ? startPoint : generator.nextVector());
-            } catch (FunctionEvaluationException fee) {
-                optima[i] = null;
-            } catch (OptimizationException oe) {
-                optima[i] = null;
-            }
-
-            totalIterations  += optimizer.getIterations();
-            totalEvaluations += optimizer.getEvaluations();
-
-        }
-
-        // sort the optima from best to worst, followed by null elements
-        Arrays.sort(optima, new Comparator<RealPointValuePair>() {
-            public int compare(final RealPointValuePair o1, final RealPointValuePair o2) {
-                if (o1 == null) {
-                    return (o2 == null) ? 0 : +1;
-                } else if (o2 == null) {
-                    return -1;
-                }
-                final double v1 = o1.getValue();
-                final double v2 = o2.getValue();
-                return (goalType == GoalType.MINIMIZE) ?
-                        Double.compare(v1, v2) : Double.compare(v2, v1);
-            }
-        });
-
-        if (optima[0] == null) {
-            throw new OptimizationException(
-                    LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT,
-                    starts);
-        }
-
-        // return the found point given the best objective function value
-        return optima[0];
-
-    }
-
 }

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=990792&r1=990791&r2=990792&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 Mon Aug 30 13:06:22 2010
@@ -24,6 +24,7 @@ import org.apache.commons.math.analysis.
  * scalar objective functions}.
  * <p>Optimization algorithms find the input point set that either {@link GoalType
  * maximize or minimize} an objective function.</p>
+ *
  * @see DifferentiableMultivariateRealOptimizer
  * @see DifferentiableMultivariateVectorialOptimizer
  * @version $Revision$ $Date$

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/RealConvergenceChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/RealConvergenceChecker.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/RealConvergenceChecker.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/RealConvergenceChecker.java Mon Aug 30 13:06:22 2010
@@ -32,8 +32,8 @@ package org.apache.commons.math.optimiza
  *
  * @version $Revision$ $Date$
  * @since 2.0
+ * @deprecated To be removed in 3.0.
  */
-
 public interface RealConvergenceChecker {
 
   /** Check if the optimization algorithm has converged considering the last points.

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java Mon Aug 30 13:06:22 2010
@@ -17,71 +17,81 @@
 
 package org.apache.commons.math.optimization;
 
-import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.exception.DimensionMismatchException;
 
 /**
- * Simple implementation of the {@link RealConvergenceChecker} interface using
+ * Simple implementation of the {@link ConvergenceChecker} interface using
  * only point coordinates.
- * <p>
+ * 
  * Convergence is considered to have been reached if either the relative
  * difference between each point coordinate are smaller than a threshold
  * or if either the absolute difference between the point coordinates are
  * smaller than another threshold.
- * </p>
+ *
  * @version $Revision$ $Date$
- * @since 2.0
+ * @since 3.0
  */
-public class SimpleRealPointChecker implements RealConvergenceChecker {
-
-    /** Default relative threshold. */
-    private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
-
-    /** Default absolute threshold. */
-    private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
-
-    /** Relative tolerance threshold. */
-    private final double relativeThreshold;
-
-    /** Absolute tolerance threshold. */
-    private final double absoluteThreshold;
-
-   /** Build an instance with default threshold.
+public class SimpleRealPointChecker
+    extends AbstractConvergenceChecker<RealPointValuePair> {
+    /**
+     * Build an instance with default threshold.
      */
-    public SimpleRealPointChecker() {
-        this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD;
-        this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD;
-    }
+    public SimpleRealPointChecker() {}
 
-    /** Build an instance with a specified threshold.
-     * <p>
+    /**
+     * Build an instance with specified thresholds.
      * In order to perform only relative checks, the absolute tolerance
      * must be set to a negative value. In order to perform only absolute
      * checks, the relative tolerance must be set to a negative value.
-     * </p>
+     *
      * @param relativeThreshold relative tolerance threshold
      * @param absoluteThreshold absolute tolerance threshold
      */
     public SimpleRealPointChecker(final double relativeThreshold,
-                                 final double absoluteThreshold) {
-        this.relativeThreshold = relativeThreshold;
-        this.absoluteThreshold = absoluteThreshold;
+                                  final double absoluteThreshold) {
+        super(relativeThreshold, absoluteThreshold);
     }
 
-    /** {@inheritDoc} */
+    /**
+     * Check if the optimization algorithm has converged considering the
+     * last two points.
+     * This method may be called several time from the same algorithm
+     * iteration with different points. This can be detected by checking the
+     * iteration number at each call if needed. Each time this method is
+     * called, the previous and current point correspond to points with the
+     * same role at each iteration, so they can be compared. As an example,
+     * simplex-based algorithms call this method for all points of the simplex,
+     * not only for the best or worst ones.
+     *
+     * @param iteration Index of current iteration
+     * @param points Points used for checking convergence. The list must
+     * contain two elements:
+     * <ul>
+     *  <li>the previous best point,</li>
+     *  <li>the current best point.</li>
+     * </ul>
+     * @return {@code true} if the algorithm has converged.
+     * @throws DimensionMismatchException if the length of the {@code points}
+     * list is not equal to 2.
+     */
     public boolean converged(final int iteration,
-                             final RealPointValuePair previous,
-                             final RealPointValuePair current) {
-        final double[] p        = previous.getPoint();
-        final double[] c        = current.getPoint();
+                             final RealPointValuePair ... points) {
+        if (points.length != 2) {
+            throw new DimensionMismatchException(points.length, 2);
+        }
+
+        final double[] p = points[0].getPoint();
+        final double[] c = points[1].getPoint();
         for (int i = 0; i < p.length; ++i) {
             final double difference = FastMath.abs(p[i] - c[i]);
-            final double size       = FastMath.max(FastMath.abs(p[i]), FastMath.abs(c[i]));
-            if ((difference > (size * relativeThreshold)) && (difference > absoluteThreshold)) {
+            final double size = FastMath.max(FastMath.abs(p[i]), FastMath.abs(c[i]));
+            if (difference > size * getRelativeThreshold() &&
+                difference > getAbsoluteThreshold()) {
                 return false;
             }
         }
         return true;
     }
-
 }



Mime
View raw message