From dc02abaa4aaf0cd7e8da391d565e813c5860bdea Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Thu, 26 Jun 2014 21:19:41 +0100 Subject: [PATCH] Pivoting --- js/simplex.js | 44 +++++++++++++++++++++++- test/simplexSpec.js | 82 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/js/simplex.js b/js/simplex.js index b52a14b..fe61742 100644 --- a/js/simplex.js +++ b/js/simplex.js @@ -1,4 +1,4 @@ -function initialSol(prob, mat) { +function solution(prob, mat) { var sol = {}; var constraints = prob.constraints; if (constraints) { @@ -12,3 +12,45 @@ function initialSol(prob, mat) { } return sol; } + +function pivotVar(mat) { + var p = { + sym: mat.vars[0], + index: 0 + }; + var obj = mat.rows[mat.rows.length - 1] ; + var min = obj[0]; + for (var i = 1; i < mat.vars.length; i++) { + if (obj[i] < min) { + p = { + sym: mat.vars[i], + index: i + } + min = obj[i]; + } + } + if (min < 0) { + return p; + } else { + return null; + } +} + +function pivotRow(mat, pivotVar) { + var index = null; + var min = null; + + for (var i = 0; i < mat.rows.length - 1; i++) { + var v = mat.rows[i][pivotVar.index]; + if (v <= 0) { + continue; + } + var r = mat.rhs[i] / v; + if (min === null || r < min) { + min = r; + index = i; + } + } + + return index; +} diff --git a/test/simplexSpec.js b/test/simplexSpec.js index 676c658..589406d 100644 --- a/test/simplexSpec.js +++ b/test/simplexSpec.js @@ -1,5 +1,5 @@ describe('simplex', function () { - describe('initialSol', function () { + describe('solution', function () { it('should set up the initial solution', function () { var prob = { constraints: [ @@ -23,7 +23,7 @@ describe('simplex', function () { }; var mat = toMatrix(prob); - var sol = initialSol(prob, mat); + var sol = solution(prob, mat); expect(sol).toEqual({ '_s0': -14, @@ -31,4 +31,82 @@ describe('simplex', function () { }); }); }); + + describe('pivotVar', function () { + it('should select the correct pivot variable', function () { + var mat = { + vars: ['a', 'b', 'c', 'd', 'e'], + varIndices: {}, + rows: [ + [], + [1, -4, 2, -1, 5] + ], + rhs: [] + }; + + var pv = pivotVar(mat); + expect(pv).toEqual({ + sym: 'b', + index: 1 + }); + }); + + it('should return null if there is no appropriate pivot variable', function () { + var mat = { + vars: ['a', 'b', 'c', 'd', 'e'], + varIndices: {}, + rows: [ + [], + [1, 0, 2, 1, 0] + ], + rhs: [] + }; + + var pv = pivotVar(mat); + expect(pv).toBe(null); + }); + }); + + describe('pivotRow', function () { + it('should select the correct pivot row', function () { + var mat = { + vars: ['a'], + varIndices: {}, + rows: [ + [1], + [0], + [2], + [3], + [-1] + ], + rhs: [4, 1, 6, 10, 1] + }; + + var pv = { + sym: 'a', + index: 0 + }; + var pr = pivotRow(mat, pv); + expect(pr).toBe(2); + }); + + it('should return null if no pivot row candidates', function () { + var mat = { + vars: ['a'], + varIndices: {}, + rows: [ + [0], + [-1] + ], + rhs: [1, 1] + }; + + var pv = { + sym: 'a', + index: 0 + }; + var pr = pivotRow(mat, pv); + expect(pr).toBe(null); + }); + }); });