org.apache.commons.math.analysis.solvers
Class BaseAbstractUnivariateRealSolver<FUNC extends UnivariateRealFunction>

java.lang.Object
  extended by org.apache.commons.math.analysis.solvers.BaseAbstractUnivariateRealSolver<FUNC>
Type Parameters:
FUNC - Type of function to solve.
All Implemented Interfaces:
BaseUnivariateRealSolver<FUNC>
Direct Known Subclasses:
AbstractDifferentiableUnivariateRealSolver, AbstractPolynomialSolver, AbstractUnivariateRealSolver

public abstract class BaseAbstractUnivariateRealSolver<FUNC extends UnivariateRealFunction>
extends java.lang.Object
implements BaseUnivariateRealSolver<FUNC>

Provide a default implementation for several functions useful to generic solvers.

Since:
2.0
Version:
$Id$

Constructor Summary
protected BaseAbstractUnivariateRealSolver(double absoluteAccuracy)
          Construct a solver with given absolute accuracy.
protected BaseAbstractUnivariateRealSolver(double relativeAccuracy, double absoluteAccuracy)
          Construct a solver with given accuracies.
protected BaseAbstractUnivariateRealSolver(double relativeAccuracy, double absoluteAccuracy, double functionValueAccuracy)
          Construct a solver with given accuracies.
 
Method Summary
protected  double computeObjectiveValue(double point)
          Compute the objective function value.
protected abstract  double doSolve()
          Method for implementing actual optimization algorithms in derived classes.
 double getAbsoluteAccuracy()
          
 int getEvaluations()
          Get the number of evaluations of the objective function.
 double getFunctionValueAccuracy()
          
 double getMax()
           
 int getMaxEvaluations()
          Get the maximal number of function evaluations.
 double getMin()
           
 double getRelativeAccuracy()
          
 double getStartValue()
           
protected  void incrementEvaluationCount()
          Increment the evaluation count by one.
protected  boolean isBracketing(double lower, double upper)
          Check whether the function takes opposite signs at the endpoints.
protected  boolean isSequence(double start, double mid, double end)
          Check whether the arguments form a (strictly) increasing sequence.
protected  void setup(int maxEval, FUNC f, double min, double max, double startValue)
          Prepare for computation.
 double solve(int maxEval, FUNC f, double startValue)
          Solve for a zero in the vicinity of startValue.
 double solve(int maxEval, FUNC f, double min, double max)
          Solve for a zero root in the given interval.
 double solve(int maxEval, FUNC f, double min, double max, double startValue)
          Solve for a zero in the given interval, start at startValue.
protected  void verifyBracketing(double lower, double upper)
          Check that the endpoints specify an interval and the function takes opposite signs at the endpoints.
protected  void verifyInterval(double lower, double upper)
          Check that the endpoints specify an interval.
protected  void verifySequence(double lower, double initial, double upper)
          Check that lower < initial < upper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BaseAbstractUnivariateRealSolver

protected BaseAbstractUnivariateRealSolver(double absoluteAccuracy)
Construct a solver with given absolute accuracy.

Parameters:
absoluteAccuracy - Maximum absolute error.

BaseAbstractUnivariateRealSolver

protected BaseAbstractUnivariateRealSolver(double relativeAccuracy,
                                           double absoluteAccuracy)
Construct a solver with given accuracies.

Parameters:
relativeAccuracy - Maximum relative error.
absoluteAccuracy - Maximum absolute error.

BaseAbstractUnivariateRealSolver

protected BaseAbstractUnivariateRealSolver(double relativeAccuracy,
                                           double absoluteAccuracy,
                                           double functionValueAccuracy)
Construct a solver with given accuracies.

Parameters:
relativeAccuracy - Maximum relative error.
absoluteAccuracy - Maximum absolute error.
functionValueAccuracy - Maximum function value error.
Method Detail

getMaxEvaluations

public int getMaxEvaluations()
Get the maximal number of function evaluations.

Specified by:
getMaxEvaluations in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Returns:
the maximal number of function evaluations.

getEvaluations

public int getEvaluations()
Get the number of evaluations of the objective function. The number of evaluations corresponds to the last call to the optimize method. It is 0 if the method has not been called yet.

Specified by:
getEvaluations in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Returns:
the number of evaluations of the objective function.

getMin

public double getMin()
Returns:
the lower end of the search interval.

getMax

public double getMax()
Returns:
the higher end of the search interval.

