Browse Source

AND operation bug fixing for divide by zero problem

Debashis Chandra Ray 5 years ago
parent
commit
ab5b42bff6
1 changed files with 64 additions and 9 deletions
  1. 64 9
      JavaScript/JavaScript_SDK/CertainTrust.js

+ 64 - 9
JavaScript/JavaScript_SDK/CertainTrust.js

@@ -339,15 +339,16 @@ CertainTrust.prototype._singleAND = function(arg){
 		return undefined;
 
 	resF = f1*f2;
-	if (this._almostEqual(resF, 1))		//avoid division by 0
-		resC = c1 + c2 - c1*c2;
+	if (this._almostEqual(resF, 1)){		//avoid division by 0
+		f1 = 0.99999;
+		f2 = 0.99999;
+		resF = f1*f2;
+		resC = c1 + c2 - c1*c2- (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
 	else
 		resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
 
 	if (this._almostEqual(resC, 0))
 		resT = 0.5;
-	else if (this._almostEqual(resF, 1))	//avoid division by 0
-		resT = (1/resC) *  (c1*t1*c2*t2);
 	else resT = (1/resC) *  ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
 
 	resT = this._adjustValue(resT);
@@ -797,15 +798,17 @@ CertainTrustSimple.prototype._singlesimpleAND = function(arg){
 		return undefined;
 
 	resF = f1*f2;
-	if (this._almostEqual(resF, 1))		//avoid division by 0
-		resC = c1 + c2 - c1*c2;
+	if (this._almostEqual(resF, 1)){		//avoid division by 0
+		f1 = 0.99999;
+		f2 = 0.99999;
+		resF = f1*f2;
+		resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
+	}
 	else
 		resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
 
 	if (this._almostEqual(resC, 0))
 		resT = 0.5;
-	else if (this._almostEqual(resF, 1))	//avoid division by 0
-		resT = (1/resC) *  (c1*t1*c2*t2);
 	else resT = (1/resC) *  ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
 
 	resT = this._adjustValue(resT);
@@ -932,4 +935,56 @@ CertainTrust.prototype.DISCOUNTING = function() {
 		result = result._singleDISCOUNTING(m);
 	}
 	return result;
-};
+};
+
+/**
+ * Computes CONSENSUS 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 CONSENSUS computation for this object and an argument.
+ */
+CertainTrustSimple.prototype._singlesimpleCONSENSUS = 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 = (c1+c2-2*c1*c2)/(1-c1*c2);
+	else
+		resC = (c1+c2-2*c1*c2)/(1-c1*c2);
+
+	if (this._almostEqual(resC, 0))
+		resT = 0.5;
+	else if (this._almostEqual(resF, 1))	//avoid division by 0
+		resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1*(1-c2)+c2*(1-c1));
+	else resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1*(1-c2)+c2*(1-c1));
+
+	resT = this._adjustValue(resT);
+	resC = this._adjustValue(resC);
+	resF = this._adjustValue(resF);
+
+	return new CertainTrustSimple(resT, resC, resF);
+};
+
+CertainTrustSimple.prototype.simpleCONSENSUS = 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._singlesimpleCONSENSUS(m);
+	}
+	return result;
+};