# Optimization

## Optimizing in QFF

At present, the following engines are supported:

- R package ("R")
- JMinuit ("JMinuit")

## Codebook

### Construction

In general, an optimizer in QFF is not instantiated directly but by means of a factory, the OptimizerFactory. The minimal requirement is to specify the name of the engine.

OptimizerFactory factory = OptimizerFactory.getInstance(); IOptimizer optimizer1 = factory.create("R");

In most cases, you will wish to minimize a certain function. All instances of `IDoubleFunction` are supported.

IDoubleFunction f = new DoubleFunctionND(3) { public double apply(double... var) { double x = var[0] - 4.0; double y = var[1] + 2.0; double z = var[2] - 1.0; return 4.0*x*x + 2.0*y*y + 1.0*z*z + 10.0; } }; IOptimizer optimizer2 = factory.create("R", f);

The aforementioned method implicitely assumes that the start values of the optimization routine are all 0. You must add an instance of `IVariableSetting` to provide start values not equal to 0.

IVariableSetting setting = new VariableSetting(f, 1.0, 0.0, 2.0); IOptimizer optimizer3 = factory.create("R", f, setting);

### Methods

The most important method is the `double getResult()` method which starts the optimization routine and returns an instance of `IOptimizerResult`.

IOptimizerResult result = optimizer2.getResult(); double minimum = result.getOptimum(); // contains 10.00000000001808 double[] minimumPoint = result.getOptimalPoint(); // contains [3.9999980012213183, -2.000000980515494, 0.9999995789902919]

It is possible to change some parameters of the optimizer.

IOptimizerConfiguration config = optimizer2.getConfiguration(); config.setConvergence(1.0e-15);

For the present problem the increase of the convergence (the tolerance of the function value) has now influence on the result of the minimization process.

In some cases, the range of admissible values is constrained for some or all variables. Assume that only nonnegative values are allowed. Then you have to create an instance of `IVariableSetting` to specify the constraints.

IDoubleInterval nonnegative = DoubleInterval.create(0.0, Double.POSITIVE_INFINITY); IVariableSetting setting = new VariableSetting(f); setting.setBounds(0, nonnegative); setting.setBounds(1, nonnegative); setting.setBounds(2, nonnegative); optimizer2.setVariableSetting(setting); result = optimizer2.getResult(); minimum = result.getOptimum(); // contains 18.0 minimumPoint = result.getOptimalPoint(); // contains [4.000000001610627, 0.0, 1.0000000000291314]