CertainTrust.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. if(atLeastOne1 && !allOne){
  395. for (i = 0; i < arrLength; ++i)
  396. if (args[i].getC() === 1) {
  397. args[i].setTC(args[i].getT(),0.99999);
  398. }
  399. }
  400. //Calculate T and C
  401. // 1. all C's = 1
  402. var numeratorT = 0, denominatorT = 0;
  403. if (allOne) {
  404. // set C
  405. resC = 1 * (1 - doc);
  406. // set T
  407. if (allWeightsZero) {// save some calculation time
  408. resT = 0;
  409. }
  410. else { // or use the function
  411. for (i = 0; i < arrLength; ++i) {
  412. numeratorT += weights[i] * args[i].getT();
  413. denominatorT += weights[i];
  414. }
  415. resT = numeratorT/denominatorT;
  416. }
  417. } else {
  418. // 2. Any other combination
  419. if (allWeightsZero) { // save some calculation time
  420. resT = 0;
  421. resC = 0;
  422. }
  423. else { // or use the function
  424. var numeratorC = 0, denominatorC = 0, mult;
  425. for (i = 0; i < arrLength; ++i) {
  426. mult = 1;
  427. for (var j = 0; j < arrLength; ++j) // Count the product for each sum element
  428. if (j !== i)
  429. mult *= 1 - args[j].getC();
  430. numeratorT += weights[i] * args[i].getT() * args[i].getC() * mult;
  431. denominatorT += weights[i] * args[i].getC() * mult;
  432. denominatorC += weights[i] * mult;
  433. }
  434. numeratorC = denominatorT;
  435. resC = (numeratorC/denominatorC) * (1 - doc);
  436. if (allZero)
  437. resT = 0.5;
  438. else
  439. resT = numeratorT/denominatorT;
  440. }
  441. // Special case for T
  442. if (allZero)
  443. resT = 0.5;
  444. }
  445. // Calculate F
  446. if (allWeightsZero)
  447. resF = 0;
  448. else {
  449. var numerator = 0, denominator = 0;
  450. for (i = 0; i < arrLength; ++i) {
  451. numerator += weights[i] * args[i].getF();
  452. denominator += weights[i];
  453. }
  454. resF = numerator/denominator;
  455. }
  456. var result = args[0].clone();
  457. result.setTC(resT, resC);
  458. result.setF(resF);
  459. result.setDoC(doc);
  460. return result;
  461. };
  462. /**
  463. * Performs weighted fusion for an array of CertainTrust objects in correspondence with
  464. * an array of weights. Returns new CertainTrust object.
  465. * Requirements: N values of CertainTrust objects must be equal.
  466. * Number of weights should equal the number of CertainTrust objects.
  467. * Arrays must be non-empty
  468. * Either all of CertainTrust must be of certainty 1 or none of them.
  469. * @param args - an array of CertainTrust objects
  470. * @param weights - an integer array of corresponding weights
  471. * @return - new CertainTrust object
  472. */
  473. CertainTrust.prototype.wFusion = function(args, weights) {
  474. //arrays should be equal
  475. if (args.length == weights.length) {
  476. //and not empty
  477. if (args.length !== 0) {
  478. for (var i = 1; i < args.length; ++i)
  479. if (!this._operationAllowed(args[0], args[i]))
  480. return undefined;
  481. return this._internalFusion(args, weights, 0);
  482. }
  483. else throw "Arrays are empty. Operation not allowed. \n";
  484. }
  485. else throw "Different lengths of arrays. Operation not allowed. \n";
  486. };
  487. /**
  488. * Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict
  489. * between given opinions (CertainTrust objects) and takes it into consideration while performing fusion.
  490. * The degree of conflict is then saved in the resulting CertainTrust object and may be checked with getDoC() function.
  491. * @param args - an array of CertainTrust objects
  492. * @param weights - an integer array of corresponding weights
  493. * @return - new CertainTrust object
  494. */
  495. CertainTrust.prototype.cFusion = function(args, weights) {
  496. //arrays should be equal
  497. if (args.length == weights.length) {
  498. //and not empty
  499. if (args.length !== 0) {
  500. for (var i = 1; i < args.length; ++i)
  501. if (!this._operationAllowed(args[0], args[i]))
  502. return undefined;
  503. var denominator = args.length*(args.length - 1) / 2;
  504. var numerator = 0;
  505. for (i = 0; i < args.length; ++i)
  506. for (var j = i; j < args.length; ++j)
  507. numerator += Math.abs(args[i].getT() - args[j].getT()) *
  508. args[i].getC() * args[j].getC() *
  509. (1 - Math.abs((weights[i] - weights[j]) /
  510. (weights[i] + weights[j])));
  511. var doc = numerator/denominator;
  512. return this._internalFusion(args, weights, doc);
  513. }
  514. else throw "Arrays are empty. Operation not allowed. \n";
  515. }
  516. else throw "Different lengths of arrays. Operation not allowed. \n";
  517. };
  518. //=========== Internal Calculations ==========
  519. /**
  520. * Normalises r and s values according to n - maximal number of expected evidence
  521. * Important! Doesn't notify observers.
  522. */
  523. CertainTrust.prototype._normaliseRS = function() {
  524. if ((this.r + this.s) > this.n) {
  525. var initR = this.r;
  526. this.r = (this.r * this.n) / (initR + this.s);
  527. this.s = (this.s * this.n) / (initR + this.s);
  528. }
  529. };
  530. /**
  531. * Calculates t and c values based on existing r and s values
  532. * Important! Doesn't notify observers.
  533. */
  534. CertainTrust.prototype._calculateRStoTC = function() {
  535. var rs = this.r + this.s;
  536. var nrs = this.n * rs;
  537. this.c = nrs / ((2 * this.weight * (this.n - this.r - this.s)) + nrs);
  538. if (this._almostEqual(this.c, 0))
  539. this.t = 0;
  540. else
  541. this.t = this.r / rs;
  542. };
  543. /**
  544. * Calculates r and s values based on existing c and t values
  545. * Important! Doesn't notify observers.
  546. */
  547. CertainTrust.prototype._calculateTCtoRS = function() {
  548. if (this._almostEqual(this.c, 0)) {
  549. this.r = 0;
  550. this.s = 0;
  551. this.t = 0;
  552. }
  553. else {
  554. var c2w = this.c * 2 * this.weight;
  555. var c2wn = c2w * this.n;
  556. var cn = this.c * this.n;
  557. this.r = (c2wn * this.t) / (c2w + this.n - cn);
  558. this.s = (c2wn - (c2wn * this.t)) / (c2w + this.n - cn);
  559. }
  560. };
  561. CertainTrust.prototype.clone = function() {
  562. var copy = new CertainTrust(this.getN());
  563. copy.c = this.c;
  564. copy.t = this.t;
  565. copy.f = this.f;
  566. copy.r = this.r;
  567. copy.s = this.s;
  568. copy.doc = this.doc;
  569. return copy;
  570. };
  571. CertainTrust.prototype._isString = function (obj) {
  572. return typeof(obj) === 'string';
  573. };
  574. //=========== Observer =================
  575. CertainTrust.prototype.notifyObservers = function(message) {
  576. for (var i = 0; i < this.observers.length; ++i)
  577. this.observers[i].update(this.observers[i], message);
  578. };
  579. CertainTrust.prototype.addObserver = function(observer) {
  580. this.observers.push(observer);
  581. };
  582. CertainTrust.prototype.deleteObserver = function(observer) {
  583. var idx = this.observers.indexOf(observer);
  584. if(idx !== -1)
  585. this.observers.splice(idx, 1);
  586. };
  587. //=== shared functions for frontends ===
  588. CertainTrust.prototype._insertElement = function(config, element) {
  589. var dom;
  590. if (config.domReturn === true) {
  591. return element;
  592. } else if (config.domParent !== undefined) {
  593. if (this._isString(config.domParent))
  594. document.getElementById(config.domParent).appendChild(element);
  595. else
  596. config.domParent.appendChild(element);
  597. } else if (config.domBefore !== undefined) {
  598. if (this._isString(config.domBefore))
  599. dom = document.getElementById(config.domBefore);
  600. else
  601. dom = config.domBefore;
  602. dom.parentNode.insertBefore(element, dom);
  603. } else {
  604. if (config.domAfter === undefined) {
  605. // the last script tag in DOM tree is the one creating this widget
  606. var scripts = document.getElementsByTagName('script');
  607. dom = scripts[scripts.length - 1];
  608. } else if (this._isString(config.domAfter))
  609. dom = document.getElementById(config.domAfter);
  610. else
  611. dom = config.domAfter;
  612. dom.parentNode.insertBefore(element, dom.nextSibling);
  613. }
  614. return undefined;
  615. };
  616. CertainTrust.prototype._getColor = function(certainty, trust, initf) {
  617. var resultp2 = ((1 - certainty) * initf);
  618. var result = (trust * certainty) + resultp2;
  619. var color;
  620. if (result < 0.5) {
  621. color = [
  622. 255,
  623. Math.min(255, (255 * 2 * result)),
  624. 0
  625. ];
  626. } else {
  627. color = [
  628. Math.min(255, ((2 - (2 * result)) * 255)),
  629. 255,
  630. 0
  631. ];
  632. }
  633. return color;
  634. };
  635. CertainTrust.prototype._pointOnCircle = function(centerx, centery, pointgrade, radius) {
  636. var pointrad = ((360 + pointgrade) % 360) * ((2 * Math.PI) / 360);
  637. var chord = 2 * radius * Math.sin((pointrad / 2));
  638. // height of our new point above the base-edge
  639. var y = Math.sqrt(2
  640. * (Math.pow(chord, 2) * Math.pow(radius, 2)
  641. + Math.pow(radius, 4) + Math.pow(radius, 2)
  642. * Math.pow(chord, 2))
  643. - (Math.pow(chord, 4) + 2 * Math.pow(radius, 4)))
  644. / (2 * radius);
  645. // distance to the cross-point of base-edge and height
  646. var a = Math.pow(radius, 2);
  647. var c = Math.pow(y, 2);
  648. // we do this to protect us from NaN cause by 1 - 1.00000004
  649. var x = (a < c) ? 0 : Math.sqrt(a - c);
  650. var directions = new Array("NE", "SE", "SW", "NW");
  651. var direction = 0;
  652. var alpharad = pointrad;
  653. while (alpharad > (0.5 * Math.PI)) {
  654. ++direction;
  655. alpharad -= (0.5 * Math.PI);
  656. }
  657. if (directions[direction] == "NE" || directions[direction] == "NW")
  658. x *= -1;
  659. if (directions[direction] == "SW" || directions[direction] == "NW")
  660. y *= -1;
  661. return new Array((centerx + x), (centery + y));
  662. };
  663. /* optional implementation of CertainTrust without R and S calculations */
  664. var CertainTrustSimple = function() {
  665. this.weight = 2;
  666. this.observers = [];
  667. var offset = 0;
  668. this.name = "";
  669. if (this._isString(arguments[0])) {
  670. this.name = arguments[0];
  671. offset = 1;
  672. }
  673. if (arguments.length == 3 + offset || arguments.length == 4 + offset) {
  674. // CertainTrustSimple(t, c, f, doc)
  675. // doc is a 'private' parameter
  676. this.t = arguments[0 + offset];
  677. this.c = arguments[1 + offset];
  678. this.f = arguments[2 + offset];
  679. this.doc = (arguments.length == 3 + offset) ? 0 : arguments[3 + offset];
  680. if (this.f < 0 && this.f > 1)
  681. throw "f should lie within [0;1]. Entered f = " + this.f + "\n";
  682. if (this.c < 0 && this.c > 1)
  683. throw "c should lie within [0;1]. Entered c = " + this.c + "\n";
  684. if (this.t < 0 && this.t > 1)
  685. throw "t should lie within [0;1]. Entered t = " + this.t + "\n";
  686. } else {
  687. this.c = 0;
  688. this.t = 0.5;
  689. this.f = 0.5;
  690. this.doc = 0;
  691. }
  692. };
  693. CertainTrustSimple.prototype = new CertainTrust(1);
  694. CertainTrustSimple.prototype.constructor = CertainTrustSimple;
  695. CertainTrustSimple.prototype._operationAllowed = function() { return true; }
  696. CertainTrustSimple.prototype._calculateTCtoRS = function() { }
  697. CertainTrustSimple.prototype._calculateRStoTC = function() { }
  698. CertainTrustSimple.prototype._normaliseRS = function() { }
  699. CertainTrustSimple.prototype.setRS = undefined;
  700. CertainTrustSimple.prototype.addR = undefined;
  701. CertainTrustSimple.prototype.addS = undefined;
  702. CertainTrustSimple.prototype.setN = undefined;
  703. CertainTrustSimple.prototype.getR = undefined;
  704. CertainTrustSimple.prototype.getS = undefined;
  705. CertainTrustSimple.prototype.getN = undefined;
  706. CertainTrustSimple.prototype.clone = function() {
  707. var copy = new CertainTrustSimple();
  708. copy.c = this.c;
  709. copy.t = this.t;
  710. copy.f = this.f;
  711. copy.doc = this.doc;
  712. return copy;
  713. };
  714. /*Added by Debashis*/
  715. /**
  716. * Computes OR function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  717. * argument and this CertainTrust object remain unchanged.
  718. * N values of both objects should be equal.
  719. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  720. * @param arg - CertainTrustSimple object
  721. * @return - result of OR computation for this object and an argument.
  722. */
  723. CertainTrust.prototype._singlesimpleOR = function(arg) {
  724. var c1 = this.getC();
  725. var t1 = this.getT();
  726. var f1 = this.getF();
  727. var c2 = arg.getC();
  728. var t2 = arg.getT();
  729. var f2 = arg.getF();
  730. var resT = 0.5, resF = 0.5, resC = 0;
  731. if (!this._operationAllowed(this, arg))
  732. return undefined;
  733. resF = f1 + f2 - f1*f2;
  734. if (this._almostEqual(resF, 0)){
  735. f1 = 0.99999;
  736. f2 = 0.99999;
  737. resF = f1 + f2 - f1*f2;
  738. resC = c1 + c2 - c1*c2- (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  739. }
  740. else
  741. resC = c1 + c2 - c1*c2 - (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  742. if (this._almostEqual(resC, 0))
  743. resT = 0.5;
  744. else resT = (1/resC) * (c1*t1 + c2*t2 - c1*c2*t1*t2);
  745. resT = this._adjustValue(resT);
  746. resC = this._adjustValue(resC);
  747. resF = this._adjustValue(resF);
  748. var result = new CertainTrustSimple(resT, resC, resF, this.n, 0);
  749. return result;
  750. };
  751. /**
  752. * Computes OR function for this CertainTrustSimple object and the specified arguments.
  753. * Result is returned as a new object, arguments and this CertainTrustSimple object remain unchanged.
  754. * Example: a.OR(b, c, d) returns new CertainTrust object that equals a OR b OR c OR d.
  755. * Multiple arguments allowed, but not less than one.
  756. * N values of all objects should be equal.
  757. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  758. * @param args - arguments
  759. * @return - result of OR computation for this object and all arguments.
  760. */
  761. CertainTrustSimple.prototype.simpleOR = function() {
  762. var result = this.clone();
  763. for (var i = 0; i < arguments.length; ++i) {
  764. var m = arguments[i];
  765. if (!this._operationAllowed(this, m))
  766. continue;
  767. result = result._singlesimpleOR(m);
  768. }
  769. return result;
  770. };
  771. /**
  772. * Computes AND function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  773. * argument and this CertainTrustSimple object remain unchanged.
  774. * N values of both objects should be equal.
  775. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  776. * @param arg - CertainTrustSimple object
  777. * @return - result of AND computation for this object and an argument.
  778. */
  779. CertainTrustSimple.prototype._singlesimpleAND = function(arg){
  780. var c1 = this.getC();
  781. var f1 = this.getF();
  782. var t1 = this.getT();
  783. var c2 = arg.getC();
  784. var f2 = arg.getF();
  785. var t2 = arg.getT();
  786. var resC = 0, resT = 0.5, resF = 0.5;
  787. if (!this._operationAllowed(this, arg))
  788. return undefined;
  789. resF = f1*f2;
  790. if (this._almostEqual(resF, 1)){ //avoid division by 0
  791. f1 = 0.99999;
  792. f2 = 0.99999;
  793. resF = f1*f2;
  794. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  795. }
  796. else
  797. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  798. if (this._almostEqual(resC, 0))
  799. resT = 0.5;
  800. else resT = (1/resC) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
  801. resT = this._adjustValue(resT);
  802. resC = this._adjustValue(resC);
  803. resF = this._adjustValue(resF);
  804. return new CertainTrustSimple(resT, resC, resF);
  805. };
  806. /**
  807. * Computes AND function for this CertainTrustSimple object and the specified arguments.
  808. * Result is returned as a new object, arguments and this CertainTrustSimple object remain unchanged.
  809. * Example: a.AND(b, c, d) returns new CertainTrustSimple object that equals a AND b AND c AND d.
  810. * Multiple arguments allowed, but not less than one.
  811. * N values of all objects should be equal.
  812. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  813. * @param args - arguments
  814. * @return - result of AND computation for this object and all arguments.
  815. */
  816. CertainTrustSimple.prototype.simpleAND = function() {
  817. var result = this.clone();
  818. for (var i = 0; i < arguments.length; i++) {
  819. var m = arguments[i];
  820. if (!this._operationAllowed(this, m))
  821. continue;
  822. result = result._singlesimpleAND(m);
  823. }
  824. return result;
  825. };
  826. /**
  827. * Returns NOT of this CertainTrustSimple object.
  828. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  829. * @return - NOT of this CertainTrustSimple object.
  830. */
  831. CertainTrustSimple.prototype.simpleNOT = function() {
  832. var result = this.clone();
  833. result.setTC(1 - this.getT(), this.getC());
  834. result.setF(1 - this.getF());
  835. result.setDoC(0);
  836. return result;
  837. };
  838. CertainTrust.prototype.setName = function(newname) {
  839. this.name = newname;
  840. };
  841. /**
  842. * Computes CONSENSUS function for this CertainTrust object and the specified argument. Result is returned as a new object,
  843. * argument and this CertainTrust object remain unchanged.
  844. * N values of both objects should be equal.
  845. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  846. * @param arg - CertainTrust object
  847. * @return - result of CONSENSUS computation for this object and an argument.
  848. */
  849. CertainTrust.prototype._singleCONSENSUS = function(arg){
  850. var c1 = this.getC();
  851. var f1 = this.getF();
  852. var t1 = this.getT();
  853. var c2 = arg.getC();
  854. var f2 = arg.getF();
  855. var t2 = arg.getT();
  856. var resC = 0, resT = 0.5, resF = 0.5;
  857. if (!this._operationAllowed(this, arg))
  858. return undefined;
  859. var tempC = c1*c2;
  860. if (this._almostEqual(tempC, 1)){ //avoid division by 0
  861. c1 = 0.99999;
  862. c2 = 0.99999;
  863. }
  864. resF = (f1*c1*(1-c2) + f2*c2*(1-c1))/(c1+c2-2*c1*c2);
  865. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  866. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1+c2-2*c1*c2);
  867. resT = this._adjustValue(resT);
  868. resC = this._adjustValue(resC);
  869. resF = this._adjustValue(resF);
  870. return new CertainTrust(resT, resC, resF, this.n, 0);
  871. };
  872. CertainTrust.prototype.CONSENSUS = function() {
  873. var result = this.clone();
  874. for (var i = 0; i < arguments.length; i++) {
  875. var m = arguments[i];
  876. if (!this._operationAllowed(this, m))
  877. continue;
  878. result = result._singleCONSENSUS(m);
  879. }
  880. return result;
  881. };
  882. /**
  883. * Computes DISCOUNTING function for this CertainTrust object and the specified argument. Result is returned as a new object,
  884. * argument and this CertainTrust object remain unchanged.
  885. * N values of both objects should be equal.
  886. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  887. * @param arg - CertainTrust object
  888. * @return - result of DISCOUNTING computation for this object and an argument.
  889. */
  890. CertainTrust.prototype._singleDISCOUNTING = function(arg){
  891. var c1 = this.getC();
  892. var f1 = this.getF();
  893. var t1 = this.getT();
  894. var c2 = arg.getC();
  895. var f2 = arg.getF();
  896. var t2 = arg.getT();
  897. var resC = 0, resT = 0, resF = 0;
  898. if (!this._operationAllowed(this, arg))
  899. return undefined;
  900. resF = f2;
  901. if (this._almostEqual(resF, 1)) //avoid division by 0
  902. resC = t1*c1*c2;
  903. else
  904. resC = t1*c1*c2;
  905. if (this._almostEqual(resC, 0))
  906. resT = t2;
  907. else if (this._almostEqual(resF, 1)) //avoid division by 0
  908. resT = t2;
  909. else resT = t2;
  910. resT = this._adjustValue(resT);
  911. resC = this._adjustValue(resC);
  912. resF = this._adjustValue(resF);
  913. return new CertainTrust(resT, resC, resF, this.n, 0);
  914. };
  915. CertainTrust.prototype.DISCOUNTING = function() {
  916. var result = this.clone();
  917. for (var i = 0; i < arguments.length; i++) {
  918. var m = arguments[i];
  919. if (!this._operationAllowed(this, m))
  920. continue;
  921. result = result._singleDISCOUNTING(m);
  922. }
  923. return result;
  924. };
  925. /**
  926. * Computes CONSENSUS function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  927. * argument and this CertainTrust object remain unchanged.
  928. * N values of both objects should be equal.
  929. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  930. * @param arg - CertainTrustSimple object
  931. * @return - result of CONSENSUS computation for this object and an argument.
  932. */
  933. CertainTrustSimple.prototype._singlesimpleCONSENSUS = function(arg){
  934. var c1 = this.getC();
  935. var f1 = this.getF();
  936. var t1 = this.getT();
  937. var c2 = arg.getC();
  938. var f2 = arg.getF();
  939. var t2 = arg.getT();
  940. var resC = 0, resT = 0.5, resF = 0.5;
  941. if (!this._operationAllowed(this, arg))
  942. return undefined;
  943. var tempC = c1*c2;
  944. if (this._almostEqual(tempC, 1)){ //avoid division by 0
  945. c1 = 0.99999;
  946. c2 = 0.99999;
  947. }
  948. resF = (f1*c1*(1-c2) + f2*c2*(1-c1))/(c1+c2-2*c1*c2);
  949. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  950. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1+c2-2*c1*c2);
  951. resT = this._adjustValue(resT);
  952. resC = this._adjustValue(resC);
  953. resF = this._adjustValue(resF);
  954. return new CertainTrustSimple(resT, resC, resF);
  955. };
  956. CertainTrustSimple.prototype.simpleCONSENSUS = function() {
  957. var result = this.clone();
  958. for (var i = 0; i < arguments.length; i++) {
  959. var m = arguments[i];
  960. if (!this._operationAllowed(this, m))
  961. continue;
  962. result = result._singlesimpleCONSENSUS(m);
  963. }
  964. return result;
  965. };
  966. /**
  967. * Computes DISCOUNTING function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  968. * argument and this CertainTrustSimple object remain unchanged.
  969. * N values of both objects should be equal.
  970. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  971. * @param arg - CertainTrustSimple object
  972. * @return - result of DISCOUNTING computation for this object and an argument.
  973. */
  974. CertainTrustSimple.prototype._singlesimpleDISCOUNTING = function(arg){
  975. var c1 = this.getC();
  976. var f1 = this.getF();
  977. var t1 = this.getT();
  978. var c2 = arg.getC();
  979. var f2 = arg.getF();
  980. var t2 = arg.getT();
  981. var resC = 0, resT = 0.5, resF = 0.5;
  982. if (!this._operationAllowed(this, arg))
  983. return undefined;
  984. //resF = f1*f2;
  985. if (this._almostEqual(resF, 1)) //avoid division by 0
  986. resC = t1*c1*c2;
  987. else
  988. resC = t1*c1*c2;
  989. if (this._almostEqual(resC, 0))
  990. resT = t2;
  991. else if (this._almostEqual(resF, 1)) //avoid division by 0
  992. resT = t2;
  993. else resT = t2;
  994. resT = this._adjustValue(resT);
  995. resC = this._adjustValue(resC);
  996. resF = this._adjustValue(resF);
  997. return new CertainTrustSimple(resT, resC, resF, this.n, 0);
  998. };
  999. CertainTrustSimple.prototype.simpleDISCOUNTING = function() {
  1000. var result = this.clone();
  1001. for (var i = 0; i < arguments.length; i++) {
  1002. var m = arguments[i];
  1003. if (!this._operationAllowed(this, m))
  1004. continue;
  1005. result = result._singlesimpleDISCOUNTING(m);
  1006. }
  1007. return result;
  1008. };
  1009. /**
  1010. * an internal implementation of fusion function.
  1011. * Is called by wFusion and cFusion
  1012. * @param args - an array of CertainTrustSimple objects
  1013. * @param weights - an integer array of corresponding weights
  1014. * @param doc - a degree of conflict (always 0 for wFusion)
  1015. * @return - new CertainTrustSimple object
  1016. */
  1017. CertainTrustSimple.prototype._simpleinternalFusion = function(args, weights, doc) {
  1018. var resC, resT, resF;
  1019. var allOne = true;
  1020. var allZero = true;
  1021. var allWeightsZero = true;
  1022. var atLeastOne1 = false;
  1023. var arrLength = args.length;
  1024. // set the flags about C and Weight values
  1025. for (var i = 0; i < arrLength; ++i)
  1026. if (args[i].getC() !== 1) {
  1027. allOne = false;
  1028. i = arrLength;
  1029. }
  1030. for (i = 0; i < arrLength; ++i)
  1031. if (args[i].getC() !== 0) {
  1032. allZero = false;
  1033. i = arrLength;
  1034. }
  1035. for (i = 0; i < arrLength; ++i)
  1036. if (weights[i] !== 0) {
  1037. allWeightsZero = false;
  1038. i = arrLength;
  1039. }
  1040. for (i = 0; i < arrLength; ++i)
  1041. if (args[i].getC() === 1) {
  1042. atLeastOne1 = true;
  1043. i = arrLength;
  1044. }
  1045. //Calculate T and C
  1046. // 1. all C's = 1
  1047. var numeratorT = 0, denominatorT = 0;
  1048. if (allOne) {
  1049. // set C
  1050. resC = 1 * (1 - doc);
  1051. // set T
  1052. if (allWeightsZero) {// save some calculation time
  1053. resT = 0;
  1054. }
  1055. else { // or use the function
  1056. for (i = 0; i < arrLength; ++i) {
  1057. numeratorT += weights[i] * args[i].getT();
  1058. denominatorT += weights[i];
  1059. }
  1060. resT = numeratorT/denominatorT;
  1061. }
  1062. } else {
  1063. if (atLeastOne1)
  1064. throw "Illegal arguments. Either all C values must equal 1 or none of them. Operation not allowed\n";
  1065. else {
  1066. // 2. Any other combination
  1067. if (allWeightsZero) { // save some calculation time
  1068. resT = 0;
  1069. resC = 0;
  1070. }
  1071. else { // or use the function
  1072. var numeratorC = 0, denominatorC = 0, mult;
  1073. for (i = 0; i < arrLength; ++i) {
  1074. mult = 1;
  1075. for (var j = 0; j < arrLength; ++j) // Count the product for each sum element
  1076. if (j !== i)
  1077. mult *= 1 - args[j].getC();
  1078. numeratorT += weights[i] * args[i].getT() * args[i].getC() * mult;
  1079. denominatorT += weights[i] * args[i].getC() * mult;
  1080. denominatorC += weights[i] * mult;
  1081. }
  1082. numeratorC = denominatorT;
  1083. resC = (numeratorC/denominatorC) * (1 - doc);
  1084. if (allZero)
  1085. resT = 0.5;
  1086. else
  1087. resT = numeratorT/denominatorT;
  1088. }
  1089. // Special case for T
  1090. if (allZero)
  1091. resT = 0.5;
  1092. }
  1093. }
  1094. // Calculate F
  1095. if (allWeightsZero)
  1096. resF = 0;
  1097. else {
  1098. var numerator = 0, denominator = 0;
  1099. for (i = 0; i < arrLength; ++i) {
  1100. numerator += weights[i] * args[i].getF();
  1101. denominator += weights[i];
  1102. }
  1103. resF = numerator/denominator;
  1104. }
  1105. var result = args[0].clone();
  1106. result.setTC(resT, resC);
  1107. result.setF(resF);
  1108. result.setDoC(doc);
  1109. return result;
  1110. };
  1111. /**
  1112. * Performs weighted fusion for an array of CertainTrustSimple objects in correspondence with
  1113. * an array of weights. Returns new CertainTrust object.
  1114. * Requirements: N values of CertainTrustSimple objects must be equal.
  1115. * Number of weights should equal the number of CertainTrust objects.
  1116. * Arrays must be non-empty
  1117. * Either all of CertainTrust must be of certainty 1 or none of them.
  1118. * @param args - an array of CertainTrustSimple objects
  1119. * @param weights - an integer array of corresponding weights
  1120. * @return - new CertainTrustSimple object
  1121. */
  1122. CertainTrustSimple.prototype.simplewFusion = function(args, weights) {
  1123. //arrays should be equal
  1124. if (args.length == weights.length) {
  1125. //and not empty
  1126. if (args.length !== 0) {
  1127. for (var i = 1; i < args.length; ++i)
  1128. if (!this._operationAllowed(args[0], args[i]))
  1129. return undefined;
  1130. return this._simpleinternalFusion(args, weights, 0);
  1131. }
  1132. else throw "Arrays are empty. Operation not allowed. \n";
  1133. }
  1134. else throw "Different lengths of arrays. Operation not allowed. \n";
  1135. };
  1136. /**
  1137. * Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict
  1138. * between given opinions (CertainTrustSimple objects) and takes it into consideration while performing fusion.
  1139. * The degree of conflict is then saved in the resulting CertainTrust object and may be checked with getDoC() function.
  1140. * @param args - an array of CertainTrustSimple objects
  1141. * @param weights - an integer array of corresponding weights
  1142. * @return - new CertainTrustSimple object
  1143. */
  1144. CertainTrustSimple.prototype.simplecFusion = function(args, weights) { //
  1145. //arrays should be equal
  1146. if (args.length == weights.length) {
  1147. //and not empty
  1148. if (args.length !== 0) {
  1149. for (var i = 1; i < args.length; ++i)
  1150. if (!this._operationAllowed(args[0], args[i]))
  1151. return undefined;
  1152. var denominator = args.length*(args.length - 1) / 2;
  1153. var numerator = 0;
  1154. for (i = 0; i < args.length; ++i)
  1155. for (var j = i; j < args.length; ++j)
  1156. numerator += Math.abs(args[i].getT() - args[j].getT()) *
  1157. args[i].getC() * args[j].getC() *
  1158. (1 - Math.abs((weights[i] - weights[j]) /
  1159. (weights[i] + weights[j])));
  1160. var doc = numerator/denominator;
  1161. return this._simpleinternalFusion(args, weights, doc);
  1162. }
  1163. else throw "Arrays are empty. Operation not allowed. \n";
  1164. }
  1165. else throw "Different lengths of arrays. Operation not allowed. \n";
  1166. };