QuadraticObjectiveFunction Class |
Namespace: Accord.Math.Optimization
The QuadraticObjectiveFunction type exposes the following members.
Name | Description | |
---|---|---|
QuadraticObjectiveFunction(ExpressionFuncDouble) |
Creates a new objective function specified through a string.
| |
QuadraticObjectiveFunction(String) |
Creates a new objective function specified through a string.
| |
QuadraticObjectiveFunction(String, CultureInfo) |
Creates a new objective function specified through a string.
| |
QuadraticObjectiveFunction(Double, Double, String) |
Creates a new objective function specified through a string.
|
Name | Description | |
---|---|---|
ConstantTerm |
Gets the constant term in the quadratic function.
| |
Function |
Gets the objective function.
(Inherited from NonlinearObjectiveFunction.) | |
Gradient |
Gets the gradient of the objective function.
(Inherited from NonlinearObjectiveFunction.) | |
Indices |
Gets the index of each input variable in the function.
(Inherited from NonlinearObjectiveFunction.) | |
InnerIndices |
Gets the index of each input variable in the function.
(Inherited from NonlinearObjectiveFunction.) | |
InnerVariables |
Gets the name of each input variable.
(Inherited from NonlinearObjectiveFunction.) | |
LinearTerms |
Gets the vector of linear terms of the quadratic function.
| |
NumberOfVariables |
Gets the number of input variables for the function.
(Inherited from NonlinearObjectiveFunction.) | |
QuadraticTerms |
Gets the quadratic terms of the quadratic function.
| |
Variables |
Gets the name of each input variable.
(Inherited from NonlinearObjectiveFunction.) |
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString |
Returns a String that represents this instance.
(Overrides ObjectToString.) | |
TryParse(String, QuadraticObjectiveFunction) |
Attempts to create a QuadraticObjectiveFunction
from a String representation.
| |
TryParse(String, CultureInfo, QuadraticObjectiveFunction) |
Attempts to create a QuadraticObjectiveFunction
from a String representation.
|
Name | Description | |
---|---|---|
Addition |
Adds two quadratic objective functions together by linearly combining
the individual terms.
| |
Division |
Divides each term in the QuadraticObjectiveFunction by the specified scalar.
| |
Multiply(Double, QuadraticObjectiveFunction) |
Multiplies each term in the QuadraticObjectiveFunction by the specified scalar.
| |
Multiply(QuadraticObjectiveFunction, Double) |
Multiplies each term in the QuadraticObjectiveFunction by the specified scalar.
| |
Subtraction |
Subtracts a quadratic objective function together by linearly subtracting
the individual terms.
| |
UnaryNegation |
Negates the quadratic objective function.
|
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(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.) | |
ToT | 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.) |
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 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