Class ConjugateGradientMultiOptimizer


public class ConjugateGradientMultiOptimizer extends LineMultiOptimizer
This class searches for a multi dimension function local minimum. The local minimum is searched by starting the algorithm at a start point and a given direction which should be close and point to the local minimum to be found to achieve the best accuracy with the lowest number of iterations. NOTE: this algorithm might not have proper convergence in some situations, but it is ensured to provide faster convergence than other algorithms such as Brent because gradient information is also provided. The implementation of this class is based on Numerical Recipes 3rd ed. Section 10.8 page 515.
  • Field Details

    • DEFAULT_TOLERANCE

      public static final double DEFAULT_TOLERANCE
      Constant defining default tolerance or accuracy to be achieved on the minimum being estimated by this class.
      See Also:
    • MIN_TOLERANCE

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

      public static final int ITMAX
      Maximum allowed iterations.
      See Also:
    • EPS

      public static final double EPS
      Constant defining a value to be considered as machine precision.
      See Also:
    • GTOL

      public static final double GTOL
      Convergence criterion for the zero gradient test.
      See Also:
    • DEFAULT_USE_POLAK_RIBIERE

      public static final boolean DEFAULT_USE_POLAK_RIBIERE
      Defines whether Polak-Ribiere is used if true, otherwise Fletcher-Reeves will be used.
      See Also:
    • tolerance

      private double tolerance
      The fractional tolerance in the function value such that failure to decrease by more than this amount on one iteration signals done-ness.
    • iter

      private int iter
      Member contains number of iterations that were needed to estimate a minimum.
    • fret

      private double fret
      Value of the function at the minimum.
    • gradientListener

      private GradientFunctionEvaluatorListener gradientListener
      Listener to obtain gradient values for the multi dimension function being evaluated. If the gradient is unknown (e.g. doesn't have a closed expression), the provided listener could use a GradientEstimator to obtain one.
    • usePolakRibiere

      private boolean usePolakRibiere
      Boolean indicating whether Polak-Ribiere method is used if true, otherwise Fletcher-Reeves will be used.
  • Constructor Details

    • ConjugateGradientMultiOptimizer

      public ConjugateGradientMultiOptimizer()
      Empty constructor.
    • ConjugateGradientMultiOptimizer

      public ConjugateGradientMultiOptimizer(MultiDimensionFunctionEvaluatorListener listener, GradientFunctionEvaluatorListener gradientListener, double[] point, double[] direction, double tolerance, boolean usePolakRibiere)
      Constructor.
      Parameters:
      listener - Listener to evaluate a multi-dimension function.
      gradientListener - Listener to obtain gradient value for the multi-dimension function being evaluated.
      point - Start point where algorithm will be started. Start point should be close to the local minimum to be found. Provided array must have a length equal to the number of dimensions of the function being evaluated, otherwise and exception will be raised when searching for the minimum.
      direction - Direction to start looking for a minimum. Provided array must have the same length as the number of dimensions of the function being evaluated. Provided direction is considered as a vector pointing to the minimum to be found.
      tolerance - Tolerance or accuracy to be expected on estimated local minimum.
      usePolakRibiere - True if Polak-Ribiere method is used, otherwise Fletcher-Reeves will be used.
      Throws:
      IllegalArgumentException - Raised if provided point and direction don't have the same length or if provided tolerance is negative.
    • ConjugateGradientMultiOptimizer

      public ConjugateGradientMultiOptimizer(MultiDimensionFunctionEvaluatorListener listener, GradientFunctionEvaluatorListener gradientListener, double[] point, double tolerance, boolean usePolakRibiere)
      Constructor.
      Parameters:
      listener - Listener to evaluate a multidimensional function.
      gradientListener - Listener to obtain gradient value for the multidimensional function being evaluated.
      point - Start point where algorithm will be started. Start point should be close to the local minimum to be found. Provided array must have a length equal to the number of dimensions of the function being evaluated, otherwise and exception will be raised when searching for the minimum.
      tolerance - Tolerance or accuracy to be expected on estimated local minimum.
      usePolakRibiere - True if Polak-Ribiere method is used, otherwise Fletcher-Reeves will be used.
      Throws:
      IllegalArgumentException - Raised if tolerance is negative.
  • Method Details

    • minimize

      public void minimize() throws LockedException, NotReadyException, OptimizationException
      This function estimates a function minimum. Implementations of this class will usually search a local minimum close to a start point and will start looking into provided start direction. Minimization of a function f. Input consists of an initial starting point p. The initial matrix xi, whose columns contain the initial set of directions, is set to the identity. Returned is the best point found, at which point fret is the minimum function value and iter is the number of iterations taken.
      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 a listener, a gradient listener and a start point haven't been provided.
      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 local minimum. An instance is ready once a listener, a gradient listener and a start point are provided.
      Overrides:
      isReady in class LineMultiOptimizer
      Returns:
      True if this instance is ready, false otherwise.
    • getTolerance

      public double getTolerance()
      Returns tolerance or accuracy to be expected on estimated local minimum.
      Returns:
      Tolerance or accuracy to be expected on estimated local minimum.
    • setTolerance

      public void setTolerance(double tolerance) throws LockedException
      Sets tolerance or accuracy to be expected on estimated local minimum.
      Parameters:
      tolerance - Tolerance or accuracy to be expected on estimated local minimum.
      Throws:
      LockedException - Raised if this instance is locked.
      IllegalArgumentException - Raised if provided tolerance is negative.
    • getGradientListener

      public GradientFunctionEvaluatorListener getGradientListener() throws NotAvailableException
      Returns gradient listener in charge of obtaining gradient values for the function to be evaluated.
      Returns:
      Gradient listener.
      Throws:
      NotAvailableException - Raised if gradient listener has not yet been provided.
    • setGradientListener

      public void setGradientListener(GradientFunctionEvaluatorListener gradientListener) throws LockedException
      Sets gradient listener in charge of obtaining gradient values for the function to be evaluated.
      Parameters:
      gradientListener - Gradient listener.
      Throws:
      LockedException - Raised if this instance is locked.
    • isGradientListenerAvailable

      public boolean isGradientListenerAvailable()
      Returns boolean indicating whether a gradient listener has already been provided and is available for retrieval.
      Returns:
      True if available, false otherwise.
    • isPolakRibiereEnabled

      public boolean isPolakRibiereEnabled()
      Returns boolean indicating whether Polak-Ribiere method is used or Fletcher-Reeves is used instead.
      Returns:
      If true, Polak-Ribiere method is used, otherwise Fletcher-Reeves is used.
    • setUsePolakRibiere

      public void setUsePolakRibiere(boolean useIt) throws LockedException
      Sets boolean indicating whether Polak-Ribiere method or Fletcher-Reeves method is used. If provided value is true, Polak-Ribiere method will be used, otherwise Flecther-Reeves method will be used.
      Parameters:
      useIt - Boolean to determine method.
      Throws:
      LockedException - Raised if this instance is locked.
    • setStartPoint

      public void setStartPoint(double[] point) throws LockedException
      Sets start point where local minimum is searched nearby.
      Parameters:
      point - Start point to search for a local minimum.
      Throws:
      LockedException - Raised if this instance is locked.
    • getIterations

      public int getIterations()
      Return number of iterations that were needed to estimate a minimum.
      Returns:
      number of iterations that were needed.
    • internalSetTolerance

      private void internalSetTolerance(double tolerance)
      Internal method to set tolerance or accuracy to be expected on estimated local minimum. This method does not check whether this instance is locked.
      Parameters:
      tolerance - Tolerance or accuracy to be expected on estimated local minimum.
      Throws:
      IllegalArgumentException - Raised if provided tolerance is negative.
    • internalSetStartPoint

      private void internalSetStartPoint(double[] point)
      Internal method to set start point where local minimum is searched nearby. This method does not check whether this instance is locked.
      Parameters:
      point - Start point to search for a local minimum.