javascript-lp-solver
Version:
Easy to use, JSON oriented Linear Programming and Mixed Int. Programming Solver
117 lines (91 loc) • 3.52 kB
JavaScript
/*global require*/
var Tableau = require("./Tableau.js");
Tableau.prototype.copy = function () {
var copy = new Tableau(this.precision);
copy.width = this.width;
copy.height = this.height;
copy.nVars = this.nVars;
copy.model = this.model;
// Making a shallow copy of integer variable indexes
// and variable ids
copy.variables = this.variables;
copy.variablesPerIndex = this.variablesPerIndex;
copy.unrestrictedVars = this.unrestrictedVars;
copy.lastElementIndex = this.lastElementIndex;
// All the other arrays are deep copied
copy.varIndexByRow = this.varIndexByRow.slice();
copy.varIndexByCol = this.varIndexByCol.slice();
copy.rowByVarIndex = this.rowByVarIndex.slice();
copy.colByVarIndex = this.colByVarIndex.slice();
copy.availableIndexes = this.availableIndexes.slice();
var optionalObjectivesCopy = [];
for(var o = 0; o < this.optionalObjectives.length; o++){
optionalObjectivesCopy[o] = this.optionalObjectives[o].copy();
}
copy.optionalObjectives = optionalObjectivesCopy;
var matrix = this.matrix;
var matrixCopy = new Array(this.height);
for (var r = 0; r < this.height; r++) {
matrixCopy[r] = matrix[r].slice();
}
copy.matrix = matrixCopy;
return copy;
};
Tableau.prototype.save = function () {
this.savedState = this.copy();
};
Tableau.prototype.restore = function () {
if (this.savedState === null) {
return;
}
var save = this.savedState;
var savedMatrix = save.matrix;
this.nVars = save.nVars;
this.model = save.model;
// Shallow restore
this.variables = save.variables;
this.variablesPerIndex = save.variablesPerIndex;
this.unrestrictedVars = save.unrestrictedVars;
this.lastElementIndex = save.lastElementIndex;
this.width = save.width;
this.height = save.height;
// Restoring matrix
var r, c;
for (r = 0; r < this.height; r += 1) {
var savedRow = savedMatrix[r];
var row = this.matrix[r];
for (c = 0; c < this.width; c += 1) {
row[c] = savedRow[c];
}
}
// Restoring all the other structures
var savedBasicIndexes = save.varIndexByRow;
for (c = 0; c < this.height; c += 1) {
this.varIndexByRow[c] = savedBasicIndexes[c];
}
while (this.varIndexByRow.length > this.height) {
this.varIndexByRow.pop();
}
var savedNonBasicIndexes = save.varIndexByCol;
for (r = 0; r < this.width; r += 1) {
this.varIndexByCol[r] = savedNonBasicIndexes[r];
}
while (this.varIndexByCol.length > this.width) {
this.varIndexByCol.pop();
}
var savedRows = save.rowByVarIndex;
var savedCols = save.colByVarIndex;
for (var v = 0; v < this.nVars; v += 1) {
this.rowByVarIndex[v] = savedRows[v];
this.colByVarIndex[v] = savedCols[v];
}
if (save.optionalObjectives.length > 0 && this.optionalObjectives.length > 0) {
this.optionalObjectives = [];
this.optionalObjectivePerPriority = {};
for(var o = 0; o < save.optionalObjectives.length; o++){
var optionalObjectiveCopy = save.optionalObjectives[o].copy();
this.optionalObjectives[o] = optionalObjectiveCopy;
this.optionalObjectivePerPriority[optionalObjectiveCopy.priority] = optionalObjectiveCopy;
}
}
};