CertainTrust.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /**
  2. * CertainTrust SDK
  3. *
  4. * Implements the computational trust model "CertainTrust"
  5. * in JavaScript.
  6. * See <http://www.tk.informatik.tu-darmstadt.de/de/research/smart-security-and-trust/> for further details.
  7. *
  8. *
  9. * Telecooperation Department, Technische Universität Darmstadt
  10. * <http://www.tk.informatik.tu-darmstadt.de/>
  11. *
  12. * Prof. Dr. Max Mühlhäuser <max@informatik.tu-darmstadt.de>
  13. * Florian Volk <florian.volk@cased.de>
  14. *
  15. *
  16. * @author Maria Pelevina
  17. * @author David Kalnischkies
  18. * @version 1.1
  19. */
  20. /* This Source Code Form is subject to the terms of the Mozilla Public
  21. * License, v. 2.0. If a copy of the MPL was not distributed with this
  22. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  23. /**
  24. * Available Constructors:
  25. * - CertainTrust(t, c, f, n)
  26. * - CertainTrust(r, s, n)
  27. * - CertainTrust(n)
  28. * optionally arguments can be preceded by name, e.g. CertainTrust(name, r, s, n)
  29. *
  30. * t - average rating value, [0; 1], from very negative to very positive
  31. * c - certainty value, [0; 1] from low certainty (no evidence) to the maximal maximal certainty.
  32. * f - initial trust value
  33. * w - weight
  34. * r - number of positive evidence
  35. * s - number of negative evidence
  36. * n - maximal number of expected evidence
  37. */
  38. var CertainTrust = function() {
  39. this.weight = 2;
  40. this.observers = [];
  41. var offset = 0;
  42. this.name = "";
  43. if (this._isString(arguments[0])) {
  44. this.name = arguments[0];
  45. offset = 1;
  46. }
  47. if (arguments.length == 4 + offset || arguments.length == 5 + offset) {
  48. // CertainTrust(t, c, f, n, doc)
  49. // doc is a 'private' parameter
  50. this.t = arguments[0 + offset];
  51. this.c = arguments[1 + offset];
  52. this.f = arguments[2 + offset];
  53. this.n = arguments[3 + offset];
  54. this.doc = (arguments.length == 4 + offset) ? 0 : arguments[4 + offset];
  55. this.r = 0;
  56. this.s = 0;
  57. if (this.n <= 0)
  58. throw "N should be greater than 0. Entered n = " + this.n + "\n";
  59. if (this.f < 0 && this.f > 1)
  60. throw "f should lie within [0;1]. Entered f = " + this.f + "\n";
  61. if (this.c < 0 && this.c > 1)
  62. throw "c should lie within [0;1]. Entered c = " + this.c + "\n";
  63. if (this.t < 0 && this.t > 1)
  64. throw "t should lie within [0;1]. Entered t = " + this.t + "\n";
  65. this._calculateTCtoRS();
  66. } else if (arguments.length == 3 + offset) {
  67. // CertainTrust(r, s, n)
  68. this.n = arguments[2 + offset];
  69. this.c = 0;
  70. this.t = 0.5;
  71. this.f = 0.5;
  72. this.r = arguments[0 + offset];
  73. this.s = arguments[1 + offset];
  74. this.doc = 0;
  75. if (this.n <= 0)
  76. throw "N should be greater than 0. Entered n = " + this.n + "\n";
  77. if (this.r < 0)
  78. throw "r should be positive. Entered r = " + this.r + "\n";
  79. if (this.s < 0)
  80. throw "s should be positive. Entered s = " + this.s + "\n";
  81. this._normaliseRS();
  82. this._calculateRStoTC();
  83. } else {
  84. if (arguments.length == 1 + offset) {
  85. // CertainTrust(n)
  86. this.n = arguments[0 + offset];
  87. if (this.n <= 0)
  88. throw "N should be greater than 0. Entered n = " + this.n + "\n";
  89. this.c = 0;
  90. this.t = 0.5;
  91. this.f = 0.5;
  92. this.r = 0;
  93. this.s = 0;
  94. this.doc = 0;
  95. }
  96. else throw "Illegal number of arguments: " + arguments.length + "\n";
  97. }
  98. };
  99. //=========== Getters =================
  100. CertainTrust.prototype.getName = function() {
  101. return this.name;
  102. };
  103. CertainTrust.prototype.getC = function() {
  104. return this.c;
  105. };
  106. CertainTrust.prototype.getT = function() {
  107. return this.t;
  108. };
  109. CertainTrust.prototype.getF = function() {
  110. return this.f;
  111. };
  112. CertainTrust.prototype.getR = function() {
  113. return this.r;
  114. };
  115. CertainTrust.prototype.getS = function() {
  116. return this.s;
  117. };
  118. CertainTrust.prototype.getN = function() {
  119. return this.n;
  120. };
  121. CertainTrust.prototype.getDoC = function() {
  122. return this.doc;
  123. };
  124. CertainTrust.prototype.getExpectation = function() {
  125. return (this.t * this.c) + ((1 - this.c) * this.f);
  126. };
  127. //=========== Setters =================
  128. /**
  129. * Resets N value. Renormalises r and s values, recalculates c and t accordingly.
  130. * @param n - new maximal number of expected evidence
  131. */
  132. CertainTrust.prototype.setN = function(n) {
  133. if (n > 0) {
  134. this.n = n;
  135. this._normaliseRS();
  136. this._calculateRStoTC();
  137. this.notifyObservers();
  138. }
  139. else throw "N should be greater than 0. Entered n = " + n + "\n";
  140. };
  141. /**
  142. * Sets f value.
  143. * @param f - initial trust value.
  144. */
  145. CertainTrust.prototype.setF = function(f) {
  146. if (f >= 0 && f <= 1) {
  147. this.f = f;
  148. this.notifyObservers();
  149. }
  150. else throw "f should lie within [0;1]. Entered f = " + f + "\n";
  151. };
  152. /**
  153. * Sets Degree of Conflict value.
  154. * @param doc is the new value for DoC
  155. */
  156. CertainTrust.prototype.setDoC = function(doc) {
  157. if (doc >= 0)
  158. this.doc = doc;
  159. else throw "DoC should be greater than 0. Entered DoC = " + doc + "\n";
  160. };
  161. /**
  162. * Sets c and t values. Recalculates r and s values accordingly.
  163. * @param t - new average trust value
  164. * @param c - new certainty value
  165. */
  166. CertainTrust.prototype.setTC = function(t, c) {
  167. if (c >= 0 && c <= 1) {
  168. if (t >= 0 && t <= 1) {
  169. this.c = c;
  170. this.t = t;
  171. this._calculateTCtoRS();
  172. this.notifyObservers();
  173. }
  174. else throw "t should be greater than 0. Entered t = " + t + "\n";
  175. }
  176. else throw "c should lie within [0;1]. Entered c = " + c + "\n";
  177. };
  178. /**
  179. * Sets r and s values. Recalculates c and t values accordingly.
  180. * @param r - new number of positive evidence
  181. * @param s - new number of negative evidence
  182. */
  183. CertainTrust.prototype.setRS = function(r, s) {
  184. if (r >= 0) {
  185. if (s >= 0) {
  186. this.r = r;
  187. this.s = s;
  188. this._normaliseRS();
  189. this._calculateRStoTC();
  190. this.notifyObservers();
  191. }
  192. else throw "s should be positive. Entered s = " + s + "\n";
  193. }
  194. else throw "r should be positive. Entered r = " + r + "\n";
  195. };
  196. /**
  197. * Add some positive evidence to r.
  198. * @param posEvidence - number of new positive evidences
  199. */
  200. CertainTrust.prototype.addR = function(posEvidence) {
  201. if (posEvidence >= 0) {
  202. this.r += posEvidence;
  203. this._normaliseRS();
  204. this._calculateRStoTC();
  205. this.notifyObservers();
  206. }
  207. else throw "Number of positive evidences should be positive. Entered " + posEvidence + "\n";
  208. };
  209. /**
  210. * Add some negative evidence to s.
  211. * @param negEvidence - number of new negative evidences
  212. */
  213. CertainTrust.prototype.addS = function(negEvidence) {
  214. if (negEvidence >= 0) {
  215. this.s += negEvidence;
  216. this._normaliseRS();
  217. this._calculateRStoTC();
  218. this.notifyObservers();
  219. }
  220. else throw "Number of negative evidences should be positive. Entered " + negEvidence + "\n";
  221. };
  222. //=========== Logic =================
  223. /**
  224. * Computes OR function for this CertainTrust object and the specified argument. Result is returned as a new object,
  225. * argument and this CertainTrust object remain unchanged.
  226. * N values of both objects should be equal.
  227. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  228. * @param arg - CertainTrust object
  229. * @return - result of OR computation for this object and an argument.
  230. */
  231. CertainTrust.prototype._singleOR = function(arg) {
  232. var c1 = this.getC();
  233. var t1 = this.getT();
  234. var f1 = this.getF();
  235. var c2 = arg.getC();
  236. var t2 = arg.getT();
  237. var f2 = arg.getF();
  238. var resT = 0.5, resF = 0.5, resC = 0;
  239. if (!this._operationAllowed(this, arg))
  240. return undefined;
  241. resF = f1 + f2 - f1*f2;
  242. if (this._almostEqual(resF, 0)){
  243. f1 = 0.99999;
  244. f2 = 0.99999;
  245. resF = f1 + f2 - f1*f2;
  246. resC = c1 + c2 - c1*c2- (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  247. }
  248. else
  249. resC = c1 + c2 - c1*c2 - (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  250. if (this._almostEqual(resC, 0))
  251. resT = 0.5;
  252. else resT = (1/resC) * (c1*t1 + c2*t2 - c1*c2*t1*t2);
  253. resT = this._adjustValue(resT);
  254. resC = this._adjustValue(resC);
  255. resF = this._adjustValue(resF);
  256. var result = new CertainTrust(resT, resC, resF, this.n, 0);
  257. return result;
  258. };
  259. /**
  260. * Computes OR function for this CertainTrust object and the specified arguments.
  261. * Result is returned as a new object, arguments and this CertainTrust object remain unchanged.
  262. * Example: a.OR(b, c, d) returns new CertainTrust object that equals a OR b OR c OR d.
  263. * Multiple arguments allowed, but not less than one.
  264. * N values of all objects should be equal.
  265. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  266. * @param args - arguments
  267. * @return - result of OR computation for this object and all arguments.
  268. */
  269. CertainTrust.prototype.OR = function() {
  270. var result = this.clone();
  271. for (var i = 0; i < arguments.length; ++i) {
  272. var m = arguments[i];
  273. if (!this._operationAllowed(this, m))
  274. continue;
  275. result = result._singleOR(m);
  276. }
  277. return result;
  278. };
  279. /**
  280. * Computes AND function for this CertainTrust object and the specified argument. Result is returned as a new object,
  281. * argument and this CertainTrust object remain unchanged.
  282. * N values of both objects should be equal.
  283. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  284. * @param arg - CertainTrust object
  285. * @return - result of AND computation for this object and an argument.
  286. */
  287. CertainTrust.prototype._singleAND = function(arg){
  288. var c1 = this.getC();
  289. var f1 = this.getF();
  290. var t1 = this.getT();
  291. var c2 = arg.getC();
  292. var f2 = arg.getF();
  293. var t2 = arg.getT();
  294. var resC = 0, resT = 0.5, resF = 0.5;
  295. if (!this._operationAllowed(this, arg))
  296. return undefined;
  297. resF = f1*f2;
  298. if (this._almostEqual(resF, 1)){ //avoid division by 0
  299. f1 = 0.99999;
  300. f2 = 0.99999;
  301. resF = f1*f2;
  302. resC = c1 + c2 - c1*c2- (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  303. }
  304. else
  305. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  306. if (this._almostEqual(resC, 0))
  307. resT = 0.5;
  308. else resT = (1/resC) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
  309. resT = this._adjustValue(resT);
  310. resC = this._adjustValue(resC);
  311. resF = this._adjustValue(resF);
  312. return new CertainTrust(resT, resC, resF, this.n, 0);
  313. };
  314. CertainTrust.prototype._adjustValue = function(arg) {
  315. return Math.max(Math.min(arg, 1), 0);
  316. };
  317. CertainTrust.prototype._almostEqual = function(value, target) {
  318. return Math.abs(value - target) < 1E-10;
  319. };
  320. CertainTrust.prototype._operationAllowed = function(arg1, arg2) {
  321. //and all N's of TC's must be equal
  322. /*if (arg1.getN() != arg2.getN()) //Disabled by Debashis C. Ray for AND calculation
  323. throw "Different N values. Operation not allowed. \n"; */
  324. return true;
  325. }
  326. /**
  327. * Computes AND function for this CertainTrust object and the specified arguments.
  328. * Result is returned as a new object, arguments and this CertainTrust object remain unchanged.
  329. * Example: a.AND(b, c, d) returns new CertainTrust object that equals a AND b AND c AND d.
  330. * Multiple arguments allowed, but not less than one.
  331. * N values of all objects should be equal.
  332. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  333. * @param args - arguments
  334. * @return - result of AND computation for this object and all arguments.
  335. */
  336. CertainTrust.prototype.AND = function() {
  337. var result = this.clone();
  338. for (var i = 0; i < arguments.length; i++) {
  339. var m = arguments[i];
  340. if (!this._operationAllowed(this, m))
  341. continue;
  342. result = result._singleAND(m);
  343. }
  344. return result;
  345. };
  346. /**
  347. * Returns NOT of this CertainTrust object.
  348. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  349. * @return - NOT of this CertainTrust object.
  350. */
  351. CertainTrust.prototype.NOT = function() {
  352. var result = this.clone();
  353. result.setTC(1 - this.getT(), this.getC());
  354. result.setF(1 - this.getF());
  355. result.setDoC(0);
  356. return result;
  357. };
  358. /**
  359. * an internal implementation of fusion function.
  360. * Is called by wFusion and cFusion
  361. * @param args - an array of CertainTrust objects
  362. * @param weights - an integer array of corresponding weights
  363. * @param doc - a degree of conflict (always 0 for wFusion)
  364. * @return - new CertainTrust object
  365. */
  366. CertainTrust.prototype._internalFusion = function(args, weights, doc) {
  367. var resC, resT, resF;
  368. var allOne = true;
  369. var allZero = true;
  370. var allWeightsZero = true;
  371. var atLeastOne1 = false;
  372. var arrLength = args.length;
  373. // set the flags about C and Weight values
  374. for (var i = 0; i < arrLength; ++i)
  375. if (args[i].getC() !== 1) {
  376. allOne = false;
  377. i = arrLength;
  378. }
  379. for (i = 0; i < arrLength; ++i)
  380. if (args[i].getC() !== 0) {
  381. allZero = false;
  382. i = arrLength;
  383. }
  384. for (i = 0; i < arrLength; ++i)
  385. if (weights[i] !== 0) {
  386. allWeightsZero = false;
  387. i = arrLength;
  388. }
  389. for (i = 0; i < arrLength; ++i)
  390. if (args[i].getC() === 1) {
  391. atLeastOne1 = true;
  392. i = arrLength;
  393. }
  394. //Calculate T and C
  395. // 1. all C's = 1
  396. var numeratorT = 0, denominatorT = 0;
  397. if (allOne) {
  398. // set C
  399. resC = 1 * (1 - doc);
  400. // set T
  401. if (allWeightsZero) {// save some calculation time
  402. resT = 0;
  403. }
  404. else { // or use the function
  405. for (i = 0; i < arrLength; ++i) {
  406. numeratorT += weights[i] * args[i].getT();
  407. denominatorT += weights[i];
  408. }
  409. resT = numeratorT/denominatorT;
  410. }
  411. } else {
  412. if (atLeastOne1)
  413. throw "Illegal arguments. Either all C values must equal 1 or none of them. Operation not allowed\n";
  414. else {
  415. // 2. Any other combination
  416. if (allWeightsZero) { // save some calculation time
  417. resT = 0;
  418. resC = 0;
  419. }
  420. else { // or use the function
  421. var numeratorC = 0, denominatorC = 0, mult;
  422. for (i = 0; i < arrLength; ++i) {
  423. mult = 1;
  424. for (var j = 0; j < arrLength; ++j) // Count the product for each sum element
  425. if (j !== i)
  426. mult *= 1 - args[j].getC();
  427. numeratorT += weights[i] * args[i].getT() * args[i].getC() * mult;
  428. denominatorT += weights[i] * args[i].getC() * mult;
  429. denominatorC += weights[i] * mult;
  430. }
  431. numeratorC = denominatorT;
  432. resC = (numeratorC/denominatorC) * (1 - doc);
  433. if (allZero)
  434. resT = 0.5;
  435. else
  436. resT = numeratorT/denominatorT;
  437. }
  438. // Special case for T
  439. if (allZero)
  440. resT = 0.5;
  441. }
  442. }
  443. // Calculate F
  444. if (allWeightsZero)
  445. resF = 0;
  446. else {
  447. var numerator = 0, denominator = 0;
  448. for (i = 0; i < arrLength; ++i) {
  449. numerator += weights[i] * args[i].getF();
  450. denominator += weights[i];
  451. }
  452. resF = numerator/denominator;
  453. }
  454. var result = args[0].clone();
  455. result.setTC(resT, resC);
  456. result.setF(resF);
  457. result.setDoC(doc);
  458. return result;
  459. };
  460. /**
  461. * Performs weighted fusion for an array of CertainTrust objects in correspondence with
  462. * an array of weights. Returns new CertainTrust object.
  463. * Requirements: N values of CertainTrust objects must be equal.
  464. * Number of weights should equal the number of CertainTrust objects.
  465. * Arrays must be non-empty
  466. * Either all of CertainTrust must be of certainty 1 or none of them.
  467. * @param args - an array of CertainTrust objects
  468. * @param weights - an integer array of corresponding weights
  469. * @return - new CertainTrust object
  470. */
  471. CertainTrust.prototype.wFusion = function(args, weights) {
  472. //arrays should be equal
  473. if (args.length == weights.length) {
  474. //and not empty
  475. if (args.length !== 0) {
  476. for (var i = 1; i < args.length; ++i)
  477. if (!this._operationAllowed(args[0], args[i]))
  478. return undefined;
  479. return this._internalFusion(args, weights, 0);
  480. }
  481. else throw "Arrays are empty. Operation not allowed. \n";
  482. }
  483. else throw "Different lengths of arrays. Operation not allowed. \n";
  484. };
  485. /**
  486. * Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict
  487. * between given opinions (CertainTrust objects) and takes it into consideration while performing fusion.
  488. * The degree of conflict is then saved in the resulting CertainTrust object and may be checked with getDoC() function.
  489. * @param args - an array of CertainTrust objects
  490. * @param weights - an integer array of corresponding weights
  491. * @return - new CertainTrust object
  492. */
  493. CertainTrust.prototype.cFusion = function(args, weights) {
  494. //arrays should be equal
  495. if (args.length == weights.length) {
  496. //and not empty
  497. if (args.length !== 0) {
  498. for (var i = 1; i < args.length; ++i)
  499. if (!this._operationAllowed(args[0], args[i]))
  500. return undefined;
  501. var denominator = args.length*(args.length - 1) / 2;
  502. var numerator = 0;
  503. for (i = 0; i < args.length; ++i)
  504. for (var j = i; j < args.length; ++j)
  505. numerator += Math.abs(args[i].getT() - args[j].getT()) *
  506. args[i].getC() * args[j].getC() *
  507. (1 - Math.abs((weights[i] - weights[j]) /
  508. (weights[i] + weights[j])));
  509. var doc = numerator/denominator;
  510. return this._internalFusion(args, weights, doc);
  511. }
  512. else throw "Arrays are empty. Operation not allowed. \n";
  513. }
  514. else throw "Different lengths of arrays. Operation not allowed. \n";
  515. };
  516. //=========== Internal Calculations ==========
  517. /**
  518. * Normalises r and s values according to n - maximal number of expected evidence
  519. * Important! Doesn't notify observers.
  520. */
  521. CertainTrust.prototype._normaliseRS = function() {
  522. if ((this.r + this.s) > this.n) {
  523. var initR = this.r;
  524. this.r = (this.r * this.n) / (initR + this.s);
  525. this.s = (this.s * this.n) / (initR + this.s);
  526. }
  527. };
  528. /**
  529. * Calculates t and c values based on existing r and s values
  530. * Important! Doesn't notify observers.
  531. */
  532. CertainTrust.prototype._calculateRStoTC = function() {
  533. var rs = this.r + this.s;
  534. var nrs = this.n * rs;
  535. this.c = nrs / ((2 * this.weight * (this.n - this.r - this.s)) + nrs);
  536. if (this._almostEqual(this.c, 0))
  537. this.t = 0.5;
  538. else
  539. this.t = this.r / rs;
  540. };
  541. /**
  542. * Calculates r and s values based on existing c and t values
  543. * Important! Doesn't notify observers.
  544. */
  545. CertainTrust.prototype._calculateTCtoRS = function() {
  546. if (this._almostEqual(this.c, 0)) {
  547. this.r = 0;
  548. this.s = 0;
  549. this.t = 0.5;
  550. }
  551. else {
  552. var c2w = this.c * 2 * this.weight;
  553. var c2wn = c2w * this.n;
  554. var cn = this.c * this.n;
  555. this.r = (c2wn * this.t) / (c2w + this.n - cn);
  556. this.s = (c2wn - (c2wn * this.t)) / (c2w + this.n - cn);
  557. }
  558. };
  559. CertainTrust.prototype.clone = function() {
  560. var copy = new CertainTrust(this.getN());
  561. copy.c = this.c;
  562. copy.t = this.t;
  563. copy.f = this.f;
  564. copy.r = this.r;
  565. copy.s = this.s;
  566. copy.doc = this.doc;
  567. return copy;
  568. };
  569. CertainTrust.prototype._isString = function (obj) {
  570. return typeof(obj) === 'string';
  571. };
  572. //=========== Observer =================
  573. CertainTrust.prototype.notifyObservers = function(message) {
  574. for (var i = 0; i < this.observers.length; ++i)
  575. this.observers[i].update(this.observers[i], message);
  576. };
  577. CertainTrust.prototype.addObserver = function(observer) {
  578. this.observers.push(observer);
  579. };
  580. CertainTrust.prototype.deleteObserver = function(observer) {
  581. var idx = this.observers.indexOf(observer);
  582. if(idx !== -1)
  583. this.observers.splice(idx, 1);
  584. };
  585. //=== shared functions for frontends ===
  586. CertainTrust.prototype._insertElement = function(config, element) {
  587. var dom;
  588. if (config.domReturn === true) {
  589. return element;
  590. } else if (config.domParent !== undefined) {
  591. if (this._isString(config.domParent))
  592. document.getElementById(config.domParent).appendChild(element);
  593. else
  594. config.domParent.appendChild(element);
  595. } else if (config.domBefore !== undefined) {
  596. if (this._isString(config.domBefore))
  597. dom = document.getElementById(config.domBefore);
  598. else
  599. dom = config.domBefore;
  600. dom.parentNode.insertBefore(element, dom);
  601. } else {
  602. if (config.domAfter === undefined) {
  603. // the last script tag in DOM tree is the one creating this widget
  604. var scripts = document.getElementsByTagName('script');
  605. dom = scripts[scripts.length - 1];
  606. } else if (this._isString(config.domAfter))
  607. dom = document.getElementById(config.domAfter);
  608. else
  609. dom = config.domAfter;
  610. dom.parentNode.insertBefore(element, dom.nextSibling);
  611. }
  612. return undefined;
  613. };
  614. CertainTrust.prototype._getColor = function(certainty, trust, initf) {
  615. var resultp2 = ((1 - certainty) * initf);
  616. var result = (trust * certainty) + resultp2;
  617. var color;
  618. if (result < 0.5) {
  619. color = [
  620. 255,
  621. Math.min(255, (255 * 2 * result)),
  622. 0
  623. ];
  624. } else {
  625. color = [
  626. Math.min(255, ((2 - (2 * result)) * 255)),
  627. 255,
  628. 0
  629. ];
  630. }
  631. return color;
  632. };
  633. CertainTrust.prototype._pointOnCircle = function(centerx, centery, pointgrade, radius) {
  634. var pointrad = ((360 + pointgrade) % 360) * ((2 * Math.PI) / 360);
  635. var chord = 2 * radius * Math.sin((pointrad / 2));
  636. // height of our new point above the base-edge
  637. var y = Math.sqrt(2
  638. * (Math.pow(chord, 2) * Math.pow(radius, 2)
  639. + Math.pow(radius, 4) + Math.pow(radius, 2)
  640. * Math.pow(chord, 2))
  641. - (Math.pow(chord, 4) + 2 * Math.pow(radius, 4)))
  642. / (2 * radius);
  643. // distance to the cross-point of base-edge and height
  644. var a = Math.pow(radius, 2);
  645. var c = Math.pow(y, 2);
  646. // we do this to protect us from NaN cause by 1 - 1.00000004
  647. var x = (a < c) ? 0 : Math.sqrt(a - c);
  648. var directions = new Array("NE", "SE", "SW", "NW");
  649. var direction = 0;
  650. var alpharad = pointrad;
  651. while (alpharad > (0.5 * Math.PI)) {
  652. ++direction;
  653. alpharad -= (0.5 * Math.PI);
  654. }
  655. if (directions[direction] == "NE" || directions[direction] == "NW")
  656. x *= -1;
  657. if (directions[direction] == "SW" || directions[direction] == "NW")
  658. y *= -1;
  659. return new Array((centerx + x), (centery + y));
  660. };
  661. /* optional implementation of CertainTrust without R and S calculations */
  662. var CertainTrustSimple = function() {
  663. this.weight = 2;
  664. this.observers = [];
  665. var offset = 0;
  666. this.name = "";
  667. if (this._isString(arguments[0])) {
  668. this.name = arguments[0];
  669. offset = 1;
  670. }
  671. if (arguments.length == 3 + offset || arguments.length == 4 + offset) {
  672. // CertainTrustSimple(t, c, f, doc)
  673. // doc is a 'private' parameter
  674. this.t = arguments[0 + offset];
  675. this.c = arguments[1 + offset];
  676. this.f = arguments[2 + offset];
  677. this.doc = (arguments.length == 3 + offset) ? 0 : arguments[3 + offset];
  678. if (this.f < 0 && this.f > 1)
  679. throw "f should lie within [0;1]. Entered f = " + this.f + "\n";
  680. if (this.c < 0 && this.c > 1)
  681. throw "c should lie within [0;1]. Entered c = " + this.c + "\n";
  682. if (this.t < 0 && this.t > 1)
  683. throw "t should lie within [0;1]. Entered t = " + this.t + "\n";
  684. } else {
  685. this.c = 0;
  686. this.t = 0.5;
  687. this.f = 0.5;
  688. this.doc = 0;
  689. }
  690. };
  691. CertainTrustSimple.prototype = new CertainTrust(1);
  692. CertainTrustSimple.prototype.constructor = CertainTrustSimple;
  693. CertainTrustSimple.prototype._operationAllowed = function() { return true; }
  694. CertainTrustSimple.prototype._calculateTCtoRS = function() { }
  695. CertainTrustSimple.prototype._calculateRStoTC = function() { }
  696. CertainTrustSimple.prototype._normaliseRS = function() { }
  697. CertainTrustSimple.prototype.setRS = undefined;
  698. CertainTrustSimple.prototype.addR = undefined;
  699. CertainTrustSimple.prototype.addS = undefined;
  700. CertainTrustSimple.prototype.setN = undefined;
  701. CertainTrustSimple.prototype.getR = undefined;
  702. CertainTrustSimple.prototype.getS = undefined;
  703. CertainTrustSimple.prototype.getN = undefined;
  704. CertainTrustSimple.prototype.clone = function() {
  705. var copy = new CertainTrustSimple();
  706. copy.c = this.c;
  707. copy.t = this.t;
  708. copy.f = this.f;
  709. copy.doc = this.doc;
  710. return copy;
  711. };
  712. /*Added by Debashis*/
  713. /**
  714. * Computes OR function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  715. * argument and this CertainTrust object remain unchanged.
  716. * N values of both objects should be equal.
  717. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  718. * @param arg - CertainTrustSimple object
  719. * @return - result of OR computation for this object and an argument.
  720. */
  721. CertainTrust.prototype._singlesimpleOR = function(arg) {
  722. var c1 = this.getC();
  723. var t1 = this.getT();
  724. var f1 = this.getF();
  725. var c2 = arg.getC();
  726. var t2 = arg.getT();
  727. var f2 = arg.getF();
  728. var resT = 0.5, resF = 0.5, resC = 0;
  729. if (!this._operationAllowed(this, arg))
  730. return undefined;
  731. resF = f1 + f2 - f1*f2;
  732. if (this._almostEqual(resF, 0)){
  733. f1 = 0.99999;
  734. f2 = 0.99999;
  735. resF = f1 + f2 - f1*f2;
  736. resC = c1 + c2 - c1*c2- (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  737. }
  738. else
  739. resC = c1 + c2 - c1*c2 - (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  740. if (this._almostEqual(resC, 0))
  741. resT = 0.5;
  742. else resT = (1/resC) * (c1*t1 + c2*t2 - c1*c2*t1*t2);
  743. resT = this._adjustValue(resT);
  744. resC = this._adjustValue(resC);
  745. resF = this._adjustValue(resF);
  746. var result = new CertainTrustSimple(resT, resC, resF, this.n, 0);
  747. return result;
  748. };
  749. /**
  750. * Computes OR function for this CertainTrustSimple object and the specified arguments.
  751. * Result is returned as a new object, arguments and this CertainTrustSimple object remain unchanged.
  752. * Example: a.OR(b, c, d) returns new CertainTrust object that equals a OR b OR c OR d.
  753. * Multiple arguments allowed, but not less than one.
  754. * N values of all objects should be equal.
  755. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  756. * @param args - arguments
  757. * @return - result of OR computation for this object and all arguments.
  758. */
  759. CertainTrustSimple.prototype.simpleOR = function() {
  760. var result = this.clone();
  761. for (var i = 0; i < arguments.length; ++i) {
  762. var m = arguments[i];
  763. if (!this._operationAllowed(this, m))
  764. continue;
  765. result = result._singlesimpleOR(m);
  766. }
  767. return result;
  768. };
  769. /**
  770. * Computes AND function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  771. * argument and this CertainTrustSimple object remain unchanged.
  772. * N values of both objects should be equal.
  773. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  774. * @param arg - CertainTrustSimple object
  775. * @return - result of AND computation for this object and an argument.
  776. */
  777. CertainTrustSimple.prototype._singlesimpleAND = function(arg){
  778. var c1 = this.getC();
  779. var f1 = this.getF();
  780. var t1 = this.getT();
  781. var c2 = arg.getC();
  782. var f2 = arg.getF();
  783. var t2 = arg.getT();
  784. var resC = 0, resT = 0.5, resF = 0.5;
  785. if (!this._operationAllowed(this, arg))
  786. return undefined;
  787. resF = f1*f2;
  788. if (this._almostEqual(resF, 1)){ //avoid division by 0
  789. f1 = 0.99999;
  790. f2 = 0.99999;
  791. resF = f1*f2;
  792. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  793. }
  794. else
  795. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  796. if (this._almostEqual(resC, 0))
  797. resT = 0.5;
  798. else resT = (1/resC) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
  799. resT = this._adjustValue(resT);
  800. resC = this._adjustValue(resC);
  801. resF = this._adjustValue(resF);
  802. return new CertainTrustSimple(resT, resC, resF);
  803. };
  804. /**
  805. * Computes AND function for this CertainTrustSimple object and the specified arguments.
  806. * Result is returned as a new object, arguments and this CertainTrustSimple object remain unchanged.
  807. * Example: a.AND(b, c, d) returns new CertainTrustSimple object that equals a AND b AND c AND d.
  808. * Multiple arguments allowed, but not less than one.
  809. * N values of all objects should be equal.
  810. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  811. * @param args - arguments
  812. * @return - result of AND computation for this object and all arguments.
  813. */
  814. CertainTrustSimple.prototype.simpleAND = function() {
  815. var result = this.clone();
  816. for (var i = 0; i < arguments.length; i++) {
  817. var m = arguments[i];
  818. if (!this._operationAllowed(this, m))
  819. continue;
  820. result = result._singlesimpleAND(m);
  821. }
  822. return result;
  823. };
  824. /**
  825. * Returns NOT of this CertainTrustSimple object.
  826. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  827. * @return - NOT of this CertainTrustSimple object.
  828. */
  829. CertainTrustSimple.prototype.simpleNOT = function() {
  830. var result = this.clone();
  831. result.setTC(1 - this.getT(), this.getC());
  832. result.setF(1 - this.getF());
  833. result.setDoC(0);
  834. return result;
  835. };
  836. CertainTrust.prototype.setName = function(newname) {
  837. this.name = newname;
  838. };
  839. /**
  840. * Computes CONSENSUS function for this CertainTrust object and the specified argument. Result is returned as a new object,
  841. * argument and this CertainTrust object remain unchanged.
  842. * N values of both objects should be equal.
  843. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  844. * @param arg - CertainTrust object
  845. * @return - result of CONSENSUS computation for this object and an argument.
  846. */
  847. CertainTrust.prototype._singleCONSENSUS = function(arg){
  848. var c1 = this.getC();
  849. var f1 = this.getF();
  850. var t1 = this.getT();
  851. var c2 = arg.getC();
  852. var f2 = arg.getF();
  853. var t2 = arg.getT();
  854. var resC = 0, resT = 0.5, resF = 0.5;
  855. if (!this._operationAllowed(this, arg))
  856. return undefined;
  857. var tempC = c1*c2;
  858. if (this._almostEqual(tempC, 1)){ //avoid division by 0
  859. c1 = 0.99999;
  860. c2 = 0.99999;
  861. }
  862. resF = (f1*c1*(1-c2) + f2*c2*(1-c1))/(c1+c2-2*c1*c2);
  863. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  864. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1+c2-2*c1*c2);
  865. resT = this._adjustValue(resT);
  866. resC = this._adjustValue(resC);
  867. resF = this._adjustValue(resF);
  868. return new CertainTrust(resT, resC, resF, this.n, 0);
  869. };
  870. CertainTrust.prototype.CONSENSUS = function() {
  871. var result = this.clone();
  872. for (var i = 0; i < arguments.length; i++) {
  873. var m = arguments[i];
  874. if (!this._operationAllowed(this, m))
  875. continue;
  876. result = result._singleCONSENSUS(m);
  877. }
  878. return result;
  879. };
  880. /**
  881. * Computes DISCOUNTING function for this CertainTrust object and the specified argument. Result is returned as a new object,
  882. * argument and this CertainTrust object remain unchanged.
  883. * N values of both objects should be equal.
  884. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  885. * @param arg - CertainTrust object
  886. * @return - result of DISCOUNTING computation for this object and an argument.
  887. */
  888. CertainTrust.prototype._singleDISCOUNTING = function(arg){
  889. var c1 = this.getC();
  890. var f1 = this.getF();
  891. var t1 = this.getT();
  892. var c2 = arg.getC();
  893. var f2 = arg.getF();
  894. var t2 = arg.getT();
  895. var resC = 0, resT = 0.5, resF = 0.5;
  896. if (!this._operationAllowed(this, arg))
  897. return undefined;
  898. //resF = f1*f2;
  899. if (this._almostEqual(resF, 1)) //avoid division by 0
  900. resC = t1*c1*c2;
  901. else
  902. resC = t1*c1*c2;
  903. if (this._almostEqual(resC, 0))
  904. resT = t2;
  905. else if (this._almostEqual(resF, 1)) //avoid division by 0
  906. resT = t2;
  907. else resT = t2;
  908. resT = this._adjustValue(resT);
  909. resC = this._adjustValue(resC);
  910. resF = this._adjustValue(resF);
  911. return new CertainTrust(resT, resC, resF, this.n, 0);
  912. };
  913. CertainTrust.prototype.DISCOUNTING = function() {
  914. var result = this.clone();
  915. for (var i = 0; i < arguments.length; i++) {
  916. var m = arguments[i];
  917. if (!this._operationAllowed(this, m))
  918. continue;
  919. result = result._singleDISCOUNTING(m);
  920. }
  921. return result;
  922. };
  923. /**
  924. * Computes CONSENSUS function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  925. * argument and this CertainTrust object remain unchanged.
  926. * N values of both objects should be equal.
  927. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  928. * @param arg - CertainTrustSimple object
  929. * @return - result of CONSENSUS computation for this object and an argument.
  930. */
  931. CertainTrustSimple.prototype._singlesimpleCONSENSUS = function(arg){
  932. var c1 = this.getC();
  933. var f1 = this.getF();
  934. var t1 = this.getT();
  935. var c2 = arg.getC();
  936. var f2 = arg.getF();
  937. var t2 = arg.getT();
  938. var resC = 0, resT = 0.5, resF = 0.5;
  939. if (!this._operationAllowed(this, arg))
  940. return undefined;
  941. var tempC = c1*c2;
  942. if (this._almostEqual(tempC, 1)){ //avoid division by 0
  943. c1 = 0.99999;
  944. c2 = 0.99999;
  945. }
  946. resF = (f1*c1*(1-c2) + f2*c2*(1-c1))/(c1+c2-2*c1*c2);
  947. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  948. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1+c2-2*c1*c2);
  949. resT = this._adjustValue(resT);
  950. resC = this._adjustValue(resC);
  951. resF = this._adjustValue(resF);
  952. return new CertainTrustSimple(resT, resC, resF);
  953. };
  954. CertainTrustSimple.prototype.simpleCONSENSUS = function() {
  955. var result = this.clone();
  956. for (var i = 0; i < arguments.length; i++) {
  957. var m = arguments[i];
  958. if (!this._operationAllowed(this, m))
  959. continue;
  960. result = result._singlesimpleCONSENSUS(m);
  961. }
  962. return result;
  963. };
  964. /**
  965. * Computes DISCOUNTING function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  966. * argument and this CertainTrustSimple object remain unchanged.
  967. * N values of both objects should be equal.
  968. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  969. * @param arg - CertainTrustSimple object
  970. * @return - result of DISCOUNTING computation for this object and an argument.
  971. */
  972. CertainTrustSimple.prototype._singlesimpleDISCOUNTING = function(arg){
  973. var c1 = this.getC();
  974. var f1 = this.getF();
  975. var t1 = this.getT();
  976. var c2 = arg.getC();
  977. var f2 = arg.getF();
  978. var t2 = arg.getT();
  979. var resC = 0, resT = 0.5, resF = 0.5;
  980. if (!this._operationAllowed(this, arg))
  981. return undefined;
  982. //resF = f1*f2;
  983. if (this._almostEqual(resF, 1)) //avoid division by 0
  984. resC = t1*c1*c2;
  985. else
  986. resC = t1*c1*c2;
  987. if (this._almostEqual(resC, 0))
  988. resT = t2;
  989. else if (this._almostEqual(resF, 1)) //avoid division by 0
  990. resT = t2;
  991. else resT = t2;
  992. resT = this._adjustValue(resT);
  993. resC = this._adjustValue(resC);
  994. resF = this._adjustValue(resF);
  995. return new CertainTrustSimple(resT, resC, resF, this.n, 0);
  996. };
  997. CertainTrustSimple.prototype.simpleDISCOUNTING = function() {
  998. var result = this.clone();
  999. for (var i = 0; i < arguments.length; i++) {
  1000. var m = arguments[i];
  1001. if (!this._operationAllowed(this, m))
  1002. continue;
  1003. result = result._singlesimpleDISCOUNTING(m);
  1004. }
  1005. return result;
  1006. };
  1007. /**
  1008. * an internal implementation of fusion function.
  1009. * Is called by wFusion and cFusion
  1010. * @param args - an array of CertainTrustSimple objects
  1011. * @param weights - an integer array of corresponding weights
  1012. * @param doc - a degree of conflict (always 0 for wFusion)
  1013. * @return - new CertainTrustSimple object
  1014. */
  1015. CertainTrustSimple.prototype._simpleinternalFusion = function(args, weights, doc) {
  1016. var resC, resT, resF;
  1017. var allOne = true;
  1018. var allZero = true;
  1019. var allWeightsZero = true;
  1020. var atLeastOne1 = false;
  1021. var arrLength = args.length;
  1022. // set the flags about C and Weight values
  1023. for (var i = 0; i < arrLength; ++i)
  1024. if (args[i].getC() !== 1) {
  1025. allOne = false;
  1026. i = arrLength;
  1027. }
  1028. for (i = 0; i < arrLength; ++i)
  1029. if (args[i].getC() !== 0) {
  1030. allZero = false;
  1031. i = arrLength;
  1032. }
  1033. for (i = 0; i < arrLength; ++i)
  1034. if (weights[i] !== 0) {
  1035. allWeightsZero = false;
  1036. i = arrLength;
  1037. }
  1038. for (i = 0; i < arrLength; ++i)
  1039. if (args[i].getC() === 1) {
  1040. atLeastOne1 = true;
  1041. i = arrLength;
  1042. }
  1043. //Calculate T and C
  1044. // 1. all C's = 1
  1045. var numeratorT = 0, denominatorT = 0;
  1046. if (allOne) {
  1047. // set C
  1048. resC = 1 * (1 - doc);
  1049. // set T
  1050. if (allWeightsZero) {// save some calculation time
  1051. resT = 0;
  1052. }
  1053. else { // or use the function
  1054. for (i = 0; i < arrLength; ++i) {
  1055. numeratorT += weights[i] * args[i].getT();
  1056. denominatorT += weights[i];
  1057. }
  1058. resT = numeratorT/denominatorT;
  1059. }
  1060. } else {
  1061. if (atLeastOne1)
  1062. throw "Illegal arguments. Either all C values must equal 1 or none of them. Operation not allowed\n";
  1063. else {
  1064. // 2. Any other combination
  1065. if (allWeightsZero) { // save some calculation time
  1066. resT = 0;
  1067. resC = 0;
  1068. }
  1069. else { // or use the function
  1070. var numeratorC = 0, denominatorC = 0, mult;
  1071. for (i = 0; i < arrLength; ++i) {
  1072. mult = 1;
  1073. for (var j = 0; j < arrLength; ++j) // Count the product for each sum element
  1074. if (j !== i)
  1075. mult *= 1 - args[j].getC();
  1076. numeratorT += weights[i] * args[i].getT() * args[i].getC() * mult;
  1077. denominatorT += weights[i] * args[i].getC() * mult;
  1078. denominatorC += weights[i] * mult;
  1079. }
  1080. numeratorC = denominatorT;
  1081. resC = (numeratorC/denominatorC) * (1 - doc);
  1082. if (allZero)
  1083. resT = 0.5;
  1084. else
  1085. resT = numeratorT/denominatorT;
  1086. }
  1087. // Special case for T
  1088. if (allZero)
  1089. resT = 0.5;
  1090. }
  1091. }
  1092. // Calculate F
  1093. if (allWeightsZero)
  1094. resF = 0;
  1095. else {
  1096. var numerator = 0, denominator = 0;
  1097. for (i = 0; i < arrLength; ++i) {
  1098. numerator += weights[i] * args[i].getF();
  1099. denominator += weights[i];
  1100. }
  1101. resF = numerator/denominator;
  1102. }
  1103. var result = args[0].clone();
  1104. result.setTC(resT, resC);
  1105. result.setF(resF);
  1106. result.setDoC(doc);
  1107. return result;
  1108. };
  1109. /**
  1110. * Performs weighted fusion for an array of CertainTrustSimple objects in correspondence with
  1111. * an array of weights. Returns new CertainTrust object.
  1112. * Requirements: N values of CertainTrustSimple objects must be equal.
  1113. * Number of weights should equal the number of CertainTrust objects.
  1114. * Arrays must be non-empty
  1115. * Either all of CertainTrust must be of certainty 1 or none of them.
  1116. * @param args - an array of CertainTrustSimple objects
  1117. * @param weights - an integer array of corresponding weights
  1118. * @return - new CertainTrustSimple object
  1119. */
  1120. CertainTrustSimple.prototype.simplewFusion = function(args, weights) {
  1121. //arrays should be equal
  1122. if (args.length == weights.length) {
  1123. //and not empty
  1124. if (args.length !== 0) {
  1125. for (var i = 1; i < args.length; ++i)
  1126. if (!this._operationAllowed(args[0], args[i]))
  1127. return undefined;
  1128. return this._simpleinternalFusion(args, weights, 0);
  1129. }
  1130. else throw "Arrays are empty. Operation not allowed. \n";
  1131. }
  1132. else throw "Different lengths of arrays. Operation not allowed. \n";
  1133. };
  1134. /**
  1135. * Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict
  1136. * between given opinions (CertainTrustSimple objects) and takes it into consideration while performing fusion.
  1137. * The degree of conflict is then saved in the resulting CertainTrust object and may be checked with getDoC() function.
  1138. * @param args - an array of CertainTrustSimple objects
  1139. * @param weights - an integer array of corresponding weights
  1140. * @return - new CertainTrustSimple object
  1141. */
  1142. CertainTrustSimple.prototype.simplecFusion = function(args, weights) { //
  1143. //arrays should be equal
  1144. if (args.length == weights.length) {
  1145. //and not empty
  1146. if (args.length !== 0) {
  1147. for (var i = 1; i < args.length; ++i)
  1148. if (!this._operationAllowed(args[0], args[i]))
  1149. return undefined;
  1150. var denominator = args.length*(args.length - 1) / 2;
  1151. var numerator = 0;
  1152. for (i = 0; i < args.length; ++i)
  1153. for (var j = i; j < args.length; ++j)
  1154. numerator += Math.abs(args[i].getT() - args[j].getT()) *
  1155. args[i].getC() * args[j].getC() *
  1156. (1 - Math.abs((weights[i] - weights[j]) /
  1157. (weights[i] + weights[j])));
  1158. var doc = numerator/denominator;
  1159. return this._simpleinternalFusion(args, weights, doc);
  1160. }
  1161. else throw "Arrays are empty. Operation not allowed. \n";
  1162. }
  1163. else throw "Different lengths of arrays. Operation not allowed. \n";
  1164. };