getConvergenceChecker() {
return checker;
}
/**
* 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 {@link
* #parseOptimizationData(OptimizationData[])} if they need to register their own options; but
* then, they must also call {@code super.parseOptimizationData(optData)} within that
* method.
*
* @param optData Optimization data. This method will register the following data:
*
* - {@link MaxEval}
*
- {@link MaxIter}
*
*
* @return a point/value pair that satisfies the convergence criteria.
* @throws TooManyEvaluationsException if the maximal number of evaluations is exceeded.
* @throws TooManyIterationsException if the maximal number of iterations is exceeded.
*/
public PAIR optimize(OptimizationData... optData)
throws TooManyEvaluationsException, TooManyIterationsException {
// Parse options.
parseOptimizationData(optData);
// Reset counters.
evaluations.resetCount();
iterations.resetCount();
// Perform optimization.
return doOptimize();
}
/**
* Performs the optimization.
*
* @return a point/value pair that satisfies the convergence criteria.
* @throws TooManyEvaluationsException if the maximal number of evaluations is exceeded.
* @throws TooManyIterationsException if the maximal number of iterations is exceeded.
*/
public PAIR optimize() throws TooManyEvaluationsException, TooManyIterationsException {
// Reset counters.
evaluations.resetCount();
iterations.resetCount();
// Perform optimization.
return doOptimize();
}
/**
* Performs the bulk of the optimization algorithm.
*
* @return the point/value pair giving the optimal value of the objective function.
*/
protected abstract PAIR doOptimize();
/**
* Increment the evaluation count.
*
* @throws TooManyEvaluationsException if the allowed evaluations have been exhausted.
*/
protected void incrementEvaluationCount() throws TooManyEvaluationsException {
evaluations.incrementCount();
}
/**
* Increment the iteration count.
*
* @throws TooManyIterationsException if the allowed iterations have been exhausted.
*/
protected void incrementIterationCount() throws TooManyIterationsException {
iterations.incrementCount();
}
/**
* Scans the list of (required and optional) optimization data that characterize the problem.
*
* @param optData Optimization data. The following data will be looked for:
*
* - {@link MaxEval}
*
- {@link MaxIter}
*
*/
protected void parseOptimizationData(OptimizationData... optData) {
// The existing values (as set by the previous call) are reused if
// not provided in the argument list.
for (OptimizationData data : optData) {
if (data instanceof MaxEval) {
evaluations.setMaximalCount(((MaxEval) data).getMaxEval());
continue;
}
if (data instanceof MaxIter) {
iterations.setMaximalCount(((MaxIter) data).getMaxIter());
continue;
}
}
}
/** Defines the action to perform when reaching the maximum number of evaluations. */
private static class MaxEvalCallback implements Incrementor.MaxCountExceededCallback {
/**
* {@inheritDoc}
*
* @throws TooManyEvaluationsException
*/
public void trigger(int max) {
throw new TooManyEvaluationsException(max);
}
}
/** Defines the action to perform when reaching the maximum number of evaluations. */
private static class MaxIterCallback implements Incrementor.MaxCountExceededCallback {
/**
* {@inheritDoc}
*
* @throws TooManyIterationsException
*/
public void trigger(int max) {
throw new TooManyIterationsException(max);
}
}
}