[TOC]
Imports System
Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Analysis
Namespace CenterSpace.NMath.Analysis.Examples.VisualBasic
' A .NET example in VB.NET showing how to solve a linear system with simplex method and
' linear programming.
Module LinearProgrammingExample
Sub Main()
Console.WriteLine()
' 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.
Dim WheatPrice As Double = 3.38
Dim WheatYield As Double = 55.0
Dim WheatRevenuePerAcre As Double = WheatPrice * WheatYield
' Currently, barley is $1.98/bushel. The farmer can expect a yield of 75 bushels/acre.
Dim barleyPrice As Double = 1.98
Dim barleyYield As Double = 75.0
Dim barleyRevenuePerAcre As Double = barleyPrice * barleyYield
' Currently, corn is $1.70/bushel. The farmer can expect a yield of 110 bushels/acre.
Dim cornPrice As Double = 1.7
Dim cornYield As Double = 110.0
Dim cornRevenuePerAcre As Double = cornPrice * cornYield
' Therefore, the objective function is:
Console.Write("Maximize " & WheatRevenuePerAcre & "w + ")
Console.WriteLine(barleyRevenuePerAcre & "b + " & cornRevenuePerAcre & "c")
Console.WriteLine("where")
Console.WriteLine()
Dim Revenue As New DoubleVector(WheatRevenuePerAcre, barleyRevenuePerAcre, cornRevenuePerAcre)
' Make a matrix big enough for 5 constraints and 3 variables.
Dim Constraints As New DoubleMatrix(5, 3)
' Make a vector of right-hand sides.
Dim RightHandSides As DoubleVector = New DoubleVector(Constraints.Rows)
' The farmer has 8,000 lbs of nitrogen fertilizer. It's known that wheat requires
' 12 lb/acre, barley 5 lb/acre and corn 22 lb/acre.
Console.WriteLine("12w + 5b + 22c <= 8000")
Constraints(0, Slice.All) = New DoubleVector(12.0, 5.0, 22.0)
RightHandSides(0) = 8000.0
' The farmer has 22,000 lbs of phosphate fertilizer. It's known that wheat requires
' 30 lb/acre, barley 8 lb/acre and corn 50 lb/acre.
Console.WriteLine("30w + 8b + 50c <= 22000")
Constraints(1, Slice.All) = New DoubleVector(30.0, 8.0, 50.0)
RightHandSides(1) = 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")
Constraints(2, Slice.All) = New DoubleVector(1.5, 0.7, 2.2)
RightHandSides(2) = 1200.0
' The farmer has a maximum of 640 acres for planting.
Console.WriteLine("w + b + c <= 640")
Constraints(3, Slice.All) = New DoubleVector(1.0, 1.0, 1.0)
RightHandSides(3) = 640.0
' Create an LP solver with an error tolerance of 0.001.
Dim Solver As New SimplexLPSolver(0.001)
' Solve
Solver.Solve(Revenue, Constraints, RightHandSides, 5, 0, 0)
' Was a finite solution found?
Console.WriteLine()
If (Solver.IsGood) Then
Console.WriteLine("solution: " & Solver.Solution.ToString("f0"))
Console.WriteLine()
Console.WriteLine("optimal value: " & Solver.OptimalValue.ToString("f0"))
End If
Console.WriteLine()
' Let's say the farmer is also contractually obligated to farm at least 50 acres
' of barley.
Console.WriteLine("Add variable bound: b >= 10")
Dim LowerBounds As New DoubleVector(0.0, 10.0, 0.0)
Dim UpperBounds As New DoubleVector(640.0, 640.0, 640.0)
' Solve again
Solver.Solve(Revenue, Constraints, RightHandSides, 5, 0, 0, LowerBounds, UpperBounds)
' Good?
Console.WriteLine()
If (Solver.IsGood) Then
Console.WriteLine("solution: " & Solver.Solution.ToString("f0"))
Console.WriteLine()
Console.WriteLine("optimal value: " & Solver.OptimalValue.ToString("f0"))
End If
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]