Class BrentSingleOptimizer


public class BrentSingleOptimizer extends BracketedSingleOptimizer
This class uses Brent algorithm to determine a local function minimum for single dimension functions. Brent's algorithm will search for a local minimum inside the provided or computed bracket of values. Brent's algorithm is not the fastest among all optimization algorithms, but it is usually one that provides good convergence for most continuous functions. It 's recommended to always set or compute a bracket of values, as the search range is reduced and results usually become more accurate. The implementation of this class is based on Numerical Recipes 3rd ed. Section 10.3. Page 496.
  • Field Details

    • ITMAX

      public static final int ITMAX
      Is the maximum allowed number of iterations.
      See Also:
    • CGOLD

      public static final double CGOLD
      Is the golden ratio.
      See Also:
    • ZEPS

      public static final double ZEPS
      Small number that protects against trying to achieve fractional accuracy for a minimum that happens to be exactly zero.
      See Also:
    • DEFAULT_TOLERANCE

      public static final double DEFAULT_TOLERANCE
      Constant defining the default accuracy of the estimated minimum.
      See Also:
    • MIN_TOLERANCE

      public static final double MIN_TOLERANCE
      Minimum allowed tolerance.
      See Also:
    • tolerance

      private double tolerance
      Tolerance value. The algorithm will iterate until the result converges below this value of accuracy or until the maximum number of iterations is achieved (and in such case, convergence will be assumed to have failed).
  • Constructor Details

    • BrentSingleOptimizer

      public BrentSingleOptimizer()
      Empty constructor.
    • BrentSingleOptimizer

      public BrentSingleOptimizer(double minEvalPoint, double middleEvalPoint, double maxEvalPoint, double tolerance) throws InvalidBracketRangeException
      Constructor. Creates an instance with provided bracket of values.
      Parameters:
      minEvalPoint - Minimum bracket evaluation point.
      middleEvalPoint - Middle bracket evaluation point.
      maxEvalPoint - Maximum bracket evaluation point.
      tolerance - Tolerance or accuracy to be obtained in estimated minimum.
      Throws:
      InvalidBracketRangeException - Raised if the following condition is not met: minEvalPoint <= middleEvalPoint <= maxEvalPoint.
      IllegalArgumentException - Raised if tolerance is negative.
    • BrentSingleOptimizer

      public BrentSingleOptimizer(SingleDimensionFunctionEvaluatorListener listener, double minEvalPoint, double middleEvalPoint, double maxEvalPoint, double tolerance) throws InvalidBracketRangeException
      Constructor. Creates an instance with provided bracket of values and a listener to get single dimension function evaluations.
      Parameters:
      listener - Listener to evaluate a function.
      minEvalPoint - Minimum bracket evaluation point.
      middleEvalPoint - Middle bracket evaluation point.
      maxEvalPoint - Maximum bracket evaluation point.
      tolerance - Tolerance or accuracy to be obtained in estimated minimum.
      Throws:
      InvalidBracketRangeException - Raised if the following condition is not met: minEvalPoint <= middleEvalPoint <= maxEvalPoint.
      IllegalArgumentException - Raised if tolerance is negative.
  • Method Details

    • getTolerance

      public double getTolerance()
      Returns tolerance value, which is the accuracy to be obtained when a minimum is estimated. The algorithm will iterate until the result converges below this value of accuracy or until the maximum number of iterations is achieved (and in such case, convergence will be assumed to have failed).
      Returns:
      Tolerance value.
    • setTolerance

      public void setTolerance(double tolerance) throws LockedException
      Sets algorithm's tolerance. The algorithm will iterate until the result converges below this value of accuracy or until the maximum number of iterations is achieved (an in such case, convergence will be assumed to have failed).
      Parameters:
      tolerance - Tolerance or accuracy to be obtained in estimated minimum.
      Throws:
      LockedException - Raised if this instance is locked. This instance will be locked while doing some operations. Attempting to change any parameter while being locked will raise this exception.
      IllegalArgumentException - Raised if tolerance is negative.
    • minimize

      public void minimize() throws LockedException, NotReadyException, OptimizationException
      This function estimates a function minimum within provided or computed bracket of values. Given a function f, and given a bracketing triplet of abscissas "ax", "bx", "cx" (such that bx is between ax and cx, and f(bx) is less than both f(ax) and f(cx), this routine isolates the minimum to a fractional prevision of about tolerance using Brent's method. The abscissa of the minimum is returned as "xmin", and the function value of the minimum is returned as "fmin", the returned function value.
      Overrides:
      minimize in class Optimizer
      Throws:
      LockedException - Raised if this instance is locked, because estimation is being computed.
      NotReadyException - Raised if this instance is not ready because either a listener or a bracket has not yet been provided or computed.
      OptimizationException - Raised if the algorithm failed because of lack of convergence or because function couldn't be evaluated.
    • isReady

      public boolean isReady()
      Returns boolean indicating whether this instance is ready to start the estimation of a minimum or not. The instance is ready when both the listener and the bracket are available.
      Overrides:
      isReady in class SingleOptimizer
      Returns:
      True if this instance is ready, false otherwise.
    • internalSetTolerance

      private void internalSetTolerance(double tolerance)
      Internal method to set algorithm tolerance. This method does not check whether this instance is locked or not. The algorithm will iterate until the result converges below this value of accuracy or until the maximum number of iterations is achieved (and in such case, convergence will be assumed to have failed).
      Parameters:
      tolerance - Tolerance or accuracy to be obtained in estimated minimum.
      Throws:
      IllegalArgumentException - Raised if tolerance is negative.