NMath User's Guide

TOC | Previous | Next | Index

29.1 Encapsulating LP Problems (.NET, C#, CSharp, VB, Visual Basic, F#)

Class LinearProgrammingProblem encapsulates an LP problem. Instances are constructed from a vector of coefficients representing the objective function.

Code Example – C# linear programming

// z = x1 + 4*x2 + 9*x3
var coeff = new DoubleVector( "[1 4 9]" );
var problem = new LinearProgrammingProblem( coeff );

MixedIntegerLinearProgrammingProblem encapsulates an LP problem which may contain integer or binary constraints.

Adding Bounds and Constraints

LinearProgrammingProblem instances maintain a list of LinearContraint objects, accessible via the Constraints property. A linear constraint on a set of variables is a constraint upon a linear combination of those variables. LinearConstraint supports to two such constraints: equality constraints and lower bound constraints. That is, given variables x0, x1,..., xn and constants b, a0, a1,..., an, two types of constraints may be formed



a0*x0 + a1*x1 + . . . + an*xn = b

and



a0*x0 + a1*x1 + . . . + an*xn >= b

NOTE—Upper bound constraints are represented as negations of lower bound constraints.

Constraints may be added to a LinearProgrammingProblem by working directly with the Constraints list, or by using the AddConstraint() method.

LinearContraint instances are constructed from a vector of coefficients, a right-hand side, and a constraint type from the ConstraintType enumeration.

Code Example – C# linear programming

// 0 <= x0 + 2*x1 + 2*x2
var coeff = new DoubleVector( 1.0, 2.0, 2.0 );
var constraint = new LinearConstraint( coeff, 0, 
  ConstraintType.GreaterThanOrEqualTo);
problem.AddConstraint( constraint );

A variety of convenience methods are also provided on LinearProgrammingProblem for adding constraints and variable bounds to an existing LP problem. These methods create the required LinearConstraint objects for you and add them to the Constraints list.

Code Example – C# linear programming

// 0 <= x0 + 2*x1 + 2*x2 <= 72
var coeff = new DoubleVector( 1.0, 2.0, 2.0 );
problem.AddConstraint( coeff, 0, 72 );

MixedIntegerLinearProgrammingProblem encapsulates an LP problem which may contain integer or binary constraints. For example, in this code the first variable is constrained to be integer valued.

Code Example – C# integer programming

problem.AddIntegralConstraint( 0 );

Here, the ith variable in the solution must be binary.

Code Example – C# binary programming

problem.AddBinaryConstraint( i );

A binary constraint restricts the variable to a value of zero or one.

Method GetIntegrality() gets the integral constraint state of the variable at the given index. IntegralVariableIndices returns the indices of variables with integral constraints.


Top

Top