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
This commit is contained in:
Gilles Sadowski 2010-12-04 11:31:12 +00:00
parent 848c37ee8b
commit 4d4aa195fe
18 changed files with 154 additions and 121 deletions

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -34,6 +34,8 @@ import org.apache.commons.math.analysis.UnivariateRealFunction;
*/
public abstract class BaseAbstractUnivariateRealSolver<FUNC extends UnivariateRealFunction>
implements BaseUnivariateRealSolver<FUNC> {
/** 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<FUNC extends UnivariateRe
this.functionValueAccuracy = functionValueAccuracy;
}
/** {@inheritDoc} */
public void setMaxEvaluations(int maxEvaluations) {
evaluations.setMaximalCount(maxEvaluations);
}
/** {@inheritDoc} */
public int getMaxEvaluations() {
return evaluations.getMaximalCount();
@ -167,8 +165,10 @@ public abstract class BaseAbstractUnivariateRealSolver<FUNC extends UnivariateRe
* @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.
*/
protected void setup(FUNC f,
protected void setup(int maxEval,
FUNC f,
double min, double max,
double startValue) {
// Checks.
@ -181,26 +181,42 @@ public abstract class BaseAbstractUnivariateRealSolver<FUNC extends UnivariateRe
searchMax = max;
searchStart = startValue;
function = f;
evaluations.setMaximalCount(maxEval);
evaluations.resetCount();
}
/** {@inheritDoc} */
public double solve(FUNC f, double min, double max, double startValue) {
return solve(DEFAULT_MAX_EVALUATIONS, f, min, max, startValue);
}
/** {@inheritDoc} */
public double solve(FUNC f, double min, double max) {
return solve(DEFAULT_MAX_EVALUATIONS, f, min, max);
}
/** {@inheritDoc} */
public double solve(FUNC f, double startValue) {
return solve(DEFAULT_MAX_EVALUATIONS, f, startValue);
}
/** {@inheritDoc} */
public double solve(int maxEval, FUNC f, double min, double max, double startValue) {
// Initialization.
setup(f, min, max, startValue);
setup(maxEval, f, min, max, startValue);
// Perform computation.
return doSolve();
}
/** {@inheritDoc} */
public double solve(FUNC f, double min, double max) {
return solve(f, min, max, min + 0.5 * (max - min));
public double solve(int maxEval, FUNC f, double min, double max) {
return solve(maxEval, f, min, max, min + 0.5 * (max - min));
}
/** {@inheritDoc} */
public double solve(FUNC f, double startValue) {
return solve(f, Double.NaN, Double.NaN, startValue);
public double solve(int maxEval, FUNC f, double startValue) {
return solve(maxEval, f, Double.NaN, Double.NaN, startValue);
}
/**

View File

@ -29,13 +29,6 @@ import org.apache.commons.math.analysis.UnivariateRealFunction;
* @since 3.0
*/
public interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction> {
/**
* 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<FUNC extends UnivariateRealFunction> {
* @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<FUNC extends UnivariateRealFunction> {
* @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);
}

View File

@ -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);
}
/**

View File

@ -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

View File

@ -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) {

View File

@ -52,6 +52,10 @@ The <action> type attribute can be add,update,fix,remove.
If the output is not quite correct, check for invisible trailing spaces!
-->
<release version="3.0" date="TBD" description="TBD">
<action dev="erans" type="fix" issue="MATH-451">
For "solvers" (package "analysis.solvers"), the number of allowed function
evaluations is passed as a parameter to the "solve" method.
</action>
<action dev="erans" type="fix" issue="MATH-447">
Created a "MathRuntimeException" to serve as a base class for exception
types that need to wrap another (lower-level) exception.

View File

@ -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());
}
}

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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());

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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 =

View File

@ -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);