You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/03/24 23:14:05 UTC
svn commit: r758056 - in /commons/proper/math/trunk/src:
java/org/apache/commons/math/optimization/univariate/
test/org/apache/commons/math/optimization/univariate/
Author: luc
Date: Tue Mar 24 22:14:03 2009
New Revision: 758056
URL: http://svn.apache.org/viewvc?rev=758056&view=rev
Log:
updated univariate algorithms with latest interfaces definitions
Added:
commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java
- copied, changed from r757188, commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/UnivariateRealMinimizerImpl.java
commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java
- copied, changed from r757188, commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentMinimizer.java
Removed:
commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentMinimizer.java
Modified:
commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/univariate/BrentMinimizerTest.java
Copied: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java (from r757188, commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/UnivariateRealMinimizerImpl.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java?p2=commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java&p1=commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/UnivariateRealMinimizerImpl.java&r1=757188&r2=758056&rev=758056&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/UnivariateRealMinimizerImpl.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/AbstractUnivariateRealOptimizer.java Tue Mar 24 22:14:03 2009
@@ -19,22 +19,23 @@
import org.apache.commons.math.ConvergingAlgorithmImpl;
import org.apache.commons.math.MathRuntimeException;
+import org.apache.commons.math.optimization.UnivariateRealOptimizer;
/**
* Provide a default implementation for several functions useful to generic
- * minimizers.
+ * optimizers.
*
* @version $Revision$ $Date$
* @since 2.0
*/
-public abstract class UnivariateRealMinimizerImpl
- extends ConvergingAlgorithmImpl implements UnivariateRealMinimizer {
+public abstract class AbstractUnivariateRealOptimizer
+ extends ConvergingAlgorithmImpl implements UnivariateRealOptimizer {
/** Serializable version identifier. */
private static final long serialVersionUID = 4543031162377070699L;
/** Indicates where a root has been computed. */
- protected boolean resultComputed = false;
+ protected boolean resultComputed;
/** The last computed root. */
protected double result;
@@ -50,9 +51,10 @@
* @throws IllegalArgumentException if f is null or the
* defaultAbsoluteAccuracy is not valid
*/
- protected UnivariateRealMinimizerImpl(int defaultMaximalIterationCount,
- double defaultAbsoluteAccuracy) {
+ protected AbstractUnivariateRealOptimizer(final int defaultMaximalIterationCount,
+ final double defaultAbsoluteAccuracy) {
super(defaultMaximalIterationCount, defaultAbsoluteAccuracy);
+ resultComputed = false;
}
/** Check if a result has been computed.
@@ -82,8 +84,8 @@
* @param result the result to set
* @param iterationCount the iteration count to set
*/
- protected final void setResult(double result, int iterationCount) {
- this.result = result;
+ protected final void setResult(final double result, final int iterationCount) {
+ this.result = result;
this.iterationCount = iterationCount;
this.resultComputed = true;
}
@@ -95,9 +97,10 @@
* @param fx the result to set
* @param iterationCount the iteration count to set
*/
- protected final void setResult(double x, double fx, int iterationCount) {
- this.result = x;
- this.functionValue = fx;
+ protected final void setResult(final double x, final double fx,
+ final int iterationCount) {
+ this.result = x;
+ this.functionValue = fx;
this.iterationCount = iterationCount;
this.resultComputed = true;
}
Copied: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java (from r757188, commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentMinimizer.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java?p2=commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java&p1=commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentMinimizer.java&r1=757188&r2=758056&rev=758056&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentMinimizer.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java Tue Mar 24 22:14:03 2009
@@ -19,6 +19,7 @@
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.MaxIterationsExceededException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.optimization.GoalType;
/**
* Implements Richard Brent's algorithm (from his book "Algorithms for
@@ -28,7 +29,7 @@
* @version $Revision$ $Date$
* @since 2.0
*/
-public class BrentMinimizer extends UnivariateRealMinimizerImpl {
+public class BrentOptimizer extends AbstractUnivariateRealOptimizer {
/** Serializable version identifier */
private static final long serialVersionUID = 7185472920191999565L;
@@ -41,40 +42,23 @@
/**
* Construct a solver.
*/
- public BrentMinimizer() {
+ public BrentOptimizer() {
super(100, 1E-10);
}
- /**
- * Find a minimum in the given interval, start at startValue.
- * <p>
- * A minimizer may require that the interval brackets a single minimum.
- * </p>
- * @param f the function to minimize.
- * @param min the lower bound for the interval.
- * @param max the upper bound for the interval.
- * @param startValue this parameter is <em>not</em> used at all
- * @return a value where the function is minimum
- * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
- * or the minimizer 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 minimizer
- */
- public double minimize(final UnivariateRealFunction f,
+ /** {@inheritDoc} */
+ public double optimize(final UnivariateRealFunction f, final GoalType goalType,
final double min, final double max, final double startValue)
throws MaxIterationsExceededException, FunctionEvaluationException {
- return minimize(f, min, max);
+ return optimize(f, goalType, min, max);
}
/** {@inheritDoc} */
- public double minimize(final UnivariateRealFunction f,
+ public double optimize(final UnivariateRealFunction f, final GoalType goalType,
final double min, final double max)
- throws MaxIterationsExceededException,
- FunctionEvaluationException {
+ throws MaxIterationsExceededException, FunctionEvaluationException {
clearResult();
- return localMin(min, max, relativeAccuracy, absoluteAccuracy, f);
+ return localMin(f, goalType, min, max, relativeAccuracy, absoluteAccuracy);
}
/**
@@ -88,26 +72,30 @@
* {@code eps} should be no smaller than <em>2 macheps</em> and preferable not
* much less than <em>sqrt(macheps)</em>, where <em>macheps</em> is the relative
* machine precision. {@code t} should be positive.
- *
* @param f the function to solve
- * @param a Lower bound of the interval.
- * @param b Higher bound of the interval.
- * @param eps Relative accuracy.
- * @param t Absolute accuracy.
+ * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
+ * or {@link GoalType#MINIMIZE}
+ * @param a Lower bound of the interval
+ * @param b Higher bound of the interval
+ * @param eps Relative accuracy
+ * @param t Absolute accuracy
* @return the point at which the function is minimal.
* @throws MaxIterationsExceededException if the maximum iteration count
* is exceeded.
* @throws FunctionEvaluationException if an error occurs evaluating
* the function.
*/
- private double localMin(double a, double b, final double eps,
- final double t, final UnivariateRealFunction f)
+ private double localMin(final UnivariateRealFunction f, final GoalType goalType,
+ double a, double b, final double eps, final double t)
throws MaxIterationsExceededException, FunctionEvaluationException {
double x = a + c * (b - a);
double v = x;
double w = x;
double e = 0;
double fx = f.value(x);
+ if (goalType == GoalType.MAXIMIZE) {
+ fx = -fx;
+ }
double fv = fx;
double fw = fx;
@@ -158,6 +146,9 @@
// f must not be evaluated too close to a or b.
u = x + ((Math.abs(d) > tol) ? d : ((d > 0) ? tol : -tol));
double fu = f.value(u);
+ if (goalType == GoalType.MAXIMIZE) {
+ fu = -fu;
+ }
// Update a, b, v, w and x.
if (fu <= fx) {
@@ -188,8 +179,8 @@
fv = fu;
}
}
- } else { // Termination.
- setResult(x, fx, count);
+ } else { // termination
+ setResult(x, (goalType == GoalType.MAXIMIZE) ? -fx : fx, count);
return x;
}
Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/univariate/BrentMinimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/univariate/BrentMinimizerTest.java?rev=758056&r1=758055&r2=758056&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/univariate/BrentMinimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/univariate/BrentMinimizerTest.java Tue Mar 24 22:14:03 2009
@@ -24,6 +24,8 @@
import org.apache.commons.math.analysis.QuinticFunction;
import org.apache.commons.math.analysis.SinFunction;
import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.optimization.GoalType;
+import org.apache.commons.math.optimization.UnivariateRealOptimizer;
/**
* @version $Revision$ $Date$
@@ -36,16 +38,16 @@
public static Test suite() {
TestSuite suite = new TestSuite(BrentMinimizerTest.class);
- suite.setName("BrentMinimizer Tests");
+ suite.setName("BrentOptimizer Tests");
return suite;
}
public void testSinMin() throws MathException {
UnivariateRealFunction f = new SinFunction();
- UnivariateRealMinimizer minimizer = new BrentMinimizer();
- assertEquals(3 * Math.PI / 2, minimizer.minimize(f, 4, 5), 70 * minimizer.getAbsoluteAccuracy());
+ UnivariateRealOptimizer minimizer = new BrentOptimizer();
+ assertEquals(3 * Math.PI / 2, minimizer.optimize(f, GoalType.MINIMIZE, 4, 5), 70 * minimizer.getAbsoluteAccuracy());
assertTrue(minimizer.getIterationCount() <= 50);
- assertEquals(3 * Math.PI / 2, minimizer.minimize(f, 1, 5), 70 * minimizer.getAbsoluteAccuracy());
+ assertEquals(3 * Math.PI / 2, minimizer.optimize(f, GoalType.MINIMIZE, 1, 5), 70 * minimizer.getAbsoluteAccuracy());
assertTrue(minimizer.getIterationCount() <= 50);
}
@@ -53,26 +55,26 @@
// The quintic function has zeros at 0, +-0.5 and +-1.
// The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
UnivariateRealFunction f = new QuinticFunction();
- UnivariateRealMinimizer minimizer = new BrentMinimizer();
- assertEquals(-0.27195613, minimizer.minimize(f, -0.3, -0.2), 1.0e-8);
- assertEquals( 0.82221643, minimizer.minimize(f, 0.3, 0.9), 1.0e-8);
+ UnivariateRealOptimizer minimizer = new BrentOptimizer();
+ assertEquals(-0.27195613, minimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2), 1.0e-8);
+ assertEquals( 0.82221643, minimizer.optimize(f, GoalType.MINIMIZE, 0.3, 0.9), 1.0e-8);
assertTrue(minimizer.getIterationCount() <= 50);
// search in a large interval
- assertEquals(-0.27195613, minimizer.minimize(f, -1.0, 0.2), 1.0e-8);
+ assertEquals(-0.27195613, minimizer.optimize(f, GoalType.MINIMIZE, -1.0, 0.2), 1.0e-8);
assertTrue(minimizer.getIterationCount() <= 50);
}
public void testMinEndpoints() throws Exception {
UnivariateRealFunction f = new SinFunction();
- UnivariateRealMinimizer solver = new BrentMinimizer();
+ UnivariateRealOptimizer solver = new BrentOptimizer();
// endpoint is minimum
- double result = solver.minimize(f, 3 * Math.PI / 2, 5);
+ double result = solver.optimize(f, GoalType.MINIMIZE, 3 * Math.PI / 2, 5);
assertEquals(3 * Math.PI / 2, result, 70 * solver.getAbsoluteAccuracy());
- result = solver.minimize(f, 4, 3 * Math.PI / 2);
+ result = solver.optimize(f, GoalType.MINIMIZE, 4, 3 * Math.PI / 2);
assertEquals(3 * Math.PI / 2, result, 70 * solver.getAbsoluteAccuracy());
}