1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """Realize a Pi-shaped fuzzy set"""
20
21 __revision__ = "$Id: PiFunction.py,v 1.19 2010-03-28 18:44:46 rliebscher Exp $"
22
23
24 from fuzzy.set.Function import Function
25 from fuzzy.set.SFunction import SFunction
26 from fuzzy.set.ZFunction import ZFunction
27
29 r"""
30 Realize a Pi-shaped fuzzy set::
31
32 _
33 /|\
34 / | \
35 _/ | \_
36 | a |
37 | |
38 delta
39
40 See also U{http://pyfuzzy.sourceforge.net/demo/set/PiFunction.png}
41
42
43 @ivar a: center of set.
44 @type a: float
45 @ivar delta: absolute distance between x-values for minimum and maximum.
46 @type delta: float
47 """
48
50 """Initialize a Pi-shaped fuzzy set.
51
52 @param a: center of set
53 @type a: float
54 @param delta: absolute distance between x-values for minimum and maximum
55 @type delta: float
56 """
57 super(PiFunction, self).__init__()
58 self.a = a
59 self.delta = delta
60 self._sfunction = SFunction(a - delta/2., delta/2)
61 self._zfunction = ZFunction(a + delta/2., delta/2)
62
64 """Return membership of x in this fuzzy set.
65 This method makes the set work like a function.
66
67 @param x: value for which the membership is to calculate
68 @type x: float
69 @return: membership
70 @rtype: float
71 """
72 if x < self.a:
73 return self._sfunction(x)
74 else:
75 return self._zfunction(x)
76
78 """Return center of gravity."""
79 return self.a
80
82 """Return sequence of x-values so we get a smooth function."""
83 for x in self._sfunction.getValuesX():
84 yield x
85
86 skippedFirst = False
87 for x in self._zfunction.getValuesX():
88 if not skippedFirst:
89 skippedFirst = True
90 else:
91 yield x
92
94 """Return representation of instance.
95
96 @return: representation of instance
97 @rtype: string
98 """
99 return "%s.%s(a=%s, delta=%s)" % (self.__class__.__module__, self.__class__.__name__, self.a, self.delta)
100