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

Source Code for Module fuzzy.defuzzify.COGS

 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 for singletons.""" 
20   
21  __revision__ = "$Id: COGS.py,v 1.8 2010-03-28 18:40:33 rliebscher Exp $" 
22   
23  from fuzzy.defuzzify.Base import Base, DefuzzificationException 
24  import fuzzy.set.Singleton 
25   
26 -class COGS(Base):
27 """Defuzzification for singletons.""" 28
29 - def __init__(self, INF=None, ACC=None, failsafe=None, *args, **keywords):
30 """ 31 @param failsafe: if is not possible to calculate a center of gravity, 32 return this value if not None or forward the exception 33 """ 34 super(COGS, self).__init__(INF, ACC, *args, **keywords) 35 self.failsafe = failsafe # which value if COG not calculable
36
37 - def getValue(self, variable):
38 """Defuzzification using center of gravity method.""" 39 sum_1, sum_2 = 0.,0. 40 for adjective in variable.adjectives.values(): 41 # get precomputed adjective set 42 set = adjective.set 43 if not isinstance(set, fuzzy.set.Singleton.Singleton): 44 raise DefuzzificationException("Only Singleton for COGS defuzzification allowed.") 45 a = (self.INF or self._INF)(set(set.x), adjective.getMembership()) 46 sum_1 += set.x*a 47 sum_2 += a 48 try: 49 if sum_2 == 0.: 50 raise DefuzzificationException("No result, all singletons set to 0.") 51 return sum_1/sum_2 52 except: 53 # was not to calculate 54 if self.failsafe is not None: 55 # user gave us a value to return 56 return self.failsafe 57 else: 58 # forward exception 59 raise
60
61 - def _repr_params(self, params):
62 """Helper for representation of instance. 63 64 Add all own params to given list in params. 65 """ 66 super(COGS, self)._repr_params(params) 67 if self.failsafe: params.append("failsafe=%s" % self.failsafe)
68