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

Source Code for Module fuzzy.set.SFunction

  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 S-shaped fuzzy set.""" 
 20   
 21  __revision__ = "$Id: SFunction.py,v 1.19 2010-03-28 18:44:46 rliebscher Exp $" 
 22   
 23   
 24  from fuzzy.set.Function import Function 
 25   
26 -class SFunction(Function):
27 r""" 28 Realize a S-shaped fuzzy set:: 29 __ 30 /| 31 / | 32 /| | 33 _/ | | 34 | a | 35 | | 36 2*delta 37 38 See also U{http://pyfuzzy.sourceforge.net/demo/set/SFunction.png} 39 40 @ivar a: center of set. 41 @type a: float 42 @ivar delta: absolute distance between x-values for minimum and maximum. 43 @type delta: float 44 """ 45
46 - def __init__(self, a=0.0, delta=1.0):
47 """Initialize a S-shaped fuzzy set. 48 49 @param a: center of set 50 @type a: float 51 @param delta: absolute distance between x-values for minimum and maximum 52 @type delta: float 53 """ 54 super(SFunction, self).__init__() 55 self.a = a 56 self.delta = delta
57
58 - def __call__(self, x):
59 """Return membership of x in this fuzzy set. 60 This method makes the set work like a function. 61 62 @param x: value for which the membership is to calculate 63 @type x: float 64 @return: membership 65 @rtype: float 66 """ 67 a = self.a 68 d = self.delta 69 if x <= a-d: 70 return 0.0 71 if x <= a: 72 t = (x-a+d)/(2.0*d) 73 return 2.0*t*t 74 if x <= a+d: 75 t = (a-x+d)/(2.0*d) 76 return 1.0-2.0*t*t 77 return 1.0
78
79 - def getCOG(self):
80 """Return center of gravity.""" 81 from fuzzy.Exception import FuzzyException 82 raise FuzzyException("COG of SFunction uncalculable")
83
84 - def getValuesX(self):
85 """Return sequence of x-values so we get a smooth function.""" 86 a = self.a 87 d = self.delta 88 stepsize = 2. * d / Function._resolution 89 x = a - d 90 for _ in range(Function._resolution): 91 yield x 92 x += stepsize 93 yield a + d
94
95 - def __repr__(self):
96 """Return representation of instance. 97 98 @return: representation of instance 99 @rtype: string 100 """ 101 return "%s.%s(a=%s, delta=%s)" % (self.__class__.__module__, self.__class__.__name__, self.a, self.delta)
102