CertainTrust.js 28 KB

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