afryca.ase.Snippet Library LPRAse Linguistic Preference Relation // // Linguistic preference relation class // // @param ids Labels // @param n Number of alternatives // var LPRAse = function LPRAse(ids, n) { this.ids = ids; // Ids this.n = n; // Number of alternatives this.preference = new FPR(n); // Asociate value in internal FPR this.labelsValues = function() { var bucket = 1 / (ids.length - 1); var result = []; for (var i = 0; i < ids.length; i++) { result[ids[i]] = (i * bucket).toFixed(4)/1; } return result; }(); // Beta of label value this.beta = function(value) { return (value * (this.ids.length - 1)) } // Linguistic value of FPR pair this.linguisticValue = function(value) { var beta = this.beta(value); var i = Math.round(beta); var label = ids[i]; var alpha = (beta - i).toFixed(4)/1; var result = {s:label, alpha:alpha, lpr:this}; result.toString = function() { return (result.alpha == 0) ? result.s : '(' + result.s + ', ' + result.alpha + ')'; } return result; } // FPR value for linguistic value this.valueFor = function (label, alpha) { var value = NaN; if (typeof label == 'string') { value = this.labelsValues[label]; if (typeof alpha == 'number') { value += (alpha * (1 / (this.ids.length - 1))).toFixed(2)/1; } } return value; } // Alternative value this.getValue = function (i, j) { var value = this.preference.getValue(i, j); return isNaN(value) ? NaN : this.linguisticValue(value); } // Set alternative value this.setValueSymmetrically = function (i, j, label, alpha) { this.preference.setValueSymmetrically(i | 0, j | 0, this.valueFor(label, alpha)); } // LPR string value this.toString = function() { var result = []; var lengths = []; var value; for (var i = 0; i < this.n; i++) { result[i] = []; for (var j = 0; j < this.n; j++) { value = this.getValue(i | 0, j | 0).toString(); result[i][j] = (i == j) ? '-' : ((value == 'NaN') ? '' : value); lengths[j] = (i == 0) ? result[i][j].length : Math.max(lengths[j], result[i][j].length); } } var output = ''; for (var i = 0; i < this.n; i++) { for (var j = 0; j < this.n; j++) { if (j > 0) { output += ' '; } output += '| ' + result[i][j]; for (var s = result[i][j].length; s < lengths[j]; s++) { output += ' '; } } output += ' |\n'; } return output; } } // // Xu's sum of two linguistic variables // // Deviation measures of linguistic preference relations in group decision making // http://dx.doi.org/10.1016/j.omega.2004.04.008 // // @param a Linguistic variable a // @param b Linguistic variable b // LPRAse.xuSum = function(a, b) { if ((typeof a == 'undefined') || (typeof b == 'undefined')) { return NaN; } if ((typeof a.lpr == 'undefined') || (typeof b.lpr == 'undefined')) { return NaN; } if (a.lpr.ids != b.lpr.ids) { return NaN; } if (!( a.lpr.ids.length & 1 )) { return NaN; } var lpr = a.lpr; var T = lpr.ids.length; var adjustment = Math.round((T-1) / 2); var s_a = lpr.beta(lpr.valueFor(a.s)) + a.alpha - adjustment; var s_b = lpr.beta(lpr.valueFor(b.s)) + b.alpha - adjustment; return s_a + s_b; } // // Xu's distance between two linguistic variables // // Deviation measures of linguistic preference relations in group decision making // http://dx.doi.org/10.1016/j.omega.2004.04.008 // // @param a Linguistic variable a // @param b Linguistic variable b // LPRAse.xuDistanceBetweenTwoVariables = function(a, b) { var sum = LPRAse.xuSum(a, b); if (isNaN(sum)) { return NaN; } var T = a.lpr.ids.length; return (sum / T).toFixed(2)/1; } // // Dong's distance between two LPR // // On consistency measures of linguistic preference relations - Definition 3 // doi:10.1016/j.ejor.2007.06.013 // // @param lpr1 LPR 1 // @param lpr2 LPR 2 // LPRAse.dongDistanceBetweenTwoLPR = function(lpr1, lpr2) { if ((typeof lpr1 == 'undefined') || (typeof lpr2 == 'undefined')) { return NaN; } if (lpr1.ids != lpr2.ids) { return NaN; } if (!(lpr1.ids.length & 1 )) { return NaN; } var result = 0; var a; var b; var n = lpr1.n; for (var i = 0; i < (n - 1); i++) { for (var j = i + 1; j < n; j++) { a = lpr1.getValue(i | 0, j | 0); b = lpr2.getValue(i | 0, j | 0); result += Math.pow(LPRAse.xuDistanceBetweenTwoVariables(a, b), 2); } } result *= 2 / (n * (n - 1)); result = Math.sqrt(result).toFixed(2)/1; return result; } // // Dong's average LPR // // On consistency measures of linguistic preference relations - Lemma 2 // doi:10.1016/j.ejor.2007.06.013 // // @param lpr LPR // LPRAse.dongAverageLPR = function(lpr) { if (typeof lpr == 'undefined') { return NaN; } if (typeof lpr.preference == 'undefined') { return NaN; } if (!(lpr.ids.length & 1 )) { return NaN; } if (consistency.containsMissingValues(lpr.preference)) { return NaN; } var ids = lpr.ids; var T = ids.length; var n = lpr.n; var result = new LPRAse(ids, n); var aux; var a_i_c; var a_c_j; var adjustment = Math.round((T-1) / 2); for (var i = 0; i < (n - 1); i++) { for (var j = i + 1; j < n; j++) { aux = 0; for (var c = 0; c < n; c++) { a_i_c = lpr.getValue(i | 0, c | 0); a_c_j = lpr.getValue(c | 0, j | 0); aux += LPRAse.xuSum(a_i_c, a_c_j); } aux /= n; aux += adjustment; aux /= T-1; aux = lpr.linguisticValue(aux); result.setValueSymmetrically(i | 0, j | 0, aux.s, aux.alpha); } } return result; }