Package fuzzy :: Package defuzzify :: Module MaxLeft
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.defuzzify.MaxLeft

 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  """Defuzzification which uses the left global maximum.""" 
20   
21  __revision__ = "$Id: MaxLeft.py,v 1.8 2010-03-28 18:40:33 rliebscher Exp $" 
22   
23  from fuzzy.defuzzify.Base import Base, DefuzzificationException 
24   
25 -class MaxLeft(Base):
26 """Defuzzification which uses the left global maximum.""" 27
28 - def __init__(self, INF=None, ACC=None, failsafe=None, *args, **keywords):
29 """Initialize the defuzzification method with INF,ACC 30 and an optional value in case defuzzification is not possible""" 31 super(MaxLeft, self).__init__(INF, ACC, *args, **keywords) 32 self.failsafe = failsafe # which value if value not calculable
33
34 - def getValue(self, variable):
35 """Defuzzification.""" 36 try: 37 temp = self.accumulate(variable) 38 39 # get polygon representation 40 table = list(self.value_table(temp)) 41 42 if len(table) == 0: 43 raise DefuzzificationException("no value calculable: complete undefined set") 44 45 y = table[0][1] 46 x = float('-inf') # left end of polygon is always -infinity 47 48 for (x_, y_) in table[1:]: 49 if y_ > y: 50 y = y_ 51 x = x_ 52 53 return x 54 except: 55 # was not to calculate 56 if self.failsafe is not None: 57 # user gave us a value to return 58 return self.failsafe 59 else: 60 raise
61
62 - def _repr_params(self, params):
63 """Helper for representation of instance. 64 65 Add all own params to given list in params. 66 """ 67 super(MaxLeft, self)._repr_params(params) 68 if self.failsafe: params.append("failsafe=%s" % self.failsafe)
69