Linear Programming in C# QuickStart Sample
Illustrates solving linear programming (LP) problems using classes in the Numerics.NET.Optimization.LinearProgramming namespace in C#.
This sample is also available in: Visual Basic, F#, IronPython.
Overview
This QuickStart sample demonstrates how to solve linear programming problems using Numerics.NET’s optimization capabilities. Linear programming is a method for achieving the best outcome in a mathematical model whose requirements are represented by linear relationships.
The sample illustrates three different approaches to creating and solving linear programs:
-
Matrix-based approach - Shows how to construct a linear program by specifying the cost vector, constraint coefficients matrix, and right-hand side vector directly. This is useful when working with problems that are naturally expressed in matrix form.
-
Incremental construction - Demonstrates building a linear program step by step by adding variables and constraints individually. This approach provides more clarity and is often more maintainable for smaller problems.
-
MPS file input - Shows how to read a linear program from an MPS format file, which is an industry standard format for representing linear and integer programming problems.
For each approach, the sample shows how to:
- Set up the linear program
- Solve it using the Revised Simplex algorithm
- Retrieve both the primal and dual solutions
- Access the optimal objective value
The sample includes proper error handling and demonstrates best practices for working with the LinearProgram class.
The code
using System;
// The linear programming classes reside in a namespace with
// other optimization-related classes.
using Numerics.NET.Optimization;
// Vectors and matrices are in the Numerics.NET
// namespace
using Numerics.NET;
// Illustrates solving linear programming problems
// using the classes in the Numerics.NET.Optimization
// namespace of Numerics.NET.
// The license is verified at runtime. We're using
// a 30 day trial key here. For more information, see
// https://numerics.net/trial-key
Numerics.NET.License.Verify("your-trial-key-here");
// This QuickStart Sample illustrates the three ways to create a Linear Program.
// The first is in terms of matrices. The coefficients
// are supplied as a matrix. The cost vector, right-hand side
// and constraints on the variables are supplied as a vector.
// The cost vector:
var c = Vector.Create(-1.0, -3.0, 0.0, 0.0, 0.0, 0.0);
// The coefficients of the constraints:
var A = Matrix.CreateFromArray(4, 6, new double[]
{
1, 1, 1, 0, 0, 0,
1, 1, 0, -1, 0, 0,
1, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 1
}, MatrixElementOrder.RowMajor);
// The right-hand sides of the constraints:
var b = Vector.Create(1.5, 0.5, 1.0, 1.0);
// We're now ready to call the constructor.
// The last parameter specifies the number of equality
// constraints.
var lp1 = new LinearProgram(c, A, b, 4);
// Now we can call the Solve method to run the Revised
// Simplex algorithm:
var x = lp1.Solve();
// The GetDualSolution method returns the dual solution:
var y = lp1.GetDualSolution();
Console.WriteLine($"Primal: {x:F1}");
Console.WriteLine($"Dual: {y:F1}");
// The optimal value is returned by the Extremum property:
Console.WriteLine($"Optimal value: {lp1.OptimalValue:F1}");
// The second way to create a Linear Program is by constructing
// it by hand. We start with an 'empty' linear program.
var lp2 = new LinearProgram();
// Next, we add two variables: we specify the name, the cost,
// and optionally the lower and upper bound.
lp2.AddVariable("X1", -1.0, 0, 1);
lp2.AddVariable("X2", -3.0, 0, 1);
// Next, we add constraints. Constraints also have a name.
// We also specify the coefficients of the variables,
// the lower bound and the upper bound.
lp2.AddLinearConstraint("C1", Vector.Create(1.0, 1.0), 0.5, 1.5);
// If a constraint is a simple equality or inequality constraint,
// you can supply a LinearProgramConstraintType value and the
// right-hand side of the constraint.
// We can now solve the linear program:
x = lp2.Solve();
y = lp2.GetDualSolution();
Console.WriteLine($"Primal: {x:F1}");
Console.WriteLine($"Dual: {y:F1}");
Console.WriteLine($"Optimal value: {lp2.OptimalValue:F1}");
// Finally, we can create a linear program from an MPS file.
// The MPS format is a standard format.
var lp3 = MpsReader.Read(@"..\..\..\..\..\data\sample.mps");
// We can go straight to solving the linear program:
x = lp3.Solve();
y = lp3.GetDualSolution();
Console.WriteLine($"Primal: {x:F1}");
Console.WriteLine($"Dual: {y:F1}");
Console.WriteLine($"Optimal value: {lp3.OptimalValue:F1}");
Console.Write("Press Enter key to exit...");
Console.ReadLine();