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

Source Code for Module fuzzy.defuzzify.COG

 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 center of gravity method.""" 
20   
21  __revision__ = "$Id: COG.py,v 1.9 2010-03-28 18:40:33 rliebscher Exp $" 
22   
23  from fuzzy.defuzzify.Base import Base 
24   
25 -class COG(Base):
26 """Defuzzification which uses 27 the center of gravity method.""" 28
29 - def __init__(self, INF=None, ACC=None, failsafe=None, segment_size=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 @param segment_size: maximum length of segment in polygon of accumulated result set 34 """ 35 super(COG, self).__init__(INF, ACC, *args, **keywords) 36 self.failsafe = failsafe # which value if COG not calculable 37 self.segment_size = segment_size # maximum length of segment in polygon of accumulated result set
38
39 - def getValue(self, variable):
40 """Defuzzification using center of gravity method.""" 41 temp = self.accumulate(variable, self.segment_size) 42 try: 43 return temp.getCOG() 44 except: 45 # was not to calculate 46 if self.failsafe is not None: 47 # user gave us a value to return 48 return self.failsafe 49 else: 50 # forward exception 51 raise
52
53 - def _repr_params(self, params):
54 """Helper for representation of instance. 55 56 Add all own params to given list in params. 57 """ 58 super(COG, self)._repr_params(params) 59 if self.failsafe: params.append("failsafe=%s" % self.failsafe) 60 if self.segment_size: params.append("segment_size=%s" % self.segment_size)
61