Last active
October 6, 2019 02:05
-
-
Save onceknown/67f2b3afee25c6ea88a0ab74b0de0105 to your computer and use it in GitHub Desktop.
Solve for Linear Programming Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Prompt: | |
A farming family wishes to plant some barley and some wheat. They can plant a maximum | |
of 100 acres of barley and a maximum of 80 acres of wheat. However, they only have 120 | |
acres of land available for planting. Barley costs $20 per acre for seeds, and wheat costs | |
$30 per acre for seeds. However, they only have $3000 available for seed costs. They expect | |
a harvest of 1000 pounds per acre of barley and 3000 pounds per acre of wheat. How many | |
acres of each crop should they plant to maximize their total harvest? | |
*/ | |
/** | |
We're measuring in acre(s). Programming languages let you add numbers, but also let you add characters to "strings" of characters | |
*/ | |
let Unit = 'acre' | |
let UnitPlural = Unit + 's' | |
let YieldUnit = 'pound' | |
let YieldUnitPlural = 'pounds' | |
/* | |
We have constraints on land, funds, cost of crops and yield of crops | |
*/ | |
let AvailableLand = 120 | |
let AvailableFunds = 3000 | |
let AmountOfBarleyThatCanBePlantedOnAvailableLand = 100 | |
let AmountOfWheatThatCanBePlantedOnAvailableLand = 80 | |
let CostPerUnitOfBarley = 20 | |
let CostPerUnitOfWheat = 30 | |
let YieldPerUnitOfBarley = 1000 | |
let YieldPerUnitOfWheat = 3000 | |
/** | |
Since we're maximizing, we need a function to compute the true/false on | |
whether a combination of barley and wheat meets the equality of each of our equations. | |
"===" is the javascript operator for testing equality, so each function below uses the | |
"return" statement to pass along either "true" or "false" | |
*/ | |
let MaximumAcresOfBarleyEquality = function(AcresOfBarley, AcresOfWheat) { | |
return AcresOfBarley === AmountOfBarleyThatCanBePlantedOnAvailableLand | |
} | |
let MaximumAcresOfWheatEquality = function(AcresOfBarley, AcresOfWheat) { | |
return AcresOfWheat === AmountOfWheatThatCanBePlantedOnAvailableLand | |
} | |
let CostEquality = function(AcresOfBarley, AcresOfWheat) { | |
return CostPerUnitOfBarley * AcresOfBarley + CostPerUnitOfWheat * AcresOfWheat === AvailableFunds | |
} | |
let LandUseEquality = function(AcresOfBarley, AcresOfWheat) { | |
return AcresOfBarley + AcresOfWheat === AvailableLand | |
} | |
/** | |
We need our two linear functions in slope intercept form so we can calculate | |
wheat for every value of barley, using the "return" statement to pass it on | |
*/ | |
let LandUseEqualityAcresOfWheatGivenAcresOfBarley = function(AcresOfBarley) { | |
return 0 - AcresOfBarley + AvailableLand | |
} | |
let CostEqualityAcresOfWheatGivenAcresOfBarley = function(AcresOfBarley) { | |
return (AvailableFunds - CostPerUnitOfBarley * AcresOfBarley) / CostPerUnitOfWheat | |
} | |
/** | |
We need our objective function to calculate which vertex maximizes yield | |
*/ | |
let ObjectiveFunction = function(AcresOfBarley, AcresOfWheat) { | |
return YieldPerUnitOfBarley * AcresOfBarley + YieldPerUnitOfWheat * AcresOfWheat | |
} | |
/** | |
We define logging functions for logging our vertices and yield | |
*/ | |
let LogVertex = function(AcresOfBarley, AcresOfWheat) { | |
console.log(AcresOfBarley + ' ' + UnitPlural + ' of barley and ' + AcresOfWheat + ' ' + UnitPlural + ' of wheat') | |
} | |
let LogYield = function(AcresOfBarley, AcresOfWheat) { | |
console.log('which yields ' + Intl.NumberFormat().format(ObjectiveFunction(AcresOfBarley, AcresOfWheat)) + ' ' + YieldUnitPlural) | |
} | |
/** | |
Order equality functions and record how many there are for later | |
*/ | |
let Equalities = [ | |
MaximumAcresOfBarleyEquality, | |
LandUseEquality, | |
CostEquality, | |
MaximumAcresOfWheatEquality | |
] | |
let NumberOfEqualities = Equalities.length | |
/** | |
Initialize our highest yield combo at 0, 0 | |
*/ | |
let HighestYield = { | |
AcresOfBarley: 0, | |
AcresOfWheat: 0 | |
} | |
/** | |
Start our algorithm | |
*/ | |
// log the header for our recorded vertices | |
console.log('Vertices:') | |
// log our max wheat vertex | |
LogVertex(0, AmountOfWheatThatCanBePlantedOnAvailableLand) | |
// loop through every unit of barley up to its max | |
for (let x = 0; x <= AmountOfBarleyThatCanBePlantedOnAvailableLand; x++) { | |
// loop through our sequence of equalities stopping before the last one | |
for (let a = 0; a < NumberOfEqualities - 1; a++) { | |
let y | |
// compute y using land use equality when checking first two equalities | |
if (a < NumberOfEqualities / 2) { | |
y = LandUseEqualityAcresOfWheatGivenAcresOfBarley(x) | |
} | |
// compute y using cost equality when checking last two equalities | |
else { | |
y = CostEqualityAcresOfWheatGivenAcresOfBarley(x) | |
} | |
// now check the two equalities to see if they resolve true together, | |
// meaning they're a valid vertice. We use the syntax for accessing items | |
// in lists to get the equality function at [a] and the next item at [a + 1] | |
if ((Equalities[a](x, y) && Equalities[a + 1](x, y))) { | |
// log resolved vertice | |
LogVertex(x, y) | |
// compute the previous highest recorded yield and current yield using objective function | |
let PreviousHighestYield = ObjectiveFunction(HighestYield.AcresOfBarley, HighestYield.AcresOfWheat) | |
let Yield = ObjectiveFunction(x, y) | |
// if our new yield is higher than our previous recorded, make the new vertice the highest yield vertice | |
if (Yield > PreviousHighestYield) { | |
HighestYield.AcresOfBarley = x | |
HighestYield.AcresOfWheat = y | |
} | |
} | |
} | |
} | |
// log our max barley vertex | |
LogVertex(AmountOfBarleyThatCanBePlantedOnAvailableLand, 0) | |
// log solution | |
console.log('\nSolution:') | |
console.log('The maximimum total production opportunity is') | |
LogVertex(HighestYield.AcresOfBarley, HighestYield.AcresOfWheat) | |
LogYield(HighestYield.AcresOfBarley, HighestYield.AcresOfWheat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment