UNPKG

javascript-lp-solver

Version:

Easy to use, JSON oriented Linear Programming and Mixed Int. Programming Solver

171 lines (146 loc) 5.36 kB
/*global require*/ /*global console*/ var Tableau = require("./Tableau.js"); //------------------------------------------------------------------- // Description: Display a tableau matrix // and additional tableau information // //------------------------------------------------------------------- Tableau.prototype.log = function (message, force) { if (false && !force) { return; } console.log("****", message, "****"); console.log("Nb Variables", this.width - 1); console.log("Nb Constraints", this.height - 1); // console.log("Variable Ids", this.variablesPerIndex); console.log("Basic Indexes", this.varIndexByRow); console.log("Non Basic Indexes", this.varIndexByCol); console.log("Rows", this.rowByVarIndex); console.log("Cols", this.colByVarIndex); var digitPrecision = 5; // Variable declaration var varNameRowString = "", spacePerColumn = [" "], j, c, s, r, variable, varIndex, varName, varNameLength, nSpaces, valueSpace, nameSpace; var row, rowString; for (c = 1; c < this.width; c += 1) { varIndex = this.varIndexByCol[c]; variable = this.variablesPerIndex[varIndex]; if (variable === undefined) { varName = "c" + varIndex; } else { varName = variable.id; } varNameLength = varName.length; nSpaces = Math.abs(varNameLength - 5); valueSpace = " "; nameSpace = "\t"; /////////// /*valueSpace = " "; nameSpace = " "; for (s = 0; s < nSpaces; s += 1) { if (varNameLength > 5) { valueSpace += " "; } else { nameSpace += " "; } }*/ /////////// if (varNameLength > 5) { valueSpace += " "; } else { nameSpace += "\t"; } spacePerColumn[c] = valueSpace; varNameRowString += nameSpace + varName; } console.log(varNameRowString); var signSpace; // Displaying reduced costs var firstRow = this.matrix[this.costRowIndex]; var firstRowString = "\t"; /////////// /*for (j = 1; j < this.width; j += 1) { signSpace = firstRow[j] < 0 ? "" : " "; firstRowString += signSpace; firstRowString += spacePerColumn[j]; firstRowString += firstRow[j].toFixed(2); } signSpace = firstRow[0] < 0 ? "" : " "; firstRowString += signSpace + spacePerColumn[0] + firstRow[0].toFixed(2); console.log(firstRowString + " Z");*/ /////////// for (j = 1; j < this.width; j += 1) { signSpace = "\t"; firstRowString += signSpace; firstRowString += spacePerColumn[j]; firstRowString += firstRow[j].toFixed(digitPrecision); } signSpace = "\t"; firstRowString += signSpace + spacePerColumn[0] + firstRow[0].toFixed(digitPrecision); console.log(firstRowString + "\tZ"); // Then the basic variable rowByVarIndex for (r = 1; r < this.height; r += 1) { row = this.matrix[r]; rowString = "\t"; /////////// /*for (c = 1; c < this.width; c += 1) { signSpace = row[c] < 0 ? "" : " "; rowString += signSpace + spacePerColumn[c] + row[c].toFixed(2); } signSpace = row[0] < 0 ? "" : " "; rowString += signSpace + spacePerColumn[0] + row[0].toFixed(2);*/ /////////// for (c = 1; c < this.width; c += 1) { signSpace = "\t"; rowString += signSpace + spacePerColumn[c] + row[c].toFixed(digitPrecision); } signSpace = "\t"; rowString += signSpace + spacePerColumn[0] + row[0].toFixed(digitPrecision); varIndex = this.varIndexByRow[r]; variable = this.variablesPerIndex[varIndex]; if (variable === undefined) { varName = "c" + varIndex; } else { varName = variable.id; } console.log(rowString + "\t" + varName); } console.log(""); // Then reduced costs for optional objectives var nOptionalObjectives = this.optionalObjectives.length; if (nOptionalObjectives > 0) { console.log(" Optional objectives:"); for (var o = 0; o < nOptionalObjectives; o += 1) { var reducedCosts = this.optionalObjectives[o].reducedCosts; var reducedCostsString = ""; for (j = 1; j < this.width; j += 1) { signSpace = reducedCosts[j] < 0 ? "" : " "; reducedCostsString += signSpace; reducedCostsString += spacePerColumn[j]; reducedCostsString += reducedCosts[j].toFixed(digitPrecision); } signSpace = reducedCosts[0] < 0 ? "" : " "; reducedCostsString += signSpace + spacePerColumn[0] + reducedCosts[0].toFixed(digitPrecision); console.log(reducedCostsString + " z" + o); } } console.log("Feasible?", this.feasible); console.log("evaluation", this.evaluation); return this; };