Optimisation

QuantExpress libraries supports several univariate and multivariate optimisation Engine with or without contrainst.

These are very easy to use. As an example for the multidimension case the user needs simply to specify:

  • Whether to search for a minimum or a maximum
  • A callback function (delegate) that calculates the value of the function

The following example summarizes how the code can be used to minimize a function. To start please select on of the two following predefined functions:

Then, please select the algorithm to use and press the "Minimize" button

v

Minimize


The solution that minimizes the function is shown herebelow:


C# Code Sample
/// <summary>
/// creates the multivariate optimization class according to the user's selection
/// </summary>
/// <returns></returns>
private QuantExpress.Maths.Optimisation.IOptimize GetOptimiseClass()
{
    switch (CBoxAlgo.SelectedIndex)
    {
        case 0:
            return new QuantExpress.Maths.Optimisation.NelderMead();
        case 1:
            return new QuantExpress.Maths.Optimisation.DifferentialEvolution();

        default:
            throw new NotImplementedException();
    }
}

/// <summary>
/// A multivariate function to optimize
/// </summary>
/// <param name="aParams"></param>
/// <returns></returns>
private double FuncXY(double[] aParams)
{
    return aParams[0] * aParams[0] - 4 * aParams[0]
        + aParams[1] * aParams[1]
        - aParams[1]
        - aParams[0] * aParams[1];
}

/// <summary>
/// Another multivariate function to optimize
/// </summary>
/// <param name="aParams"></param>
/// <returns></returns>
private double FuncXYZ(double[] aParams)
{
    double A = (aParams[0] - 10);
    double B = (aParams[1] + 10);
    double C = (aParams[2] - 2);
    return A * A + B * B + C * C;
}

private void OptimizeMultivariate()
{
    // creates a new instance of the selected optimisation class
    QuantExpress.Maths.Optimisation.IOptimize oMinEngine = this.GetOptimiseClass();
    double[] wVecSolution;
    double wMinimum;

    // sets the original multi dimension point from which the optimisation engine starts the optimisation search
    if (this.RBFunctionToMinimize.SelectedIndex == 0)
    {
        wVecSolution = new double[2];
        wVecSolution[0] = 1;
        wVecSolution[1] = 1;
    }
    else
    {
        wVecSolution = new double[3];
        wVecSolution[0] = 1;
        wVecSolution[1] = 1;
        wVecSolution[2] = 1;
    }

    // finds the minimum
    wMinimum = oMinEngine.FindExtremum(QuantExpress.Maths.Optimisation.Extremum.Minimum,
        this.FuncXY, ref wVecSolution);

    // displays the output solution
    EditSolution.Text = string.Format("Minimum: {0} - Vector Solution: {1}", 
        wMinimum,
        QuantExpress.StdClasses.StringHelper.AsCsv<double>(wVecSolution, ", "));
}