From ef60915aa9d88b314d68a7fb0c987437abb93953 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Mon, 21 Jul 2014 08:57:10 +0100 Subject: [PATCH] Detect unbounded problem --- js/simplex.js | 14 ++++++++++---- test/simplexSpec.js | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/js/simplex.js b/js/simplex.js index a799496..38e8a7b 100644 --- a/js/simplex.js +++ b/js/simplex.js @@ -224,18 +224,24 @@ function solve(mat, debug) { if (pv === null) { pv = pivotVar(mat); } + if (debug) { + console.log(pv); + } if (pv === null) { break; } var pr = pivotRow(mat, pv); - if (pr === null) { - break; - } if (debug) { - console.log(pv); console.log(pr); } + if (pr === null) { + mat.status = 'unbounded'; + break; + } mat = pivot(mat, pr, pv.col); } + if (!mat.status) { + mat.status = 'optimal'; + } return mat; } diff --git a/test/simplexSpec.js b/test/simplexSpec.js index e39e96c..bd722f3 100644 --- a/test/simplexSpec.js +++ b/test/simplexSpec.js @@ -364,6 +364,21 @@ describe('simplex', function () { expect(sol._obj).toBeCloseTo(200); }); + iit('should detect an unbouned problem', function () { + var prob = readLp( + "maximise\n\ + 3x - 2y\n\ + subject to\n\ + y <= 100\n\ + end\n" + ); + + var mat = toMatrix(prob); + var finalMat = solve(mat); + + expect(finalMat.status).toBe('unbounded'); + }); + it('should solve a small MPS problem', function () { var prob = readMps("\ NAME T \n\