Skip to content

Commit

Permalink
added web-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
JWally committed Oct 10, 2019
1 parent 7779aa0 commit 55aa3f4
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 30 deletions.
106 changes: 91 additions & 15 deletions prod/solver.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ Solution.prototype.generateSolutionSet = function () {

var varValue = matrix[r][rhsColumn];
solutionSet[variable.id] =
Math.round(varValue * roundingCoeff) / roundingCoeff;
Math.round((Number.EPSILON + varValue) * roundingCoeff) / roundingCoeff;
}

return solutionSet;
Expand Down Expand Up @@ -1116,6 +1116,8 @@ Tableau.prototype.initialize = function (width, height, variables, unrestrictedV
this.width = width;
this.height = height;


// console.time("tableau_build");
// BUILD AN EMPTY ARRAY OF THAT WIDTH
var tmpRow = new Array(width);
for (var i = 0; i < width; i++) {
Expand All @@ -1128,6 +1130,17 @@ Tableau.prototype.initialize = function (width, height, variables, unrestrictedV
this.matrix[j] = tmpRow.slice();
}

//
// TODO: Benchmark This
//this.matrix = new Array(height).fill(0).map(() => new Array(width).fill(0));

// console.timeEnd("tableau_build");
// console.log("height",height);
// console.log("width",width);
// console.log("------");
// console.log("");


this.varIndexByRow = new Array(this.height);
this.varIndexByCol = new Array(this.width);

Expand Down Expand Up @@ -1247,7 +1260,7 @@ Tableau.prototype.setEvaluation = function () {
var roundingCoeff = Math.round(1 / this.precision);
var evaluation = this.matrix[this.costRowIndex][this.rhsColumn];
var roundedEvaluation =
Math.round(evaluation * roundingCoeff) / roundingCoeff;
Math.round((Number.EPSILON + evaluation) * roundingCoeff) / roundingCoeff;

this.evaluation = roundedEvaluation;
if (this.simplexIters === 0) {
Expand Down Expand Up @@ -2003,7 +2016,7 @@ Tableau.prototype.updateVariableValues = function () {
} else {
// Variable is basic
var varValue = this.matrix[r][this.rhsColumn];
variable.value = Math.round(varValue * roundingCoeff) / roundingCoeff;
variable.value = Math.round((varValue + Number.EPSILON) * roundingCoeff) / roundingCoeff;
}
}
};
Expand Down Expand Up @@ -2564,15 +2577,24 @@ Tableau.prototype.phase1 = function () {
var iterations = 0;

while (true) {
// ******************************************
// ** PHASE 1 - STEP 1 : FIND PIVOT ROW **
//
// Selecting leaving variable (feasibility condition):
// Basic variable with most negative value
//
// ******************************************
var leavingRowIndex = 0;
var rhsValue = -this.precision;
for (var r = 1; r <= lastRow; r++) {
unrestricted = this.unrestrictedVars[this.varIndexByRow[r]] === true;
if (unrestricted) {
continue;
}

//
// *Don't think this does anything...
//
//if (unrestricted) {
// continue;
//}

var value = matrix[r][rhsColumn];
if (value < rhsValue) {
Expand All @@ -2588,16 +2610,26 @@ Tableau.prototype.phase1 = function () {
return iterations;
}


// ******************************************
// ** PHASE 1 - STEP 2 : FIND PIVOT COLUMN **
//
//
// ******************************************
// Selecting entering variable
var enteringColumn = 0;
var maxQuotient = -Infinity;
var costRow = matrix[0];
var leavingRow = matrix[leavingRowIndex];
for (var c = 1; c <= lastColumn; c++) {
var coefficient = leavingRow[c];
if (-this.precision < coefficient && coefficient < this.precision) {
continue;
}
//
// *Don't think this does anything...
//
//if (-this.precision < coefficient && coefficient < this.precision) {
// continue;
//}
//

unrestricted = this.unrestrictedVars[this.varIndexByCol[c]] === true;
if (unrestricted || coefficient < -this.precision) {
Expand Down Expand Up @@ -2801,6 +2833,8 @@ Tableau.prototype.phase2 = function () {
// on the pivot row
// Shared by all tableaux for smaller overhead and lower memory usage
var nonZeroColumns = [];


//-------------------------------------------------------------------
// Description: Execute pivot operations over a 2d array,
// on a given row, and column
Expand Down Expand Up @@ -2831,10 +2865,12 @@ Tableau.prototype.pivot = function (pivotRowIndex, pivotColumnIndex) {
var pivotRow = matrix[pivotRowIndex];
var nNonZeroColumns = 0;
for (var c = 0; c <= lastColumn; c++) {
if (pivotRow[c] !== 0) {
if (!(pivotRow[c] >= -1e-16 && pivotRow[c] <= 1e-16)) {
pivotRow[c] /= quotient;
nonZeroColumns[nNonZeroColumns] = c;
nNonZeroColumns += 1;
} else {
pivotRow[c] = 0;
}
}
pivotRow[pivotColumnIndex] = 1 / quotient;
Expand All @@ -2845,27 +2881,60 @@ Tableau.prototype.pivot = function (pivotRowIndex, pivotColumnIndex) {
// row by ... yuck... just look below; better explanation later
var coefficient, i, v0;
var precision = this.precision;

// //////////////////////////////////////
//
// This is step 2 of the pivot function.
// It is, by far, the most expensive piece of
// this whole process where the code can be optimized (faster code)
// without changing the whole algorithm (fewer cycles)
//
// 1.) For every row but the pivot row
// 2.) Update each column to
// a.) itself
// less
// b.) active-row's pivot column
// times
// c.) whatever-the-hell this is: nonZeroColumns[i]
//
// //////////////////////////////////////
// console.time("step-2");
for (var r = 0; r <= lastRow; r++) {
var row = matrix[r];
if (r !== pivotRowIndex) {

// Set reference to the row we're working on
//
var row = matrix[r];

// Catch the coefficient that we're going to end up dividing everything by
coefficient = row[pivotColumnIndex];

// No point Burning Cycles if
// Zero to the thing
if (coefficient !== 0) {
if (!(coefficient >= -1e-16 && coefficient <= 1e-16)) {
for (i = 0; i < nNonZeroColumns; i++) {
c = nonZeroColumns[i];
// No point in doing math if you're just adding
// Zero to the thing
v0 = pivotRow[c];
if (v0 !== 0) {
if (!(v0 >= -1e-16 && v0 <= 1e-16)) {
row[c] = row[c] - coefficient * v0;
} else {
if(v0 !== 0){
pivotRow[c] = 0;
}
}
}

row[pivotColumnIndex] = -coefficient / quotient;
} else {
if(coefficient !== 0){
row[pivotColumnIndex] = 0;
}
}
}
}
// console.timeEnd("step-2");

var nOptionalObjectives = this.optionalObjectives.length;
if (nOptionalObjectives > 0) {
Expand Down Expand Up @@ -3302,8 +3371,15 @@ var Solver = function () {

// 3.) Load all of the variable values
Object.keys(solution.solutionSet)
.map(function (d) {
store[d] = solution.solutionSet[d];
.forEach(function (d) {
//
// When returning data in standard format,
// Remove all 0's
//
if(solution.solutionSet[d] !== 0){
store[d] = solution.solutionSet[d];
}

});

return store;
Expand Down
Loading

0 comments on commit 55aa3f4

Please sign in to comment.