Last active
October 24, 2023 21:23
-
-
Save Wetbikeboy2500/6816207434d2559e5e22573cbbaa9e47 to your computer and use it in GitHub Desktop.
Attempt at implementing the simplex method from a given example. Svelte example: https://svelte.dev/repl/fcf9e27ad4b2461aace83709ebb7f3d5?version=4.2.2
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
// Marbles | |
// x + y <= 8 | |
// y + z <= 4 | |
// x + y + z <= 10 | |
// max: 3x + 5y + 5z = P | |
// x + y + s1 = 8 | |
// y + z + s2 = 4 | |
// x + y + z + s3 = 10 | |
// -3x - 5y - 4z + p = 0 | |
const marbles = [ | |
['x1', 'x2', 'x3', 's1', 's2', 's3', 'p', 'c', 'label'], | |
[1, 1, 0, 1, 0, 0, 0, 8, 's1'], //s1 | |
[0, 1, 1, 0, 1, 0, 0, 4, 's2'], //s2 | |
[1, 1, 1, 0, 0, 1, 0, 10, 's3'], //s3 | |
[-3, -5, -4, 0, 0, 0, 1, 0, 'p'] //p | |
] | |
simplex(marbles); | |
// Marketing | |
// B + M + TV + S1 = 25 | |
// 500B + 100M + 1000TV + S2 = 20000 | |
// -20000B - 10000M - 50000TV + R = 0; | |
let marketing = [ | |
['B', 'M', 'TV', 'S1', 'S2', 'R', 'C', 'label'], | |
[1, 1, 1, 1, 0, 0, 25, 'S1'], //S1 | |
[500, 100, 1000, 0, 1, 0, 20000, 'S2'], //S2 | |
[-20000, -10000, -50000, 0, 0, 1, 0, 'R'] //R | |
] | |
simplex(marketing); | |
function simplex(matrix) { | |
for (let i = 0; i < 100; i++) { | |
console.log(`Iteration ${i + 1}`); | |
//are there any negative numbers in the last row? | |
const negativeNumbers = matrix[matrix.length - 1].some((x) => typeof x !== 'string' && x < 0); | |
if (!negativeNumbers) { | |
console.log('No negative numbers in the last row, so we are done!'); | |
console.log('Final matrix:'); | |
console.log(matrix); | |
//pretty print the solution | |
console.log('Final solution:'); | |
matrix.slice(1).forEach((row) => { | |
console.log(`${row[row.length - 2]} = ${row[row.length - 1]}`); | |
}); | |
break; | |
} | |
//find the pivot column | |
const pivotColumn = matrix[matrix.length - 1].indexOf(Math.min(...matrix[matrix.length - 1].slice(0, -1))); | |
//find the pivot row | |
let constants = Array.from({ length: matrix.length - 1 }, (_, i) => matrix[i][matrix[i].length - 2]).slice(1); | |
console.log(constants); | |
constants = constants.map((x, i) => { | |
if (typeof x === 'string') return x; | |
return x / matrix[i + 1][pivotColumn]; | |
}); | |
console.log(constants); | |
const pivotRow = constants.indexOf(Math.min(...constants.filter((e) => e >= 0))) + 1; | |
//find the pivot | |
const pivot = matrix[pivotRow][pivotColumn]; | |
//swap column label with pivot row label | |
const temp = matrix[0][pivotColumn]; | |
matrix[pivotRow][matrix[pivotRow].length - 1] = temp; | |
//convert other rows to zeros for the pivot | |
//convert pivot row to 1 | |
matrix[pivotRow] = matrix[pivotRow].map((x) => { | |
if (typeof x === 'string') return x; | |
return x / pivot; | |
}); | |
//convert other rows to zeros for the pivot | |
matrix.forEach((row, i) => { | |
if (i === pivotRow || i === 0) return; | |
const rowPivot = row[pivotColumn]; | |
matrix[i] = row.map((x, j) => { | |
if (typeof x === 'string') return x; | |
return x - rowPivot * matrix[pivotRow][j] | |
}); | |
}); | |
console.log(matrix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment