CertainTrust.js 28 KB

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