Skip to content

Instantly share code, notes, and snippets.

@MartinBodocky
Created April 26, 2016 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinBodocky/5fd3881f867fe367cb0726894ca0c66d to your computer and use it in GitHub Desktop.
Save MartinBodocky/5fd3881f867fe367cb0726894ca0c66d to your computer and use it in GitHub Desktop.
CenterSpace NMath Linear Programming example in F#
open System
open System.IO
#r @"C:\Program Files (x86)\CenterSpace\NMath 6.2\Assemblies\NMath.dll"
open CenterSpace.NMath
open CenterSpace.NMath.Core
open CenterSpace.NMath.Analysis
// 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.
let wheatPrice = 3.38
let wheatYield = 55.0
let wheatRevenuePerAcre = wheatPrice * wheatYield
// Currently, barley is $1.98/bushel. The farmer can expect a yield of 75 bushels/acre.
let barleyPrice = 1.98
let barleyYield = 75.0
let barleyRevenuePerAcre = barleyPrice * barleyYield
// Currently, corn is $1.70/bushel. The farmer can expect a yield of 110 bushels/acre.
let cornPrice = 1.70
let cornYield = 110.0
let cornRevenuePerAcre = cornPrice * cornYield
printfn "Maximize %Aw %Ab %Ac where" wheatRevenuePerAcre barleyRevenuePerAcre cornRevenuePerAcre
let revenue = new DoubleVector(wheatRevenuePerAcre, barleyRevenuePerAcre, cornRevenuePerAcre)
let lpProblem = new LinearProgrammingProblem(revenue)
// 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.
printfn "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. It's known that wheat requires
// 30 lb/acre, barley 8 lb/acre and corn 50 lb/acre.
printfn "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.
printfn "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.
printfn "w + b + c <= 640"
lpProblem.AddUpperBoundConstraint(new DoubleVector(1.0, 1.0, 1.0), 640.0)
for i in 0..revenue.Length - 1 do
lpProblem.AddLowerBound(i, 0.0)
// Create an LP solver
let solver = new PrimalSimplexSolver()
// Solve
solver.Solve(lpProblem)
// Was a finite solution found?
if solver.Result = PrimalSimplexSolver.SolveResult.Optimal then
printfn "solution: %s" (solver.OptimalX.ToString("f0"))
printfn "optimal value: %s" (solver.OptimalObjectiveFunctionValue.ToString("f0"))
// Let's say the farmer is also contractually obligated to farm at least 10 acres
// of barley.
printfn "Add variable bound: b >= 10"
lpProblem.AddLowerBound(1, 10.0)
// Solve again
solver.Solve(lpProblem)
// Good?
if solver.Result = PrimalSimplexSolver.SolveResult.Optimal then
printfn "solution: %s" (solver.OptimalX.ToString("f0"))
printfn "optimal value: %s" (solver.OptimalObjectiveFunctionValue.ToString("f0"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment