Class MultiStartUnivariateRealOptimizer

  • All Implemented Interfaces:
    ConvergingAlgorithm, UnivariateRealOptimizer

    public class MultiStartUnivariateRealOptimizer
    extends java.lang.Object
    implements UnivariateRealOptimizer
    Special implementation of the UnivariateRealOptimizer interface adding multi-start features to an existing optimizer.

    This class wraps a classical optimizer to use it several times in turn with different starting points in order to avoid being trapped into a local extremum when looking for a global one.

    Since:
    2.0
    • Constructor Detail

      • MultiStartUnivariateRealOptimizer

        public MultiStartUnivariateRealOptimizer​(UnivariateRealOptimizer optimizer,
                                                 int starts,
                                                 RandomGenerator generator)
        Create a multi-start optimizer from a single-start optimizer
        Parameters:
        optimizer - single-start optimizer to wrap
        starts - number of starts to perform (including the first one), multi-start is disabled if value is less than or equal to 1
        generator - random generator to use for restarts
    • Method Detail

      • getFunctionValue

        public double getFunctionValue()
        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.
      • getResult

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

        public int getIterationCount()
        Get the number of iterations in the last run of the algorithm.

        This is mainly meant for testing purposes. It may occasionally help track down performance problems: if the iteration count is notoriously high, check whether the problem is evaluated properly, and whether another algorithm is more amenable to the problem.

        Specified by:
        getIterationCount in interface ConvergingAlgorithm
        Returns:
        the last iteration count.
      • getMaximalIterationCount

        public int getMaximalIterationCount()
        Get the upper limit for the number of iterations.
        Specified by:
        getMaximalIterationCount in interface ConvergingAlgorithm
        Returns:
        the actual upper limit
      • 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.
      • resetAbsoluteAccuracy

        public void resetAbsoluteAccuracy()
        Reset the absolute accuracy to the default.

        The default value is provided by the algorithm implementation.

        Specified by:
        resetAbsoluteAccuracy in interface ConvergingAlgorithm
      • resetRelativeAccuracy

        public void resetRelativeAccuracy()
        Reset the relative accuracy to the default. The default value is provided by the algorithm implementation.
        Specified by:
        resetRelativeAccuracy in interface ConvergingAlgorithm
      • setAbsoluteAccuracy

        public void setAbsoluteAccuracy​(double accuracy)
        Set the absolute accuracy.

        The default is usually chosen so that results in the interval -10..-0.1 and +0.1..+10 can be found with a reasonable accuracy. If the expected absolute value of your results is of much smaller magnitude, set this to a smaller value.

        Algorithms are advised to do a plausibility check with the relative accuracy, but clients should not rely on this.

        Specified by:
        setAbsoluteAccuracy in interface ConvergingAlgorithm
        Parameters:
        accuracy - the accuracy.
      • setMaximalIterationCount

        public void setMaximalIterationCount​(int count)
        Set the upper limit for the number of iterations.

        Usually a high iteration count indicates convergence problems. However, the "reasonable value" varies widely for different algorithms. Users are advised to use the default value supplied by the algorithm.

        A ConvergenceException will be thrown if this number is exceeded.

        Specified by:
        setMaximalIterationCount in interface ConvergingAlgorithm
        Parameters:
        count - maximum number of iterations
      • 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
      • setRelativeAccuracy

        public void setRelativeAccuracy​(double accuracy)
        Set the relative accuracy.

        This is used to stop iterations if the absolute accuracy can't be achieved due to large values or short mantissa length.

        If this should be the primary criterion for convergence rather then a safety measure, set the absolute accuracy to a ridiculously small value, like MathUtils.SAFE_MIN.

        Specified by:
        setRelativeAccuracy in interface ConvergingAlgorithm
        Parameters:
        accuracy - the relative accuracy.
      • getOptima

        public double[] getOptima()
                           throws java.lang.IllegalStateException
        Get all the optima found during the last call to optimize.

        The optimizer stores all the optima found during a set of restarts. The optimize method returns the best point only. This method returns all the points found at the end of each starts, including the best one already returned by the optimize method.

        The returned array as one element for each start as specified in the constructor. It is ordered with the results from the runs that did converge first, sorted from best to worst objective value (i.e in ascending order if minimizing and in descending order if maximizing), followed by Double.NaN elements corresponding to the runs that did not converge. This means all elements will be NaN if the optimize method did throw a ConvergenceException). This also means that if the first element is not NaN, it is the best point found across all starts.

        Returns:
        array containing the optima
        Throws:
        java.lang.IllegalStateException - if optimize has not been called
        See Also:
        getOptimaValues()
      • getOptimaValues

        public double[] getOptimaValues()
                                 throws java.lang.IllegalStateException
        Get all the function values at optima found during the last call to optimize.

        The returned array as one element for each start as specified in the constructor. It is ordered with the results from the runs that did converge first, sorted from best to worst objective value (i.e in ascending order if minimizing and in descending order if maximizing), followed by Double.NaN elements corresponding to the runs that did not converge. This means all elements will be NaN if the optimize method did throw a ConvergenceException). This also means that if the first element is not NaN, it is the best point found across all starts.

        Returns:
        array containing the optima
        Throws:
        java.lang.IllegalStateException - if optimize has not been called
        See Also:
        getOptima()