This package provides classes with numerical algorithm for optimization of an objective function and a factory to easy construction of the optimizers.
Why a package for optimization algorithms?
Given that there are a variety of numerical libraries featuring optimization algorithms
(e.g., Apache Commons Math), why do we provide a package inside finmath lib?
This packages provides a unified interface for passing optimizers to other classes
via an OptimizationFactoryInterface and an OptimizerInterface
and an OptimizerInterface.ObjectiveFunction.
This allows use of different optimization frameworks without bothering with the
framework specific constructors and framework specific definitions of objective functions.
A class implementing the OptimizationFactoryInterface allows the
specification of parameters specific to the optimizer, but leave the specification
of the initial values and the objective function still open. It provides a factory
method which takes the objective function and initial values as parameters and
constructs the specific optimizer by returning an object implementing
OptimizerInterface.
The following code is an example of an optimization problem using an OptimizerFactoryInterface
as argument.
public void testOptimizerWithRosenbrockFunction(OptimizerFactoryInterface optimizerFactory) throws SolverException {
OptimizerInterface.ObjectiveFunction objectiveFunction = new OptimizerInterface.ObjectiveFunction() {
@Override
public void setValues(double[] parameters, double[] values) {
values[0] = 10.0 * (parameters[1] - parameters[0]*parameters[0]);
values[1] = 1.0 - parameters[0];
}
};
OptimizerInterface optimizer = optimizerFactory.getOptimizer(
objectiveFunction,
new double[] { 0.5, 0.5 } /* initialParameters */,
new double[] { Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY } /* lowerBound */,
new double[] { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY } /* upperBound */,
new double[] { 0.5, 0.5 } /* parameterStep */,
new double[] { 0.0, 0.0 } /* targetValues */);
optimizer.run();
double[] bestParameters = optimizer.getBestFitParameters();
System.out.println("The solver " + optimizer.getClass() + " for problem 'Rosebrock' required " + optimizer.getIterations() + " iterations. Accuracy is " + optimizer.getRootMeanSquaredError() + ". The best fit parameters are:");
for (int i = 0; i < bestParameters.length; i++) System.out.println("\tparameter[" + i + "]: " + bestParameters[i]);
System.out.println();
Assert.assertTrue(Math.abs(bestParameters[0] - 1.0) < 1E-10);
Assert.assertTrue(Math.abs(bestParameters[1] - 1.0) < 1E-10);
}
Now, we may pass different optimizers to the optimization problem. For example the CMA-ES solver from commons math:
public void testRosenbrockFunctionWithCMAES() throws SolverException {
OptimizerFactoryInterface optimizerFactory = new OptimizerFactoryCMAES(0.0 /* accuracy */, 200 /* maxIterations */);
this.testOptimizerWithRosenbrockFunction(optimizerFactory);
}
Or the multi-threadded Levenberg Marquardt solver (using two threads) from finmath-lib:
@Test
public void testRosenbrockFunctionWithLevenbergMarquard() throws SolverException {
OptimizerFactoryInterface optimizerFactory = new OptimizerFactoryLevenbergMarquardt(200 /* maxIterations */, 2 /* maxThreads */);
this.testOptimizerWithRosenbrockFunction(optimizerFactory);
}
Optimization algorithms
The package also contains an implementation of the Levenberg Marquardt optimizer,
a multi-dimensional non-linear least-square.
In addition we provide wrappers (via specific OptimizationFactoryInterface
implementations) to some optimizers from Apache commons-math.