Created
January 21, 2017 14:14
-
-
Save BraedonWooding/d39cb782c990bedc966b6d3615ada5b0 to your computer and use it in GitHub Desktop.
The javascript implementation of the shultze method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pairwiseMatrix = []; | |
var strongestPairMatrix = []; | |
var voteOptions = []; | |
function Main() { | |
// Dummy Data | |
voteOptions = ["A", "B", "C", "D", "E"]; | |
InitialiseMatrix(); | |
// Dummy Data | |
var votes = []; | |
votes.push({ votes: ["A","C","B","E","D"], value: 5 }); | |
votes.push({ votes: ["A","D","E","C","B"], value: 5 }); | |
votes.push({ votes: ["B","E","D","A","C"], value: 8 }); | |
votes.push({ votes: ["C","A","B","E","D"], value: 3 }); | |
votes.push({ votes: ["C","A","E","B","D"], value: 7 }); | |
votes.push({ votes: ["C","B","A","D","E"], value: 2 }); | |
votes.push({ votes: ["D","C","E","B","A"], value: 7 }); | |
votes.push({ votes: ["E","B","A","D","C"], value: 8 }); | |
SetMatrixValues(votes); | |
SetWidestPathMatrix(); | |
PrintMatrix(pairwiseMatrix); | |
console.log("NEW"); | |
PrintMatrix(strongestPairMatrix); | |
} | |
function PrintMatrix(matrix) { | |
for (var x = 0; x < matrix.length; x++) { | |
var yText = ""; | |
for (var y = 0; y < matrix[x].column.length; y++) { | |
if (matrix[x].column[y].value == 0) { | |
yText += " - | "; | |
} | |
else { | |
yText += matrix[x].column[y].value + " | "; | |
} | |
} | |
console.log(yText); | |
} | |
} | |
// Sets the matrixes with values | |
// Verbose just to make it make more sense | |
// Since it merges two functions into one | |
function SetMatrixValues(votes) { | |
for (var i = 0; i < votes.length; i++) { | |
var numberOfVotes = votes[i].value; | |
var ranking = votes[i].votes; | |
for (var x = 0; x < voteOptions.length; x++) { | |
var optionXRank = ranking.indexOf(voteOptions[x]); | |
// Optimisation | |
if (optionXRank == -1) { | |
continue; | |
} | |
for (var y = 0; y < voteOptions.length; y++) { | |
var optionYRank = ranking.indexOf(voteOptions[y]); | |
console.log("Comparing option x = " + voteOptions[x] + | |
". With option y = " + voteOptions[y] + | |
". in" + ranking.toString() + | |
". with the value of: " + numberOfVotes); | |
if (optionXRank != optionYRank) { | |
if (optionXRank < optionYRank || optionYRank == -1) { | |
pairwiseMatrix[x].column[y].value += numberOfVotes; | |
} | |
} | |
} | |
} | |
} | |
} | |
function SetWidestPathMatrix() { | |
// Replicate matrix | |
for (var x = 0; x < voteOptions.length; x++) { | |
for (var y = 0; y < voteOptions.length; y++) { | |
if (voteOptions[x] != voteOptions[y]) { | |
if (pairwiseMatrix[x].column[y].value > pairwiseMatrix[y].column[x].value) { | |
strongestPairMatrix[x].column[y].value = pairwiseMatrix[x].column[y].value; | |
} | |
else { | |
strongestPairMatrix[x].column[y].value = 0; | |
} | |
for (var z = 0; z < voteOptions.length; z++) { | |
if (voteOptions[x] != voteOptions[z] && voteOptions[y] != voteOptions[z]) { | |
strongestPairMatrix[y].column[z].value = | |
Math.max(strongestPairMatrix[y].column[z].value, | |
Math.min(strongestPairMatrix[y].column[x].value, strongestPairMatrix[x].column[z].value)); | |
} | |
} | |
} | |
} | |
} | |
} | |
// Creates the matrixes | |
function InitialiseMatrix() { | |
pairwiseMatrix = []; | |
strongestPairMatrix = []; | |
// Create matrixes, via for loops | |
for (var x = 0; x < voteOptions.length; x++) { | |
// Create rows | |
pairwiseMatrix.push({ rowVote: voteOptions[x].toString(), column: [] }); | |
strongestPairMatrix.push({ rowVote: voteOptions[x].toString(), column: [] }); | |
for (var y = 0; y < voteOptions.length; y++) { | |
// Create columns | |
pairwiseMatrix[x].column.push({ columnVote: voteOptions[y].toString(), value: 0 }); | |
strongestPairMatrix[x].column.push({ columnVote: voteOptions[y].toString(), value: 0 }); | |
} | |
} | |
} | |
Main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment