CertainTrust.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. resC = c1 + c2 - c1*c2;
  244. else
  245. resC = c1 + c2 - c1*c2 - (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  246. if (this._almostEqual(resC, 0))
  247. resT = 0.5;
  248. else resT = (1/resC) * (c1*t1 + c2*t2 - c1*c2*t1*t2);
  249. resT = this._adjustValue(resT);
  250. resC = this._adjustValue(resC);
  251. resF = this._adjustValue(resF);
  252. var result = new CertainTrust(resT, resC, resF, this.n, 0);
  253. return result;
  254. };
  255. /**
  256. * Computes OR function for this CertainTrust object and the specified arguments.
  257. * Result is returned as a new object, arguments and this CertainTrust object remain unchanged.
  258. * Example: a.OR(b, c, d) returns new CertainTrust object that equals a OR b OR c OR d.
  259. * Multiple arguments allowed, but not less than one.
  260. * N values of all objects should be equal.
  261. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  262. * @param args - arguments
  263. * @return - result of OR computation for this object and all arguments.
  264. */
  265. CertainTrust.prototype.OR = function() {
  266. var result = this.clone();
  267. for (var i = 0; i < arguments.length; ++i) {
  268. var m = arguments[i];
  269. if (!this._operationAllowed(this, m))
  270. continue;
  271. result = result._singleOR(m);
  272. }
  273. return result;
  274. };
  275. /**
  276. * Computes AND function for this CertainTrust object and the specified argument. Result is returned as a new object,
  277. * argument and this CertainTrust object remain unchanged.
  278. * N values of both objects should be equal.
  279. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  280. * @param arg - CertainTrust object
  281. * @return - result of AND computation for this object and an argument.
  282. */
  283. CertainTrust.prototype._singleAND = function(arg){
  284. var c1 = this.getC();
  285. var f1 = this.getF();
  286. var t1 = this.getT();
  287. var c2 = arg.getC();
  288. var f2 = arg.getF();
  289. var t2 = arg.getT();
  290. var resC = 0, resT = 0.5, resF = 0.5;
  291. if (!this._operationAllowed(this, arg))
  292. return undefined;
  293. resF = f1*f2;
  294. if (this._almostEqual(resF, 1)){ //avoid division by 0
  295. f1 = 0.99999;
  296. f2 = 0.99999;
  297. resF = f1*f2;
  298. resC = c1 + c2 - c1*c2- (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  299. else
  300. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  301. if (this._almostEqual(resC, 0))
  302. resT = 0.5;
  303. else resT = (1/resC) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
  304. resT = this._adjustValue(resT);
  305. resC = this._adjustValue(resC);
  306. resF = this._adjustValue(resF);
  307. return new CertainTrust(resT, resC, resF, this.n, 0);
  308. };
  309. CertainTrust.prototype._adjustValue = function(arg) {
  310. return Math.max(Math.min(arg, 1), 0);
  311. };
  312. CertainTrust.prototype._almostEqual = function(value, target) {
  313. return Math.abs(value - target) < 1E-10;
  314. };
  315. CertainTrust.prototype._operationAllowed = function(arg1, arg2) {
  316. //and all N's of TC's must be equal
  317. /*if (arg1.getN() != arg2.getN()) //Disabled by Debashis C. Ray for AND calculation
  318. throw "Different N values. Operation not allowed. \n"; */
  319. return true;
  320. }
  321. /**
  322. * Computes AND function for this CertainTrust object and the specified arguments.
  323. * Result is returned as a new object, arguments and this CertainTrust object remain unchanged.
  324. * Example: a.AND(b, c, d) returns new CertainTrust object that equals a AND b AND c AND d.
  325. * Multiple arguments allowed, but not less than one.
  326. * N values of all objects should be equal.
  327. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  328. * @param args - arguments
  329. * @return - result of AND computation for this object and all arguments.
  330. */
  331. CertainTrust.prototype.AND = function() {
  332. var result = this.clone();
  333. for (var i = 0; i < arguments.length; i++) {
  334. var m = arguments[i];
  335. if (!this._operationAllowed(this, m))
  336. continue;
  337. result = result._singleAND(m);
  338. }
  339. return result;
  340. };
  341. /**
  342. * Returns NOT of this CertainTrust object.
  343. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  344. * @return - NOT of this CertainTrust object.
  345. */
  346. CertainTrust.prototype.NOT = function() {
  347. var result = this.clone();
  348. result.setTC(1 - this.getT(), this.getC());
  349. result.setF(1 - this.getF());
  350. result.setDoC(0);
  351. return result;
  352. };
  353. /**
  354. * an internal implementation of fusion function.
  355. * Is called by wFusion and cFusion
  356. * @param args - an array of CertainTrust objects
  357. * @param weights - an integer array of corresponding weights
  358. * @param doc - a degree of conflict (always 0 for wFusion)
  359. * @return - new CertainTrust object
  360. */
  361. CertainTrust.prototype._internalFusion = function(args, weights, doc) {
  362. var resC, resT, resF;
  363. var allOne = true;
  364. var allZero = true;
  365. var allWeightsZero = true;
  366. var atLeastOne1 = false;
  367. var arrLength = args.length;
  368. // set the flags about C and Weight values
  369. for (var i = 0; i < arrLength; ++i)
  370. if (args[i].getC() !== 1) {
  371. allOne = false;
  372. i = arrLength;
  373. }
  374. for (i = 0; i < arrLength; ++i)
  375. if (args[i].getC() !== 0) {
  376. allZero = false;
  377. i = arrLength;
  378. }
  379. for (i = 0; i < arrLength; ++i)
  380. if (weights[i] !== 0) {
  381. allWeightsZero = false;
  382. i = arrLength;
  383. }
  384. for (i = 0; i < arrLength; ++i)
  385. if (args[i].getC() === 1) {
  386. atLeastOne1 = true;
  387. i = arrLength;
  388. }
  389. //Calculate T and C
  390. // 1. all C's = 1
  391. var numeratorT = 0, denominatorT = 0;
  392. if (allOne) {
  393. // set C
  394. resC = 1 * (1 - doc);
  395. // set T
  396. if (allWeightsZero) {// save some calculation time
  397. resT = 0;
  398. }
  399. else { // or use the function
  400. for (i = 0; i < arrLength; ++i) {
  401. numeratorT += weights[i] * args[i].getT();
  402. denominatorT += weights[i];
  403. }
  404. resT = numeratorT/denominatorT;
  405. }
  406. } else {
  407. if (atLeastOne1)
  408. throw "Illegal arguments. Either all C values must equal 1 or none of them. Operation not allowed\n";
  409. else {
  410. // 2. Any other combination
  411. if (allWeightsZero) { // save some calculation time
  412. resT = 0;
  413. resC = 0;
  414. }
  415. else { // or use the function
  416. var numeratorC = 0, denominatorC = 0, mult;
  417. for (i = 0; i < arrLength; ++i) {
  418. mult = 1;
  419. for (var j = 0; j < arrLength; ++j) // Count the product for each sum element
  420. if (j !== i)
  421. mult *= 1 - args[j].getC();
  422. numeratorT += weights[i] * args[i].getT() * args[i].getC() * mult;
  423. denominatorT += weights[i] * args[i].getC() * mult;
  424. denominatorC += weights[i] * mult;
  425. }
  426. numeratorC = denominatorT;
  427. resC = (numeratorC/denominatorC) * (1 - doc);
  428. if (allZero)
  429. resT = 0.5;
  430. else
  431. resT = numeratorT/denominatorT;
  432. }
  433. // Special case for T
  434. if (allZero)
  435. resT = 0.5;
  436. }
  437. }
  438. // Calculate F
  439. if (allWeightsZero)
  440. resF = 0;
  441. else {
  442. var numerator = 0, denominator = 0;
  443. for (i = 0; i < arrLength; ++i) {
  444. numerator += weights[i] * args[i].getF();
  445. denominator += weights[i];
  446. }
  447. resF = numerator/denominator;
  448. }
  449. var result = args[0].clone();
  450. result.setTC(resT, resC);
  451. result.setF(resF);
  452. result.setDoC(doc);
  453. return result;
  454. };
  455. /**
  456. * Performs weighted fusion for an array of CertainTrust objects in correspondence with
  457. * an array of weights. Returns new CertainTrust object.
  458. * Requirements: N values of CertainTrust objects must be equal.
  459. * Number of weights should equal the number of CertainTrust objects.
  460. * Arrays must be non-empty
  461. * Either all of CertainTrust must be of certainty 1 or none of them.
  462. * @param args - an array of CertainTrust objects
  463. * @param weights - an integer array of corresponding weights
  464. * @return - new CertainTrust object
  465. */
  466. CertainTrust.prototype.wFusion = function(args, weights) {
  467. //arrays should be equal
  468. if (args.length == weights.length) {
  469. //and not empty
  470. if (args.length !== 0) {
  471. for (var i = 1; i < args.length; ++i)
  472. if (!this._operationAllowed(args[0], args[i]))
  473. return undefined;
  474. return this._internalFusion(args, weights, 0);
  475. }
  476. else throw "Arrays are empty. Operation not allowed. \n";
  477. }
  478. else throw "Different lengths of arrays. Operation not allowed. \n";
  479. };
  480. /**
  481. * Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict
  482. * between given opinions (CertainTrust objects) and takes it into consideration while performing fusion.
  483. * The degree of conflict is then saved in the resulting CertainTrust object and may be checked with getDoC() function.
  484. * @param args - an array of CertainTrust objects
  485. * @param weights - an integer array of corresponding weights
  486. * @return - new CertainTrust object
  487. */
  488. CertainTrust.prototype.cFusion = function(args, weights) {
  489. //arrays should be equal
  490. if (args.length == weights.length) {
  491. //and not empty
  492. if (args.length !== 0) {
  493. for (var i = 1; i < args.length; ++i)
  494. if (!this._operationAllowed(args[0], args[i]))
  495. return undefined;
  496. var denominator = args.length*(args.length - 1) / 2;
  497. var numerator = 0;
  498. for (i = 0; i < args.length; ++i)
  499. for (var j = i; j < args.length; ++j)
  500. numerator += Math.abs(args[i].getT() - args[j].getT()) *
  501. args[i].getC() * args[j].getC() *
  502. (1 - Math.abs((weights[i] - weights[j]) /
  503. (weights[i] + weights[j])));
  504. var doc = numerator/denominator;
  505. return this._internalFusion(args, weights, doc);
  506. }
  507. else throw "Arrays are empty. Operation not allowed. \n";
  508. }
  509. else throw "Different lengths of arrays. Operation not allowed. \n";
  510. };
  511. //=========== Internal Calculations ==========
  512. /**
  513. * Normalises r and s values according to n - maximal number of expected evidence
  514. * Important! Doesn't notify observers.
  515. */
  516. CertainTrust.prototype._normaliseRS = function() {
  517. if ((this.r + this.s) > this.n) {
  518. var initR = this.r;
  519. this.r = (this.r * this.n) / (initR + this.s);
  520. this.s = (this.s * this.n) / (initR + this.s);
  521. }
  522. };
  523. /**
  524. * Calculates t and c values based on existing r and s values
  525. * Important! Doesn't notify observers.
  526. */
  527. CertainTrust.prototype._calculateRStoTC = function() {
  528. var rs = this.r + this.s;
  529. var nrs = this.n * rs;
  530. this.c = nrs / ((2 * this.weight * (this.n - this.r - this.s)) + nrs);
  531. if (this._almostEqual(this.c, 0))
  532. this.t = 0.5;
  533. else
  534. this.t = this.r / rs;
  535. };
  536. /**
  537. * Calculates r and s values based on existing c and t values
  538. * Important! Doesn't notify observers.
  539. */
  540. CertainTrust.prototype._calculateTCtoRS = function() {
  541. if (this._almostEqual(this.c, 0)) {
  542. this.r = 0;
  543. this.s = 0;
  544. this.t = 0.5;
  545. }
  546. else {
  547. var c2w = this.c * 2 * this.weight;
  548. var c2wn = c2w * this.n;
  549. var cn = this.c * this.n;
  550. this.r = (c2wn * this.t) / (c2w + this.n - cn);
  551. this.s = (c2wn - (c2wn * this.t)) / (c2w + this.n - cn);
  552. }
  553. };
  554. CertainTrust.prototype.clone = function() {
  555. var copy = new CertainTrust(this.getN());
  556. copy.c = this.c;
  557. copy.t = this.t;
  558. copy.f = this.f;
  559. copy.r = this.r;
  560. copy.s = this.s;
  561. copy.doc = this.doc;
  562. return copy;
  563. };
  564. CertainTrust.prototype._isString = function (obj) {
  565. return typeof(obj) === 'string';
  566. };
  567. //=========== Observer =================
  568. CertainTrust.prototype.notifyObservers = function(message) {
  569. for (var i = 0; i < this.observers.length; ++i)
  570. this.observers[i].update(this.observers[i], message);
  571. };
  572. CertainTrust.prototype.addObserver = function(observer) {
  573. this.observers.push(observer);
  574. };
  575. CertainTrust.prototype.deleteObserver = function(observer) {
  576. var idx = this.observers.indexOf(observer);
  577. if(idx !== -1)
  578. this.observers.splice(idx, 1);
  579. };
  580. //=== shared functions for frontends ===
  581. CertainTrust.prototype._insertElement = function(config, element) {
  582. var dom;
  583. if (config.domReturn === true) {
  584. return element;
  585. } else if (config.domParent !== undefined) {
  586. if (this._isString(config.domParent))
  587. document.getElementById(config.domParent).appendChild(element);
  588. else
  589. config.domParent.appendChild(element);
  590. } else if (config.domBefore !== undefined) {
  591. if (this._isString(config.domBefore))
  592. dom = document.getElementById(config.domBefore);
  593. else
  594. dom = config.domBefore;
  595. dom.parentNode.insertBefore(element, dom);
  596. } else {
  597. if (config.domAfter === undefined) {
  598. // the last script tag in DOM tree is the one creating this widget
  599. var scripts = document.getElementsByTagName('script');
  600. dom = scripts[scripts.length - 1];
  601. } else if (this._isString(config.domAfter))
  602. dom = document.getElementById(config.domAfter);
  603. else
  604. dom = config.domAfter;
  605. dom.parentNode.insertBefore(element, dom.nextSibling);
  606. }
  607. return undefined;
  608. };
  609. CertainTrust.prototype._getColor = function(certainty, trust, initf) {
  610. var resultp2 = ((1 - certainty) * initf);
  611. var result = (trust * certainty) + resultp2;
  612. var color;
  613. if (result < 0.5) {
  614. color = [
  615. 255,
  616. Math.min(255, (255 * 2 * result)),
  617. 0
  618. ];
  619. } else {
  620. color = [
  621. Math.min(255, ((2 - (2 * result)) * 255)),
  622. 255,
  623. 0
  624. ];
  625. }
  626. return color;
  627. };
  628. CertainTrust.prototype._pointOnCircle = function(centerx, centery, pointgrade, radius) {
  629. var pointrad = ((360 + pointgrade) % 360) * ((2 * Math.PI) / 360);
  630. var chord = 2 * radius * Math.sin((pointrad / 2));
  631. // height of our new point above the base-edge
  632. var y = Math.sqrt(2
  633. * (Math.pow(chord, 2) * Math.pow(radius, 2)
  634. + Math.pow(radius, 4) + Math.pow(radius, 2)
  635. * Math.pow(chord, 2))
  636. - (Math.pow(chord, 4) + 2 * Math.pow(radius, 4)))
  637. / (2 * radius);
  638. // distance to the cross-point of base-edge and height
  639. var a = Math.pow(radius, 2);
  640. var c = Math.pow(y, 2);
  641. // we do this to protect us from NaN cause by 1 - 1.00000004
  642. var x = (a < c) ? 0 : Math.sqrt(a - c);
  643. var directions = new Array("NE", "SE", "SW", "NW");
  644. var direction = 0;
  645. var alpharad = pointrad;
  646. while (alpharad > (0.5 * Math.PI)) {
  647. ++direction;
  648. alpharad -= (0.5 * Math.PI);
  649. }
  650. if (directions[direction] == "NE" || directions[direction] == "NW")
  651. x *= -1;
  652. if (directions[direction] == "SW" || directions[direction] == "NW")
  653. y *= -1;
  654. return new Array((centerx + x), (centery + y));
  655. };
  656. /* optional implementation of CertainTrust without R and S calculations */
  657. var CertainTrustSimple = function() {
  658. this.weight = 2;
  659. this.observers = [];
  660. var offset = 0;
  661. this.name = "";
  662. if (this._isString(arguments[0])) {
  663. this.name = arguments[0];
  664. offset = 1;
  665. }
  666. if (arguments.length == 3 + offset || arguments.length == 4 + offset) {
  667. // CertainTrustSimple(t, c, f, doc)
  668. // doc is a 'private' parameter
  669. this.t = arguments[0 + offset];
  670. this.c = arguments[1 + offset];
  671. this.f = arguments[2 + offset];
  672. this.doc = (arguments.length == 3 + offset) ? 0 : arguments[3 + offset];
  673. if (this.f < 0 && this.f > 1)
  674. throw "f should lie within [0;1]. Entered f = " + this.f + "\n";
  675. if (this.c < 0 && this.c > 1)
  676. throw "c should lie within [0;1]. Entered c = " + this.c + "\n";
  677. if (this.t < 0 && this.t > 1)
  678. throw "t should lie within [0;1]. Entered t = " + this.t + "\n";
  679. } else {
  680. this.c = 0;
  681. this.t = 0.5;
  682. this.f = 0.5;
  683. this.doc = 0;
  684. }
  685. };
  686. CertainTrustSimple.prototype = new CertainTrust(1);
  687. CertainTrustSimple.prototype.constructor = CertainTrustSimple;
  688. CertainTrustSimple.prototype._operationAllowed = function() { return true; }
  689. CertainTrustSimple.prototype._calculateTCtoRS = function() { }
  690. CertainTrustSimple.prototype._calculateRStoTC = function() { }
  691. CertainTrustSimple.prototype._normaliseRS = function() { }
  692. CertainTrustSimple.prototype.setRS = undefined;
  693. CertainTrustSimple.prototype.addR = undefined;
  694. CertainTrustSimple.prototype.addS = undefined;
  695. CertainTrustSimple.prototype.setN = undefined;
  696. CertainTrustSimple.prototype.getR = undefined;
  697. CertainTrustSimple.prototype.getS = undefined;
  698. CertainTrustSimple.prototype.getN = undefined;
  699. CertainTrustSimple.prototype.clone = function() {
  700. var copy = new CertainTrustSimple();
  701. copy.c = this.c;
  702. copy.t = this.t;
  703. copy.f = this.f;
  704. copy.doc = this.doc;
  705. return copy;
  706. };
  707. /*Added by Debashis*/
  708. CertainTrustSimple.prototype._singlesimpleAND = function(arg){
  709. var c1 = this.getC();
  710. var f1 = this.getF();
  711. var t1 = this.getT();
  712. var c2 = arg.getC();
  713. var f2 = arg.getF();
  714. var t2 = arg.getT();
  715. var resC = 0, resT = 0.5, resF = 0.5;
  716. if (!this._operationAllowed(this, arg))
  717. return undefined;
  718. resF = f1*f2;
  719. if (this._almostEqual(resF, 1)){ //avoid division by 0
  720. f1 = 0.99999;
  721. f2 = 0.99999;
  722. resF = f1*f2;
  723. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  724. }
  725. else
  726. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  727. if (this._almostEqual(resC, 0))
  728. resT = 0.5;
  729. else resT = (1/resC) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
  730. resT = this._adjustValue(resT);
  731. resC = this._adjustValue(resC);
  732. resF = this._adjustValue(resF);
  733. return new CertainTrustSimple(resT, resC, resF);
  734. };
  735. CertainTrustSimple.prototype.simpleAND = function() {
  736. var result = this.clone();
  737. for (var i = 0; i < arguments.length; i++) {
  738. var m = arguments[i];
  739. if (!this._operationAllowed(this, m))
  740. continue;
  741. result = result._singlesimpleAND(m);
  742. }
  743. return result;
  744. };
  745. CertainTrust.prototype.setName = function(newname) {
  746. this.name = newname;
  747. };
  748. /**
  749. * Computes CONSENSUS function for this CertainTrust object and the specified argument. Result is returned as a new object,
  750. * argument and this CertainTrust object remain unchanged.
  751. * N values of both objects should be equal.
  752. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  753. * @param arg - CertainTrust object
  754. * @return - result of CONSENSUS computation for this object and an argument.
  755. */
  756. CertainTrust.prototype._singleCONSENSUS = function(arg){
  757. var c1 = this.getC();
  758. var f1 = this.getF();
  759. var t1 = this.getT();
  760. var c2 = arg.getC();
  761. var f2 = arg.getF();
  762. var t2 = arg.getT();
  763. var resC = 0, resT = 0.5, resF = 0.5;
  764. if (!this._operationAllowed(this, arg))
  765. return undefined;
  766. //resF = f1*f2;
  767. if (this._almostEqual(resF, 1)) //avoid division by 0
  768. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  769. else
  770. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  771. if (this._almostEqual(resC, 0))
  772. resT = 0.5;
  773. else if (this._almostEqual(resF, 1)) //avoid division by 0
  774. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1*(1-c2)+c2*(1-c1));
  775. else resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1*(1-c2)+c2*(1-c1));
  776. resT = this._adjustValue(resT);
  777. resC = this._adjustValue(resC);
  778. resF = this._adjustValue(resF);
  779. return new CertainTrust(resT, resC, resF, this.n, 0);
  780. };
  781. CertainTrust.prototype.CONSENSUS = function() {
  782. var result = this.clone();
  783. for (var i = 0; i < arguments.length; i++) {
  784. var m = arguments[i];
  785. if (!this._operationAllowed(this, m))
  786. continue;
  787. result = result._singleCONSENSUS(m);
  788. }
  789. return result;
  790. };
  791. /**
  792. * Computes DISCOUNTING function for this CertainTrust object and the specified argument. Result is returned as a new object,
  793. * argument and this CertainTrust object remain unchanged.
  794. * N values of both objects should be equal.
  795. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  796. * @param arg - CertainTrust object
  797. * @return - result of DISCOUNTING computation for this object and an argument.
  798. */
  799. CertainTrust.prototype._singleDISCOUNTING = function(arg){
  800. var c1 = this.getC();
  801. var f1 = this.getF();
  802. var t1 = this.getT();
  803. var c2 = arg.getC();
  804. var f2 = arg.getF();
  805. var t2 = arg.getT();
  806. var resC = 0, resT = 0.5, resF = 0.5;
  807. if (!this._operationAllowed(this, arg))
  808. return undefined;
  809. //resF = f1*f2;
  810. if (this._almostEqual(resF, 1)) //avoid division by 0
  811. resC = t1*c1*c2;
  812. else
  813. resC = t1*c1*c2;
  814. if (this._almostEqual(resC, 0))
  815. resT = t2;
  816. else if (this._almostEqual(resF, 1)) //avoid division by 0
  817. resT = t2;
  818. else resT = t2;
  819. resT = this._adjustValue(resT);
  820. resC = this._adjustValue(resC);
  821. resF = this._adjustValue(resF);
  822. return new CertainTrust(resT, resC, resF, this.n, 0);
  823. };
  824. CertainTrust.prototype.DISCOUNTING = function() {
  825. var result = this.clone();
  826. for (var i = 0; i < arguments.length; i++) {
  827. var m = arguments[i];
  828. if (!this._operationAllowed(this, m))
  829. continue;
  830. result = result._singleDISCOUNTING(m);
  831. }
  832. return result;
  833. };
  834. /**
  835. * Computes CONSENSUS function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  836. * argument and this CertainTrust object remain unchanged.
  837. * N values of both objects should be equal.
  838. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  839. * @param arg - CertainTrustSimple object
  840. * @return - result of CONSENSUS computation for this object and an argument.
  841. */
  842. CertainTrustSimple.prototype._singlesimpleCONSENSUS = function(arg){
  843. var c1 = this.getC();
  844. var f1 = this.getF();
  845. var t1 = this.getT();
  846. var c2 = arg.getC();
  847. var f2 = arg.getF();
  848. var t2 = arg.getT();
  849. var resC = 0, resT = 0.5, resF = 0.5;
  850. if (!this._operationAllowed(this, arg))
  851. return undefined;
  852. //resF = f1*f2;
  853. if (this._almostEqual(resF, 1)) //avoid division by 0
  854. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  855. else
  856. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  857. if (this._almostEqual(resC, 0))
  858. resT = 0.5;
  859. else if (this._almostEqual(resF, 1)) //avoid division by 0
  860. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1*(1-c2)+c2*(1-c1));
  861. else resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1*(1-c2)+c2*(1-c1));
  862. resT = this._adjustValue(resT);
  863. resC = this._adjustValue(resC);
  864. resF = this._adjustValue(resF);
  865. return new CertainTrustSimple(resT, resC, resF);
  866. };
  867. CertainTrustSimple.prototype.simpleCONSENSUS = function() {
  868. var result = this.clone();
  869. for (var i = 0; i < arguments.length; i++) {
  870. var m = arguments[i];
  871. if (!this._operationAllowed(this, m))
  872. continue;
  873. result = result._singlesimpleCONSENSUS(m);
  874. }
  875. return result;
  876. };