CertainTrust.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import numpy as np
  2. class Opinion:
  3. """
  4. This class represents opinion.
  5. t - average rating
  6. c - certainty
  7. f - initial expectation value
  8. doc - degree of conflict
  9. """
  10. def __init__(self, t=0, c=0, f=1, doc=0):
  11. self.t = t
  12. self.c = c
  13. self.f = f
  14. self.doc = 0
  15. def __str__(self):
  16. return "Opinion{t: "+str(self.t)+", c: "+str(self.c)+", f: "+str(self.f)+"}"
  17. def expectation_value(self):
  18. return self.c * self.t + ( 1 - self.c ) * self.f
  19. def _single_or(o_A, o_B):
  20. """
  21. Computes OR function of two Opinion objects. Result is returned as a new object,
  22. arguments remain unchanged.
  23. N values of both objects should be equal.
  24. For detailed information see TODO
  25. :param o_A: opinion
  26. :param o_B: opinion
  27. :return: opinion as described above
  28. """
  29. r_f = o_A.f + o_B.f - o_A.f * o_B.f
  30. # FIXME: js implementation of this comparison is different from paper, using paper variant now
  31. if np.isclose(r_f, 0):
  32. # c1 + c2 - c1*c2
  33. r_c = o_A.c + o_B.c - o_A.c * o_B.c
  34. else:
  35. # restC = restC - (c1*f2*(1-c2)*(1-t1)+c2*f1*(1-c1)*(1-t2)) / resF
  36. r_c = o_A.c + o_B.c - o_A.c * o_B.c - (o_A.c * o_B.f * (1 - o_B.c) * (1 - o_A.t) + o_B.c * o_A.f * (1 - o_A.c) * (1 - o_B.t)) / r_f
  37. if np.isclose(r_c, 0):
  38. r_t = 0.5
  39. else:
  40. # resT = (1/resC) * (c1*t1 + c2*t2 - c1*c2*t1*t2);
  41. r_t = (1 / r_c) * (o_A.c * o_A.t + o_B.c * o_B.t - (o_A.c * o_B.c * o_A.t * o_B.t))
  42. r_t = Opinion._adjust_value(r_t)
  43. r_c = Opinion._adjust_value(r_c)
  44. r_f = Opinion._adjust_value(r_f)
  45. return Opinion(r_t, r_c, r_f)
  46. @staticmethod
  47. def _single_and(o_A, o_B):
  48. """
  49. Computes AND function of two Opinion objects. Result is returned as a new object,
  50. arguments remain unchanged.
  51. N values of both objects should be equal.
  52. For detailed information see TODO
  53. :param o_A: opinion
  54. :param o_B: opinion
  55. :return: opinion as described above
  56. """
  57. t1 = o_A.t
  58. c1 = o_A.c
  59. f1 = o_A.f
  60. t2 = o_B.t
  61. c2 = o_B.c
  62. f2 = o_B.f
  63. if np.isclose(f1*f2, 1):
  64. f1 = 0.999
  65. f2 = 0.999
  66. r_f = f1*f2
  67. if not np.isclose(r_f, 1):
  68. r_c = c1 + c2 - c1*c2 - (c2*t2*(1-c1)*(1-f1)+(c1*t1*(1-c2)*(1-f2))) / (1 - r_f);
  69. else:
  70. r_c = c1 + c2 - c1 * c2
  71. #FIXME: js implementation of this comparison is different from paper, using js variant now
  72. if np.isclose(r_c, 0):
  73. r_t = 0.5
  74. else:
  75. r_t = (1/r_c) * ((c1*t1*c2*t2) + (c1*f2*t1*(1-c2)*(1-f1)+c2*f1*t2*(1-c1)*(1-f2)) / (1 - r_f));
  76. r_t = Opinion._adjust_value(r_t)
  77. r_c = Opinion._adjust_value(r_c)
  78. r_f = Opinion._adjust_value(r_f)
  79. return Opinion(r_t, r_c, r_f)
  80. @staticmethod
  81. def _not(o):
  82. """
  83. Computes NOT function of an Opinion object. Result is returned as a new object,
  84. argument remains unchanged.
  85. :param o: Opinion
  86. :return: opinion as described above
  87. """
  88. r_t=o.t
  89. r_c=o.c
  90. r_f=1-o.f
  91. r_doc=0
  92. return Opinion(r_t, r_c, r_f, r_doc)
  93. @staticmethod
  94. def _cum_or(opar):
  95. """
  96. For a list of opinions, compute a cummulated OR opinion.
  97. :param opar:
  98. :return: ORO-cummulated opinion
  99. """
  100. res = opar[0]
  101. for o in opar[1:]:
  102. res = Opinion._single_or(res, o)
  103. return res
  104. @staticmethod
  105. def _cum_and(opar):
  106. """
  107. For a list of opinions, compute a cummulated AND opinion.
  108. :param opar:
  109. :return: AND-cummulated opinion
  110. """
  111. res = opar[0]
  112. for o in opar[1:]:
  113. res = Opinion._single_and(res, o)
  114. return res
  115. @staticmethod
  116. def _internal_fusion(args, weights, doc=1.0):
  117. """
  118. An internal implementation of fusion function.
  119. Is called by _weighted_fusion and cFusion.
  120. :param args: list of opinions
  121. :param weights: list of weights
  122. :param doc: degree of conflict (float)
  123. :return:
  124. """
  125. allOne = True;
  126. allZero = True;
  127. allWeightsZero = True;
  128. atLeastOne1 = False;
  129. arrLength = len(args);
  130. for o in args:
  131. if o.c != 1:
  132. allOne = False
  133. if o.c != 0:
  134. allZero = False
  135. if o.c == 1:
  136. atLeastOne1 = True
  137. for w in weights:
  138. if w != 0:
  139. allWeightsZero = False
  140. break
  141. numeratorT = 0
  142. denominatorT = 0
  143. if allOne:
  144. r_c = 1 * (1 - doc)
  145. if(allWeightsZero):
  146. r_t = 0
  147. else:
  148. for i in range(0,arrLength):
  149. numeratorT += weights[i] * args[i].t
  150. denominatorT += weights[i]
  151. else:
  152. if atLeastOne1:
  153. raise Exception("Illegal arguments. Either all C values must equal 1 or none of them. Operation not allowed!")
  154. else:
  155. if allWeightsZero:
  156. r_t = 0
  157. r_c = 0
  158. else:
  159. denominatorC = 0
  160. numeratorC = 0
  161. for i in range(0, arrLength):
  162. mult = 1
  163. for j in range(0, arrLength):
  164. if j != i:
  165. mult *= 1 - args[j].c
  166. numeratorT = numeratorT + weights[i] * args[i].t * args[i].c * mult
  167. denominatorT = denominatorT + weights[i] * args[i].c * mult
  168. denominatorC = denominatorC + weights[i] * mult
  169. numeratorC = denominatorT
  170. r_c = (numeratorC / denominatorC) * (1 - doc)
  171. if allZero:
  172. r_t = 0.5
  173. else:
  174. r_t = numeratorT / denominatorT
  175. if allZero:
  176. r_t = 0.5;
  177. if allWeightsZero:
  178. r_f = 0;
  179. else:
  180. numerator = 0
  181. denominator = 0
  182. for i in range(0, arrLength):
  183. numerator = numerator + weights[i] * args[i].f
  184. denominator = denominator + weights[i]
  185. r_f = numerator / denominator
  186. return Opinion(r_t, r_c, r_f, doc);
  187. @staticmethod
  188. def _weighted_fusion(args, weights):
  189. """
  190. Performs weighted fusion for an array of Opinions objects in correspondence with
  191. an array of weights. Returns new Opinion object.
  192. Requirements: N values of Opinion objects must be equal.
  193. Number of weights should equal the number of Opinion objects.
  194. Arrays must be non-empty
  195. Either all of CertainTrust must be of certainty 1 or none of them.
  196. :param args: an array of Opinions
  197. :param weights: an integer array of corresponding weights
  198. :return: new Opinion
  199. """
  200. if len(args) == len(weights) and len(args) != 0:
  201. return Opinion._internal_fusion(args, weights, 0)
  202. else:
  203. raise Exception("_weighted_fusion is not allowed for these arguments")
  204. @staticmethod
  205. def _conflicted_fusion(args, weights):
  206. """
  207. Conflicted Fusion is a variation of weighted fusion, which additionally computes the degree of conflict
  208. between given Opinions and takes it into consideration while performing fusion.
  209. The degree of conflict is then saved in the resulting Opinion object.
  210. :param args:
  211. :param weights:
  212. :return:
  213. """
  214. if len(args) == len(weights) and len(args) != 0:
  215. denominator = len(args)*(len(args) - 1) / 2
  216. numerator = 0
  217. for i in range(0,len(args)):
  218. for j in range(i,len(args)):
  219. numerator = numerator + abs(args[i].t - args[j].t) * args[i].c * args[j].c * (1 - abs((weights[i] - weights[j]) / (weights[i] + weights[j])))
  220. doc = numerator/denominator
  221. return Opinion._internal_fusion(args, weights, doc)
  222. else:
  223. raise Exception("_conflicted_fusion is not allowed for these arguments")
  224. @staticmethod
  225. def _adjust_value(val):
  226. return max(min(val, 1), 0)
  227. """
  228. // TESTS
  229. os = [Opinion(0.7,0.9,0.9), Opinion(0.4,0.7,1),Opinion(0.7,0.7,1),Opinion(0.2,0.9,1),Opinion(0.55,0.8,1)]
  230. w = [1, 0.3, 0.2, 1, 0.5]
  231. print(Opinion._single_or(os[0],os[1]))
  232. print(Opinion._single_and(os[0],os[1]))
  233. print(Opinion._cum_or(os))
  234. print(Opinion._cum_and(os))
  235. print(Opinion._internal_fusion(os, w, 1))
  236. print(Opinion._conflicted_fusion(os, w))
  237. """