Extreme Optimization >
QuickStart Samples >
Linear Programming QuickStart Sample (C#)
Extreme Optimization QuickStart Samples
Linear Programming QuickStart Sample (C#)
Illustrates solving linear programming (LP) problems
using classes in the Extreme.Mathematics.Optimization.LinearProgramming namespace in
C#.
VB.NET code Back
to QuickStart Samples
using System;
namespace Extreme.Mathematics.QuickStart.CSharp
{
// The linear programming classes reside in their own namespace.
using Extreme.Mathematics.Optimization.LinearProgramming;
// Vectors and matrices are in the Extreme.Mathematics.LinearAlgebra
// namespace
using Extreme.Mathematics.LinearAlgebra;
/// <summary>
/// Illustrates solving linear programming problems
/// using the classes in the Extreme.Mathematics.Optimization.LinearProgramming
/// namespace of the Extreme Optimization Numerical Libraries for .NET.
/// </summary>
class LinearProgramming
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// 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:
Vector c = new GeneralVector(-1.0, -3.0, 0.0, 0.0, 0.0, 0.0);
// The coefficients of the constraints:
Matrix A = new GeneralMatrix(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:
Vector b = new GeneralVector(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.
LinearProgram lp1 = new LinearProgram(c, A, b, 4);
// Now we can call the Solve method to run the Revised
// Simplex algorithm:
Vector x = lp1.Solve();
// The GetDualSolution method returns the dual solution:
Vector y = lp1.GetDualSolution();
Console.WriteLine("Primal: {0:F1}", x);
Console.WriteLine("Dual: {0:F1}", y);
// The optimal value is returned by the Extremum property:
Console.WriteLine("Optimal value: {0:F1}", lp1.Extremum);
// The second way to create a Linear Program is by constructing
// it by hand. We start with an 'empty' linear program.
LinearProgram 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.AddConstraint("C1", new GeneralVector(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: {0:F1}", x);
Console.WriteLine("Dual: {0:F1}", y);
Console.WriteLine("Optimal value: {0:F1}", lp2.Extremum);
// Finally, we can create a linear program from an MPS file.
// The MPS format is a standard format.
LinearProgram lp3 = MpsReader.Read(@"..\..\sample.mps");
// We can go straight to solving the linear program:
x = lp2.Solve();
y = lp2.GetDualSolution();
Console.WriteLine("Primal: {0:F1}", x);
Console.WriteLine("Dual: {0:F1}", y);
Console.WriteLine("Optimal value: {0:F1}", lp2.Extremum);
Console.Write("Press Enter key to exit...");
Console.ReadLine();
}
}
}
Copyright 2004-2007,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, and Visual
Studio.NET are registered trademarks of Microsoft Corporation