org.apache.commons.math.optimization.univariate

## 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 Summary

Fields
`private int` `evaluations` Number of evaluations already performed.
`private UnivariateRealFunction` `function` Function to optimize.
`protected double` `functionValue` Value of the function at the last computed result.
`private int` `maxEvaluations` Maximal number of evaluations allowed.
`private GoalType` `optimizationGoal` Optimization type
`protected double` `result` The last computed root.
`protected boolean` `resultComputed` Indicates where a root has been computed.
`private double` `searchMax` Higher end of search interval.
`private double` `searchMin` Lower end of search interval.
`private double` `searchStart` Initial guess .

### Fields inherited from class org.apache.commons.math.ConvergingAlgorithmImpl

`absoluteAccuracy, defaultAbsoluteAccuracy, defaultMaximalIterationCount, defaultRelativeAccuracy, iterationCount, maximalIterationCount, relativeAccuracy`

### Constructor Summary

Constructors
`protected` `AbstractUnivariateRealOptimizer()` Default constructor.
`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.

### Method Summary

Methods
`protected void` `checkResultComputed()` Deprecated. in 2.2 (no alternative).
`protected void` `clearResult()` Deprecated. in 2.2 (no alternative).
`protected double` `computeObjectiveValue(double point)` Compute the objective function value.
`protected double` `computeObjectiveValue(UnivariateRealFunction f, double point)` Deprecated. in 2.2. Use this `replacement` instead.
`protected double` `doOptimize()` Method for implementing actual optimization algorithms in derived classes.
`int` `getEvaluations()` Get the number of evaluations of the objective function.
`double` `getFunctionValue()` Get the result of the last run of the optimizer.
`GoalType` `getGoalType()`
`double` `getMax()`
`int` `getMaxEvaluations()` Get the maximal number of functions evaluations.
`double` `getMin()`
`double` `getResult()` Get the result of the last run of the optimizer.
`double` `getStartValue()`
`double` `optimize(UnivariateRealFunction f, GoalType goal, double min, double max)` Find an optimum in the given interval.
`double` `optimize(UnivariateRealFunction f, GoalType goal, double min, double max, double startValue)` Find an optimum in the given interval, start at startValue.
`protected void` `setFunctionValue(double functionValue)` Set the value at the optimum.
`void` `setMaxEvaluations(int maxEvaluations)` Set the maximal number of functions evaluations.
`protected void` `setResult(double x, double fx, int iterationCount)` Deprecated. in 2.2 (no alternative).

### Methods inherited from class org.apache.commons.math.ConvergingAlgorithmImpl

`getAbsoluteAccuracy, getIterationCount, getMaximalIterationCount, getRelativeAccuracy, incrementIterationsCounter, resetAbsoluteAccuracy, resetIterationsCounter, resetMaximalIterationCount, resetRelativeAccuracy, setAbsoluteAccuracy, setMaximalIterationCount, setRelativeAccuracy`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.apache.commons.math.ConvergingAlgorithm

`getAbsoluteAccuracy, getIterationCount, getMaximalIterationCount, getRelativeAccuracy, resetAbsoluteAccuracy, resetMaximalIterationCount, resetRelativeAccuracy, setAbsoluteAccuracy, setMaximalIterationCount, setRelativeAccuracy`

### Field Detail

#### 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.

#### maxEvaluations

    private int maxEvaluations

Maximal number of evaluations allowed.

#### evaluations

    private int evaluations

Number of evaluations already performed.

#### optimizationGoal

    private GoalType optimizationGoal

Optimization type

#### searchMin

    private double searchMin

Lower end of search interval.

#### searchMax

    private double searchMax

Higher end of search interval.

#### searchStart

    private double searchStart

Initial guess .

#### function

    private UnivariateRealFunction function

Function to optimize.

### Constructor Detail

#### 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:
  `defaultAbsoluteAccuracy` - maximum absolute error
  `defaultMaximalIterationCount` - maximum number of iterations
Throws:
  `java.lang.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 Detail

#### 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.