CertainTrust.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. CertainTrustSimple.prototype._singlesimpleAND = function(arg){
  714. var c1 = this.getC();
  715. var f1 = this.getF();
  716. var t1 = this.getT();
  717. var c2 = arg.getC();
  718. var f2 = arg.getF();
  719. var t2 = arg.getT();
  720. var resC = 0, resT = 0.5, resF = 0.5;
  721. if (!this._operationAllowed(this, arg))
  722. return undefined;
  723. resF = f1*f2;
  724. if (this._almostEqual(resF, 1)){ //avoid division by 0
  725. f1 = 0.99999;
  726. f2 = 0.99999;
  727. resF = f1*f2;
  728. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  729. }
  730. else
  731. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  732. if (this._almostEqual(resC, 0))
  733. resT = 0.5;
  734. else resT = (1/resC) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
  735. resT = this._adjustValue(resT);
  736. resC = this._adjustValue(resC);
  737. resF = this._adjustValue(resF);
  738. return new CertainTrustSimple(resT, resC, resF);
  739. };
  740. CertainTrustSimple.prototype.simpleAND = function() {
  741. var result = this.clone();
  742. for (var i = 0; i < arguments.length; i++) {
  743. var m = arguments[i];
  744. if (!this._operationAllowed(this, m))
  745. continue;
  746. result = result._singlesimpleAND(m);
  747. }
  748. return result;
  749. };
  750. CertainTrust.prototype.setName = function(newname) {
  751. this.name = newname;
  752. };
  753. /**
  754. * Computes CONSENSUS function for this CertainTrust object and the specified argument. Result is returned as a new object,
  755. * argument and this CertainTrust object remain unchanged.
  756. * N values of both objects should be equal.
  757. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  758. * @param arg - CertainTrust object
  759. * @return - result of CONSENSUS computation for this object and an argument.
  760. */
  761. CertainTrust.prototype._singleCONSENSUS = function(arg){
  762. var c1 = this.getC();
  763. var f1 = this.getF();
  764. var t1 = this.getT();
  765. var c2 = arg.getC();
  766. var f2 = arg.getF();
  767. var t2 = arg.getT();
  768. var resC = 0, resT = 0.5, resF = 0.5;
  769. if (!this._operationAllowed(this, arg))
  770. return undefined;
  771. var tempC = c1*c2;
  772. if (this._almostEqual(tempC, 1)){ //avoid division by 0
  773. c1 = 0.99999;
  774. c2 = 0.99999;
  775. }
  776. resF = (f1*c1*(1-c2) + f2*c2*(1-c1))/(c1+c2-2*c1*c2);
  777. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  778. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1+c2-2*c1*c2);
  779. resT = this._adjustValue(resT);
  780. resC = this._adjustValue(resC);
  781. resF = this._adjustValue(resF);
  782. return new CertainTrust(resT, resC, resF, this.n, 0);
  783. };
  784. CertainTrust.prototype.CONSENSUS = function() {
  785. var result = this.clone();
  786. for (var i = 0; i < arguments.length; i++) {
  787. var m = arguments[i];
  788. if (!this._operationAllowed(this, m))
  789. continue;
  790. result = result._singleCONSENSUS(m);
  791. }
  792. return result;
  793. };
  794. /**
  795. * Computes DISCOUNTING function for this CertainTrust object and the specified argument. Result is returned as a new object,
  796. * argument and this CertainTrust object remain unchanged.
  797. * N values of both objects should be equal.
  798. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  799. * @param arg - CertainTrust object
  800. * @return - result of DISCOUNTING computation for this object and an argument.
  801. */
  802. CertainTrust.prototype._singleDISCOUNTING = function(arg){
  803. var c1 = this.getC();
  804. var f1 = this.getF();
  805. var t1 = this.getT();
  806. var c2 = arg.getC();
  807. var f2 = arg.getF();
  808. var t2 = arg.getT();
  809. var resC = 0, resT = 0.5, resF = 0.5;
  810. if (!this._operationAllowed(this, arg))
  811. return undefined;
  812. //resF = f1*f2;
  813. if (this._almostEqual(resF, 1)) //avoid division by 0
  814. resC = t1*c1*c2;
  815. else
  816. resC = t1*c1*c2;
  817. if (this._almostEqual(resC, 0))
  818. resT = t2;
  819. else if (this._almostEqual(resF, 1)) //avoid division by 0
  820. resT = t2;
  821. else resT = t2;
  822. resT = this._adjustValue(resT);
  823. resC = this._adjustValue(resC);
  824. resF = this._adjustValue(resF);
  825. return new CertainTrust(resT, resC, resF, this.n, 0);
  826. };
  827. CertainTrust.prototype.DISCOUNTING = function() {
  828. var result = this.clone();
  829. for (var i = 0; i < arguments.length; i++) {
  830. var m = arguments[i];
  831. if (!this._operationAllowed(this, m))
  832. continue;
  833. result = result._singleDISCOUNTING(m);
  834. }
  835. return result;
  836. };
  837. /**
  838. * Computes CONSENSUS function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  839. * argument and this CertainTrust object remain unchanged.
  840. * N values of both objects should be equal.
  841. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  842. * @param arg - CertainTrustSimple object
  843. * @return - result of CONSENSUS computation for this object and an argument.
  844. */
  845. CertainTrustSimple.prototype._singlesimpleCONSENSUS = function(arg){
  846. var c1 = this.getC();
  847. var f1 = this.getF();
  848. var t1 = this.getT();
  849. var c2 = arg.getC();
  850. var f2 = arg.getF();
  851. var t2 = arg.getT();
  852. var resC = 0, resT = 0.5, resF = 0.5;
  853. if (!this._operationAllowed(this, arg))
  854. return undefined;
  855. var tempC = c1*c2;
  856. if (this._almostEqual(tempC, 1)){ //avoid division by 0
  857. c1 = 0.99999;
  858. c2 = 0.99999;
  859. }
  860. resF = (f1*c1*(1-c2) + f2*c2*(1-c1))/(c1+c2-2*c1*c2);
  861. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  862. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1+c2-2*c1*c2);
  863. resT = this._adjustValue(resT);
  864. resC = this._adjustValue(resC);
  865. resF = this._adjustValue(resF);
  866. return new CertainTrustSimple(resT, resC, resF);
  867. };
  868. CertainTrustSimple.prototype.simpleCONSENSUS = function() {
  869. var result = this.clone();
  870. for (var i = 0; i < arguments.length; i++) {
  871. var m = arguments[i];
  872. if (!this._operationAllowed(this, m))
  873. continue;
  874. result = result._singlesimpleCONSENSUS(m);
  875. }
  876. return result;
  877. };