1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """Defuzzification for singletons."""
20
21 __revision__ = "$Id: COGS.py,v 1.8 2010-03-28 18:40:33 rliebscher Exp $"
22
23 from fuzzy.defuzzify.Base import Base, DefuzzificationException
24 import fuzzy.set.Singleton
25
27 """Defuzzification for singletons."""
28
29 - def __init__(self, INF=None, ACC=None, failsafe=None, *args, **keywords):
30 """
31 @param failsafe: if is not possible to calculate a center of gravity,
32 return this value if not None or forward the exception
33 """
34 super(COGS, self).__init__(INF, ACC, *args, **keywords)
35 self.failsafe = failsafe
36
38 """Defuzzification using center of gravity method."""
39 sum_1, sum_2 = 0.,0.
40 for adjective in variable.adjectives.values():
41
42 set = adjective.set
43 if not isinstance(set, fuzzy.set.Singleton.Singleton):
44 raise DefuzzificationException("Only Singleton for COGS defuzzification allowed.")
45 a = (self.INF or self._INF)(set(set.x), adjective.getMembership())
46 sum_1 += set.x*a
47 sum_2 += a
48 try:
49 if sum_2 == 0.:
50 raise DefuzzificationException("No result, all singletons set to 0.")
51 return sum_1/sum_2
52 except:
53
54 if self.failsafe is not None:
55
56 return self.failsafe
57 else:
58
59 raise
60
62 """Helper for representation of instance.
63
64 Add all own params to given list in params.
65 """
66 super(COGS, self)._repr_params(params)
67 if self.failsafe: params.append("failsafe=%s" % self.failsafe)
68