Class BrentOptimizer
(lo, hi)
, this class
finds an approximation x
to the point at which the function
attains its minimum.
It implements Richard Brent's algorithm (from his book "Algorithms for
Minimization without Derivatives", p. 79) for finding minima of real
univariate functions.
This code is an adaptation, partly based on the Python code from SciPy (module "optimize.py" v0.5); the original algorithm is also modified:
- to use an initial guess provided by the user,
- to ensure that the best point encountered is the one returned.
This class has been extracted from o.a.c.math4.optim.univariate
and simplified to remove support for the UnivariateOptimizer interface.
This removed the options: to find the maximum; use a custom convergence checker
on the function value; and remove the maximum function evaluation count.
The class now implements a single optimize method within the provided bracket
from the given start position (with value).
- Since:
- 1.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
This class holds a point and the value of an objective function at this point. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final double
Absolute threshold.private int
The number of function evaluations from the most recent call to optimize.private static final double
Golden section.private static final double
Minimum relative tolerance.private final double
Relative threshold. -
Constructor Summary
ConstructorsConstructorDescriptionBrentOptimizer
(double rel, double abs) The arguments are used to implement the original stopping criterion of Brent's algorithm. -
Method Summary
Modifier and TypeMethodDescription(package private) int
Gets the number of function evaluations from the most recent call tooptimize
.(package private) BrentOptimizer.PointValuePair
optimize
(DoubleUnaryOperator func, double lo, double hi, double mid, double fMid) Search for the minimum inside the provided interval.
-
Field Details
-
GOLDEN_SECTION
private static final double GOLDEN_SECTIONGolden section. (3 - sqrt(5)) / 2.- See Also:
-
MIN_RELATIVE_TOLERANCE
private static final double MIN_RELATIVE_TOLERANCEMinimum relative tolerance. 2 * eps = 2^-51.- See Also:
-
relativeThreshold
private final double relativeThresholdRelative threshold. -
absoluteThreshold
private final double absoluteThresholdAbsolute threshold. -
evaluations
private int evaluationsThe number of function evaluations from the most recent call to optimize.
-
-
Constructor Details
-
BrentOptimizer
BrentOptimizer(double rel, double abs) The arguments are used to implement the original stopping criterion of Brent's algorithm.abs
andrel
define a tolerancetol = rel |x| + abs
.rel
should be no smaller than 2 macheps and preferably not much less than sqrt(macheps), where macheps is the relative machine precision.abs
must be positive.- Parameters:
rel
- Relative threshold.abs
- Absolute threshold.- Throws:
IllegalArgumentException
- ifabs <= 0
; or ifrel < 2 * Math.ulp(1.0)
-
-
Method Details
-
getEvaluations
int getEvaluations()Gets the number of function evaluations from the most recent call tooptimize
.- Returns:
- the function evaluations
-
optimize
BrentOptimizer.PointValuePair optimize(DoubleUnaryOperator func, double lo, double hi, double mid, double fMid) Search for the minimum inside the provided interval. The bracket must satisfy the equalitieslo < mid < hi
orhi < mid < lo
.Note: This function accepts the initial guess and the function value at that point. This is done for convenience as this internal class is used where the caller already knows the function value.
- Parameters:
func
- Function to solve.lo
- Lower bound of the search interval.hi
- Higher bound of the search interval.mid
- Start point.fMid
- Function value at the start point.- Returns:
- the value where the function is minimum.
- Throws:
IllegalArgumentException
- if start point is not within the search intervalIllegalStateException
- if the maximum number of iterations is exceeded
-