Class MultiStartUnivariateOptimizer
- java.lang.Object
-
- org.apache.commons.math3.optim.BaseOptimizer<UnivariatePointValuePair>
-
- org.apache.commons.math3.optim.univariate.UnivariateOptimizer
-
- org.apache.commons.math3.optim.univariate.MultiStartUnivariateOptimizer
-
public class MultiStartUnivariateOptimizer extends UnivariateOptimizer
Special implementation of theUnivariateOptimizerinterface adding multi-start features to an existing optimizer.
This class wraps an optimizer in order to use it several times in turn with different starting points (trying to avoid being trapped in a local extremum when looking for a global one).- Since:
- 3.0
-
-
Field Summary
Fields Modifier and Type Field Description private RandomGeneratorgeneratorRandom generator for multi-start.private intmaxEvalIndexLocation inoptimDatawhere the updated maximum number of evaluations will be stored.private UnivariatePointValuePair[]optimaFound optima.private OptimizationData[]optimDataOptimization data.private UnivariateOptimizeroptimizerUnderlying classical optimizer.private intsearchIntervalIndexLocation inoptimDatawhere the updated start value will be stored.private intstartsNumber of starts to go.private inttotalEvaluationsNumber of evaluations already performed for all starts.-
Fields inherited from class org.apache.commons.math3.optim.BaseOptimizer
evaluations, iterations
-
-
Constructor Summary
Constructors Constructor Description MultiStartUnivariateOptimizer(UnivariateOptimizer optimizer, int starts, RandomGenerator generator)Create a multi-start optimizer from a single-start optimizer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected UnivariatePointValuePairdoOptimize()Performs the bulk of the optimization algorithm.intgetEvaluations()Gets the number of evaluations of the objective function.UnivariatePointValuePair[]getOptima()Gets all the optima found during the last call tooptimize.UnivariatePointValuePairoptimize(OptimizationData... optData)Stores data and performs the optimization.private voidsortPairs(GoalType goal)Sort the optima from best to worst, followed bynullelements.-
Methods inherited from class org.apache.commons.math3.optim.univariate.UnivariateOptimizer
computeObjectiveValue, getGoalType, getMax, getMin, getStartValue, parseOptimizationData
-
Methods inherited from class org.apache.commons.math3.optim.BaseOptimizer
getConvergenceChecker, getIterations, getMaxEvaluations, getMaxIterations, incrementEvaluationCount, incrementIterationCount, optimize
-
-
-
-
Field Detail
-
optimizer
private final UnivariateOptimizer optimizer
Underlying classical optimizer.
-
totalEvaluations
private int totalEvaluations
Number of evaluations already performed for all starts.
-
starts
private int starts
Number of starts to go.
-
generator
private RandomGenerator generator
Random generator for multi-start.
-
optima
private UnivariatePointValuePair[] optima
Found optima.
-
optimData
private OptimizationData[] optimData
Optimization data.
-
maxEvalIndex
private int maxEvalIndex
Location inoptimDatawhere the updated maximum number of evaluations will be stored.
-
searchIntervalIndex
private int searchIntervalIndex
Location inoptimDatawhere the updated start value will be stored.
-
-
Constructor Detail
-
MultiStartUnivariateOptimizer
public MultiStartUnivariateOptimizer(UnivariateOptimizer 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. Ifstarts == 1, theoptimizemethods will return the same solution asoptimizerwould.generator- Random generator to use for restarts.- Throws:
NotStrictlyPositiveException- ifstarts < 1.
-
-
Method Detail
-
getEvaluations
public int getEvaluations()
Gets the number of evaluations of the objective function. The number of evaluations corresponds to the last call to theoptimizemethod. It is 0 if the method has not been called yet.- Overrides:
getEvaluationsin classBaseOptimizer<UnivariatePointValuePair>- Returns:
- the number of evaluations of the objective function.
-
getOptima
public UnivariatePointValuePair[] getOptima()
Gets all the optima found during the last call tooptimize. The optimizer stores all the optima found during a set of restarts. Theoptimizemethod 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 theoptimizemethod.
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 bynullelements corresponding to the runs that did not converge. This means all elements will benullif theoptimizemethod did throw an exception. This also means that if the first element is notnull, it is the best point found across all starts.- Returns:
- an array containing the optima.
- Throws:
MathIllegalStateException- ifoptimizehas not been called.
-
optimize
public UnivariatePointValuePair optimize(OptimizationData... optData)
Stores data and performs the optimization.The list of parameters is open-ended so that sub-classes can extend it with arguments specific to their concrete implementations.
When the method is called multiple times, instance data is overwritten only when actually present in the list of arguments: when not specified, data set in a previous call is retained (and thus is optional in subsequent calls).
Important note: Subclasses must override
BaseOptimizer.parseOptimizationData(OptimizationData[])if they need to register their own options; but then, they must also callsuper.parseOptimizationData(optData)within that method.- Overrides:
optimizein classUnivariateOptimizer- Parameters:
optData- Optimization data. In addition to those documented inBaseOptimizer, this method will register the following data:- Returns:
- a point/value pair that satisfies the convergence criteria.
- Throws:
MathIllegalStateException- ifoptDatadoes not contain an instance ofMaxEvalorSearchInterval.
-
doOptimize
protected UnivariatePointValuePair doOptimize()
Performs the bulk of the optimization algorithm.- Specified by:
doOptimizein classBaseOptimizer<UnivariatePointValuePair>- Returns:
- the point/value pair giving the optimal value of the objective function.
-
sortPairs
private void sortPairs(GoalType goal)
Sort the optima from best to worst, followed bynullelements.- Parameters:
goal- Goal type.
-
-