Package org.hipparchus.analysis.solvers
Class BaseAbstractUnivariateSolver<F extends UnivariateFunction>
- java.lang.Object
-
- org.hipparchus.analysis.solvers.BaseAbstractUnivariateSolver<F>
-
- Type Parameters:
F- Type of function to solve.
- All Implemented Interfaces:
BaseUnivariateSolver<F>
- Direct Known Subclasses:
AbstractPolynomialSolver,AbstractUnivariateDifferentiableSolver,AbstractUnivariateSolver
public abstract class BaseAbstractUnivariateSolver<F extends UnivariateFunction> extends Object implements BaseUnivariateSolver<F>
Provide a default implementation for several functions useful to generic solvers. The default values for relative and function tolerances are 1e-14 and 1e-15, respectively. It is however highly recommended to not rely on the default, but rather carefully consider values that match user's expectations, as well as the specifics of each implementation.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseAbstractUnivariateSolver(double absoluteAccuracy)Construct a solver with given absolute accuracy.protectedBaseAbstractUnivariateSolver(double relativeAccuracy, double absoluteAccuracy)Construct a solver with given accuracies.protectedBaseAbstractUnivariateSolver(double relativeAccuracy, double absoluteAccuracy, double functionValueAccuracy)Construct a solver with given accuracies.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected doublecomputeObjectiveValue(double point)Compute the objective function value.protected abstract doubledoSolve()Method for implementing actual optimization algorithms in derived classes.doublegetAbsoluteAccuracy()Get the absolute accuracy of the solver.intgetEvaluations()Get the number of evaluations of the objective function.doublegetFunctionValueAccuracy()Get the function value accuracy of the solver.doublegetMax()Get higher end of the search interval.doublegetMin()Get lower end of the search interval.doublegetRelativeAccuracy()Get the relative accuracy of the solver.doublegetStartValue()Get initial guess.protected voidincrementEvaluationCount()Increment the evaluation count by one.protected booleanisBracketing(double lower, double upper)Check whether the function takes opposite signs at the endpoints.protected booleanisSequence(double start, double mid, double end)Check whether the arguments form a (strictly) increasing sequence.protected voidsetup(int maxEval, F f, double min, double max, double startValue)Prepare for computation.doublesolve(int maxEval, F f, double startValue)Solve for a zero in the vicinity ofstartValue.doublesolve(int maxEval, F f, double min, double max)Solve for a zero root in the given interval.doublesolve(int maxEval, F f, double min, double max, double startValue)Solve for a zero in the given interval, start atstartValue.protected voidverifyBracketing(double lower, double upper)Check that the endpoints specify an interval and the function takes opposite signs at the endpoints.protected voidverifyInterval(double lower, double upper)Check that the endpoints specify an interval.protected voidverifySequence(double lower, double initial, double upper)Check thatlower < initial < upper.
-
-
-
Constructor Detail
-
BaseAbstractUnivariateSolver
protected BaseAbstractUnivariateSolver(double absoluteAccuracy)
Construct a solver with given absolute accuracy.- Parameters:
absoluteAccuracy- Maximum absolute error.
-
BaseAbstractUnivariateSolver
protected BaseAbstractUnivariateSolver(double relativeAccuracy, double absoluteAccuracy)Construct a solver with given accuracies.- Parameters:
relativeAccuracy- Maximum relative error.absoluteAccuracy- Maximum absolute error.
-
BaseAbstractUnivariateSolver
protected BaseAbstractUnivariateSolver(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
-
getEvaluations
public int getEvaluations()
Get the number of evaluations of the objective function. The number of evaluations corresponds to the last call to theoptimizemethod. It is 0 if the method has not been called yet.- Specified by:
getEvaluationsin interfaceBaseUnivariateSolver<F extends UnivariateFunction>- Returns:
- the number of evaluations of the objective function.
-
getMin
public double getMin()
Get lower end of the search interval.- Returns:
- the lower end of the search interval
-
getMax
public double getMax()
Get higher end of the search interval.- Returns:
- the higher end of the search interval
-
getStartValue
public double getStartValue()
Get initial guess.- Returns:
- the initial guess
-
getAbsoluteAccuracy
public double getAbsoluteAccuracy()
Get the absolute accuracy of the solver. Solutions returned by the solver should be accurate to this tolerance, i.e., if ε is the absolute accuracy of the solver andvis a value returned by one of thesolvemethods, then a root of the function should exist somewhere in the interval (v- ε,v+ ε).- Specified by:
getAbsoluteAccuracyin interfaceBaseUnivariateSolver<F extends UnivariateFunction>- Returns:
- the absolute accuracy.
-
getRelativeAccuracy
public double getRelativeAccuracy()
Get the relative accuracy of the solver. The contract for relative accuracy is the same asBaseUnivariateSolver.getAbsoluteAccuracy(), but using relative, rather than absolute error. If ρ is the relative accuracy configured for a solver andvis a value returned, then a root of the function should exist somewhere in the interval (v- ρv,v+ ρv).- Specified by:
getRelativeAccuracyin interfaceBaseUnivariateSolver<F extends UnivariateFunction>- Returns:
- the relative accuracy.
-
getFunctionValueAccuracy
public double getFunctionValueAccuracy()
Get the function value accuracy of the solver. Ifvis a value returned by the solver for a functionf, then by contract,|f(v)|should be less than or equal to the function value accuracy configured for the solver.- Specified by:
getFunctionValueAccuracyin interfaceBaseUnivariateSolver<F extends UnivariateFunction>- Returns:
- the function value accuracy.
-
computeObjectiveValue
protected double computeObjectiveValue(double point) throws MathIllegalStateExceptionCompute the objective function value.- Parameters:
point- Point at which the objective function must be evaluated.- Returns:
- the objective function value at specified point.
- Throws:
MathIllegalStateException- if the maximal number of evaluations is exceeded.
-
setup
protected void setup(int maxEval, F f, double min, double max, double startValue) throws NullArgumentExceptionPrepare for computation. Subclasses must call this method if they override any of thesolvemethods.- 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.- Throws:
NullArgumentException- if f is null
-
solve
public double solve(int maxEval, F f, double min, double max, double startValue) throws MathIllegalArgumentException, MathIllegalStateExceptionSolve for a zero in the given interval, start atstartValue. 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:
solvein interfaceBaseUnivariateSolver<F extends UnivariateFunction>- 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.
- Throws:
MathIllegalArgumentException- if the arguments do not satisfy the requirements specified by the solver.MathIllegalStateException- if the allowed number of evaluations is exceeded.
-
solve
public double solve(int maxEval, F 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:
solvein interfaceBaseUnivariateSolver<F extends UnivariateFunction>- 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, F f, double startValue) throws MathIllegalArgumentException, MathIllegalStateExceptionSolve for a zero in the vicinity ofstartValue.- Specified by:
solvein interfaceBaseUnivariateSolver<F extends UnivariateFunction>- Parameters:
maxEval- Maximum number of evaluations.f- Function to solve.startValue- Start value to use.- Returns:
- a value where the function is zero.
- Throws:
MathIllegalArgumentException- if the arguments do not satisfy the requirements specified by the solver.MathIllegalStateException- if the allowed number of evaluations is exceeded.
-
doSolve
protected abstract double doSolve() throws MathIllegalArgumentException, MathIllegalStateExceptionMethod for implementing actual optimization algorithms in derived classes.- Returns:
- the root.
- Throws:
MathIllegalStateException- if the maximal number of evaluations is exceeded.MathIllegalArgumentException- 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:
trueif 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:
trueif the arguments form an increasing sequence.
-
verifyInterval
protected void verifyInterval(double lower, double upper) throws MathIllegalArgumentExceptionCheck that the endpoints specify an interval.- Parameters:
lower- Lower endpoint.upper- Upper endpoint.- Throws:
MathIllegalArgumentException- iflower >= upper.
-
verifySequence
protected void verifySequence(double lower, double initial, double upper) throws MathIllegalArgumentExceptionCheck thatlower < initial < upper.- Parameters:
lower- Lower endpoint.initial- Initial value.upper- Upper endpoint.- Throws:
MathIllegalArgumentException- iflower >= initialorinitial >= upper.
-
verifyBracketing
protected void verifyBracketing(double lower, double upper) throws MathIllegalArgumentException, NullArgumentExceptionCheck that the endpoints specify an interval and the function takes opposite signs at the endpoints.- Parameters:
lower- Lower endpoint.upper- Upper endpoint.- Throws:
NullArgumentException- if the function has not been set.MathIllegalArgumentException- if the function has the same sign at the endpoints.
-
incrementEvaluationCount
protected void incrementEvaluationCount() throws MathIllegalStateExceptionIncrement the evaluation count by one. MethodcomputeObjectiveValue(double)calls this method internally. It is provided for subclasses that do not exclusively usecomputeObjectiveValueto solve the function. See e.g.AbstractUnivariateDifferentiableSolver.- Throws:
MathIllegalStateException- when the allowed number of function evaluations has been exhausted.
-
-