Package fuzzy :: Package set :: Module Triangle
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.set.Triangle

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (C) 2009  Rene Liebscher 
  4  # 
  5  # This program is free software; you can redistribute it and/or modify it under 
  6  # the terms of the GNU Lesser General Public License as published by the Free  
  7  # Software Foundation; either version 3 of the License, or (at your option) any 
  8  # later version. 
  9  # 
 10  # This program is distributed in the hope that it will be useful, but WITHOUT  
 11  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 12  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 13  # details. 
 14  #  
 15  # You should have received a copy of the GNU Lesser General Public License 
 16  # along with this program; if not, see <http://www.gnu.org/licenses/>.  
 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 
27 28 -class Triangle(Polygon):
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() # update polygon
60 61 # pylint: disable=E0211,W0212 62 @prop
63 - def y_max(): #@NoSelf
64 """y-value at top of the triangle 65 @type: float""" 66 def fget(self): # pylint: disable=W0612,C0111 67 return self._y_max
68 def fset(self, value): # pylint: disable=W0612,C0111 69 self._y_max = float(value) 70 self._update() 71 return locals() 72 73 # pylint: disable=E0211,W0212 74 @prop
75 - def y_min(): #@NoSelf
76 """y-value outside the triangle 77 @type: float""" 78 def fget(self): # pylint: disable=W0612,C0111 79 return self._y_min 80 def fset(self, value): # pylint: disable=W0612,C0111 81 self._y_min = float(value) 82 self._update() 83 return locals() 84 85 # pylint: disable=E0211,W0212 86 @prop
87 - def m(): #@NoSelf
88 """x-value of top of triangle 89 @type: float""" 90 def fget(self): # pylint: disable=W0612,C0111 91 return self._m 92 def fset(self, value): # pylint: disable=W0612,C0111 93 self._m = float(value) 94 self._update() 95 return locals() 96 97 # pylint: disable=E0211,W0212 98 @prop
99 - def alpha(): #@NoSelf
100 """distance of left corner to m 101 @type: float""" 102 def fget(self): # pylint: disable=W0612,C0111 103 return self._alpha 104 def fset(self, value): # pylint: disable=W0612,C0111 105 self._alpha = float(value) 106 self._update() 107 return locals() 108 109 # pylint: disable=E0211,W0212 110 @prop
111 - def beta(): #@NoSelf
112 """distance of right corner to m 113 @type: float""" 114 def fget(self): # pylint: disable=W0612,C0111 115 return self._beta 116 def fset(self, value): # pylint: disable=W0612,C0111 117 self._beta = float(value) 118 self._update() 119 return locals() 120
121 - def _update(self):
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
129 - def add(self, x, y, where=Polygon.END):
130 """Don't let anyone destroy our triangle.""" 131 raise FuzzyException()
132
133 - def remove(self, x, where=Polygon.END):
134 """Don't let anyone destroy our triangle.""" 135 raise FuzzyException()
136
137 - def clear(self):
138 """Don't let anyone destroy our triangle.""" 139 raise FuzzyException()
140
141 - def __repr__(self):
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