You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2010/08/30 15:06:24 UTC
svn commit: r990792 [4/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...
Copied: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizer.java (from r984406, commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizer.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizer.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java&r1=984406&r2=990792&rev=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/MultiStartUnivariateRealOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/MultiStartUnivariateRealOptimizer.java Mon Aug 30 13:06:22 2010
@@ -15,101 +15,78 @@
* limitations under the License.
*/
-package org.apache.commons.math.optimization;
+package org.apache.commons.math.optimization.univariate;
+
+import java.util.Arrays;
+import java.util.Comparator;
-import org.apache.commons.math.ConvergenceException;
import org.apache.commons.math.FunctionEvaluationException;
-import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.exception.MathIllegalStateException;
+import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.random.RandomGenerator;
+import org.apache.commons.math.optimization.GoalType;
+import org.apache.commons.math.optimization.ConvergenceChecker;
+import org.apache.commons.math.util.FastMath;
/**
- * Special implementation of the {@link UnivariateRealOptimizer} interface adding
- * multi-start features to an existing optimizer.
- * <p>
+ * Special implementation of the {@link UnivariateRealOptimizer} 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>
+ *
+ * @param <FUNC> Type of the objective function to be optimized.
+ *
* @version $Revision$ $Date$
- * @since 2.0
+ * @since 3.0
*/
-public class MultiStartUnivariateRealOptimizer implements UnivariateRealOptimizer {
-
- /** Serializable version identifier. */
- private static final long serialVersionUID = 5983375963110961019L;
-
+public class MultiStartUnivariateRealOptimizer<FUNC extends UnivariateRealFunction>
+ implements BaseUnivariateRealOptimizer<FUNC> {
/** Underlying classical optimizer. */
- private final UnivariateRealOptimizer optimizer;
-
- /** Maximal number of iterations allowed. */
- private int maxIterations;
-
+ private final BaseUnivariateRealOptimizer<FUNC> optimizer;
/** 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 RandomGenerator generator;
-
/** Found optima. */
- private double[] optima;
-
- /** Found function values at optima. */
- private double[] optimaValues;
+ private UnivariateRealPointValuePair[] 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 generator to use for restarts
+ * equal to 1.
+ * @param generator Random generator to use for restarts.
*/
- public MultiStartUnivariateRealOptimizer(final UnivariateRealOptimizer optimizer,
+ public MultiStartUnivariateRealOptimizer(final BaseUnivariateRealOptimizer<FUNC> optimizer,
final int starts,
final RandomGenerator generator) {
- this.optimizer = optimizer;
- this.totalIterations = 0;
- this.starts = starts;
- this.generator = generator;
- this.optima = null;
- setMaximalIterationCount(Integer.MAX_VALUE);
- setMaxEvaluations(Integer.MAX_VALUE);
- }
-
- /** {@inheritDoc} */
- public double getFunctionValue() {
- return optimaValues[0];
- }
-
- /** {@inheritDoc} */
- public double getResult() {
- return optima[0];
- }
-
- /** {@inheritDoc} */
- public double getAbsoluteAccuracy() {
- return optimizer.getAbsoluteAccuracy();
+ this.optimizer = optimizer;
+ this.starts = starts;
+ this.generator = generator;
}
- /** {@inheritDoc} */
- public int getIterationCount() {
- return totalIterations;
+ /**
+ * {@inheritDoc}
+ */
+ public void setConvergenceChecker(ConvergenceChecker<UnivariateRealPointValuePair> checker) {
+ optimizer.setConvergenceChecker(checker);
}
- /** {@inheritDoc} */
- public int getMaximalIterationCount() {
- return maxIterations;
+ /**
+ * {@inheritDoc}
+ */
+ public ConvergenceChecker<UnivariateRealPointValuePair> getConvergenceChecker() {
+ return optimizer.getConvergenceChecker();
}
/** {@inheritDoc} */
@@ -123,196 +100,112 @@ public class MultiStartUnivariateRealOpt
}
/** {@inheritDoc} */
- public double getRelativeAccuracy() {
- return optimizer.getRelativeAccuracy();
- }
-
- /** {@inheritDoc} */
- public void resetAbsoluteAccuracy() {
- optimizer.resetAbsoluteAccuracy();
- }
-
- /** {@inheritDoc} */
- public void resetMaximalIterationCount() {
- optimizer.resetMaximalIterationCount();
- }
-
- /** {@inheritDoc} */
- public void resetRelativeAccuracy() {
- optimizer.resetRelativeAccuracy();
- }
-
- /** {@inheritDoc} */
- public void setAbsoluteAccuracy(double accuracy) {
- optimizer.setAbsoluteAccuracy(accuracy);
- }
-
- /** {@inheritDoc} */
- public void setMaximalIterationCount(int count) {
- this.maxIterations = count;
- }
-
- /** {@inheritDoc} */
public void setMaxEvaluations(int maxEvaluations) {
this.maxEvaluations = maxEvaluations;
+ optimizer.setMaxEvaluations(maxEvaluations);
}
- /** {@inheritDoc} */
- public void setRelativeAccuracy(double accuracy) {
- optimizer.setRelativeAccuracy(accuracy);
- }
-
- /** Get all the optima found during the last call to {@link
- * #optimize(UnivariateRealFunction, GoalType, double, double) optimize}.
- * <p>The optimizer stores all the optima found during a set of
- * restarts. The {@link #optimize(UnivariateRealFunction, GoalType,
- * 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(UnivariateRealFunction, GoalType, double, double) optimize}
+ /**
+ * Get all the optima found during the last call to {@link
+ * #optimize(FUNC,GoalType,double,double) optimize}.
+ * The optimizer stores all the optima found during a set of
+ * restarts. The {@link #optimize(FUNC,GoalType,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,GoalType,double,double) optimize}
* method.
- * </p>
- * <p>
+ * <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 Double.NaN elements
+ * descending order if maximizing), followed by {@code null} elements
* corresponding to the runs that did not converge. This means all
- * elements will be NaN if the {@link #optimize(UnivariateRealFunction,
- * GoalType, double, double) optimize} method did throw a {@link
- * ConvergenceException ConvergenceException}). This also means that
- * if the first element is not NaN, it is the best point found across
- * all starts.</p>
- * @return array containing the optima
- * @exception IllegalStateException if {@link #optimize(UnivariateRealFunction,
- * GoalType, double, double) optimize} has not been called
- * @see #getOptimaValues()
+ * elements will be {@code null} if the {@link
+ * #optimize(FUNC,GoalType,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 an array containing the optima.
+ * @throws MathIllegalStateException if {@link
+ * #optimize(FUNC,GoalType,double,double) optimize} has not been called.
*/
- public double[] getOptima() throws IllegalStateException {
+ public UnivariateRealPointValuePair[] getOptima() {
if (optima == null) {
- throw MathRuntimeException.createIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
+ throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
}
return optima.clone();
}
- /** Get all the function values at optima found during the last call to {@link
- * #optimize(UnivariateRealFunction, GoalType, double, double) optimize}.
- * <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 Double.NaN elements
- * corresponding to the runs that did not converge. This means all
- * elements will be NaN if the {@link #optimize(UnivariateRealFunction,
- * GoalType, double, double) optimize} method did throw a {@link
- * ConvergenceException ConvergenceException}). This also means that
- * if the first element is not NaN, it is the best point found across
- * all starts.</p>
- * @return array containing the optima
- * @exception IllegalStateException if {@link #optimize(UnivariateRealFunction,
- * GoalType, double, double) optimize} has not been called
- * @see #getOptima()
- */
- public double[] getOptimaValues() throws IllegalStateException {
- if (optimaValues == null) {
- throw MathRuntimeException.createIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
- }
- return optimaValues.clone();
- }
-
/** {@inheritDoc} */
- public double optimize(final UnivariateRealFunction f, final GoalType goalType,
- final double min, final double max)
- throws ConvergenceException,
- FunctionEvaluationException {
-
- optima = new double[starts];
- optimaValues = new double[starts];
- totalIterations = 0;
+ public UnivariateRealPointValuePair optimize(final FUNC f,
+ final GoalType goal,
+ final double min, final double max)
+ throws FunctionEvaluationException {
+
+ optima = new UnivariateRealPointValuePair[starts];
totalEvaluations = 0;
- // multi-start loop
+ // Multi-start loop.
for (int i = 0; i < starts; ++i) {
-
try {
- optimizer.setMaximalIterationCount(maxIterations - totalIterations);
- optimizer.setMaxEvaluations(maxEvaluations - totalEvaluations);
final double bound1 = (i == 0) ? min : min + generator.nextDouble() * (max - min);
final double bound2 = (i == 0) ? max : min + generator.nextDouble() * (max - min);
- optima[i] = optimizer.optimize(f, goalType,
- Math.min(bound1, bound2),
- Math.max(bound1, bound2));
- optimaValues[i] = optimizer.getFunctionValue();
+ optima[i] = optimizer.optimize(f, goal,
+ FastMath.min(bound1, bound2),
+ FastMath.max(bound1, bound2));
} catch (FunctionEvaluationException fee) {
- optima[i] = Double.NaN;
- optimaValues[i] = Double.NaN;
+ optima[i] = null;
} catch (ConvergenceException ce) {
- optima[i] = Double.NaN;
- optimaValues[i] = Double.NaN;
+ optima[i] = null;
}
- totalIterations += optimizer.getIterationCount();
- totalEvaluations += optimizer.getEvaluations();
-
+ final int usedEvaluations = optimizer.getEvaluations();
+ optimizer.setMaxEvaluations(optimizer.getMaxEvaluations() - usedEvaluations);
+ totalEvaluations += usedEvaluations;
}
- // sort the optima from best to worst, followed by NaN elements
- int lastNaN = optima.length;
- for (int i = 0; i < lastNaN; ++i) {
- if (Double.isNaN(optima[i])) {
- optima[i] = optima[--lastNaN];
- optima[lastNaN + 1] = Double.NaN;
- optimaValues[i] = optimaValues[--lastNaN];
- optimaValues[lastNaN + 1] = Double.NaN;
- }
- }
-
- double currX = optima[0];
- double currY = optimaValues[0];
- for (int j = 1; j < lastNaN; ++j) {
- final double prevY = currY;
- currX = optima[j];
- currY = optimaValues[j];
- if ((goalType == GoalType.MAXIMIZE) ^ (currY < prevY)) {
- // the current element should be inserted closer to the beginning
- int i = j - 1;
- double mIX = optima[i];
- double mIY = optimaValues[i];
- while ((i >= 0) && ((goalType == GoalType.MAXIMIZE) ^ (currY < mIY))) {
- optima[i + 1] = mIX;
- optimaValues[i + 1] = mIY;
- if (i-- != 0) {
- mIX = optima[i];
- mIY = optimaValues[i];
- } else {
- mIX = Double.NaN;
- mIY = Double.NaN;
- }
- }
- optima[i + 1] = currX;
- optimaValues[i + 1] = currY;
- currX = optima[j];
- currY = optimaValues[j];
- }
- }
+ sortPairs(goal);
- if (Double.isNaN(optima[0])) {
- throw new OptimizationException(
- LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT,
- starts);
+ 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 the point with the best objective function value.
return optima[0];
-
}
/** {@inheritDoc} */
- public double optimize(final UnivariateRealFunction f, final GoalType goalType,
- final double min, final double max, final double startValue)
- throws ConvergenceException, FunctionEvaluationException {
+ public UnivariateRealPointValuePair optimize(final FUNC f, final GoalType goalType,
+ final double min, final double max,
+ final double startValue)
+ throws FunctionEvaluationException {
+ // XXX Main code should be here, using "startValue" for the first start.
+ // XXX This method should set "startValue" to min + 0.5 * (max - min)
return optimize(f, goalType, min, max);
}
+
+ /**
+ * 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<UnivariateRealPointValuePair>() {
+ public int compare(final UnivariateRealPointValuePair o1,
+ final UnivariateRealPointValuePair 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);
+ }
+ });
+ }
}
Copied: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealOptimizer.java (from r984406, commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealOptimizer.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealOptimizer.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java&r1=984406&r2=990792&rev=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/UnivariateRealOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealOptimizer.java Mon Aug 30 13:06:22 2010
@@ -14,102 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.math.optimization;
+package org.apache.commons.math.optimization.univariate;
-import org.apache.commons.math.ConvergenceException;
-import org.apache.commons.math.ConvergingAlgorithm;
-import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
-
/**
- * Interface for (univariate real) optimization algorithms.
+ * Interface for univariate optimization algorithms.
*
* @version $Revision$ $Date$
- * @since 2.0
+ * @since 3.0
*/
-public interface UnivariateRealOptimizer extends ConvergingAlgorithm {
-
- /** Set the maximal number of functions evaluations.
- * @param maxEvaluations maximal number of function evaluations
- */
- void setMaxEvaluations(int maxEvaluations);
-
- /** Get the maximal number of functions evaluations.
- * @return the maximal number of functions evaluations.
- */
- int getMaxEvaluations();
-
- /** Get the number of evaluations of the objective function.
- * <p>
- * The number of evaluations corresponds to the last call to the
- * {@link #optimize(UnivariateRealFunction, GoalType, double, double) optimize}
- * method. It is 0 if the method has not been called yet.
- * </p>
- * @return the number of evaluations of the objective function.
- */
- int getEvaluations();
-
- /**
- * Find an optimum in the given interval.
- * <p>
- * An optimizer may require that the interval brackets a single optimum.
- * </p>
- * @param f the function to optimize.
- * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
- * or {@link GoalType#MINIMIZE}.
- * @param min the lower bound for the interval.
- * @param max the upper bound for the interval.
- * @return a value where the function is optimum.
- * @throws ConvergenceException if the maximum iteration count is exceeded
- * or the optimizer detects convergence problems otherwise.
- * @throws FunctionEvaluationException if an error occurs evaluating the
- * function.
- * @throws IllegalArgumentException if min > max or the endpoints do not
- * satisfy the requirements specified by the optimizer.
- */
- double optimize(UnivariateRealFunction f, GoalType goalType,
- double min, double max)
- throws ConvergenceException, FunctionEvaluationException;
-
- /**
- * Find an optimum in the given interval, start at startValue.
- * <p>
- * An optimizer may require that the interval brackets a single optimum.
- * </p>
- * @param f the function to optimize.
- * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
- * or {@link GoalType#MINIMIZE}.
- * @param min the lower bound for the interval.
- * @param max the upper bound for the interval.
- * @param startValue the start value to use.
- * @return a value where the function is optimum.
- * @throws ConvergenceException if the maximum iteration count is exceeded
- * or the optimizer detects convergence problems otherwise.
- * @throws FunctionEvaluationException if an error occurs evaluating the
- * function.
- * @throws IllegalArgumentException if min > max or the arguments do not
- * satisfy the requirements specified by the optimizer.
- */
- double optimize(UnivariateRealFunction f, GoalType goalType,
- double min, double max, double startValue)
- throws ConvergenceException, FunctionEvaluationException;
-
- /**
- * Get the result of the last run of the optimizer.
- *
- * @return the optimum.
- * @throws IllegalStateException if there is no result available, either
- * because no result was yet computed or the last attempt failed.
- */
- double getResult();
-
- /**
- * Get the result of the last run of the optimizer.
- *
- * @return the value of the function at the optimum.
- * @throws IllegalStateException if there is no result available, either
- * because no result was yet computed or the last attempt failed.
- */
- double getFunctionValue();
-}
+public interface UnivariateRealOptimizer
+ extends BaseUnivariateRealOptimizer<UnivariateRealFunction> {}
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealPointValuePair.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealPointValuePair.java?rev=990792&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealPointValuePair.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealPointValuePair.java Mon Aug 30 13:06:22 2010
@@ -0,0 +1,67 @@
+/*
+ * 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.univariate;
+
+import java.io.Serializable;
+
+/**
+ * This class holds a point and the value of an objective function at this
+ * point.
+ * This is a simple immutable container.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class UnivariateRealPointValuePair implements Serializable {
+ /** Serializable version identifier. */
+ private static final long serialVersionUID = 1003888396256744753L;
+ /** Point. */
+ private final double point;
+ /** Value of the objective function at the point. */
+ private final double value;
+
+ /**
+ * Build a point/objective function value pair.
+ *
+ * @param point Point.
+ * @param value Value of an objective function at the point
+ */
+ public UnivariateRealPointValuePair(final double point,
+ final double value) {
+ this.point = point;
+ this.value = value;
+ }
+
+ /**
+ * Get the point.
+ *
+ * @return the point.
+ */
+ public double getPoint() {
+ return point;
+ }
+
+ /**
+ * Get the value of the objective function.
+ *
+ * @return the stored value of the objective function.
+ */
+ public double getValue() {
+ return value;
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/UnivariateRealPointValuePair.java
------------------------------------------------------------------------------
svn:eol-style = native
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=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Mon Aug 30 13:06:22 2010
@@ -51,6 +51,17 @@ The <action> type attribute can be add,u
with a new-line in the release notes. (These spaces are ignored when displaying HTML).
If the output is not quite correct, check for invisible trailing spaces!
-->
+ <release version="3.0" date="TBD" description="TBD">
+ <action dev="erans" type="update" issue="MATH-397">
+ Removed methods referring to the concept of "iteration".
+ Removed interface methods to access the number of evaluations of the
+ gradient and Jacobian.
+ Added new "Incrementor" utility to be used as a bounded counter for
+ objective function evaluations.
+ Removed all references to "OptimizationException" (replaced by
+ "ConvergenceException").
+ </action>
+ </release>
<release version="2.2" date="TBD" description="TBD">
<action dev="luc" type="fix" issue="MATH-375" due-to="Bill Rossi">
Added faster and more accurate version of traditional mathematical functions in a FastMath
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java Mon Aug 30 13:06:22 2010
@@ -39,7 +39,7 @@ import org.junit.Test;
public class MultiStartDifferentiableMultivariateRealOptimizerTest {
@Test
- public void testCircleFitting() throws FunctionEvaluationException, OptimizationException {
+ public void testCircleFitting() throws FunctionEvaluationException {
Circle circle = new Circle();
circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0);
@@ -55,14 +55,10 @@ public class MultiStartDifferentiableMul
new GaussianRandomGenerator(g));
MultiStartDifferentiableMultivariateRealOptimizer optimizer =
new MultiStartDifferentiableMultivariateRealOptimizer(underlying, 10, generator);
- optimizer.setMaxIterations(100);
- assertEquals(100, optimizer.getMaxIterations());
optimizer.setMaxEvaluations(100);
assertEquals(100, optimizer.getMaxEvaluations());
optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-10, 1.0e-10));
BrentSolver solver = new BrentSolver();
- solver.setAbsoluteAccuracy(1.0e-13);
- solver.setRelativeAccuracy(1.0e-15);
RealPointValuePair optimum =
optimizer.optimize(circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 });
RealPointValuePair[] optima = optimizer.getOptima();
@@ -72,12 +68,8 @@ public class MultiStartDifferentiableMul
assertEquals(96.075902096, center.x, 1.0e-8);
assertEquals(48.135167894, center.y, 1.0e-8);
}
- assertTrue(optimizer.getGradientEvaluations() > 650);
- assertTrue(optimizer.getGradientEvaluations() < 700);
assertTrue(optimizer.getEvaluations() > 70);
assertTrue(optimizer.getEvaluations() < 90);
- assertTrue(optimizer.getIterations() > 70);
- assertTrue(optimizer.getIterations() < 90);
assertEquals(3.1267527, optimum.getValue(), 1.0e-8);
}
@@ -119,7 +111,6 @@ public class MultiStartDifferentiableMul
dJdY *= 2;
return new double[] { dJdX, dJdY };
-
}
public double value(double[] variables)
@@ -133,9 +124,7 @@ public class MultiStartDifferentiableMul
double di = point.distance(center) - radius;
sum += di * di;
}
-
return sum;
-
}
public MultivariateVectorialFunction gradient() {
@@ -153,7 +142,5 @@ public class MultiStartDifferentiableMul
}
};
}
-
}
-
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateVectorialOptimizerTest.java Mon Aug 30 13:06:22 2010
@@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
import java.io.Serializable;
import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
import org.apache.commons.math.analysis.MultivariateMatrixFunction;
import org.apache.commons.math.linear.BlockRealMatrix;
@@ -112,7 +113,7 @@ public class MultiStartDifferentiableMul
MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
10, generator);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
// no optima before first optimization attempt
@@ -134,14 +135,10 @@ public class MultiStartDifferentiableMul
}
assertTrue(optimizer.getEvaluations() > 20);
assertTrue(optimizer.getEvaluations() < 50);
- assertTrue(optimizer.getIterations() > 20);
- assertTrue(optimizer.getIterations() < 50);
- assertTrue(optimizer.getJacobianEvaluations() > 20);
- assertTrue(optimizer.getJacobianEvaluations() < 50);
- assertEquals(100, optimizer.getMaxIterations());
+ assertEquals(100, optimizer.getMaxEvaluations());
}
- @Test(expected = OptimizationException.class)
+ @Test(expected = ConvergenceException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
new GaussNewtonOptimizer(true);
@@ -152,7 +149,7 @@ public class MultiStartDifferentiableMul
MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
10, generator);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
public MultivariateMatrixFunction jacobian() {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/MultiStartMultivariateRealOptimizerTest.java Mon Aug 30 13:06:22 2010
@@ -48,13 +48,13 @@ public class MultiStartMultivariateRealO
MultiStartMultivariateRealOptimizer optimizer =
new MultiStartMultivariateRealOptimizer(underlying, 10, generator);
optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(1100);
RealPointValuePair optimum =
optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });
assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
- assertTrue(optimizer.getEvaluations() > 20);
- assertTrue(optimizer.getEvaluations() < 250);
+ assertTrue(optimizer.getEvaluations() > 900);
+ assertTrue(optimizer.getEvaluations() < 1200);
assertTrue(optimum.getValue() < 8.0e-4);
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/MultiDirectionalTest.java Mon Aug 30 13:06:22 2010
@@ -91,7 +91,7 @@ public class MultiDirectionalTest {
MultiDirectional optimizer = new MultiDirectional();
optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-11, 1.0e-30));
- optimizer.setMaxIterations(200);
+ optimizer.setMaxEvaluations(200);
optimizer.setStartConfiguration(new double[] { 0.2, 0.2 });
RealPointValuePair optimum;
@@ -146,7 +146,7 @@ public class MultiDirectionalTest {
count = 0;
MultiDirectional optimizer = new MultiDirectional();
optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setStartConfiguration(new double[][] {
{ -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 }
});
@@ -180,7 +180,7 @@ public class MultiDirectionalTest {
count = 0;
MultiDirectional optimizer = new MultiDirectional();
optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
- optimizer.setMaxIterations(1000);
+ optimizer.setMaxEvaluations(1000);
RealPointValuePair optimum =
optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
Assert.assertEquals(count, optimizer.getEvaluations());
@@ -196,7 +196,6 @@ public class MultiDirectionalTest {
// fails because MultiDirectional.iterateSimplex is looping forever
// the while(true) should be replaced with a convergence check
MultiDirectional multiDirectional = new MultiDirectional();
- multiDirectional.setMaxIterations(100);
multiDirectional.setMaxEvaluations(1000);
final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/NelderMeadTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/NelderMeadTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/NelderMeadTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/NelderMeadTest.java Mon Aug 30 13:06:22 2010
@@ -23,315 +23,278 @@ import static org.junit.Assert.assertNul
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import org.apache.commons.math.ConvergenceException;
import org.apache.commons.math.FunctionEvaluationException;
-import org.apache.commons.math.MathException;
-import org.apache.commons.math.MaxEvaluationsExceededException;
-import org.apache.commons.math.MaxIterationsExceededException;
+import org.apache.commons.math.exception.TooManyEvaluationsException;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
import org.apache.commons.math.linear.Array2DRowRealMatrix;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.LeastSquaresConverter;
-import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.apache.commons.math.optimization.SimpleRealPointChecker;
import org.apache.commons.math.optimization.SimpleScalarValueChecker;
-import org.apache.commons.math.util.FastMath;
import org.junit.Test;
public class NelderMeadTest {
-
- @Test
- public void testFunctionEvaluationExceptions() {
- MultivariateRealFunction wrong =
- new MultivariateRealFunction() {
- private static final long serialVersionUID = 4751314470965489371L;
- public double value(double[] x) throws FunctionEvaluationException {
- if (x[0] < 0) {
- throw new FunctionEvaluationException(x, "{0}", "oops");
- } else if (x[0] > 1) {
- throw new FunctionEvaluationException(new RuntimeException("oops"), x);
- } else {
- return x[0] * (1 - x[0]);
+ @Test
+ public void testFunctionEvaluationExceptions() {
+ MultivariateRealFunction wrong =
+ new MultivariateRealFunction() {
+ private static final long serialVersionUID = 4751314470965489371L;
+ public double value(double[] x) throws FunctionEvaluationException {
+ if (x[0] < 0) {
+ throw new FunctionEvaluationException(x, "{0}", "oops");
+ } else if (x[0] > 1) {
+ throw new FunctionEvaluationException(new RuntimeException("oops"), x);
+ } else {
+ return x[0] * (1 - x[0]);
+ }
}
- }
- };
- try {
- NelderMead optimizer = new NelderMead(0.9, 1.9, 0.4, 0.6);
- optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { -1.0 });
- fail("an exception should have been thrown");
- } catch (FunctionEvaluationException ce) {
- // expected behavior
- assertNull(ce.getCause());
- } catch (Exception e) {
- fail("wrong exception caught: " + e.getMessage());
- }
- try {
- NelderMead optimizer = new NelderMead(0.9, 1.9, 0.4, 0.6);
- optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { +2.0 });
- fail("an exception should have been thrown");
- } catch (FunctionEvaluationException ce) {
- // expected behavior
- assertNotNull(ce.getCause());
- } catch (Exception e) {
- fail("wrong exception caught: " + e.getMessage());
- }
- }
-
- @Test
- public void testMinimizeMaximize()
- throws FunctionEvaluationException, ConvergenceException {
-
- // the following function has 4 local extrema:
- final double xM = -3.841947088256863675365;
- final double yM = -1.391745200270734924416;
- final double xP = 0.2286682237349059125691;
- final double yP = -yM;
- final double valueXmYm = 0.2373295333134216789769; // local maximum
- final double valueXmYp = -valueXmYm; // local minimum
- final double valueXpYm = -0.7290400707055187115322; // global minimum
- final double valueXpYp = -valueXpYm; // global maximum
- MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
- private static final long serialVersionUID = -7039124064449091152L;
- public double value(double[] variables) throws FunctionEvaluationException {
- final double x = variables[0];
- final double y = variables[1];
- return ((x == 0) || (y == 0)) ? 0 : (FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y));
- }
- };
-
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-10, 1.0e-30));
- optimizer.setMaxIterations(100);
- optimizer.setStartConfiguration(new double[] { 0.2, 0.2 });
- RealPointValuePair optimum;
-
- // minimization
- optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { -3.0, 0 });
- assertEquals(xM, optimum.getPoint()[0], 2.0e-7);
- assertEquals(yP, optimum.getPoint()[1], 2.0e-5);
- assertEquals(valueXmYp, optimum.getValue(), 6.0e-12);
- assertTrue(optimizer.getEvaluations() > 60);
- assertTrue(optimizer.getEvaluations() < 90);
-
- optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { +1, 0 });
- assertEquals(xP, optimum.getPoint()[0], 5.0e-6);
- assertEquals(yM, optimum.getPoint()[1], 6.0e-6);
- assertEquals(valueXpYm, optimum.getValue(), 1.0e-11);
- assertTrue(optimizer.getEvaluations() > 60);
- assertTrue(optimizer.getEvaluations() < 90);
-
- // maximization
- optimum = optimizer.optimize(fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
- assertEquals(xM, optimum.getPoint()[0], 1.0e-5);
- assertEquals(yM, optimum.getPoint()[1], 3.0e-6);
- assertEquals(valueXmYm, optimum.getValue(), 3.0e-12);
- assertTrue(optimizer.getEvaluations() > 60);
- assertTrue(optimizer.getEvaluations() < 90);
-
- optimum = optimizer.optimize(fourExtrema, GoalType.MAXIMIZE, new double[] { +1, 0 });
- assertEquals(xP, optimum.getPoint()[0], 4.0e-6);
- assertEquals(yP, optimum.getPoint()[1], 5.0e-6);
- assertEquals(valueXpYp, optimum.getValue(), 7.0e-12);
- assertTrue(optimizer.getEvaluations() > 60);
- assertTrue(optimizer.getEvaluations() < 90);
-
- }
-
- @Test
- public void testRosenbrock()
- throws FunctionEvaluationException, ConvergenceException {
-
- Rosenbrock rosenbrock = new Rosenbrock();
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
- optimizer.setMaxIterations(100);
- optimizer.setStartConfiguration(new double[][] {
- { -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 }
- });
- RealPointValuePair optimum =
- optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });
-
- assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
- assertTrue(optimizer.getEvaluations() > 40);
- assertTrue(optimizer.getEvaluations() < 50);
- assertTrue(optimum.getValue() < 8.0e-4);
-
- }
-
- @Test
- public void testPowell()
- throws FunctionEvaluationException, ConvergenceException {
-
- Powell powell = new Powell();
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
- optimizer.setMaxIterations(200);
- RealPointValuePair optimum =
- optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
- assertEquals(powell.getCount(), optimizer.getEvaluations());
- assertTrue(optimizer.getEvaluations() > 110);
- assertTrue(optimizer.getEvaluations() < 130);
- assertTrue(optimum.getValue() < 2.0e-3);
-
- }
-
- @Test
- public void testLeastSquares1()
- throws FunctionEvaluationException, ConvergenceException {
-
- final RealMatrix factors =
- new Array2DRowRealMatrix(new double[][] {
- { 1.0, 0.0 },
- { 0.0, 1.0 }
- }, false);
- LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
- public double[] value(double[] variables) {
- return factors.operate(variables);
- }
- }, new double[] { 2.0, -3.0 });
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
- optimizer.setMaxIterations(200);
- RealPointValuePair optimum =
- optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
- assertEquals( 2.0, optimum.getPointRef()[0], 3.0e-5);
- assertEquals(-3.0, optimum.getPointRef()[1], 4.0e-4);
- assertTrue(optimizer.getEvaluations() > 60);
- assertTrue(optimizer.getEvaluations() < 80);
- assertTrue(optimum.getValue() < 1.0e-6);
- }
-
- @Test
- public void testLeastSquares2()
- throws FunctionEvaluationException, ConvergenceException {
-
- final RealMatrix factors =
- new Array2DRowRealMatrix(new double[][] {
- { 1.0, 0.0 },
- { 0.0, 1.0 }
- }, false);
- LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
- public double[] value(double[] variables) {
- return factors.operate(variables);
- }
- }, new double[] { 2.0, -3.0 }, new double[] { 10.0, 0.1 });
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
- optimizer.setMaxIterations(200);
- RealPointValuePair optimum =
- optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
- assertEquals( 2.0, optimum.getPointRef()[0], 5.0e-5);
- assertEquals(-3.0, optimum.getPointRef()[1], 8.0e-4);
- assertTrue(optimizer.getEvaluations() > 60);
- assertTrue(optimizer.getEvaluations() < 80);
- assertTrue(optimum.getValue() < 1.0e-6);
- }
-
- @Test
- public void testLeastSquares3()
- throws FunctionEvaluationException, ConvergenceException {
-
- final RealMatrix factors =
- new Array2DRowRealMatrix(new double[][] {
- { 1.0, 0.0 },
- { 0.0, 1.0 }
- }, false);
- LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
- public double[] value(double[] variables) {
- return factors.operate(variables);
- }
- }, new double[] { 2.0, -3.0 }, new Array2DRowRealMatrix(new double [][] {
- { 1.0, 1.2 }, { 1.2, 2.0 }
- }));
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
- optimizer.setMaxIterations(200);
- RealPointValuePair optimum =
- optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
- assertEquals( 2.0, optimum.getPointRef()[0], 2.0e-3);
- assertEquals(-3.0, optimum.getPointRef()[1], 8.0e-4);
- assertTrue(optimizer.getEvaluations() > 60);
- assertTrue(optimizer.getEvaluations() < 80);
- assertTrue(optimum.getValue() < 1.0e-6);
- }
-
- @Test(expected = MaxIterationsExceededException.class)
- public void testMaxIterations() throws MathException {
- try {
- Powell powell = new Powell();
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
- optimizer.setMaxIterations(20);
- optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
- } catch (OptimizationException oe) {
- if (oe.getCause() instanceof ConvergenceException) {
- throw (ConvergenceException) oe.getCause();
- }
- throw oe;
- }
- }
-
- @Test(expected = MaxEvaluationsExceededException.class)
- public void testMaxEvaluations() throws MathException {
- try {
- Powell powell = new Powell();
- NelderMead optimizer = new NelderMead();
- optimizer.setConvergenceChecker(new SimpleRealPointChecker(-1.0, 1.0e-3));
- optimizer.setMaxEvaluations(20);
- optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
- } catch (FunctionEvaluationException fee) {
- if (fee.getCause() instanceof ConvergenceException) {
- throw (ConvergenceException) fee.getCause();
- }
- throw fee;
- }
- }
-
- private static class Rosenbrock implements MultivariateRealFunction {
-
- private int count;
-
- public Rosenbrock() {
- count = 0;
- }
-
- public double value(double[] x) throws FunctionEvaluationException {
- ++count;
- double a = x[1] - x[0] * x[0];
- double b = 1.0 - x[0];
- return 100 * a * a + b * b;
- }
-
- public int getCount() {
- return count;
- }
-
- }
-
- private static class Powell implements MultivariateRealFunction {
-
- private int count;
-
- public Powell() {
- count = 0;
- }
-
- public double value(double[] x) throws FunctionEvaluationException {
- ++count;
- double a = x[0] + 10 * x[1];
- double b = x[2] - x[3];
- double c = x[1] - 2 * x[2];
- double d = x[0] - x[3];
- return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
- }
-
- public int getCount() {
- return count;
- }
-
- }
+ };
+ try {
+ NelderMead optimizer = new NelderMead(0.9, 1.9, 0.4, 0.6);
+ optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { -1.0 });
+ fail("an exception should have been thrown");
+ } catch (FunctionEvaluationException ce) {
+ // expected behavior
+ assertNull(ce.getCause());
+ } catch (Exception e) {
+ fail("wrong exception caught: " + e.getMessage());
+ }
+ try {
+ NelderMead optimizer = new NelderMead(0.9, 1.9, 0.4, 0.6);
+ optimizer.optimize(wrong, GoalType.MINIMIZE, new double[] { +2.0 });
+ fail("an exception should have been thrown");
+ } catch (FunctionEvaluationException ce) {
+ // expected behavior
+ assertNotNull(ce.getCause());
+ } catch (Exception e) {
+ fail("wrong exception caught: " + e.getMessage());
+ }
+ }
+
+ @Test
+ public void testMinimizeMaximize()
+ throws FunctionEvaluationException {
+
+ // the following function has 4 local extrema:
+ final double xM = -3.841947088256863675365;
+ final double yM = -1.391745200270734924416;
+ final double xP = 0.2286682237349059125691;
+ final double yP = -yM;
+ final double valueXmYm = 0.2373295333134216789769; // local maximum
+ final double valueXmYp = -valueXmYm; // local minimum
+ final double valueXpYm = -0.7290400707055187115322; // global minimum
+ final double valueXpYp = -valueXpYm; // global maximum
+ MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
+ private static final long serialVersionUID = -7039124064449091152L;
+ public double value(double[] variables) throws FunctionEvaluationException {
+ final double x = variables[0];
+ final double y = variables[1];
+ return ((x == 0) || (y == 0)) ? 0 : (Math.atan(x) * Math.atan(x + 2) * Math.atan(y) * Math.atan(y) / (x * y));
+ }
+ };
+ NelderMead optimizer = new NelderMead();
+ optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-10, 1.0e-30));
+ optimizer.setMaxEvaluations(100);
+ optimizer.setStartConfiguration(new double[] { 0.2, 0.2 });
+ RealPointValuePair optimum;
+
+ // minimization
+ optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { -3.0, 0 });
+ assertEquals(xM, optimum.getPoint()[0], 2.0e-7);
+ assertEquals(yP, optimum.getPoint()[1], 2.0e-5);
+ assertEquals(valueXmYp, optimum.getValue(), 6.0e-12);
+ assertTrue(optimizer.getEvaluations() > 60);
+ assertTrue(optimizer.getEvaluations() < 90);
+
+ optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { +1, 0 });
+ assertEquals(xP, optimum.getPoint()[0], 5.0e-6);
+ assertEquals(yM, optimum.getPoint()[1], 6.0e-6);
+ assertEquals(valueXpYm, optimum.getValue(), 1.0e-11);
+ assertTrue(optimizer.getEvaluations() > 60);
+ assertTrue(optimizer.getEvaluations() < 90);
+
+ // maximization
+ optimum = optimizer.optimize(fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
+ assertEquals(xM, optimum.getPoint()[0], 1.0e-5);
+ assertEquals(yM, optimum.getPoint()[1], 3.0e-6);
+ assertEquals(valueXmYm, optimum.getValue(), 3.0e-12);
+ assertTrue(optimizer.getEvaluations() > 60);
+ assertTrue(optimizer.getEvaluations() < 90);
+
+ optimum = optimizer.optimize(fourExtrema, GoalType.MAXIMIZE, new double[] { +1, 0 });
+ assertEquals(xP, optimum.getPoint()[0], 4.0e-6);
+ assertEquals(yP, optimum.getPoint()[1], 5.0e-6);
+ assertEquals(valueXpYp, optimum.getValue(), 7.0e-12);
+ assertTrue(optimizer.getEvaluations() > 60);
+ assertTrue(optimizer.getEvaluations() < 90);
+ }
+
+ @Test
+ public void testRosenbrock()
+ throws FunctionEvaluationException {
+
+ Rosenbrock rosenbrock = new Rosenbrock();
+ NelderMead optimizer = new NelderMead();
+ optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
+ optimizer.setMaxEvaluations(100);
+ optimizer.setStartConfiguration(new double[][] {
+ { -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 }
+ });
+ RealPointValuePair optimum =
+ optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });
+
+ assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
+ assertTrue(optimizer.getEvaluations() > 40);
+ assertTrue(optimizer.getEvaluations() < 50);
+ assertTrue(optimum.getValue() < 8.0e-4);
+ }
+
+ @Test
+ public void testPowell()
+ throws FunctionEvaluationException {
+
+ Powell powell = new Powell();
+ NelderMead optimizer = new NelderMead();
+ optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
+ optimizer.setMaxEvaluations(200);
+ RealPointValuePair optimum =
+ optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
+ assertEquals(powell.getCount(), optimizer.getEvaluations());
+ assertTrue(optimizer.getEvaluations() > 110);
+ assertTrue(optimizer.getEvaluations() < 130);
+ assertTrue(optimum.getValue() < 2.0e-3);
+ }
+
+ @Test
+ public void testLeastSquares1()
+ throws FunctionEvaluationException {
+
+ final RealMatrix factors =
+ new Array2DRowRealMatrix(new double[][] {
+ { 1.0, 0.0 },
+ { 0.0, 1.0 }
+ }, false);
+ LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
+ public double[] value(double[] variables) {
+ return factors.operate(variables);
+ }
+ }, new double[] { 2.0, -3.0 });
+ NelderMead optimizer = new NelderMead();
+ optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
+ optimizer.setMaxEvaluations(200);
+ RealPointValuePair optimum =
+ optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
+ assertEquals( 2.0, optimum.getPointRef()[0], 3.0e-5);
+ assertEquals(-3.0, optimum.getPointRef()[1], 4.0e-4);
+ assertTrue(optimizer.getEvaluations() > 60);
+ assertTrue(optimizer.getEvaluations() < 80);
+ assertTrue(optimum.getValue() < 1.0e-6);
+ }
+
+ @Test
+ public void testLeastSquares2()
+ throws FunctionEvaluationException {
+
+ final RealMatrix factors =
+ new Array2DRowRealMatrix(new double[][] {
+ { 1.0, 0.0 },
+ { 0.0, 1.0 }
+ }, false);
+ LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
+ public double[] value(double[] variables) {
+ return factors.operate(variables);
+ }
+ }, new double[] { 2.0, -3.0 }, new double[] { 10.0, 0.1 });
+ NelderMead optimizer = new NelderMead();
+ optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
+ optimizer.setMaxEvaluations(200);
+ RealPointValuePair optimum =
+ optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
+ assertEquals( 2.0, optimum.getPointRef()[0], 5.0e-5);
+ assertEquals(-3.0, optimum.getPointRef()[1], 8.0e-4);
+ assertTrue(optimizer.getEvaluations() > 60);
+ assertTrue(optimizer.getEvaluations() < 80);
+ assertTrue(optimum.getValue() < 1.0e-6);
+ }
+
+ @Test
+ public void testLeastSquares3()
+ throws FunctionEvaluationException {
+
+ final RealMatrix factors =
+ new Array2DRowRealMatrix(new double[][] {
+ { 1.0, 0.0 },
+ { 0.0, 1.0 }
+ }, false);
+ LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
+ public double[] value(double[] variables) {
+ return factors.operate(variables);
+ }
+ }, new double[] { 2.0, -3.0 }, new Array2DRowRealMatrix(new double [][] {
+ { 1.0, 1.2 }, { 1.2, 2.0 }
+ }));
+ NelderMead optimizer = new NelderMead();
+ optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
+ optimizer.setMaxEvaluations(200);
+ RealPointValuePair optimum =
+ optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
+ assertEquals( 2.0, optimum.getPointRef()[0], 2.0e-3);
+ assertEquals(-3.0, optimum.getPointRef()[1], 8.0e-4);
+ assertTrue(optimizer.getEvaluations() > 60);
+ assertTrue(optimizer.getEvaluations() < 80);
+ assertTrue(optimum.getValue() < 1.0e-6);
+ }
+
+ @Test(expected = TooManyEvaluationsException.class)
+ public void testMaxIterations() throws FunctionEvaluationException {
+ Powell powell = new Powell();
+ NelderMead optimizer = new NelderMead();
+ optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
+ optimizer.setMaxEvaluations(20);
+ optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
+ }
+
+ private static class Rosenbrock implements MultivariateRealFunction {
+ private int count;
+
+ public Rosenbrock() {
+ count = 0;
+ }
+
+ public double value(double[] x) throws FunctionEvaluationException {
+ ++count;
+ double a = x[1] - x[0] * x[0];
+ double b = 1.0 - x[0];
+ return 100 * a * a + b * b;
+ }
+
+ public int getCount() {
+ return count;
+ }
+ }
+
+ private static class Powell implements MultivariateRealFunction {
+ private int count;
+
+ public Powell() {
+ count = 0;
+ }
+
+ public double value(double[] x) throws FunctionEvaluationException {
+ ++count;
+ double a = x[0] + 10 * x[1];
+ double b = x[2] - x[3];
+ double c = x[1] - 2 * x[2];
+ double d = x[0] - x[3];
+ return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
+ }
+
+ public int getCount() {
+ return count;
+ }
+ }
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java Mon Aug 30 13:06:22 2010
@@ -23,8 +23,8 @@ import static org.junit.Assert.assertTru
import java.util.Random;
import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
+import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
-import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.general.GaussNewtonOptimizer;
import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;
import org.apache.commons.math.util.FastMath;
@@ -33,7 +33,7 @@ import org.junit.Test;
public class PolynomialFitterTest {
@Test
- public void testNoError() throws OptimizationException {
+ public void testNoError() {
Random randomizer = new Random(64925784252l);
for (int degree = 1; degree < 10; ++degree) {
PolynomialFunction p = buildRandomPolynomial(degree, randomizer);
@@ -57,7 +57,7 @@ public class PolynomialFitterTest {
}
@Test
- public void testSmallError() throws OptimizationException {
+ public void testSmallError() {
Random randomizer = new Random(53882150042l);
double maxError = 0;
for (int degree = 0; degree < 10; ++degree) {
@@ -116,7 +116,7 @@ public class PolynomialFitterTest {
try {
fitter.fit();
assertTrue(solvable || (degree == 0));
- } catch(OptimizationException e) {
+ } catch(ConvergenceException e) {
assertTrue((! solvable) && (degree > 0));
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java?rev=990792&r1=990791&r2=990792&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java Mon Aug 30 13:06:22 2010
@@ -25,11 +25,13 @@ import java.util.Arrays;
import junit.framework.TestCase;
import org.apache.commons.math.FunctionEvaluationException;
+import org.apache.commons.math.exception.ConvergenceException;
+import org.apache.commons.math.exception.TooManyEvaluationsException;
+import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
import org.apache.commons.math.analysis.MultivariateMatrixFunction;
import org.apache.commons.math.linear.BlockRealMatrix;
import org.apache.commons.math.linear.RealMatrix;
-import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.SimpleVectorialPointChecker;
import org.apache.commons.math.optimization.SimpleVectorialValueChecker;
import org.apache.commons.math.optimization.VectorialPointValuePair;
@@ -104,11 +106,11 @@ extends TestCase {
super(name);
}
- public void testTrivial() throws FunctionEvaluationException, OptimizationException {
+ public void testTrivial() throws FunctionEvaluationException {
LinearProblem problem =
new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum =
optimizer.optimize(problem, problem.target, new double[] { 1 }, new double[] { 0 });
@@ -117,14 +119,14 @@ extends TestCase {
assertEquals(3.0, optimum.getValue()[0], 1.0e-10);
}
- public void testColumnsPermutation() throws FunctionEvaluationException, OptimizationException {
+ public void testColumnsPermutation() throws FunctionEvaluationException {
LinearProblem problem =
new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } },
new double[] { 4.0, 6.0, 1.0 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum =
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0 });
@@ -137,7 +139,7 @@ extends TestCase {
}
- public void testNoDependency() throws FunctionEvaluationException, OptimizationException {
+ public void testNoDependency() throws FunctionEvaluationException {
LinearProblem problem = new LinearProblem(new double[][] {
{ 2, 0, 0, 0, 0, 0 },
{ 0, 2, 0, 0, 0, 0 },
@@ -147,7 +149,7 @@ extends TestCase {
{ 0, 0, 0, 0, 0, 2 }
}, new double[] { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum =
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1, 1, 1, 1 },
@@ -158,7 +160,7 @@ extends TestCase {
}
}
- public void testOneSet() throws FunctionEvaluationException, OptimizationException {
+ public void testOneSet() throws FunctionEvaluationException {
LinearProblem problem = new LinearProblem(new double[][] {
{ 1, 0, 0 },
@@ -166,7 +168,7 @@ extends TestCase {
{ 0, -1, 1 }
}, new double[] { 1, 1, 1});
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum =
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
@@ -177,7 +179,7 @@ extends TestCase {
}
- public void testTwoSets() throws FunctionEvaluationException, OptimizationException {
+ public void testTwoSets() throws FunctionEvaluationException {
double epsilon = 1.0e-7;
LinearProblem problem = new LinearProblem(new double[][] {
{ 2, 1, 0, 4, 0, 0 },
@@ -189,7 +191,7 @@ extends TestCase {
}, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2});
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum =
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1, 1, 1, 1 },
@@ -212,19 +214,19 @@ extends TestCase {
{ -3, 0, -9 }
}, new double[] { 1, 1, 1 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
try {
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
fail("an exception should have been caught");
- } catch (OptimizationException ee) {
+ } catch (ConvergenceException ee) {
// expected behavior
} catch (Exception e) {
fail("wrong exception type caught");
}
}
- public void testIllConditioned() throws FunctionEvaluationException, OptimizationException {
+ public void testIllConditioned() throws FunctionEvaluationException {
LinearProblem problem1 = new LinearProblem(new double[][] {
{ 10.0, 7.0, 8.0, 7.0 },
{ 7.0, 5.0, 6.0, 5.0 },
@@ -232,7 +234,7 @@ extends TestCase {
{ 7.0, 5.0, 9.0, 10.0 }
}, new double[] { 32, 23, 33, 31 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum1 =
optimizer.optimize(problem1, problem1.target, new double[] { 1, 1, 1, 1 },
@@ -269,13 +271,13 @@ extends TestCase {
}, new double[] { 7.0, 3.0, 5.0 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
try {
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 },
new double[] { 7, 6, 5, 4 });
fail("an exception should have been caught");
- } catch (OptimizationException ee) {
+ } catch (ConvergenceException ee) {
// expected behavior
} catch (Exception e) {
fail("wrong exception type caught");
@@ -292,20 +294,20 @@ extends TestCase {
{ 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 }
}, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
try {
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1, 1, 1 },
new double[] { 2, 2, 2, 2, 2, 2 });
fail("an exception should have been caught");
- } catch (OptimizationException ee) {
+ } catch (ConvergenceException ee) {
// expected behavior
} catch (Exception e) {
fail("wrong exception type caught");
}
}
- public void testRedundantEquations() throws FunctionEvaluationException, OptimizationException {
+ public void testRedundantEquations() throws FunctionEvaluationException {
LinearProblem problem = new LinearProblem(new double[][] {
{ 1.0, 1.0 },
{ 1.0, -1.0 },
@@ -313,7 +315,7 @@ extends TestCase {
}, new double[] { 3.0, 1.0, 5.0 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum =
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 },
@@ -324,7 +326,7 @@ extends TestCase {
}
- public void testInconsistentEquations() throws FunctionEvaluationException, OptimizationException {
+ public void testInconsistentEquations() throws FunctionEvaluationException {
LinearProblem problem = new LinearProblem(new double[][] {
{ 1.0, 1.0 },
{ 1.0, -1.0 },
@@ -332,18 +334,18 @@ extends TestCase {
}, new double[] { 3.0, 1.0, 4.0 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 1, 1 });
assertTrue(optimizer.getRMS() > 0.1);
}
- public void testInconsistentSizes() throws FunctionEvaluationException, OptimizationException {
+ public void testInconsistentSizes() throws FunctionEvaluationException {
LinearProblem problem =
new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 });
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
VectorialPointValuePair optimum =
@@ -357,7 +359,7 @@ extends TestCase {
new double[] { 1 },
new double[] { 0, 0 });
fail("an exception should have been thrown");
- } catch (OptimizationException oe) {
+ } catch (DimensionMismatchException oe) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
@@ -376,7 +378,7 @@ extends TestCase {
}
- public void testMaxIterations() {
+ public void testMaxEvaluations() {
Circle circle = new Circle();
circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0);
@@ -384,21 +386,21 @@ extends TestCase {
circle.addPoint( 35.0, 15.0);
circle.addPoint( 45.0, 97.0);
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialPointChecker(1.0e-30, 1.0e-30));
try {
optimizer.optimize(circle, new double[] { 0, 0, 0, 0, 0 },
new double[] { 1, 1, 1, 1, 1 },
new double[] { 98.680, 47.345 });
fail("an exception should have been caught");
- } catch (OptimizationException ee) {
+ } catch (TooManyEvaluationsException ee) {
// expected behavior
} catch (Exception e) {
fail("wrong exception type caught");
}
}
- public void testCircleFitting() throws FunctionEvaluationException, OptimizationException {
+ public void testCircleFitting() throws FunctionEvaluationException {
Circle circle = new Circle();
circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0);
@@ -406,7 +408,7 @@ extends TestCase {
circle.addPoint( 35.0, 15.0);
circle.addPoint( 45.0, 97.0);
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-13, 1.0e-13));
VectorialPointValuePair optimum =
optimizer.optimize(circle, new double[] { 0, 0, 0, 0, 0 },
@@ -419,7 +421,7 @@ extends TestCase {
assertEquals(48.135167894714, center.y, 1.0e-10);
}
- public void testCircleFittingBadInit() throws FunctionEvaluationException, OptimizationException {
+ public void testCircleFittingBadInit() throws FunctionEvaluationException {
Circle circle = new Circle();
double[][] points = new double[][] {
{-0.312967, 0.072366}, {-0.339248, 0.132965}, {-0.379780, 0.202724},
@@ -460,12 +462,12 @@ extends TestCase {
circle.addPoint(points[i][0], points[i][1]);
}
GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true);
- optimizer.setMaxIterations(100);
+ optimizer.setMaxEvaluations(100);
optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
try {
optimizer.optimize(circle, target, weights, new double[] { -12, -12 });
fail("an exception should have been caught");
- } catch (OptimizationException ee) {
+ } catch (ConvergenceException ee) {
// expected behavior
} catch (Exception e) {
fail("wrong exception type caught");