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

Source Code for Module fuzzy.defuzzify.RM

 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 right most (local) maximum.""" 
20   
21  __revision__ = "$Id: RM.py,v 1.6 2010-03-28 18:40:33 rliebscher Exp $" 
22   
23  from fuzzy.defuzzify.Base import Base, DefuzzificationException 
24   
25 -class RM(Base):
26 """Defuzzification which uses the right most (local) 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(RM, 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 table.reverse() 46 47 y = table[0][1] 48 x = float('+inf') # right end of polygon is always infinity 49 50 for (x_, y_) in table[1:]: 51 if y_ > y: 52 y = y_ 53 x = x_ 54 else: 55 break 56 57 return x 58 except: 59 # was not to calculate 60 if self.failsafe is not None: 61 # user gave us a value to return 62 return self.failsafe 63 else: 64 raise
65
66 - def _repr_params(self, params):
67 """Helper for representation of instance. 68 69 Add all own params to given list in params. 70 """ 71 super(RM, self)._repr_params(params) 72 if self.failsafe: params.append("failsafe=%s" % self.failsafe)
73