1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """Realize a triangle-shaped fuzzy set."""
20
21 __revision__ = "$Id: Triangle.py,v 1.20 2010-10-29 19:24:41 rliebscher Exp $"
22
23
24 from fuzzy.set.Polygon import Polygon
25 from fuzzy.utils import prop
26 from fuzzy.Exception import FuzzyException
29 r"""Realize a triangle-shaped fuzzy set::
30 ______ y_max
31 A
32 /|\
33 / | \
34 / | \
35 _/ | \_ y_min
36 | m |
37 | | |
38 alpha|beta
39
40 See also U{http://pyfuzzy.sourceforge.net/demo/set/Triangle.png}
41 """
42
43 - def __init__(self, m=0.0, alpha=1.0, beta=1.0, y_max=1.0, y_min=0.0):
44 """
45 Initialize a triangle-shaped fuzzy set.
46
47 @param y_max: y-value at top of the triangle (1.0)
48 @param y_min: y-value outside the triangle (0.0)
49 @param m: x-value of top of triangle (0.0)
50 @param alpha: distance of left corner to m (1.0)
51 @param beta: distance of right corner to m (1.0)
52 """
53 super(Triangle, self).__init__()
54 self._y_max = float(y_max)
55 self._y_min = float(y_min)
56 self._m = float(m)
57 self._alpha = float(alpha)
58 self._beta = float(beta)
59 self._update()
60
61
62 @prop
64 """y-value at top of the triangle
65 @type: float"""
66 def fget(self):
67 return self._y_max
68 def fset(self, value):
69 self._y_max = float(value)
70 self._update()
71 return locals()
72
73
74 @prop
76 """y-value outside the triangle
77 @type: float"""
78 def fget(self):
79 return self._y_min
80 def fset(self, value):
81 self._y_min = float(value)
82 self._update()
83 return locals()
84
85
86 @prop
88 """x-value of top of triangle
89 @type: float"""
90 def fget(self):
91 return self._m
92 def fset(self, value):
93 self._m = float(value)
94 self._update()
95 return locals()
96
97
98 @prop
100 """distance of left corner to m
101 @type: float"""
102 def fget(self):
103 return self._alpha
104 def fset(self, value):
105 self._alpha = float(value)
106 self._update()
107 return locals()
108
109
110 @prop
112 """distance of right corner to m
113 @type: float"""
114 def fget(self):
115 return self._beta
116 def fset(self, value):
117 self._beta = float(value)
118 self._update()
119 return locals()
120
122 """update polygon"""
123 p = super(Triangle, self)
124 p.clear()
125 p.add(self._m-self._alpha, self._y_min)
126 p.add(self._m, self._y_max)
127 p.add(self._m+self._beta, self._y_min)
128
130 """Don't let anyone destroy our triangle."""
131 raise FuzzyException()
132
134 """Don't let anyone destroy our triangle."""
135 raise FuzzyException()
136
138 """Don't let anyone destroy our triangle."""
139 raise FuzzyException()
140
142 """Return representation of instance.
143
144 @return: representation of instance
145 @rtype: string
146 """
147 return "%s.%s(m=%s, alpha=%s, beta=%s, y_max=%s, y_min=%s)" % (
148 self.__class__.__module__,
149 self.__class__.__name__,
150 self._m,
151 self._alpha,
152 self._beta,
153 self._y_max,
154 self._y_min,
155 )
156