Click or drag to resize
Accord.NET (logo)

GoldfarbIdnani Class

Goldfarb-Idnani Quadratic Programming Solver.
Inheritance Hierarchy
SystemObject
  Accord.Math.OptimizationBaseOptimizationMethod
    Accord.Math.OptimizationBaseGradientOptimizationMethod
      Accord.Math.OptimizationGoldfarbIdnani

Namespace:  Accord.Math.Optimization
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public class GoldfarbIdnani : BaseGradientOptimizationMethod, 
	IOptimizationMethod, IOptimizationMethod<double[], double>, IOptimizationMethod<GoldfarbIdnaniStatus>, 
	IOptimizationMethod<double[], double, GoldfarbIdnaniStatus>
Request Example View Source

The GoldfarbIdnani type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyActiveConstraints
Public propertyConstraintMatrix
Gets the constraint matrix A for the problem.
Public propertyConstraintTolerances
Gets the constraint tolerances b for the problem.
Public propertyConstraintValues
Gets the constraint values b for the problem.
Public propertyDeletions
Public propertyFunction
Gets or sets the function to be optimized.
(Inherited from BaseOptimizationMethod.)
Public propertyGradient
Gets or sets a function returning the gradient vector of the function to be optimized for a given value of its free parameters.
(Inherited from BaseGradientOptimizationMethod.)
Public propertyIterations
Public propertyLagrangian
Gets the Lagrangian multipliers for the last solution found.
Public propertyLinearTerms
Gets the vector of linear terms of the quadratic optimization problem.
Public propertyMaxIterations
Gets or sets the maximum number of iterations that should be performed before the method terminates. If set to zero, the method will run to completion. Default is 0.
Public propertyNumberOfConstraints
Gets the total number of constraints in the problem.
Public propertyNumberOfEqualities
Gets how many constraints are inequality constraints.
Public propertyNumberOfVariables
Gets the number of variables (free parameters) in the optimization problem.
(Inherited from BaseOptimizationMethod.)
Public propertyQuadraticTerms
Gets the matrix of quadratic terms of the quadratic optimization problem.
Public propertySolution
Gets the current solution found, the values of the parameters which optimizes the function.
(Inherited from BaseOptimizationMethod.)
Public propertyStatus
Public propertyToken
Gets or sets a cancellation token that can be used to stop the learning algorithm while it is running.
(Inherited from BaseOptimizationMethod.)
Public propertyValue
Gets the output of the function at the current Solution.
(Inherited from BaseOptimizationMethod.)
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodMaximize
Finds the maximum value of a function. The solution vector will be made available at the Solution property.
(Overrides BaseGradientOptimizationMethodMaximize.)
Public methodMaximize(Double)
Finds the maximum value of a function. The solution vector will be made available at the Solution property.
(Inherited from BaseOptimizationMethod.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodMinimize
Finds the minimum value of a function. The solution vector will be made available at the Solution property.
(Overrides BaseGradientOptimizationMethodMinimize.)
Public methodMinimize(Double)
Finds the minimum value of a function. The solution vector will be made available at the Solution property.
(Inherited from BaseOptimizationMethod.)
Protected methodOnNumberOfVariablesChanged
Called when the NumberOfVariables property has changed.
(Inherited from BaseOptimizationMethod.)
Protected methodOptimize
Not available.
(Overrides BaseOptimizationMethodOptimize.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks
Examples

There are three ways to state a quadratic programming problem in this framework.

  • The first is to state the problem in its canonical form, explicitly stating the matrix Q and vector d specifying the quadratic function and the matrices A and vector b specifying the problem constraints.
  • The second is to state the problem with lambda expressions using symbolic variables.
  • The third is to state the problem using text strings.

In the following section we will provide examples for those ways.

This is an example stating the problem using lambdas:

// Solve the following optimization problem:
// 
//  min f(x) = 2x² - xy + 4y² - 5x - 6y
// 
//  s.t.   x - y  ==   5  (x minus y should be equal to 5)
//             x  >=  10  (x should be greater than or equal to 10)
// 

// In this example we will be using some symbolic processing. 
// The following variables could be initialized to any value.
double x = 0, y = 0;

// Create our objective function using a lambda expression
var f = new QuadraticObjectiveFunction(() => 2 * (x * x) - (x * y) + 4 * (y * y) - 5 * x - 6 * y);

// Now, create the constraints
List<LinearConstraint> constraints = new List<LinearConstraint>()
{
    new LinearConstraint(f, () => x - y == 5),
    new LinearConstraint(f, () => x >= 10)
};

// Now we create the quadratic programming solver 
var solver = new GoldfarbIdnani(f, constraints);

// And attempt solve for the min:
bool success = solver.Minimize();

// The solution was { 10, 5 }
double[] solution = solver.Solution;

// With the minimum value 170.0
double minValue = solver.Value;

This is an example stating the problem using strings:

// Solve the following optimization problem:
// 
//  max f(x) = -2x² + xy - y² + 5y
// 
//  s.t.   x + y  <= 0
//             y  >= 0
// 

// Create our objective function using a text string
var f = new QuadraticObjectiveFunction("-2x² + xy - y² + 5y");

// Now, create the constraints
List<LinearConstraint> constraints = new List<LinearConstraint>()
{
    new LinearConstraint(f, "x + y <= 0"),
    new LinearConstraint(f, "    y >= 0")
};

// Now we create the quadratic programming solver 
var solver = new GoldfarbIdnani(f, constraints);

// And attempt solve for the max:
bool success = solver.Maximize();

// The solution was { -0.625, 0.625 }
double[] solution = solver.Solution;

// With the minimum value 1.5625
double maxValue = solver.Value;

And finally, an example stating the problem using matrices:

// Solve the following optimization problem:
// 
//  min f(x) = 2x² - xy + 4y² - 5x - 6y
// 
//  s.t.   x - y  ==   5  (x minus y should be equal to 5)
//             x  >=  10  (x should be greater than or equal to 10)
// 

// Lets first group the quadratic and linear terms. The
// quadratic terms are +2x², +3y² and -4xy. The linear 
// terms are -2x and +1y. So our matrix of quadratic
// terms can be expressed as:

double[,] Q = // 2x² -1xy +4y²
{   
    /*           x              y      */
    /*x*/ { +2 /*xx*/ *2,  -1 /*xy*/    }, 
    /*y*/ { -1 /*xy*/   ,  +4 /*yy*/ *2 },
};

// Accordingly, our vector of linear terms is given by:

double[] d = { -5 /*x*/, -6 /*y*/ }; // -5x -6y

// We have now to express our constraints. We can do it
// either by directly specifying a matrix A in which each
// line refers to one of the constraints, expressing the
// relationship between the different variables in the
// constraint, like this:

double[,] A =
{
    { 1, -1 }, // This line says that x + (-y) ... (a)
    { 1,  0 }, // This line says that x alone  ... (b)
};

double[] b =
{
     5, // (a) ... should be equal to 5.
    10, // (b) ... should be greater than or equal to 10.
};

// Equalities must always come first, and in this case
// we have to specify how many of the constraints are
// actually equalities:

int numberOfEqualities = 1;


// Alternatively, we may use an explicit form:
var constraints = new List<LinearConstraint>()
{
    // Define the first constraint, which involves only x
    new LinearConstraint(numberOfVariables: 1)
    {
        // x is the first variable, thus located at
        // index 0. We are specifying that x >= 10:

        VariablesAtIndices = new[] { 0 }, // index 0 (x)
        ShouldBe = ConstraintType.GreaterThanOrEqualTo,
        Value = 10
    },

    // Define the second constraint, which involves x and y
    new LinearConstraint(numberOfVariables: 2)
    {
        // x is the first variable, located at index 0, and y is
        // the second, thus located at 1. We are specifying that
        // x - y = 5 by saying that the variable at position 0 
        // times 1 plus the variable at position 1 times -1 
        // should be equal to 5.

        VariablesAtIndices = new int[] { 0, 1 }, // index 0 (x) and index 1 (y)
        CombinedAs = new double[] { 1, -1 }, // when combined as x - y
        ShouldBe = ConstraintType.EqualTo,
        Value = 5
    }
};


// Now we can finally create our optimization problem
var solver = new GoldfarbIdnani(
    function: new QuadraticObjectiveFunction(Q, d),
    constraints: constraints);


// And attempt solve for the min:
bool success = solver.Minimize();

// The solution was { 10, 5 }
double[] solution = solver.Solution;

// With the minimum value 170.0
double minValue = solver.Value;
See Also