How to: Use Linear Programming using the Solver Foundation Solver APIs

You can use linear programming to minimize or maximize functions. In this example, an oil refinery must procure crude oil from two sources. The objective is to minimize the purchase cost of crude oils that differ in quality and to meet minimum production levels of 2,000 barrels of gasoline, 1,500 barrels of jet fuel, and 500 barrels of machine lubricant. Meanwhile, the suppliers cannot exceed their maximum daily production of crude oil. The following table shows the costs and capabilities of the two different crude oils.

Source:

Saudi Arabia refining

Venezuela refining

Cost:

$20 per barrel

$15 per barrel

Maximum daily production:

9,000 barrels

6,000 barrels

Refining percentages:

30% gasoline

40% jet fuel

20% lubricant

10% waste

40% gasoline

20% jet fuel

30% lubricant

10% waste

The following steps show how to use Solver Foundation to create and solve the refining model by developing directly against the simplex solver.

To use linear programming and the solver APIs to create the model

  1. Create a console application named PetroChem.

  2. Add a reference to Microsoft Solver Foundation on the .NET tab of the Add Reference dialog box.

  3. Add the following Imports or using statements to the top of the Program code file.

    Imports Microsoft.SolverFoundation.Common
    Imports Microsoft.SolverFoundation.Solvers
    
    using Microsoft.SolverFoundation.Common;
    using Microsoft.SolverFoundation.Solvers;
    
  4. In the Main method, use the following code to add a solver.

    Dim solver As New SimplexSolver()
    
    SimplexSolver solver = new SimplexSolver();
    
  5. Add variables and their lower and upper bounds to the solver. The sources of crude oil are Saudi Arabia and Venezuela. In the following code, the AddVariable method is used to add these sources. Their maximum daily production of crude oil is defined by the SetBounds method.

    Dim savid As Integer, vzvid As Integer
    solver.AddVariable("Saudi Arabia", savid)
    solver.SetBounds(savid, 0, 9000)
    solver.AddVariable("Venezuela", vzvid)
    solver.SetBounds(vzvid, 0, 6000)
    
    int savid, vzvid;
    solver.AddVariable("Saudi Arabia", out savid);
    solver.SetBounds(savid, 0, 9000);
    solver.AddVariable("Venezuela", out vzvid);
    solver.SetBounds(vzvid, 0, 6000);
    
  6. Add three row identifiers for the gasoline, jet fuel, and machine lubricant constraints. Then add a row identifier to the model to represent the minimum cost of refining.

    Dim gasoline As Integer, jetfuel As Integer, machinelubricant As Integer, cost As Integer
    solver.AddRow("gasoline", gasoline)
    solver.AddRow("jetfuel", jetfuel)
    solver.AddRow("machinelubricant", machinelubricant)
    solver.AddRow("cost", cost)
    
    int gasoline, jetfuel, machinelubricant, cost;
    solver.AddRow("gasoline", out gasoline);
    solver.AddRow("jetfuel", out jetfuel);
    solver.AddRow("machinelubricant", out machinelubricant);
    solver.AddRow("cost", out cost);
    
  7. Add coefficients to the constraint rows and set bounds. In the following code, the refining capabilities of each crude oil for the two suppliers are added as decimals. For example, the Saudi Arabian crude oil produces 30% gasoline and the Venezuelan crude oil produces 40% gasoline. The SetCoefficient method is used to set these values. The SetBounds method is used to set the minimum production levels of 2,000 barrels of gasoline, 1,500 barrels of jet fuel, and 500 barrels of machine lubricant.

    solver.SetCoefficient(gasoline, savid, 0.3)
    solver.SetCoefficient(gasoline, vzvid, 0.4)
    solver.SetBounds(gasoline, 2000, Rational.PositiveInfinity)
    solver.SetCoefficient(jetfuel, savid, 0.4)
    solver.SetCoefficient(jetfuel, vzvid, 0.2)
    solver.SetBounds(jetfuel, 1500, Rational.PositiveInfinity)
    solver.SetCoefficient(machinelubricant, savid, 0.2)
    solver.SetCoefficient(machinelubricant, vzvid, 0.3)
    solver.SetBounds(machinelubricant, 500, Rational.PositiveInfinity)
    
    solver.SetCoefficient(gasoline, savid, 0.3);
    solver.SetCoefficient(gasoline, vzvid, 0.4);
    solver.SetBounds(gasoline, 2000, Rational.PositiveInfinity);
    solver.SetCoefficient(jetfuel, savid, 0.4);
    solver.SetCoefficient(jetfuel, vzvid, 0.2);
    solver.SetBounds(jetfuel, 1500, Rational.PositiveInfinity);
    solver.SetCoefficient(machinelubricant, savid, 0.2);
    solver.SetCoefficient(machinelubricant, vzvid, 0.3);
    solver.SetBounds(machinelubricant, 500, Rational.PositiveInfinity);
    
  8. Add the costs of the crude oils to the model. Specify that the solver should minimize the goal by setting the third parameter to true. A value of false maximizes the goal.

    solver.SetCoefficient(cost, savid, 20)
    solver.SetCoefficient(cost, vzvid, 15)
    solver.AddGoal(cost, 1, True)
    
    solver.SetCoefficient(cost, savid, 20);
    solver.SetCoefficient(cost, vzvid, 15);
    solver.AddGoal(cost, 1, true);
    
  9. Solve the model and get the report.

    solver.Solve(New SimplexSolverParams())
    
    Console.WriteLine("SA {0}, VZ {1}, Gasoline {2}, Jet Fuel {3}, Machine Lubricant {4}, Cost {5}",
                      solver.GetValue(savid).ToDouble(),
                      solver.GetValue(vzvid).ToDouble(),
                      solver.GetValue(gasoline).ToDouble(),
                      solver.GetValue(jetfuel).ToDouble(),
                      solver.GetValue(machinelubricant).ToDouble(),
                      solver.GetValue(cost).ToDouble())
    Console.ReadLine()
    
    solver.Solve(new SimplexSolverParams());
    
    Console.WriteLine("SA {0}, VZ {1}, Gasoline {2}, Jet Fuel {3}, Machine Lubricant {4}, Cost {5}",
        solver.GetValue(savid).ToDouble(),
        solver.GetValue(vzvid).ToDouble(),
        solver.GetValue(gasoline).ToDouble(),
        solver.GetValue(jetfuel).ToDouble(),
        solver.GetValue(machinelubricant).ToDouble(),
        solver.GetValue(cost).ToDouble());
    Console.ReadLine();
    
  10. Press F5 to build and run the code.

    The Command window shows the following results.

    SA 2000, VZ 3500, Gasoline 2000, Jet Fuel 1500, Machine Lubricant 1450, Cost 92500

See Also

Concepts

Developing with Solver Foundation Services (SFS)