From 4d4aa195fea0f1d7550ca816e1a85802aebfc875 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sat, 4 Dec 2010 11:31:12 +0000 Subject: [PATCH] MATH-451 In solvers, the maximum number of evaluations is passed as a parameter to the method "solve". The "setMaxEvaluations" accessor is removed. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1042151 13f79535-47bb-0310-9956-ffa450edef68 --- ...actDifferentiableUnivariateRealSolver.java | 7 +- .../solvers/AbstractPolynomialSolver.java | 7 +- .../BaseAbstractUnivariateRealSolver.java | 36 ++++++--- .../solvers/BaseUnivariateRealSolver.java | 80 +++++++++++++++---- .../solvers/UnivariateRealSolverUtils.java | 6 +- .../commons/math/ode/events/EventState.java | 5 +- .../NonLinearConjugateGradientOptimizer.java | 19 +++-- src/site/xdoc/changes.xml | 4 + .../analysis/solvers/BisectionSolverTest.java | 16 +--- .../analysis/solvers/BrentSolverTest.java | 31 +++---- .../analysis/solvers/LaguerreSolverTest.java | 14 ++-- .../analysis/solvers/MullerSolver2Test.java | 8 +- .../analysis/solvers/MullerSolverTest.java | 8 +- .../analysis/solvers/NewtonSolverTest.java | 2 - .../analysis/solvers/RiddersSolverTest.java | 8 +- .../analysis/solvers/SecantSolverTest.java | 18 ++--- ...entiableMultivariateRealOptimizerTest.java | 4 +- ...nLinearConjugateGradientOptimizerTest.java | 2 +- 18 files changed, 154 insertions(+), 121 deletions(-) diff --git a/src/main/java/org/apache/commons/math/analysis/solvers/AbstractDifferentiableUnivariateRealSolver.java b/src/main/java/org/apache/commons/math/analysis/solvers/AbstractDifferentiableUnivariateRealSolver.java index f1e82c892..37c92159e 100644 --- a/src/main/java/org/apache/commons/math/analysis/solvers/AbstractDifferentiableUnivariateRealSolver.java +++ b/src/main/java/org/apache/commons/math/analysis/solvers/AbstractDifferentiableUnivariateRealSolver.java @@ -72,10 +72,9 @@ public abstract class AbstractDifferentiableUnivariateRealSolver * {@inheritDoc} */ @Override - protected void setup(DifferentiableUnivariateRealFunction f, - double min, double max, - double startValue) { - super.setup(f, min, max, startValue); + protected void setup(int maxEval, DifferentiableUnivariateRealFunction f, + double min, double max, double startValue) { + super.setup(maxEval, f, min, max, startValue); functionDerivative = f.derivative(); } } diff --git a/src/main/java/org/apache/commons/math/analysis/solvers/AbstractPolynomialSolver.java b/src/main/java/org/apache/commons/math/analysis/solvers/AbstractPolynomialSolver.java index 8961bcd8d..c5e66d5c7 100644 --- a/src/main/java/org/apache/commons/math/analysis/solvers/AbstractPolynomialSolver.java +++ b/src/main/java/org/apache/commons/math/analysis/solvers/AbstractPolynomialSolver.java @@ -66,10 +66,9 @@ public abstract class AbstractPolynomialSolver * {@inheritDoc} */ @Override - protected void setup(PolynomialFunction f, - double min, double max, - double startValue) { - super.setup(f, min, max, startValue); + protected void setup(int maxEval, PolynomialFunction f, + double min, double max, double startValue) { + super.setup(maxEval, f, min, max, startValue); polynomialFunction = f; } diff --git a/src/main/java/org/apache/commons/math/analysis/solvers/BaseAbstractUnivariateRealSolver.java b/src/main/java/org/apache/commons/math/analysis/solvers/BaseAbstractUnivariateRealSolver.java index 39b6cde52..567580377 100644 --- a/src/main/java/org/apache/commons/math/analysis/solvers/BaseAbstractUnivariateRealSolver.java +++ b/src/main/java/org/apache/commons/math/analysis/solvers/BaseAbstractUnivariateRealSolver.java @@ -34,6 +34,8 @@ import org.apache.commons.math.analysis.UnivariateRealFunction; */ public abstract class BaseAbstractUnivariateRealSolver implements BaseUnivariateRealSolver { + /** Default maximum number of evaluations */ + public static final int DEFAULT_MAX_EVALUATIONS = 100; /** Default absolute accuracy */ public static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** Default relative accuracy. */ @@ -96,10 +98,6 @@ public abstract class BaseAbstractUnivariateRealSolver { - /** - * 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. * @@ -76,9 +69,10 @@ public interface BaseUnivariateRealSolver { * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @return a value where the function is zero. - * @throws IllegalArgumentException if {@code min > max} or the endpoints - * do not satisfy the requirements specified by the solver. - * @since 2.0 + * @throws org.apache.commons.math.exception.MathIllegalArgumentException + * if the arguments do not satisfy the requirements specified by the solver. + * @throws org.apache.commons.math.exception.TooManyEvaluationsException if + * the default allowed number of evaluations is exceeded. */ double solve(FUNC f, double min, double max); @@ -93,22 +87,74 @@ public interface BaseUnivariateRealSolver { * @param max Upper bound for the interval. * @param startValue Start value to use. * @return a value where the function is zero. - * @throws IllegalArgumentException if {@code min > max} or the arguments - * do not satisfy the requirements specified by the solver. - * @since 2.0 + * @throws org.apache.commons.math.exception.MathIllegalArgumentException + * if the arguments do not satisfy the requirements specified by the solver. + * @throws org.apache.commons.math.exception.TooManyEvaluationsException if + * the default allowed number of evaluations is exceeded. */ double solve(FUNC f, double min, double max, double startValue); /** * Solve for a zero in the vicinity of {@code startValue}. - * A solver may require that the interval brackets a single zero root. * * @param f Function to solve. * @param startValue Start value to use. * @return a value where the function is zero. - * @throws IllegalArgumentException if {@code min > max} or the arguments - * do not satisfy the requirements specified by the solver. - * @since 2.0 + * @throws org.apache.commons.math.exception.MathIllegalArgumentException + * if the arguments do not satisfy the requirements specified by the solver. + * @throws org.apache.commons.math.exception.TooManyEvaluationsException if + * the default allowed number of evaluations is exceeded. */ double solve(FUNC f, double startValue); + + /** + * Solve for a zero root in the given interval. + * A solver may require that the interval brackets a single zero root. + * Solvers that do require bracketing should be able to handle the case + * where one of the endpoints is itself a root. + * + * @param f Function to solve. + * @param min Lower bound for the interval. + * @param max Upper bound for the interval. + * @param maxEval Maximum number of evaluations. + * @return a value where the function is zero. + * @throws org.apache.commons.math.exception.MathIllegalArgumentException + * if the arguments do not satisfy the requirements specified by the solver. + * @throws org.apache.commons.math.exception.TooManyEvaluationsException if + * the allowed number of evaluations is exceeded. + */ + double solve(int maxEval, FUNC f, double min, double max); + + /** + * Solve for a zero in the given interval, start at {@code startValue}. + * A solver may require that the interval brackets a single zero root. + * Solvers that do require bracketing should be able to handle the case + * where one of the endpoints is itself a root. + * + * @param f Function to solve. + * @param min Lower bound for the interval. + * @param max Upper bound for the interval. + * @param startValue Start value to use. + * @param maxEval Maximum number of evaluations. + * @return a value where the function is zero. + * @throws org.apache.commons.math.exception.MathIllegalArgumentException + * if the arguments do not satisfy the requirements specified by the solver. + * @throws org.apache.commons.math.exception.TooManyEvaluationsException if + * the allowed number of evaluations is exceeded. + */ + double solve(int maxEval, FUNC f, double min, double max, double startValue); + + /** + * Solve for a zero in the vicinity of {@code startValue}. + * + * @param f Function to solve. + * @param startValue Start value to use. + * @return a value where the function is zero. + * @param maxEval Maximum number of evaluations. + * @throws org.apache.commons.math.exception.MathIllegalArgumentException + * if the arguments do not satisfy the requirements specified by the solver. + * @throws org.apache.commons.math.exception.TooManyEvaluationsException if + * the allowed number of evaluations is exceeded. + */ + double solve(int maxEval, FUNC f, double startValue); } diff --git a/src/main/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java b/src/main/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java index cdd4ff547..4d84051e3 100644 --- a/src/main/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java +++ b/src/main/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtils.java @@ -51,8 +51,7 @@ public class UnivariateRealSolverUtils { throw new NullArgumentException(LocalizedFormats.FUNCTION); } final UnivariateRealSolver solver = new BrentSolver(); - solver.setMaxEvaluations(Integer.MAX_VALUE); - return solver.solve(function, x0, x1); + return solver.solve(Integer.MAX_VALUE, function, x0, x1); } /** @@ -75,8 +74,7 @@ public class UnivariateRealSolverUtils { throw new NullArgumentException(LocalizedFormats.FUNCTION); } final UnivariateRealSolver solver = new BrentSolver(absoluteAccuracy); - solver.setMaxEvaluations(Integer.MAX_VALUE); - return solver.solve(function, x0, x1); + return solver.solve(Integer.MAX_VALUE, function, x0, x1); } /** diff --git a/src/main/java/org/apache/commons/math/ode/events/EventState.java b/src/main/java/org/apache/commons/math/ode/events/EventState.java index 930b5136c..5a1a01ea9 100644 --- a/src/main/java/org/apache/commons/math/ode/events/EventState.java +++ b/src/main/java/org/apache/commons/math/ode/events/EventState.java @@ -253,8 +253,9 @@ public class EventState { } }; final BrentSolver solver = new BrentSolver(convergence); - solver.setMaxEvaluations(maxIterationCount); - final double root = (ta <= tb) ? solver.solve(f, ta, tb) : solver.solve(f, tb, ta); + final double root = (ta <= tb) ? + solver.solve(maxIterationCount, f, ta, tb) : + solver.solve(maxIterationCount, f, tb, ta); if ((FastMath.abs(root - ta) <= convergence) && (FastMath.abs(root - previousEventTime) <= convergence)) { // we have either found nothing or found (again ?) a past event, we simply ignore it diff --git a/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java b/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java index 8fe478cab..1a8ce266e 100644 --- a/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java +++ b/src/main/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.java @@ -18,7 +18,6 @@ package org.apache.commons.math.optimization.general; import org.apache.commons.math.exception.MathIllegalStateException; -import org.apache.commons.math.exception.MathUserException; import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.solvers.BrentSolver; import org.apache.commons.math.analysis.solvers.UnivariateRealSolver; @@ -85,7 +84,6 @@ public class NonLinearConjugateGradientOptimizer */ public void setLineSearchSolver(final UnivariateRealSolver lineSearchSolver) { solver = lineSearchSolver; - solver.setMaxEvaluations(getMaxEvaluations()); } /** @@ -108,14 +106,13 @@ public class NonLinearConjugateGradientOptimizer /** {@inheritDoc} */ @Override - protected RealPointValuePair doOptimize() throws MathUserException { + protected RealPointValuePair doOptimize() { // Initialization. if (preconditioner == null) { preconditioner = new IdentityPreconditioner(); } if (solver == null) { solver = new BrentSolver(); - solver.setMaxEvaluations(getMaxEvaluations()); } point = getStartPoint(); final GoalType goal = getGoalType(); @@ -138,6 +135,7 @@ public class NonLinearConjugateGradientOptimizer RealPointValuePair current = null; int iter = 0; + int maxEval = getMaxEvaluations(); while (true) { ++iter; @@ -162,7 +160,8 @@ public class NonLinearConjugateGradientOptimizer // XXX Last parameters is set to a value clode to zero in order to // work around the divergence problem in the "testCircleFitting" // unit test (see MATH-439). - final double step = solver.solve(lsf, 0, uB, 1e-15); + final double step = solver.solve(maxEval, lsf, 0, uB, 1e-15); + maxEval -= solver.getEvaluations(); // Subtract used up evaluations. // Validate new point. for (int i = 0; i < point.length; ++i) { @@ -217,12 +216,12 @@ public class NonLinearConjugateGradientOptimizer * @param a lower bound of the interval. * @param h initial step to try. * @return b such that f(a) and f(b) have opposite signs. - * @exception MathIllegalStateException if no bracket can be found. - * @exception MathUserException if function throws one. + * @throws MathIllegalStateException if no bracket can be found. + * @throws org.apache.commons.math.exception.MathUserException if the + * function throws one. */ private double findUpperBound(final UnivariateRealFunction f, - final double a, final double h) - throws MathUserException { + final double a, final double h) { final double yA = f.value(a); double yB = yA; for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) { @@ -265,7 +264,7 @@ public class NonLinearConjugateGradientOptimizer } /** {@inheritDoc} */ - public double value(double x) throws MathUserException { + public double value(double x) { // current point in the search direction final double[] shiftedPoint = point.clone(); for (int i = 0; i < shiftedPoint.length; ++i) { diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 20225647a..170503012 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -52,6 +52,10 @@ The type attribute can be add,update,fix,remove. If the output is not quite correct, check for invisible trailing spaces! --> + + For "solvers" (package "analysis.solvers"), the number of allowed function + evaluations is passed as a parameter to the "solve" method. + Created a "MathRuntimeException" to serve as a base class for exception types that need to wrap another (lower-level) exception. diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/BisectionSolverTest.java b/src/test/java/org/apache/commons/math/analysis/solvers/BisectionSolverTest.java index abbf22761..06762cb52 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/BisectionSolverTest.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/BisectionSolverTest.java @@ -33,7 +33,6 @@ public final class BisectionSolverTest { double result; BisectionSolver solver = new BisectionSolver(); - solver.setMaxEvaluations(50); result = solver.solve(f, 3, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); @@ -47,7 +46,6 @@ public final class BisectionSolverTest { double result; BisectionSolver solver = new BisectionSolver(); - solver.setMaxEvaluations(50); result = solver.solve(f, -0.2, 0.2); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); @@ -85,21 +83,9 @@ public final class BisectionSolverTest { } @Test - public void testMath369() throws Exception { + public void testMath369() { UnivariateRealFunction f = new SinFunction(); BisectionSolver solver = new BisectionSolver(); - solver.setMaxEvaluations(40); Assert.assertEquals(FastMath.PI, solver.solve(f, 3.0, 3.2, 3.1), solver.getAbsoluteAccuracy()); } - - /** - * - */ - @Test - public void testSetMaximalEvaluationCount(){ - int expected = 100; - BisectionSolver solver = new BisectionSolver(); - solver.setMaxEvaluations(expected); - Assert.assertEquals(expected, solver.getMaxEvaluations()); - } } diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java b/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java index 74cca8afc..07f7382a2 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/BrentSolverTest.java @@ -21,6 +21,9 @@ 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.util.FastMath; +import org.apache.commons.math.exception.NumberIsTooLargeException; +import org.apache.commons.math.exception.NoBracketingException; +import org.apache.commons.math.exception.TooManyEvaluationsException; import org.junit.Assert; import org.junit.Test; @@ -44,7 +47,6 @@ public final class BrentSolverTest { UnivariateRealFunction f = new SinFunction(); double result; UnivariateRealSolver solver = new BrentSolver(); - solver.setMaxEvaluations(10); // Somewhat benign interval. The function is monotone. result = solver.solve(f, 3, 4); // System.out.println( @@ -72,7 +74,6 @@ public final class BrentSolverTest { double result; // Brent-Dekker solver. UnivariateRealSolver solver = new BrentSolver(); - solver.setMaxEvaluations(20); // Symmetric bracket around 0. Test whether solvers can handle hitting // the root in the first iteration. result = solver.solve(f, -0.2, 0.2); @@ -145,13 +146,18 @@ public final class BrentSolverTest { // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 15); + + try { + result = solver.solve(5, f, 0.85, 5); + } catch (TooManyEvaluationsException e) { + // Expected. + } } @Test public void testRootEndpoints() { UnivariateRealFunction f = new SinFunction(); BrentSolver solver = new BrentSolver(); - solver.setMaxEvaluations(10); // endpoint is root double result = solver.solve(f, FastMath.PI, 4); @@ -165,30 +171,28 @@ public final class BrentSolverTest { result = solver.solve(f, 3, FastMath.PI, 3.07); Assert.assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy()); - } @Test public void testBadEndpoints() { UnivariateRealFunction f = new SinFunction(); BrentSolver solver = new BrentSolver(); - solver.setMaxEvaluations(10); try { // bad interval solver.solve(f, 1, -1); - Assert.fail("Expecting IllegalArgumentException - bad interval"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NumberIsTooLargeException - bad interval"); + } catch (NumberIsTooLargeException ex) { // expected } try { // no bracket solver.solve(f, 1, 1.5); - Assert.fail("Expecting IllegalArgumentException - non-bracketing"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NoBracketingException - non-bracketing"); + } catch (NoBracketingException ex) { // expected } try { // no bracket solver.solve(f, 1, 1.5, 1.2); - Assert.fail("Expecting IllegalArgumentException - non-bracketing"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NoBracketingException - non-bracketing"); + } catch (NoBracketingException ex) { // expected } } @@ -197,7 +201,6 @@ public final class BrentSolverTest { public void testInitialGuess() { MonitoredFunction f = new MonitoredFunction(new QuinticFunction()); BrentSolver solver = new BrentSolver(); - solver.setMaxEvaluations(20); double result; // no guess @@ -209,8 +212,8 @@ public final class BrentSolverTest { // invalid guess (it *is* a root, but outside of the range) try { result = solver.solve(f, 0.6, 7.0, 0.0); - Assert.fail("an IllegalArgumentException was expected"); - } catch (IllegalArgumentException iae) { + Assert.fail("a NumberIsTooLargeException was expected"); + } catch (NumberIsTooLargeException iae) { // expected behaviour } diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/LaguerreSolverTest.java b/src/test/java/org/apache/commons/math/analysis/solvers/LaguerreSolverTest.java index b6ef3f76a..30b404529 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/LaguerreSolverTest.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/LaguerreSolverTest.java @@ -21,6 +21,8 @@ import org.apache.commons.math.TestUtils; import org.apache.commons.math.analysis.SinFunction; import org.apache.commons.math.analysis.polynomials.PolynomialFunction; import org.apache.commons.math.complex.Complex; +import org.apache.commons.math.exception.NumberIsTooLargeException; +import org.apache.commons.math.exception.NoBracketingException; import org.apache.commons.math.util.FastMath; import org.junit.Assert; import org.junit.Test; @@ -48,7 +50,6 @@ public final class LaguerreSolverTest { double coefficients[] = { -1.0, 4.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); - solver.setMaxEvaluations(10); min = 0.0; max = 1.0; expected = 0.25; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), @@ -68,7 +69,6 @@ public final class LaguerreSolverTest { double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); - solver.setMaxEvaluations(10); min = 0.0; max = 2.0; expected = 0.5; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), @@ -94,7 +94,6 @@ public final class LaguerreSolverTest { double coefficients[] = { -12.0, -1.0, 1.0, -12.0, -1.0, 1.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); - solver.setMaxEvaluations(10); min = -2.0; max = 2.0; expected = -1.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), @@ -162,20 +161,19 @@ public final class LaguerreSolverTest { double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); - solver.setMaxEvaluations(10); try { // bad interval solver.solve(f, 1, -1); - Assert.fail("Expecting IllegalArgumentException - bad interval"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NumberIsTooLargeException - bad interval"); + } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(f, 2, 3); - Assert.fail("Expecting IllegalArgumentException - no bracketing"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NoBracketingException - no bracketing"); + } catch (NoBracketingException ex) { // expected } } diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolver2Test.java b/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolver2Test.java index 56cb297e3..a7617c257 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolver2Test.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolver2Test.java @@ -47,7 +47,6 @@ public final class MullerSolver2Test { public void testSinFunction() { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new MullerSolver2(); - solver.setMaxEvaluations(10); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; @@ -70,7 +69,6 @@ public final class MullerSolver2Test { public void testQuinticFunction() { UnivariateRealFunction f = new QuinticFunction(); UnivariateRealSolver solver = new MullerSolver2(); - solver.setMaxEvaluations(10); double min, max, expected, result, tolerance; min = -0.4; max = 0.2; expected = 0.0; @@ -101,7 +99,6 @@ public final class MullerSolver2Test { public void testExpm1Function() { UnivariateRealFunction f = new Expm1Function(); UnivariateRealSolver solver = new MullerSolver2(); - solver.setMaxEvaluations(55); double min, max, expected, result, tolerance; min = -1.0; max = 2.0; expected = 0.0; @@ -130,19 +127,18 @@ public final class MullerSolver2Test { public void testParameters() throws Exception { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new MullerSolver2(); - solver.setMaxEvaluations(10); try { // bad interval solver.solve(f, 1, -1); - Assert.fail("Expecting IllegalArgumentException - bad interval"); + Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(f, 2, 3); - Assert.fail("Expecting IllegalArgumentException - no bracketing"); + Assert.fail("Expecting NoBracketingException - no bracketing"); } catch (NoBracketingException ex) { // expected } diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolverTest.java b/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolverTest.java index 46e14e7ef..4a4e2f421 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolverTest.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/MullerSolverTest.java @@ -47,7 +47,6 @@ public final class MullerSolverTest { public void testSinFunction() { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new MullerSolver(); - solver.setMaxEvaluations(10); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; @@ -70,7 +69,6 @@ public final class MullerSolverTest { public void testQuinticFunction() { UnivariateRealFunction f = new QuinticFunction(); UnivariateRealSolver solver = new MullerSolver(); - solver.setMaxEvaluations(15); double min, max, expected, result, tolerance; min = -0.4; max = 0.2; expected = 0.0; @@ -103,7 +101,6 @@ public final class MullerSolverTest { public void testExpm1Function() { UnivariateRealFunction f = new Expm1Function(); UnivariateRealSolver solver = new MullerSolver(); - solver.setMaxEvaluations(25); double min, max, expected, result, tolerance; min = -1.0; max = 2.0; expected = 0.0; @@ -132,20 +129,19 @@ public final class MullerSolverTest { public void testParameters() throws Exception { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new MullerSolver(); - solver.setMaxEvaluations(10); try { // bad interval double root = solver.solve(f, 1, -1); System.out.println("root=" + root); - Assert.fail("Expecting IllegalArgumentException - bad interval"); + Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(f, 2, 3); - Assert.fail("Expecting IllegalArgumentException - no bracketing"); + Assert.fail("Expecting NoBracketingException - no bracketing"); } catch (NoBracketingException ex) { // expected } diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/NewtonSolverTest.java b/src/test/java/org/apache/commons/math/analysis/solvers/NewtonSolverTest.java index f59bf127f..8cb15bfa2 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/NewtonSolverTest.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/NewtonSolverTest.java @@ -37,7 +37,6 @@ public final class NewtonSolverTest { double result; NewtonSolver solver = new NewtonSolver(); - solver.setMaxEvaluations(10); result = solver.solve(f, 3, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); @@ -56,7 +55,6 @@ public final class NewtonSolverTest { double result; NewtonSolver solver = new NewtonSolver(); - solver.setMaxEvaluations(30); result = solver.solve(f, -0.2, 0.2); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/RiddersSolverTest.java b/src/test/java/org/apache/commons/math/analysis/solvers/RiddersSolverTest.java index 8f4a5adc5..b269744c3 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/RiddersSolverTest.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/RiddersSolverTest.java @@ -45,7 +45,6 @@ public final class RiddersSolverTest { public void testSinFunction() { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new RiddersSolver(); - solver.setMaxEvaluations(10); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; @@ -68,7 +67,6 @@ public final class RiddersSolverTest { public void testQuinticFunction() { UnivariateRealFunction f = new QuinticFunction(); UnivariateRealSolver solver = new RiddersSolver(); - solver.setMaxEvaluations(15); double min, max, expected, result, tolerance; min = -0.4; max = 0.2; expected = 0.0; @@ -97,7 +95,6 @@ public final class RiddersSolverTest { public void testExpm1Function() { UnivariateRealFunction f = new Expm1Function(); UnivariateRealSolver solver = new RiddersSolver(); - solver.setMaxEvaluations(20); double min, max, expected, result, tolerance; min = -1.0; max = 2.0; expected = 0.0; @@ -126,19 +123,18 @@ public final class RiddersSolverTest { public void testParameters() { UnivariateRealFunction f = new SinFunction(); UnivariateRealSolver solver = new RiddersSolver(); - solver.setMaxEvaluations(10); try { // bad interval solver.solve(f, 1, -1); - Assert.fail("Expecting IllegalArgumentException - bad interval"); + Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(f, 2, 3); - Assert.fail("Expecting IllegalArgumentException - no bracketing"); + Assert.fail("Expecting NoBracketingException - no bracketing"); } catch (NoBracketingException ex) { // expected } diff --git a/src/test/java/org/apache/commons/math/analysis/solvers/SecantSolverTest.java b/src/test/java/org/apache/commons/math/analysis/solvers/SecantSolverTest.java index 12b71e7af..0b0bbb316 100644 --- a/src/test/java/org/apache/commons/math/analysis/solvers/SecantSolverTest.java +++ b/src/test/java/org/apache/commons/math/analysis/solvers/SecantSolverTest.java @@ -20,6 +20,8 @@ import org.apache.commons.math.analysis.MonitoredFunction; 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.exception.NumberIsTooLargeException; +import org.apache.commons.math.exception.NoBracketingException; import org.apache.commons.math.util.FastMath; import org.junit.Assert; import org.junit.Test; @@ -38,7 +40,6 @@ public final class SecantSolverTest { UnivariateRealFunction f = new SinFunction(); double result; UnivariateRealSolver solver = new SecantSolver(); - solver.setMaxEvaluations(10); result = solver.solve(f, 3, 4); //System.out.println( @@ -65,7 +66,6 @@ public final class SecantSolverTest { double result; // Brent-Dekker solver. UnivariateRealSolver solver = new SecantSolver(); - solver.setMaxEvaluations(20); result = solver.solve(f, -0.2, 0.2); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); @@ -129,7 +129,6 @@ public final class SecantSolverTest { public void testRootEndpoints() { UnivariateRealFunction f = new SinFunction(); SecantSolver solver = new SecantSolver(); - solver.setMaxEvaluations(10); // endpoint is root double result = solver.solve(f, FastMath.PI, 4); @@ -150,23 +149,22 @@ public final class SecantSolverTest { public void testBadEndpoints() { UnivariateRealFunction f = new SinFunction(); SecantSolver solver = new SecantSolver(); - solver.setMaxEvaluations(10); try { // bad interval solver.solve(f, 1, -1); - Assert.fail("Expecting IllegalArgumentException - bad interval"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NumberIsTooLargeException - bad interval"); + } catch (NumberIsTooLargeException ex) { // expected } try { // no bracket solver.solve(f, 1, 1.5); - Assert.fail("Expecting IllegalArgumentException - non-bracketing"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NoBracketingException - non-bracketing"); + } catch (NoBracketingException ex) { // expected } try { // no bracket solver.solve(f, 1, 1.5, 1.2); - Assert.fail("Expecting IllegalArgumentException - non-bracketing"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting NoBracketingException - non-bracketing"); + } catch (NoBracketingException ex) { // expected } } diff --git a/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java b/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java index 9c3aa8756..66cfe01a0 100644 --- a/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java +++ b/src/test/java/org/apache/commons/math/optimization/MultiStartDifferentiableMultivariateRealOptimizerTest.java @@ -55,8 +55,8 @@ public class MultiStartDifferentiableMultivariateRealOptimizerTest { new GaussianRandomGenerator(g)); MultiStartDifferentiableMultivariateRealOptimizer optimizer = new MultiStartDifferentiableMultivariateRealOptimizer(underlying, 10, generator); - optimizer.setMaxEvaluations(100); - assertEquals(100, optimizer.getMaxEvaluations()); + optimizer.setMaxEvaluations(200); + assertEquals(200, optimizer.getMaxEvaluations()); optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-10, 1.0e-10)); BrentSolver solver = new BrentSolver(); RealPointValuePair optimum = diff --git a/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java b/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java index b8b84bb6e..aa7c3317a 100644 --- a/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java +++ b/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java @@ -235,7 +235,7 @@ public class NonLinearConjugateGradientOptimizerTest { }, new double[] { 32, 23, 33, 31 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE); - optimizer.setMaxEvaluations(100); + optimizer.setMaxEvaluations(200); optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-13, 1.0e-13)); BrentSolver solver = new BrentSolver(1e-15, 1e-15); optimizer.setLineSearchSolver(solver);