Click or drag to resize
Accord.NET (logo)

QuadraticObjectiveFunction Class

Quadratic objective function.
Inheritance Hierarchy
SystemObject
  Accord.Math.OptimizationNonlinearObjectiveFunction
    Accord.Math.OptimizationQuadraticObjectiveFunction

Namespace:  Accord.Math.Optimization
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public class QuadraticObjectiveFunction : NonlinearObjectiveFunction, 
	IObjectiveFunction
Request Example View Source

The QuadraticObjectiveFunction type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyConstantTerm
Gets the constant term in the quadratic function.
Public propertyFunction
Gets the objective function.
(Inherited from NonlinearObjectiveFunction.)
Public propertyGradient
Gets the gradient of the objective function.
(Inherited from NonlinearObjectiveFunction.)
Public propertyIndices
Gets the index of each input variable in the function.
(Inherited from NonlinearObjectiveFunction.)
Protected propertyInnerIndices
Gets the index of each input variable in the function.
(Inherited from NonlinearObjectiveFunction.)
Protected propertyInnerVariables
Gets the name of each input variable.
(Inherited from NonlinearObjectiveFunction.)
Public propertyLinearTerms
Gets the vector of linear terms of the quadratic function.
Public propertyNumberOfVariables
Gets the number of input variables for the function.
(Inherited from NonlinearObjectiveFunction.)
Public propertyQuadraticTerms
Gets the quadratic terms of the quadratic function.
Public propertyVariables
Gets the name of each input variable.
(Inherited from NonlinearObjectiveFunction.)
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.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a String that represents this instance.
(Overrides ObjectToString.)
Public methodStatic memberTryParse(String, QuadraticObjectiveFunction)
Attempts to create a QuadraticObjectiveFunction from a String representation.
Public methodStatic memberTryParse(String, CultureInfo, QuadraticObjectiveFunction)
Attempts to create a QuadraticObjectiveFunction from a String representation.
Top
Operators
  NameDescription
Public operatorStatic memberAddition
Adds two quadratic objective functions together by linearly combining the individual terms.
Public operatorStatic memberDivision
Divides each term in the QuadraticObjectiveFunction by the specified scalar.
Public operatorStatic memberMultiply(Double, QuadraticObjectiveFunction)
Multiplies each term in the QuadraticObjectiveFunction by the specified scalar.
Public operatorStatic memberMultiply(QuadraticObjectiveFunction, Double)
Multiplies each term in the QuadraticObjectiveFunction by the specified scalar.
Public operatorStatic memberSubtraction
Subtracts a quadratic objective function together by linearly subtracting the individual terms.
Public operatorStatic memberUnaryNegation
Negates the quadratic objective function.
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

In mathematics, a quadratic function, a quadratic polynomial, a polynomial of degree 2, or simply a quadratic, is a polynomial function in one or more variables in which the highest-degree term is of the second degree. For example, a quadratic function in three variables x, y, and z contains exclusively terms x², y², z², xy, xz, yz, x, y, z, and a constant:

f(x,y,z) = ax² + by² +cz² + dxy + exz + fyz + gx + hy + iz + j

Please note that the function's constructor expects the function expression to be given on this form. Scalar values must be located on the left of the variables, and no term should be duplicated in the quadratic expression. Please take a look on the examples section of this page for some examples of expected functions.

References:

Examples

Examples of valid quadratic functions are:

var f1 = new QuadraticObjectiveFunction("x² + 1");
var f2 = new QuadraticObjectiveFunction("-x*y + y*z");
var f3 = new QuadraticObjectiveFunction("-2x² + xy - y² - 10xz + z²");
var f4 = new QuadraticObjectiveFunction("-2x² + xy - y² + 5y");

It is also possible to specify quadratic functions using lambda expressions. In this case, it is first necessary to create some dummy symbol variables to act as placeholders in the quadratic expressions. Their value is not important, as they will only be used to parse the form of the expression, not its value.

// Declare symbol variables
double x = 0, y = 0, z = 0;

var g1 = new QuadraticObjectiveFunction(() => x * x + 1);
var g2 = new QuadraticObjectiveFunction(() => -x * y + y * z);
var g3 = new QuadraticObjectiveFunction(() => -2 * x * x + x * y - y * y - 10 * x * z + z * z);
var g4 = new QuadraticObjectiveFunction(() => -2 * x * x + x * y - y * y + 5 * y);

Finally, for large problems, it is usually best to declare quadratic problems using matrices and vectors. Without loss of generality, the quadratic matrix can always be taken to be symmetric (as the anti- symmetric part has no contribution to the function). For efficiency reasons, the quadratic matrix must be symmetric.

var h1 = new QuadraticObjectiveFunction("3x² - 4y² + 6xy + 3x + 2y");

double[,] Q = { { 6, 6 }, { 6, -8 } }; // Note the factor of 2
double[] d = { 3, 2 };

// Equivalently this can be written:
var h2 = new QuadraticObjectiveFunction(Q, d);

After those functions are created, you can either query their values using

f1.Function(new [] { 5.0 }); // x*x+1 = x² + 1 = 25 + 1 = 26

Or you can pass it to a quadratic optimization method such as Goldfarb-Idnani to explore its minimum or maximal points:

// Declare symbol variables
double x = 0, y = 0, z = 0;

// Create the function to be optimized
var f = new QuadraticObjectiveFunction(() => x * x - 2 * x * y + 3 * y * y + z * z - 4 * x - 5 * y - z);

// Create some constraints for the solution
var constraints = new List<LinearConstraint>();
constraints.Add(new LinearConstraint(f, () => 6 * x - 7 * y <= 8));
constraints.Add(new LinearConstraint(f, () => 9 * x + 1 * y <= 11));
constraints.Add(new LinearConstraint(f, () => 9 * x - y <= 11));
constraints.Add(new LinearConstraint(f, () => -z - y == 12));

// Create the Quadratic Programming solver
GoldfarbIdnani solver = new GoldfarbIdnani(f, constraints);

// Minimize the function
bool success = solver.Minimize();

double value = solver.Value;
double[] solutions = solver.Solution;

Sometimes it is easiest to compose quadratic functions as linear combinations of other quadratic functions. The QuadraticObjectiveFunction supports this by overloading the addition and multiplication operators.

// The quadratic objective function supports the notion
// of vector addition and scalar multiplication. That is,
// QP programs can be linearly combined to create new QP
// problems. This idea can be useful when composing
// objective functions.
var f1 = new QuadraticObjectiveFunction("2x² + 4y² - 2xy + 6");
var f2 = new QuadraticObjectiveFunction("3x² - 4y² + 6xy + 3x + 2y");

// Suppose we have the functions:
//      f₁(x,y) = 2x² - 2xy + 4y² + 6
//      f₂(x,y) = 3x² + 6xy - 4y² + 3x + 2y
// 
// Then we can create a new function - f(x,y) - defined as
// some linear combination of f₁ and f₂. e.g.
//      f(x,y) = {f₁ + 2f₂}(x,y)
// 
// In code, we can write this:
QuadraticObjectiveFunction f = f1 + (2 * f2); // 8x² -4y² +10xy +6x +4y +6

// And now we can test our new objective function:
double[] x = { 1, 2 };

double result1 = f1.Function(x);
double result2 = f2.Function(x);

double result = f.Function(x);          // should be 32
double check = result1 + 2 * result2;   // should be the same
See Also