Class AbstractUnivariateRealOptimizer

java.lang.Object
org.apache.commons.math.ConvergingAlgorithmImpl
org.apache.commons.math.optimization.univariate.AbstractUnivariateRealOptimizer
All Implemented Interfaces:
ConvergingAlgorithm, UnivariateRealOptimizer
Direct Known Subclasses:
BrentOptimizer

public abstract class AbstractUnivariateRealOptimizer extends ConvergingAlgorithmImpl implements UnivariateRealOptimizer
Provide a default implementation for several functions useful to generic optimizers.
Since:
2.0
Version:
$Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
  • Field Details

    • resultComputed

      protected boolean resultComputed
      Indicates where a root has been computed.
    • result

      protected double result
      The last computed root.
    • functionValue

      protected double functionValue
      Value of the function at the last computed result.
  • Constructor Details

    • AbstractUnivariateRealOptimizer

      @Deprecated protected AbstractUnivariateRealOptimizer(int defaultMaximalIterationCount, double defaultAbsoluteAccuracy)
      Deprecated.
      in 2.2. Please use the "setter" methods to assign meaningful values to the maximum numbers of iterations and evaluations, and to the absolute and relative accuracy thresholds.
      Construct a solver with given iteration count and accuracy.
      Parameters:
      defaultMaximalIterationCount - maximum number of iterations
      defaultAbsoluteAccuracy - maximum absolute error
      Throws:
      IllegalArgumentException - if f is null or the defaultAbsoluteAccuracy is not valid
    • AbstractUnivariateRealOptimizer

      protected AbstractUnivariateRealOptimizer()
      Default constructor. To be removed once the single non-default one has been removed.
  • Method Details

    • checkResultComputed

      @Deprecated protected void checkResultComputed()
      Deprecated.
      in 2.2 (no alternative).
      Check whether a result has been computed.
      Throws:
      NoDataException - if no result has been computed
    • getResult

      public double getResult()
      Get the result of the last run of the optimizer.
      Specified by:
      getResult in interface UnivariateRealOptimizer
      Returns:
      the optimum.
    • getFunctionValue

      public double getFunctionValue() throws FunctionEvaluationException
      Get the result of the last run of the optimizer.
      Specified by:
      getFunctionValue in interface UnivariateRealOptimizer
      Returns:
      the value of the function at the optimum.
      Throws:
      FunctionEvaluationException - if an error occurs evaluating the function.
    • setResult

      @Deprecated protected final void setResult(double x, double fx, int iterationCount)
      Deprecated.
      in 2.2 (no alternative).
      Convenience function for implementations.
      Parameters:
      x - the result to set
      fx - the result to set
      iterationCount - the iteration count to set
    • clearResult

      @Deprecated protected final void clearResult()
      Deprecated.
      in 2.2 (no alternative).
      Convenience function for implementations.
    • setMaxEvaluations

      public void setMaxEvaluations(int maxEvaluations)
      Set the maximal number of functions evaluations.
      Specified by:
      setMaxEvaluations in interface UnivariateRealOptimizer
      Parameters:
      maxEvaluations - maximal number of function evaluations
    • getMaxEvaluations

      public int getMaxEvaluations()
      Get the maximal number of functions evaluations.
      Specified by:
      getMaxEvaluations in interface UnivariateRealOptimizer
      Returns:
      the maximal number of functions 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 UnivariateRealOptimizer
      Returns:
      the number of evaluations of the objective function.
    • getGoalType

      public GoalType getGoalType()
      Returns:
      the optimization type.
    • getMin

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

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

      public double getStartValue()
      Returns:
      the initial guess.
    • computeObjectiveValue

      @Deprecated protected double computeObjectiveValue(UnivariateRealFunction f, double point) throws FunctionEvaluationException
      Deprecated.
      in 2.2. Use this replacement instead.
      Compute the objective function value.
      Parameters:
      f - objective function
      point - point at which the objective function must be evaluated
      Returns:
      objective function value at specified point
      Throws:
      FunctionEvaluationException - if the function cannot be evaluated or the maximal number of iterations is exceeded
    • computeObjectiveValue

      protected double computeObjectiveValue(double point) throws FunctionEvaluationException
      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:
      FunctionEvaluationException - if the function cannot be evaluated or the maximal number of iterations is exceeded.
    • optimize

      public double optimize(UnivariateRealFunction f, GoalType goal, double min, double max, double startValue) throws MaxIterationsExceededException, FunctionEvaluationException
      Find an optimum in the given interval, start at startValue.

      An optimizer may require that the interval brackets a single optimum.

      Specified by:
      optimize in interface UnivariateRealOptimizer
      Parameters:
      f - the function to optimize.
      goal - type of optimization goal: either GoalType.MAXIMIZE or GoalType.MINIMIZE.
      min - the lower bound for the interval.
      max - the upper bound for the interval.
      startValue - the start value to use.
      Returns:
      a value where the function is optimum.
      Throws:
      FunctionEvaluationException - if an error occurs evaluating the function.
      MaxIterationsExceededException
    • setFunctionValue

      protected void setFunctionValue(double functionValue)
      Set the value at the optimum.
      Parameters:
      functionValue - Value of the objective function at the optimum.
    • optimize

      public double optimize(UnivariateRealFunction f, GoalType goal, double min, double max) throws MaxIterationsExceededException, FunctionEvaluationException
      Find an optimum in the given interval.

      An optimizer may require that the interval brackets a single optimum.

      Specified by:
      optimize in interface UnivariateRealOptimizer
      Parameters:
      f - the function to optimize.
      goal - type of optimization goal: either GoalType.MAXIMIZE or GoalType.MINIMIZE.
      min - the lower bound for the interval.
      max - the upper bound for the interval.
      Returns:
      a value where the function is optimum.
      Throws:
      FunctionEvaluationException - if an error occurs evaluating the function.
      MaxIterationsExceededException
    • doOptimize

      protected double doOptimize() throws MaxIterationsExceededException, FunctionEvaluationException
      Method for implementing actual optimization algorithms in derived classes. From version 3.0 onwards, this method will be abstract - i.e. concrete implementations will have to implement it. If this method is not implemented, subclasses must override optimize(UnivariateRealFunction, GoalType, double, double).
      Returns:
      the optimum.
      Throws:
      MaxIterationsExceededException - if the maximum iteration count is exceeded.
      FunctionEvaluationException - if an error occurs evaluating the function.