Browse Source

CertainTrustSimple updated

Debashis Chandra Ray 6 years ago
parent
commit
6f68d39005
1 changed files with 319 additions and 0 deletions
  1. 319 0
      JavaScript/JavaScript_SDK/CertainTrust.js

+ 319 - 0
JavaScript/JavaScript_SDK/CertainTrust.js

@@ -788,6 +788,83 @@ CertainTrustSimple.prototype.clone = function() {
 };
 
 /*Added by Debashis*/
+
+/**
+ * Computes OR function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
+ * argument and this CertainTrust object remain unchanged.
+ * N values of both objects should be equal.
+ * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
+ * @param arg - CertainTrustSimple object
+ * @return - result of OR computation for this object and an argument.
+ */
+CertainTrust.prototype._singlesimpleOR = function(arg) {
+	var c1 = this.getC();
+	var t1 = this.getT();
+	var f1 = this.getF();
+
+	var c2 = arg.getC();
+	var t2 = arg.getT();
+	var f2 = arg.getF();
+
+	var resT = 0.5, resF = 0.5, resC = 0;
+
+	if (!this._operationAllowed(this, arg))
+		return undefined;
+
+	resF = f1 + f2 - f1*f2;
+
+	if (this._almostEqual(resF, 0)){
+		f1 = 0.99999;
+		f2 = 0.99999;
+		resF = f1 + f2 - f1*f2;
+		resC = c1 + c2 - c1*c2- (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
+	}
+	else
+		resC = c1 + c2 - c1*c2 - (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
+
+	if (this._almostEqual(resC, 0))
+		resT = 0.5;
+	else resT = (1/resC) * (c1*t1 + c2*t2 - c1*c2*t1*t2);
+
+	resT = this._adjustValue(resT);
+	resC = this._adjustValue(resC);
+	resF = this._adjustValue(resF);
+
+	var result = new CertainTrustSimple(resT, resC, resF, this.n, 0);
+
+	return result;
+};
+
+/**
+ * Computes OR function for this CertainTrustSimple object and the specified arguments.
+ * Result is returned as a new object, arguments and this CertainTrustSimple object remain unchanged.
+ * Example: a.OR(b, c, d) returns new CertainTrust object that equals a OR b OR c OR d.
+ * Multiple arguments allowed, but not less than one.
+ * N values of all objects should be equal.
+ * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
+ * @param args - arguments
+ * @return - result of OR computation for this object and all arguments.
+ */
+CertainTrustSimple.prototype.simpleOR = function() {
+	var result = this.clone();
+	for (var i = 0; i < arguments.length; ++i) {
+		var m = arguments[i];
+		if (!this._operationAllowed(this, m))
+			continue;
+		result = result._singlesimpleOR(m);
+	}
+	return result;
+};
+
+/**
+ * Computes AND function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
+ * argument and this CertainTrustSimple object remain unchanged.
+ * N values of both objects should be equal.
+ * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
+ * @param arg - CertainTrustSimple object
+ * @return - result of AND computation for this object and an argument.
+ */
+
 CertainTrustSimple.prototype._singlesimpleAND = function(arg){
 	var c1 = this.getC();
 	var f1 = this.getF();
@@ -823,6 +900,16 @@ CertainTrustSimple.prototype._singlesimpleAND = function(arg){
 	return new CertainTrustSimple(resT, resC, resF);
 };
 
+/**
+ * Computes AND function for this CertainTrustSimple object and the specified arguments.
+ * Result is returned as a new object, arguments and this CertainTrustSimple object remain unchanged.
+ * Example: a.AND(b, c, d) returns new CertainTrustSimple object that equals a AND b AND c AND d.
+ * Multiple arguments allowed, but not less than one.
+ * N values of all objects should be equal.
+ * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
+ * @param args - arguments
+ * @return - result of AND computation for this object and all arguments.
+ */
 CertainTrustSimple.prototype.simpleAND = function() {
 	var result = this.clone();
 	for (var i = 0; i < arguments.length; i++) {
@@ -834,6 +921,19 @@ CertainTrustSimple.prototype.simpleAND = function() {
 	return result;
 };
 
+/**
+ * Returns NOT of this CertainTrustSimple object.
+ * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
+ * @return - NOT of this CertainTrustSimple object.
+ */
+CertainTrustSimple.prototype.simpleNOT = function() {
+	var result = this.clone();
+	result.setTC(1 - this.getT(), this.getC());
+	result.setF(1 - this.getF());
+	result.setDoC(0);
+	return result;
+};
+
 CertainTrust.prototype.setName = function(newname) {
 	this.name = newname;
 };
@@ -989,3 +1089,222 @@ CertainTrustSimple.prototype.simpleCONSENSUS = function() {
 	}
 	return result;
 };
+
+/**
+ * Computes DISCOUNTING function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
+ * argument and this CertainTrustSimple object remain unchanged.
+ * N values of both objects should be equal.
+ * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
+ * @param arg - CertainTrustSimple object
+ * @return - result of DISCOUNTING computation for this object and an argument.
+ */
+CertainTrustSimple.prototype._singlesimpleDISCOUNTING = function(arg){
+	var c1 = this.getC();
+	var f1 = this.getF();
+	var t1 = this.getT();
+
+	var c2 = arg.getC();
+	var f2 = arg.getF();
+	var t2 = arg.getT();
+
+	var resC = 0, resT = 0.5, resF = 0.5;
+
+	if (!this._operationAllowed(this, arg))
+		return undefined;
+
+	//resF = f1*f2;
+	if (this._almostEqual(resF, 1))		//avoid division by 0
+		resC = t1*c1*c2;
+	else
+		resC = t1*c1*c2;
+
+	if (this._almostEqual(resC, 0))
+		resT = t2;
+	else if (this._almostEqual(resF, 1))	//avoid division by 0
+		resT = t2;
+	else resT = t2;
+
+	resT = this._adjustValue(resT);
+	resC = this._adjustValue(resC);
+	resF = this._adjustValue(resF);
+
+	return new CertainTrustSimple(resT, resC, resF, this.n, 0);
+};
+
+CertainTrustSimple.prototype.simpleDISCOUNTING = function() {
+	var result = this.clone();
+	for (var i = 0; i < arguments.length; i++) {
+		var m = arguments[i];
+		if (!this._operationAllowed(this, m))
+			continue;
+		result = result._singlesimpleDISCOUNTING(m);
+	}
+	return result;
+};
+
+/**
+ * an internal implementation of fusion function.
+ * Is called by wFusion and cFusion
+ * @param args - an array of CertainTrustSimple objects
+ * @param weights - an integer array of corresponding weights
+ * @param doc - a degree of conflict (always 0 for wFusion)
+ * @return - new CertainTrustSimple object
+ */
+CertainTrustSimple.prototype._simpleinternalFusion = function(args, weights, doc) {
+	var resC, resT, resF;
+	var allOne = true;
+	var allZero = true;
+	var allWeightsZero = true;
+	var atLeastOne1 = false;
+	var arrLength = args.length;
+
+	// set the flags about C and Weight values
+	for (var i = 0; i < arrLength; ++i)
+		if (args[i].getC() !== 1) {
+			allOne = false;
+			i = arrLength;
+		}
+	for (i = 0; i < arrLength; ++i)
+		if (args[i].getC() !== 0) {
+			allZero = false;
+			i = arrLength;
+		}
+	for (i = 0; i < arrLength; ++i)
+		if (weights[i] !== 0) {
+			allWeightsZero = false;
+			i = arrLength;
+		}
+	for (i = 0; i < arrLength; ++i)
+		if (args[i].getC() === 1) {
+			atLeastOne1 = true;
+			i = arrLength;
+		}
+
+	//Calculate T and C
+
+	// 1. all C's  = 1
+	var numeratorT = 0, denominatorT = 0;
+	if (allOne) {
+		// set C
+		resC = 1 * (1 - doc);
+		// set T
+		if (allWeightsZero) {// save some calculation time
+			resT = 0;
+		}
+		else {				// or use the function
+			for (i = 0; i < arrLength; ++i) {
+				numeratorT += weights[i] * args[i].getT();
+				denominatorT += weights[i];
+			}
+			resT = numeratorT/denominatorT;
+		}
+	} else {
+		if (atLeastOne1)
+			throw "Illegal arguments. Either all C values must equal 1 or none of them. Operation not allowed\n";
+		else {
+			// 2. Any other combination
+			if (allWeightsZero) { // save some calculation time
+				resT = 0;
+				resC = 0;
+			}
+			else {					// or use the function
+				var numeratorC = 0, denominatorC = 0, mult;
+				for (i = 0; i < arrLength; ++i) {
+					mult = 1;
+					for (var j = 0; j < arrLength; ++j) // Count the product for each sum element
+						if (j !== i)
+							mult *= 1 - args[j].getC();
+					numeratorT += weights[i] * args[i].getT() * args[i].getC() * mult;
+					denominatorT += weights[i] * args[i].getC() * mult;
+					denominatorC += weights[i] * mult;
+				}
+				numeratorC = denominatorT;
+				resC = (numeratorC/denominatorC) * (1 - doc);
+				if (allZero)
+					resT = 0.5;
+				else
+					resT = numeratorT/denominatorT;
+			}
+			// Special case for T
+			if (allZero)
+				resT = 0.5;
+		}
+	}
+
+	// Calculate F
+	if (allWeightsZero)
+		resF = 0;
+	else {
+		var numerator = 0, denominator = 0;
+		for (i = 0; i < arrLength; ++i) {
+			numerator += weights[i] * args[i].getF();
+			denominator += weights[i];
+		}
+		resF = numerator/denominator;
+	}
+
+	var result = args[0].clone();
+	result.setTC(resT, resC);
+	result.setF(resF);
+	result.setDoC(doc);
+	return result;
+};
+
+/**
+ * Performs weighted fusion for an array of CertainTrustSimple objects in correspondence with
+ * an array of weights. Returns new CertainTrust object.
+ * Requirements: N values of CertainTrustSimple objects must be equal.
+ * Number of weights should equal the number of CertainTrust objects.
+ * Arrays must be non-empty
+ * Either all of CertainTrust must be of certainty 1 or none of them.
+ * @param args - an array of CertainTrustSimple objects
+ * @param weights - an integer array of corresponding weights
+ * @return - new CertainTrustSimple object
+ */
+CertainTrustSimple.prototype.simplewFusion = function(args, weights) {
+	//arrays should be equal
+	if (args.length == weights.length) {
+		//and not empty
+		if (args.length !== 0) {
+			for (var i = 1; i < args.length; ++i)
+				if (!this._operationAllowed(args[0], args[i]))
+					return undefined;
+
+			return this._simpleinternalFusion(args, weights, 0);
+		}
+		else throw "Arrays are empty. Operation not allowed. \n";
+	}
+	else throw "Different lengths of arrays. Operation not allowed. \n";
+};
+
+/**
+ * Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict 
+ * between given opinions (CertainTrustSimple objects) and takes it into consideration while performing fusion.
+ * The degree of conflict is then saved in the resulting CertainTrust object and may be checked with getDoC() function.
+ * @param args - an array of CertainTrustSimple objects
+ * @param weights - an integer array of corresponding weights
+ * @return - new CertainTrustSimple object
+ */
+CertainTrustSimple.prototype.simplecFusion = function(args, weights) {
+	//arrays should be equal
+	if (args.length == weights.length) {
+		//and not empty
+		if (args.length !== 0) {
+			for (var i = 1; i < args.length; ++i)
+				if (!this._operationAllowed(args[0], args[i]))
+					return undefined;
+			var denominator = args.length*(args.length - 1) / 2;
+			var numerator = 0;
+			for (i = 0; i < args.length; ++i)
+				for (var j = i; j < args.length; ++j)
+					numerator += Math.abs(args[i].getT() - args[j].getT()) *
+						args[i].getC() * args[j].getC() *
+						(1 - Math.abs((weights[i] - weights[j]) /
+							      (weights[i] + weights[j])));
+			var doc = numerator/denominator;
+			return this._simpleinternalFusion(args, weights, doc);
+		}
+		else throw "Arrays are empty. Operation not allowed. \n";
+	}
+	else throw "Different lengths of arrays. Operation not allowed. \n";
+};