NMath User's Guide

TOC | Previous | Next | Index

29.2 Solving LP Problems (.NET, C#, CSharp, VB, Visual Basic, F#)

Class PrimalSimplexSolver solves linear programming problems using the primal simplex method. DualSimplexSolver uses the dual simplex method. The simplex method solves LP problems by constructing an initial solution at a vertex of a simplex, then walking along edges of the simplex to vertices with successively higher values of the objective function until the optimum is reached.

The Solve() method takes a LinearProgrammingProblem or MixedIntegerLinearProgrammingProblem and, optionally, a solver parameter object.

Code Example – C# linear programming

var solver = new PrimalSimplexSolver();
solver.Solve( problem );

This code demonstrates using a solver parameter object.

Code Example – C# linear programming

var solverParams = new DualSimplexSolverParams
  // Use steepest edge pivoting
  Costing = DualSimplexCosting.SteepestEdge,
  // Do not perform more than 1000 pivots
  MaxPivotCount = 1000,
  // Minimize, rather than maximize, the objective function
  Minimize = true

var solver = new DualSimplexSolver();
solver.Solve( problem, solverParams );

It is important to check whether a finite solution was found, since your problem may be unbounded or infeasible. If a finite solution was found, you can access the solution using the OptimalX property. The OptimalObjectiveFunctionValue property gets the value of the objective function evaluated at the solution.

Code Example – C# linear programming

if ( solver.Result == PrimalSimplexSolver.SolveResult.Optimal )
  Console.WriteLine( solver.OptimalX );
  Console.WriteLine( solver.OptimalObjectiveFunctionValue );

If the solver result is SolverResult.UnexpectedException, you can use the ExceptionMessage property to get the exception message.