Files
d3-spring-model/examples/js/distances/distancePokerHands.js
Pitchaya Boonsarngsuk 61f2be55fe Init from given files
2017-11-07 21:33:16 +00:00

43 lines
1.4 KiB
JavaScript

/**
* The distance function that is specifically made for Poker Hands data set.
* The suit of the cards does not play an important role when finding
* the differences in poker hands so it was not used in calculations.
* @param {node} source
* @param {node} target
* @return {number} the distance between source and target nodes.
*/
function calculateDistancePoker(source, target) {
var sumDiff = 0.0,
ordDiff = 1.0,
ORD_FACTOR = 1.5,
cards = ["C1", "C2", "C3", "C4", "C5"],
cols = 0;
// Iterate through cards
for (var i = 0; i < cards.length; i++) {
card = cards[i];
if (source.hasOwnProperty(card) && target.hasOwnProperty(card)) {
var s = parseInt(source[card]),
t = parseInt(target[card]);
// Calculate the squared difference.
sumDiff += (s-t) * (s-t);
}
}
// Class of poker hands describes the similarities the best
// so give it more priority than checking the differences between cards.
if (source.hasOwnProperty("CLASS") && target.hasOwnProperty("CLASS")) {
var s = parseInt(source["CLASS"]),
t = parseInt(target["CLASS"]);
// If classes differ, then scale them by a factor.
if (s !== t) {
ordDiff *= (ORD_FACTOR * (Math.abs(s-t)))
}
}
sumDiff = Math.sqrt(sumDiff);
sumDiff *= ordDiff;
return sumDiff;
}