CertainTrustSimple.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /**
  2. * CertainTrust SDK
  3. *
  4. * Implements the computational trust model "CertainTrust"
  5. * in Java.
  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. package CertainTrust;
  24. import java.util.Observable;
  25. /**
  26. *
  27. * t - average rating value, [0; 1], from very negative to very positive
  28. * c - certainty value, [0; 1] from low certainty (no evidence) to the maximal maximal certainty.
  29. * f - initial trust value
  30. * w - weight
  31. * r - number of positive evidence
  32. * s - number of negative evidence
  33. * n - maximal number of expected evidence
  34. * doc - degree of conflict
  35. *
  36. */
  37. public class CertainTrustSimple extends Observable {
  38. private double c, t, f, r, s, doc;
  39. private int n = 1, weight = 2;
  40. //=========== Constructors =================
  41. /**
  42. * Constructs a CertainTrustSimple object with predefined n value.
  43. * @param n - maximal number of expected evidence
  44. */
  45. public CertainTrustSimple(int n) {
  46. if (n > 0) {
  47. this.n = n;
  48. c = 0;
  49. t = 0.5;
  50. f = 0.5;
  51. r = 0;
  52. s = 0;
  53. }
  54. else throw new IllegalArgumentException("N should be greater than 0. Entered n = " + n + "\n");
  55. }
  56. /**
  57. * Constructs a CertainTrustSimple object with predefined c, t, f, n values.
  58. * @param c - certainty
  59. * @param t - average rating
  60. * @param f - initial trust value
  61. */
  62. public CertainTrustSimple(double t, double c, double f) {
  63. this.f = f;
  64. setTC(t, c);
  65. }
  66. /**
  67. * Constructs a CertainTrustSimple object with predefined c, t, f, n values.
  68. * @param c - certainty
  69. * @param t - average rating
  70. * @param f - initial trust value
  71. * @param n - maximal number of expected evidence
  72. * @param doc - initial degree of conflict
  73. */
  74. private CertainTrustSimple(double t, double c, double f, double doc) {
  75. if (n > 0) {
  76. if (f <= 1 && f >= 0) {
  77. this.f = f;
  78. this.doc = doc;
  79. setTC(t, c);
  80. }
  81. else throw new IllegalArgumentException("f should lie within [0;1]. Entered f = " + f + "\n");
  82. }
  83. else throw new IllegalArgumentException("N should be greater than 0. Entered n = " + n + "\n");
  84. }
  85. //=========== Setters =================
  86. /**
  87. * Sets f value.
  88. * @param f - initial trust value.
  89. */
  90. public void setF(double f) {
  91. if (f >= 0 && f <= 1) {
  92. this.f = f;
  93. setChanged();
  94. notifyObservers();
  95. }
  96. else throw new IllegalArgumentException("f should lie within [0;1]. Entered f = " + f + "\n");
  97. }
  98. /**
  99. * Sets Distance of Conflict value.
  100. * @param doc is the new value for DoC
  101. */
  102. public void setDoC(double doc) {
  103. if (doc > 0) {
  104. this.doc = doc;
  105. }
  106. else throw new IllegalArgumentException("DoC should be greater than 0. Entered DoC = " + doc + "\n");
  107. }
  108. /**
  109. * Sets c and t values. Recalculates r and s values accordingly.
  110. * @param t - new average trust value
  111. * @param c - new certainty value
  112. */
  113. public void setTC(double t, double c) {
  114. if (c >= 0 && c <= 1) {
  115. if (t >= 0 && t <= 1) {
  116. this.c = c;
  117. this.t = t;
  118. setChanged();
  119. notifyObservers();
  120. }
  121. else throw new IllegalArgumentException("t should be greater than 0. Entered t = " + t + "\n");
  122. }
  123. else throw new IllegalArgumentException("c should lie within [0;1]. Entered c = " + c + "\n");
  124. }
  125. //=========== Internal Calculations ==========
  126. //=========== Getters =================
  127. public double getC() {
  128. return c;
  129. }
  130. public double getT() {
  131. return t;
  132. }
  133. public double getF() {
  134. return f;
  135. }
  136. public double getDoC() {
  137. return doc;
  138. }
  139. public double getExpectation() {
  140. return t*c + (1-c)*f;
  141. }
  142. //=========== Logic =================
  143. /**
  144. * Computes OR function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  145. * argument and this CertainTrust object remain unchanged.
  146. * N values of both objects should be equal.
  147. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  148. * @param arg - CertainTrust object
  149. * @return - result of OR computation for this object and an argument.
  150. */
  151. private CertainTrustSimple OR(CertainTrustSimple arg){
  152. double c1 = getC();
  153. double t1 = getT();
  154. double f1 = getF();
  155. double c2 = arg.getC();
  156. double t2 = arg.getT();
  157. double f2 = arg.getF();
  158. double resT = 0.5, resF = 0.5, resC = 0;
  159. // if (this.getN() != arg.getN())
  160. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  161. resF = f1 + f2 - f1*f2;
  162. if (almostEqual(resF, 0)) { //--------Modified by Debashis----------------//
  163. f1 = 0.99999;
  164. f2 = 0.99999;
  165. resF = f1 + f2 - f1*f2;
  166. resC = c1 + c2 - c1*c2- (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  167. }
  168. else
  169. resC = c1 + c2 - c1*c2 - (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF;
  170. if (almostEqual(resC, 0))
  171. resT = 0.5;
  172. else resT = (1/resC) * (c1*t1 + c2*t2 - c1*c2*t1*t2);
  173. resT = adjustValue(resT);
  174. resC = adjustValue(resC);
  175. resF = adjustValue(resF);
  176. CertainTrustSimple result = new CertainTrustSimple(resT, resC, resF,0);
  177. return result;
  178. }
  179. /**
  180. * Computes OR function for this CertainTrustSimple object and the specified arguments.
  181. * Result is returned as a new object, arguments and this CertainTrust object remain unchanged.
  182. * Example: a.OR(b, c, d) returns new CertainTrust object that equals a OR b OR c OR d.
  183. * Multiple arguments allowed, but not less than one.
  184. * N values of all objects should be equal.
  185. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  186. * @param args - arguments
  187. * @return - result of OR computation for this object and all arguments.
  188. */
  189. public CertainTrustSimple OR(CertainTrustSimple ...args) {
  190. CertainTrustSimple result = clone();
  191. for (CertainTrustSimple m: args) {
  192. // if (n != m.getN())
  193. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  194. result = result.OR(m);
  195. }
  196. return result;
  197. }
  198. /**
  199. * Computes AND function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  200. * argument and this CertainTrust object remain unchanged.
  201. * N values of both objects should be equal.
  202. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  203. * @param arg - CertainTrust object
  204. * @return - result of AND computation for this object and an argument.
  205. */
  206. private CertainTrustSimple AND(CertainTrustSimple arg){
  207. double c1 = getC();
  208. double f1 = getF();
  209. double t1 = getT();
  210. double c2 = arg.getC();
  211. double f2 = arg.getF();
  212. double t2 = arg.getT();
  213. double resC = 0, resT = 0.5, resF = 0.5;
  214. // if (n != arg.getN())
  215. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  216. resF = f1*f2;
  217. if (almostEqual(resF, 1)) { //--------Modified by Debashis----------------//
  218. f1 = 0.99999;
  219. f2 = 0.99999;
  220. resF = f1*f2;
  221. resC = c1 + c2 - c1*c2- (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  222. }
  223. else
  224. resC = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+c1*t1*(1-c2)*(1-f2)) / (1 - resF);
  225. if (almostEqual(resC, 0))
  226. resT = 0.5;
  227. else if (almostEqual(resF, 1)) //avoid division by 0
  228. resT = (1/resC) * (c1*t1*c2*t2);
  229. else resT = (1/resC) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - resF));
  230. resT = adjustValue(resT);
  231. resC = adjustValue(resC);
  232. resF = adjustValue(resF);
  233. CertainTrustSimple result = new CertainTrustSimple(resT, resC, resF,0);
  234. return result;
  235. }
  236. /**
  237. * Computes AND function for this CertainTrustSimple object and the specified arguments.
  238. * Result is returned as a new object, arguments and this CertainTrust object remain unchanged.
  239. * Example: a.AND(b, c, d) returns new CertainTrust object that equals a AND b AND c AND d.
  240. * Multiple arguments allowed, but not less than one.
  241. * N values of all objects should be equal.
  242. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  243. * @param args - arguments
  244. * @return - result of AND computation for this object and all arguments.
  245. */
  246. public CertainTrustSimple AND(CertainTrustSimple ...args) {
  247. CertainTrustSimple result = clone();
  248. for (CertainTrustSimple m: args) {
  249. // if (n != m.getN())
  250. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  251. result = result.AND(m);
  252. }
  253. return result;
  254. }
  255. /**
  256. * Returns NOT of this CertainTrustSimple object.
  257. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  258. * @return - NOT of this CertainTrustSimple object.
  259. */
  260. public CertainTrustSimple NOT(){
  261. return new CertainTrustSimple(getC(), adjustValue(1 - getT()), adjustValue(1 - getF()));
  262. }
  263. /**
  264. * an internal implementation of fusion function.
  265. * Is called by wFusion and cFusion
  266. * @param args - an array of CertainTrustSimple objects
  267. * @param weights - an integer array of corresponding weights
  268. * @param doc - a degree of conflict (always 0 for wFusion)
  269. * @return - new CertainTrustSimple object
  270. */
  271. private static CertainTrustSimple internalFusion(CertainTrustSimple[] args, int[] weights, double doc) {
  272. double resC, resT, resF;
  273. boolean allOne = true;
  274. boolean allZero = true;
  275. boolean allWeightsZero = true;
  276. boolean atLeastOne1 = false;
  277. int arrLength = args.length;
  278. // set the flags about C and Weight values
  279. for (int i = 0; i < arrLength; i++)
  280. if (args[i].getC() != 1) {
  281. allOne = false;
  282. i = arrLength;
  283. }
  284. for (int i = 0; i < arrLength; i++)
  285. if (args[i].getC() != 0) {
  286. allZero = false;
  287. i = arrLength;
  288. }
  289. for (int i = 0; i < arrLength; i++)
  290. if (weights[i] != 0) {
  291. allWeightsZero = false;
  292. i = arrLength;
  293. }
  294. for (int i = 0; i < arrLength; i++)
  295. if (args[i].getC() == 1) {
  296. atLeastOne1 = true;
  297. i = arrLength;
  298. }
  299. //-------------Modified by Debashis-----------------//
  300. if(atLeastOne1 && !allOne){
  301. for (int i = 0; i < arrLength; ++i)
  302. if (args[i].getC() == 1) {
  303. args[i].setTC(args[i].getT(),0.99999);
  304. }
  305. }
  306. //Calculate T and C
  307. // 1. all C's = 1
  308. if (allOne) {
  309. // set C
  310. resC = 1 * (1 - doc);
  311. // set T
  312. if (allWeightsZero) {// save some calculation time
  313. resT = 0;
  314. }
  315. else { // or use the function
  316. double numeratorT = 0, denominatorT = 0;
  317. for (int i = 0; i < arrLength; i++) {
  318. numeratorT += weights[i] * args[i].getT();
  319. denominatorT += weights[i];
  320. }
  321. resT = numeratorT/denominatorT;
  322. }
  323. }
  324. else {
  325. if (atLeastOne1)
  326. throw new IllegalStateException("Illeagal arguments. Either all C values must equal 1 or none of them. Operation not allowed \n");
  327. // 2. Any other combination
  328. if (allWeightsZero) { // save some calculation time
  329. resT = 0;
  330. resC = 0;
  331. }
  332. else { // or use the function
  333. double numeratorT = 0, denominatorT = 0, numeratorC = 0, denominatorC = 0, mult;
  334. for (int i = 0; i < arrLength; i++) {
  335. mult = 1;
  336. for (int j = 0; j < arrLength; j++) // Count the product for each sum element
  337. if (j != i)
  338. mult *= 1 - args[j].getC();
  339. numeratorT += weights[i] * args[i].getT() * args[i].getC() * mult;
  340. denominatorT += weights[i] * args[i].getC() * mult;
  341. denominatorC += weights[i] * mult;
  342. }
  343. numeratorC = denominatorT;
  344. resC = (numeratorC/denominatorC) * (1 - doc);
  345. if (allZero)
  346. resT = 0.5;
  347. else
  348. resT = numeratorT/denominatorT;
  349. }
  350. // Special case for T
  351. if (allZero)
  352. resT = 0.5;
  353. }
  354. // Calculate F
  355. if (allWeightsZero)
  356. resF = 0;
  357. else {
  358. double numerator = 0, denominator = 0;
  359. for (int i = 0; i < arrLength; i ++) {
  360. numerator += weights[i] * args[i].getF();
  361. denominator += weights[i];
  362. }
  363. resF = numerator/denominator;
  364. }
  365. return new CertainTrustSimple(resT, resC, resF, doc);
  366. }
  367. /**
  368. * Performs weighted fusion for an array of CertainTrustSimple objects in correspondence with
  369. * an array of weights. Returns new CertainTrust object.
  370. * Requirements: N values of CertainTrust objects must be equal.
  371. * Number of weights should equal the number of CertainTrustSimple objects.
  372. * Arrays must be non-empty
  373. * Either all of CertainTrustSimple must be of certainty 1 or none of them.
  374. * @param args - an array of CertainTrustSimple objects
  375. * @param weights - an integer array of corresponding weights
  376. * @return - new CertainTrustSimple object
  377. */
  378. public static CertainTrustSimple wFusion(CertainTrustSimple[] args, int[] weights) {
  379. //arrays should be equal
  380. if (args.length == weights.length) {
  381. //and not empty
  382. if (args.length != 0) {
  383. boolean equalNs = true;
  384. // int N = args[0].getN();
  385. for (int i = 1; i < args.length; i++)
  386. // if (N != args[i].getN()) {
  387. equalNs = false;
  388. // i = args.length;
  389. // }
  390. //and all N's of TC's must be equal
  391. if (equalNs) {
  392. return internalFusion(args, weights, 0);
  393. }
  394. throw new IllegalStateException("Different N values. Operation not allowed. \n");
  395. }
  396. throw new IllegalStateException("Arrays are empty. Operation not allowed. \n");
  397. }
  398. throw new IllegalStateException("Different lengths of arrays. Operation not allowed. \n");
  399. }
  400. /**
  401. * Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict
  402. * between given opinions (CertainTrustSimple objects) and takes it into consideration while performing fusion.
  403. * The degree of conflict is then saved in the resulting CertainTrustSimple object and may be checked with getDoC() function.
  404. * @param args - an array of CertainTrustSimple objects
  405. * @param weights - an integer array of corresponding weights
  406. * @return - new CertainTrustSimple object
  407. */
  408. public static CertainTrustSimple cFusion(CertainTrustSimple[] args, int[] weights) {
  409. //arrays should be equal
  410. if (args.length == weights.length) {
  411. //and not empty
  412. if (args.length != 0) {
  413. boolean equalNs = true;
  414. // int N = args[0].getN();
  415. // for (int i = 1; i < args.length; i++)
  416. // if (N != args[i].getN()) {
  417. equalNs = false;
  418. // i = args.length;
  419. // }
  420. //and all N's of TC's must be equal
  421. if (equalNs) {
  422. double denominator = args.length*(args.length - 1) / 2;
  423. double numerator = 0;
  424. for (int i = 0; i < args.length; i++)
  425. for (int j = i; j < args.length; j++)
  426. numerator += Math.abs(args[i].getT() - args[j].getT()) * args[i].getC() * args[j].getC()*(1 - Math.abs((double)(weights[i] - weights[j])/(weights[i] + weights[j])));
  427. double doc = numerator/denominator;
  428. return internalFusion(args, weights, doc);
  429. }
  430. throw new IllegalStateException("Different N values. Operation not allowed. \n");
  431. }
  432. throw new IllegalStateException("Arrays are empty. Operation not allowed. \n");
  433. }
  434. throw new IllegalStateException("Different lengths of arrays. Operation not allowed. \n");
  435. }
  436. /**
  437. * Computes CONSENSUS function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  438. * argument and this CertainTrustSimple object remain unchanged.
  439. * N values of both objects should be equal.
  440. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  441. * @param arg - CertainTrustSimple object
  442. * @return - result of CONSENSUS computation for this object and an argument.
  443. */
  444. private CertainTrustSimple CONSENSUS(CertainTrustSimple arg){
  445. double c1 = getC();
  446. double f1 = getF();
  447. double t1 = getT();
  448. double c2 = arg.getC();
  449. double f2 = arg.getF();
  450. double t2 = arg.getT();
  451. double resC = 0, resT = 0.5, resF = 0.5;
  452. // if (n != arg.getN())
  453. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  454. double tempC = c1*c2;
  455. if (this.almostEqual(tempC, 1)){ //avoid division by 0
  456. c1 = 0.99999;
  457. c2 = 0.99999;
  458. }
  459. resF = (f1*c1*(1-c2) + f2*c2*(1-c1))/(c1+c2-2*c1*c2);
  460. resC = (c1+c2-2*c1*c2)/(1-c1*c2);
  461. resT = (c1*t1*(1-c2)+c2*t2*(1-c1))/(c1+c2-2*c1*c2);
  462. resT = adjustValue(resT);
  463. resC = adjustValue(resC);
  464. resF = adjustValue(resF);
  465. CertainTrustSimple result = new CertainTrustSimple(resT, resC, resF, 0);
  466. return result;
  467. }
  468. /**
  469. * Computes CONSENSUS function for this CertainTrustSimple object and the specified arguments.
  470. * Result is returned as a new object, arguments and this CertainTrustSimple object remain unchanged.
  471. * Example: a.CONSENSUS(b, c, d) returns new CertainTrustSimple object that equals a CONSENSUS b CONSENSUS c CONSENSUS d.
  472. * Multiple arguments allowed, but not less than one.
  473. * N values of all objects should be equal.
  474. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  475. * @param args - arguments
  476. * @return - result of CONSENSUS computation for this object and all arguments.
  477. */
  478. public CertainTrustSimple CONSENSUS(CertainTrustSimple ...args) {
  479. CertainTrustSimple result = clone();
  480. for (CertainTrustSimple m: args) {
  481. // if (n != m.getN())
  482. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  483. result = result.CONSENSUS(m);
  484. }
  485. return result;
  486. }
  487. /**
  488. * Computes DISCOUNTING function for this CertainTrustSimple object and the specified argument. Result is returned as a new object,
  489. * argument and this CertainTrustSimple object remain unchanged.
  490. * N values of both objects should be equal.
  491. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  492. * @param arg - CertainTrust object
  493. * @return - result of DISCOUNTING computation for this object and an argument.
  494. */
  495. private CertainTrustSimple DISCOUNTING(CertainTrustSimple arg){
  496. double c1 = getC();
  497. double f1 = getF();
  498. double t1 = getT();
  499. double c2 = arg.getC();
  500. double f2 = arg.getF();
  501. double t2 = arg.getT();
  502. double resC = 0, resT = 0.5, resF = 0.5;
  503. // if (n != arg.getN())
  504. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  505. resF = f2;
  506. if (almostEqual(resF, 1))
  507. resC = t1*c1*c2;
  508. else
  509. resC = t1*c1*c2;
  510. if (almostEqual(resC, 0))
  511. resT = 0.5;
  512. else if (almostEqual(resF, 1))
  513. resT = t2;
  514. else resT = t2;
  515. resT = adjustValue(resT);
  516. resC = adjustValue(resC);
  517. resF = adjustValue(resF);
  518. CertainTrustSimple result = new CertainTrustSimple(resT, resC, resF, 0);
  519. return result;
  520. }
  521. /**
  522. * Computes DISCOUNTING function for this CertainTrust object and the specified arguments.
  523. * Result is returned as a new object, arguments and this CertainTrust object remain unchanged.
  524. * Example: a.DISCOUNTING(b, c, d) returns new CertainTrust object that equals a DISCOUNTING b DISCOUNTING c DISCOUNTING d.
  525. * Multiple arguments allowed, but not less than one.
  526. * N values of all objects should be equal.
  527. * For detailed information see CertainLogic: A Logic for Modeling Trust and Uncertainty
  528. * @param args - arguments
  529. * @return - result of DISCOUNTING computation for this object and all arguments.
  530. */
  531. public CertainTrustSimple DISCOUNTING(CertainTrustSimple ...args) {
  532. CertainTrustSimple result = clone();
  533. for (CertainTrustSimple m: args) {
  534. // if (n != m.getN())
  535. // throw new IllegalStateException("Different N values. Operation not allowed. \n");
  536. result = result.DISCOUNTING(m);
  537. }
  538. return result;
  539. }
  540. //=========== Additional Functions =================
  541. @Override
  542. public CertainTrustSimple clone() {
  543. CertainTrustSimple copy = new CertainTrustSimple(n);
  544. copy.c = this.c;
  545. copy.t = this.t;
  546. copy.f = this.f;
  547. copy.r = this.r;
  548. copy.s = this.s;
  549. copy.doc = this.doc;
  550. return copy;
  551. }
  552. /**
  553. * Adjusts the value of a into the allowed range [0,1].
  554. * @param a - value to be adjusted
  555. */
  556. private double adjustValue(double a) {
  557. return Math.max(Math.min(a, 1), 0);
  558. }
  559. /**
  560. * compares two double values using an epsilon
  561. * @param value - given value
  562. * @param target - expected value
  563. * @return
  564. */
  565. private boolean almostEqual(double value, double target) {
  566. return Math.abs(value - target) < 1E-10;
  567. }
  568. }