getStartValue

public double getStartValue()
Returns:
the initial guess.

getAbsoluteAccuracy

public double getAbsoluteAccuracy()

Specified by:
getAbsoluteAccuracy in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Returns:
the absolute accuracy.

getRelativeAccuracy

public double getRelativeAccuracy()

Specified by:
getRelativeAccuracy in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Returns:
the relative accuracy.

getFunctionValueAccuracy

public double getFunctionValueAccuracy()

Specified by:
getFunctionValueAccuracy in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Returns:
the function value accuracy.

computeObjectiveValue

protected double computeObjectiveValue(double point)
                                throws TooManyEvaluationsException
Compute the objective function value.

Parameters:
point - Point at which the objective function must be evaluated.
Returns:
the objective function value at specified point.
Throws:
TooManyEvaluationsException - if the maximal number of evaluations is exceeded.

setup

protected void setup(int maxEval,
                     FUNC f,
                     double min,
                     double max,
                     double startValue)
Prepare for computation. Subclasses must call this method if they override any of the solve methods.

Parameters:
f - Function to solve.
min - Lower bound for the interval.
max - Upper bound for the interval.
startValue - Start value to use.
maxEval - Maximum number of evaluations.

solve

public double solve(int maxEval,
                    FUNC f,
                    double min,
                    double max,
                    double startValue)
Solve for a zero in the given interval, start at 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.

Specified by:
solve in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Parameters:
maxEval - Maximum number of evaluations.
f - Function to solve.
min - Lower bound for the interval.
max - Upper bound for the interval.
startValue - Start value to use.
Returns:
a value where the function is zero.

solve

public double solve(int maxEval,
                    FUNC f,
                    double min,
                    double max)
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.

Specified by:
solve in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Parameters:
maxEval - Maximum number of evaluations.
f - Function to solve.
min - Lower bound for the interval.
max - Upper bound for the interval.
Returns:
a value where the function is zero.

solve

public double solve(int maxEval,
                    FUNC f,
                    double startValue)
Solve for a zero in the vicinity of startValue.

Specified by:
solve in interface BaseUnivariateRealSolver<FUNC extends UnivariateRealFunction>
Parameters:
maxEval - Maximum number of evaluations.
f - Function to solve.
startValue - Start value to use.
Returns:
a value where the function is zero.

doSolve

protected abstract double doSolve()
                           throws TooManyEvaluationsException,
                                  NoBracketingException
Method for implementing actual optimization algorithms in derived classes.

Returns:
the root.
Throws:
TooManyEvaluationsException - if the maximal number of evaluations is exceeded.
NoBracketingException - if the initial search interval does not bracket a root and the solver requires it.

isBracketing

protected boolean isBracketing(double lower,
                               double upper)
Check whether the function takes opposite signs at the endpoints.

Parameters:
lower - Lower endpoint.
upper - Upper endpoint.
Returns:
true if the function values have opposite signs at the given points.

isSequence

protected boolean isSequence(double start,
                             double mid,
                             double end)
Check whether the arguments form a (strictly) increasing sequence.

Parameters:
start - First number.
mid - Second number.
end - Third number.
Returns:
true if the arguments form an increasing sequence.

verifyInterval

protected void verifyInterval(double lower,
                              double upper)
Check that the endpoints specify an interval.

Parameters:
lower - Lower endpoint.
upper - Upper endpoint.
Throws:
NumberIsTooLargeException - if lower >= upper.

verifySequence

protected void verifySequence(double lower,
                              double initial,
                              double upper)
Check that lower < initial < upper.

Parameters:
lower - Lower endpoint.
initial - Initial value.
upper - Upper endpoint.
Throws:
NumberIsTooLargeException - if lower >= initial or initial >= upper.

verifyBracketing

protected void verifyBracketing(double lower,
                                double upper)
Check that the endpoints specify an interval and the function takes opposite signs at the endpoints.

Parameters:
lower - Lower endpoint.
upper - Upper endpoint.
Throws:
NoBracketingException - if the function has the same sign at the endpoints.

incrementEvaluationCount

protected void incrementEvaluationCount()
Increment the evaluation count by one. Method computeObjectiveValue(double) calls this method internally. It is provided for subclasses that do not exclusively use computeObjectiveValue to solve the function. See e.g. AbstractDifferentiableUnivariateRealSolver.



Copyright © 2003-2011 The Apache Software Foundation. All Rights Reserved.