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.4.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
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(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.)
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 Matrix.)
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);

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;
See Also