C# Linear Programming Example

← All NMath Code Examples

 

using System;

using CenterSpace.NMath.Core;


namespace CenterSpace.NMath.Examples.CSharp
{
  class LinearProgrammingExample
  {
    /// <summary>
    /// A .NET example in C# showing how to solve a linear system using linear programming and
    /// the primal simplex method.
    /// </summary>
    static void Main( string[] args )
    {
      // A farmer has 640 acres of farmland. It can be planted with wheat, barley, corn or a
      // combination of the three. The farmer wishes to maximize his profit subject to the
      // limits on land, fertilizer, and water.

      // Currently, wheat is $3.38/bushel. The farmer can expect a yield of 55 bushels/acre.
      double wheatPrice = 3.38;
      double wheatYield = 55.0;
      double wheatRevenuePerAcre = wheatPrice * wheatYield;

      // Currently, barley is $1.98/bushel. The farmer can expect a yield of 75 bushels/acre.
      double barleyPrice = 1.98;
      double barleyYield = 75.0;
      double barleyRevenuePerAcre = barleyPrice * barleyYield;

      // Currently, corn is $1.70/bushel. The farmer can expect a yield of 110 bushels/acre.
      double cornPrice = 1.70;
      double cornYield = 110.0;
      double cornRevenuePerAcre = cornPrice * cornYield;

      Console.WriteLine();

      // Therefore, the objective function is:
      Console.Write( "Maximize " + wheatRevenuePerAcre + "w + " );
      Console.WriteLine( barleyRevenuePerAcre + "b + " + cornRevenuePerAcre + "c" );
      Console.WriteLine( "where" );
      Console.WriteLine();

      var revenue = new DoubleVector( wheatRevenuePerAcre, barleyRevenuePerAcre, cornRevenuePerAcre );
      var lpProblem = new LinearProgrammingProblem( revenue );

      // The farmer has 8,000 lbs of nitrogen fertilizer. Its known that wheat requires
      // 12 lb/acre, barley 5 lb/acre and corn 22 lb/acre.
      Console.WriteLine( "12w + 5b + 22c <= 8000" );
      lpProblem.AddUpperBoundConstraint( new DoubleVector( 12.0, 5.0, 22.0 ), 8000.0 );

      // The farmer has 22,000 lbs of phosphate fertilizer. Its known that wheat requires
      // 30 lb/acre, barley 8 lb/acre and corn 50 lb/acre.
      Console.WriteLine( "30w + 8b + 50c <= 22000" );
      lpProblem.AddUpperBoundConstraint( new DoubleVector( 30.0, 8.0, 50.0 ), 22000.0 );

      // The farmer has a permit for 1,000 acre-feet of water. Wheat requires 1.5 ft of water, 
      // barley requires 0.7 and corn 2.2.
      Console.WriteLine( "1.5w + 0.7b + 2.2c <= 1200" );
      lpProblem.AddUpperBoundConstraint( new DoubleVector( 1.5, 0.7, 2.2 ), 1200.0 );

      // The farmer has a maximum of 640 acres for planting.
      Console.WriteLine( "w + b + c <= 640" );
      lpProblem.AddUpperBoundConstraint( new DoubleVector( 1.0, 1.0, 1.0 ), 640.0 );
      for ( int i = 0; i < revenue.Length; i++ )
      {
        lpProblem.AddLowerBound( i, 0.0 );
      }
      // Create an LP solver
      var solver = new PrimalSimplexSolver();

      // Solve
      solver.Solve( lpProblem );

      // Was a finite solution found?
      Console.WriteLine();
      if ( solver.Result == PrimalSimplexSolver.SolveResult.Optimal )
      {
        Console.WriteLine( "solution: " + solver.OptimalX.ToString( "f0" ) );
        Console.WriteLine();
        Console.WriteLine( "optimal value: " + solver.OptimalObjectiveFunctionValue.ToString( "f0" ) );
      }
      Console.WriteLine();

      // Lets say the farmer is also contractually obligated to farm at least 10 acres
      // of barley.
      Console.WriteLine( "Add variable bound: b >= 10" );
      lpProblem.AddLowerBound( 1, 10.0 );

      // Solve again
      solver.Solve( lpProblem );

      // Good?
      Console.WriteLine();
      if ( solver.Result == PrimalSimplexSolver.SolveResult.Optimal )
      {
        Console.WriteLine( "solution: " + solver.OptimalX.ToString( "f0" ) );
        Console.WriteLine();
        Console.WriteLine( "optimal value: " + solver.OptimalObjectiveFunctionValue.ToString( "f0" ) );
      }

      Console.WriteLine();
      Console.WriteLine( "Press Enter Key" );
      Console.Read();

    }  // Main

  }  // class

} // namespace

← All NMath Code Examples
Top