Click or drag to resize
Accord.NET (logo)

LinearConstraint Class

Constraint with only linear terms.
Inheritance Hierarchy
SystemObject
  Accord.Math.OptimizationLinearConstraint

Namespace:  Accord.Math.Optimization
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public class LinearConstraint : IConstraint
Request Example View Source

The LinearConstraint type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyCombinedAs
Gets the scalar coefficients combining the variables specified by the constraints.
Public propertyNumberOfVariables
Gets the number of variables in the constraint.
Public propertyShouldBe
Gets the type of the constraint.
Public propertyTolerance
Gets the violation tolerance for the constraint. Equality constraints should set this to a small positive value.
Public propertyValue
Gets the value to be compared to the combined values of the variables.
Public propertyVariablesAtIndices
Gets the index of the variables (in respective to the objective function) of the variables participating in this constraint.
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 methodFunction
Calculates the left hand side of the constraint equation given a vector x.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodGradient
Calculates the gradient of the constraint.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodStatic memberTryParse(String, IObjectiveFunction, LinearConstraint)
Attempts to create a LinearConstraint from a String representation.
Public methodStatic memberTryParse(String, CultureInfo, IObjectiveFunction, LinearConstraint)
Attempts to create a LinearConstraint from a String representation.
Top
Fields
  NameDescription
Public fieldStatic memberDefaultTolerance
Gets the default constant violation tolerance (1e-12).
Top
Extension Methods
  NameDescription
Public Extension MethodGetViolation
Gets how much the constraint is being violated.
(Defined by ConstraintExtensions.)
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 MethodIsViolated
Gets whether this constraint is being violated (within the current tolerance threshold).
(Defined by ConstraintExtensions.)
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
Examples

Linear constraints are commonly used in optimisation routines. The framework provides support for linear constraints to be specified using a String representation, an Expression or using a vector of constraint values.

// Linear constraints are common in numerical optimization.
// Constraints can be defined using strings, expressions or
// vectors. Suppose we have a quadratic objective function:
var f = new QuadraticObjectiveFunction("2x² + 4y² - 2xy + 6");

// Then the following three are all equivalent:
var lc1 = new LinearConstraint(f, "3*x + 5*y <= 7");

double x = 0, y = 0; // Define some dummy variables
var lc2 = new LinearConstraint(f, () => 3*x + 5*y <= 7);

var lc3 = new LinearConstraint(numberOfVariables: 2)
{
    CombinedAs = new double[] { 3, 5 },
    ShouldBe = ConstraintType.LesserThanOrEqualTo,
    Value = 7
};

// Then, we can check whether a constraint is violated and, if so,
// by how much.
double[] vector = { -2, 3 };

if (lc1.IsViolated(vector))
{
    // act on violation...
}

double violation = lc2.GetViolation(vector); // negative if violated
See Also