Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional objectives now considered in MILP #40

Merged
merged 1 commit into from
Mar 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 49 additions & 33 deletions src/MILP.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/*global console*/
/*global process*/
var Solution = require("./Solution.js");
var Tableau = require("./Tableau/Tableau.js");

//-------------------------------------------------------------------
//-------------------------------------------------------------------
Expand Down Expand Up @@ -42,45 +43,46 @@ function sortByEvaluation(a, b) {
//-------------------------------------------------------------------
// Applying cuts on a tableau and resolving
//-------------------------------------------------------------------
function applyCuts(tableau, cuts){
Tableau.prototype.applyCuts = function (branchingCuts){
// Restoring initial solution
tableau.restore();

tableau.addCutConstraints(cuts);
tableau.solve();
this.restore();

this.addCutConstraints(branchingCuts);
this.simplex();
// Adding MIR cuts
var fractionalVolumeImproved = true;
while(fractionalVolumeImproved){
var fractionalVolumeBefore = tableau.computeFractionalVolume(true);

tableau.applyMIRCuts();
tableau.solve();
var fractionalVolumeBefore = this.computeFractionalVolume(true);
this.applyMIRCuts();
this.simplex();

var fractionalVolumeAfter = tableau.computeFractionalVolume(true);
var fractionalVolumeAfter = this.computeFractionalVolume(true);

// If the new fractional volume is bigger than 90% of the previous one
// we assume there is no improvement from the MIR cuts
if(fractionalVolumeAfter >= 0.9 * fractionalVolumeBefore){
fractionalVolumeImproved = false;
}
}
}
};

//-------------------------------------------------------------------
// Function: MILP
// Detail: Main function, my attempt at a mixed integer linear programming
// solver
//-------------------------------------------------------------------
function MILP(model) {
Tableau.prototype.MILP = function () {
var branches = [];
var iterations = 0;
var tableau = model.tableau;

// This is the default result
// If nothing is both *integral* and *feasible*
var bestEvaluation = Infinity;
var bestBranch = null;
var bestOptionalObjectivesEvaluations = [];
for (var oInit = 0; oInit < this.optionalObjectives.length; oInit += 1){
bestOptionalObjectivesEvaluations.push(Infinity);
}

// And here...we...go!

Expand All @@ -92,7 +94,7 @@ function MILP(model) {
while (branches.length > 0) {
// Get a model from the queue
branch = branches.pop();
if (branch.relaxedEvaluation >= bestEvaluation) {
if (branch.relaxedEvaluation > bestEvaluation) {
continue;
}

Expand All @@ -101,37 +103,53 @@ function MILP(model) {

// Adding cut constraints
var cuts = branch.cuts;

applyCuts(tableau, cuts);

// console.log(iterations, tableau.matrix[0][tableau.rhsColumn], tableau.feasible, branches.length);
this.applyCuts(cuts);

iterations++;
if (tableau.feasible === false) {
if (this.feasible === false) {
continue;
}

var evaluation = tableau.evaluation;
if (evaluation >= bestEvaluation) {
var evaluation = this.evaluation;
if (evaluation > bestEvaluation) {
// This branch does not contain the optimal solution
continue;
}

// To deal with the optional objectives
if (evaluation === bestEvaluation){
var isCurrentEvaluationWorse = true;
for (var o = 0; o < this.optionalObjectives.length; o += 1){
if (this.optionalObjectives[o].reducedCosts[0] > bestOptionalObjectivesEvaluations[o]){
break;
} else if (this.optionalObjectives[o].reducedCosts[0] < bestOptionalObjectivesEvaluations[o]) {
isCurrentEvaluationWorse = false;
break;
}
}

if (isCurrentEvaluationWorse){
continue;
}
}

// Is the model both integral and feasible?
if (tableau.isIntegral() === true) {
if (this.isIntegral() === true) {
if (iterations === 1) {
tableau.updateVariableValues();
return new MilpSolution(tableau.getSolution(), iterations);
// tableau.updateVariableValues();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commented line also can go

return new MilpSolution(this.getSolution(), iterations);
}

// Store the solution as the bestSolution
bestBranch = branch;
bestEvaluation = evaluation;
for (var oCopy = 0; oCopy < this.optionalObjectives.length; oCopy += 1){
bestOptionalObjectivesEvaluations[oCopy] = this.optionalObjectives[oCopy].reducedCosts[0];
}
} else {
if (iterations === 1) {
// Saving the first iteration
// TODO: implement a better strategy for saving the tableau?
tableau.save();
this.save();
}

// If the solution is
Expand Down Expand Up @@ -171,7 +189,7 @@ function MILP(model) {
// the model is not integral at this point, and fails.

// Find out where we want to split the solution
var variable = tableau.getMostFractionalVar();
var variable = this.getMostFractionalVar();
// var variable = tableau.getFractionalVarWithLowestCost();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be removed

var varIndex = variable.index;

Expand Down Expand Up @@ -215,12 +233,10 @@ function MILP(model) {
// Adding cut constraints for the optimal solution
if (bestBranch !== null) {
// The model is feasible
applyCuts(tableau, bestBranch.cuts);
tableau.updateVariableValues();
this.applyCuts(bestBranch.cuts);
// tableau.updateVariableValues();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be removed

}

// Solving a last time
return new MilpSolution(tableau.getSolution(), iterations);
}

module.exports = MILP;
return new MilpSolution(this.getSolution(), iterations);
};
19 changes: 8 additions & 11 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,14 @@ Model.prototype.solve = function () {
this.tableauInitialized = true;
}

if (this.getNumberOfIntegerVariables() > 0) {
return MILP(this);
} else {
var solution = this.tableau.solve().getSolution();
this.tableau.updateVariableValues();
return solution;
}
};

Model.prototype.compileSolution = function () {
return this.tableau.compileSolution();
return this.tableau.solve();
// if (this.getNumberOfIntegerVariables() > 0) {
// return MILP(this);
// } else {
// var solution = this.tableau.solve().getSolution();
// this.tableau.updateVariableValues();
// return solution;
// }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments can be removed

};

Model.prototype.isFeasible = function () {
Expand Down
16 changes: 16 additions & 0 deletions src/Tableau/Tableau.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ function Tableau(precision) {
}
module.exports = Tableau;

Tableau.prototype.solve = function () {
if (this.model.getNumberOfIntegerVariables() > 0) {
this.MILP();
} else {
this.simplex();
}
this.updateVariableValues();
return this.getSolution();
};

function OptionalObjective(priority, nColumns) {
this.priority = priority;
this.reducedCosts = new Array(nColumns);
Expand All @@ -66,6 +76,12 @@ function OptionalObjective(priority, nColumns) {
}
}

OptionalObjective.prototype.copy = function () {
var copy = new OptionalObjective(this.priority, this.reducedCosts.length);
copy.reducedCosts = this.reducedCosts.slice();
return copy;
};

Tableau.prototype.setOptionalObjective = function (priority, column, cost) {
var objectiveForPriority = this.objectivesByPriority[priority];
if (objectiveForPriority === undefined) {
Expand Down
17 changes: 16 additions & 1 deletion src/Tableau/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Tableau.prototype.copy = function () {

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);
Expand Down Expand Up @@ -97,5 +103,14 @@ Tableau.prototype.restore = function () {
this.colByVarIndex[v] = savedCols[v];
}

this.availableIndexes = save.availableIndexes.slice();

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;
}
}
};
7 changes: 6 additions & 1 deletion src/Tableau/simplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var Tableau = require("./Tableau.js");
// Function: solve
// Detail: Main function, linear programming solver
//-------------------------------------------------------------------
Tableau.prototype.solve = function () {
Tableau.prototype.simplex = function () {
// Bounded until proven otherwise
this.bounded = true;

Expand Down Expand Up @@ -159,6 +159,9 @@ Tableau.prototype.phase2 = function () {
while (enteringColumn === 0 && optionalCostsColumns.length > 0 && o < nOptionalObjectives) {
var optionalCostsColumns2 = [];
var reducedCosts = this.optionalObjectives[o].reducedCosts;

enteringValue = this.precision;

for (var i = 0; i <= optionalCostsColumns.length; i++) {
c = optionalCostsColumns[i];
reducedCost = reducedCosts[c];
Expand Down Expand Up @@ -199,6 +202,8 @@ Tableau.prototype.phase2 = function () {
var leavingRow = 0;
var minQuotient = Infinity;

var varIndexByRow = this.varIndexByRow;

for (var r = 1; r <= lastRow; r++) {
var row = matrix[r];
var rhsValue = row[rhsColumn];
Expand Down
2 changes: 1 addition & 1 deletion src/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Constraint.prototype.setVariableCoefficient = function (newCoefficient, variable

Constraint.prototype.relax = function (weight, priority) {
this.relaxation = createRelaxationVariable(this.model, weight, priority);
this._relax(this.relaxation, priority);
this._relax(this.relaxation);
};

Constraint.prototype._relax = function (error) {
Expand Down
Loading