LinearConstraint Class 
Namespace: Accord.Math.Optimization
The LinearConstraint type exposes the following members.
Name  Description  

LinearConstraint(Double) 
Constructs a new linear constraint.
 
LinearConstraint(Int32) 
Constructs a new linear constraint.
 
LinearConstraint(IObjectiveFunction, ExpressionFuncBoolean) 
Constructs a new linear constraint.
 
LinearConstraint(IObjectiveFunction, String) 
Constructs a new linear constraint.
 
LinearConstraint(IObjectiveFunction, String, CultureInfo) 
Constructs a new linear constraint.

Name  Description  

CombinedAs 
Gets the scalar coefficients combining the
variables specified by the constraints.
 
NumberOfVariables 
Gets the number of variables in the constraint.
 
ShouldBe 
Gets the type of the constraint.
 
Tolerance 
Gets the violation tolerance for the constraint. Equality
constraints should set this to a small positive value.
 
Value 
Gets the value to be compared to the combined values
of the variables.
 
VariablesAtIndices 
Gets the index of the variables (in respective to the
objective function) of the variables participating
in this constraint.

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.)  
Function 
Calculates the left hand side of the constraint
equation given a vector x.
 
GetHashCode  Serves as the default hash function. (Inherited from Object.)  
GetType  Gets the Type of the current instance. (Inherited from Object.)  
Gradient 
Calculates the gradient of the constraint.
 
MemberwiseClone  Creates a shallow copy of the current Object. (Inherited from Object.)  
ToString  Returns a string that represents the current object. (Inherited from Object.)  
TryParse(String, IObjectiveFunction, LinearConstraint) 
Attempts to create a LinearConstraint
from a String representation.
 
TryParse(String, CultureInfo, IObjectiveFunction, LinearConstraint) 
Attempts to create a LinearConstraint
from a String representation.

Name  Description  

DefaultTolerance 
Gets the default constant violation tolerance (1e12).

Name  Description  

GetViolation 
Gets how much the constraint is being violated.
(Defined by ConstraintExtensions.)  
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.)  
IsViolated 
Gets whether this constraint is being violated
(within the current tolerance threshold).
(Defined by ConstraintExtensions.)  
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.) 
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