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

Source Code for Module fuzzy.set.PiFunction

  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 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   
28 -class PiFunction(Function):
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
49 - def __init__(self, a=0.0, delta=1.0):
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
63 - def __call__(self, x):
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
77 - def getCOG(self):
78 """Return center of gravity.""" 79 return self.a
80
81 - def getValuesX(self):
82 """Return sequence of x-values so we get a smooth function.""" 83 for x in self._sfunction.getValuesX(): 84 yield x 85 # first value is equal the last of the previous sequence 86 skippedFirst = False 87 for x in self._zfunction.getValuesX(): 88 if not skippedFirst: 89 skippedFirst = True 90 else: 91 yield x
92
93 - def __repr__(self):
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