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

Source Code for Module fuzzy.defuzzify.Base

  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  """Abstract base class for defuzzification which results in a numeric value.""" 
 20   
 21  __revision__ = "$Id: Base.py,v 1.12 2010-03-28 18:40:33 rliebscher Exp $" 
 22   
 23   
 24  from fuzzy.norm.Max import Max 
 25  from fuzzy.norm.Min import Min 
 26  from fuzzy.set.Set import norm, merge 
 27  from fuzzy.Exception import FuzzyException 
 28   
29 -class DefuzzificationException(FuzzyException):
30 pass
31
32 -class Base(object):
33 """Abstract base class for defuzzification 34 which results in a numeric value. 35 36 @ivar INF: inference norm, used with set of adjective and given value for it 37 @type INF: L{fuzzy.norm.Norm.Norm} 38 @ivar ACC: norm for accumulation of set of adjectives 39 @type ACC: L{fuzzy.norm.Norm.Norm} 40 @cvar _INF: default value when INF is None 41 @type _INF: L{fuzzy.norm.Norm.Norm} 42 @cvar _ACC: default value when ACC is None 43 @type _ACC: L{fuzzy.norm.Norm.Norm} 44 @ivar activated_sets: results of activation of adjectives of variable. 45 @type activated_sets: {string:L{fuzzy.set.Polygon.Polygon}} 46 @ivar accumulated_set: result of accumulation of activated sets 47 @type accumulated_set: L{fuzzy.set.Polygon.Polygon} 48 """ 49 50 # default values if instance values are not set 51 _INF = Min() 52 _ACC = Max() 53
54 - def __init__(self, INF=None, ACC=None):
55 """ 56 @param INF: inference norm, used with set of adjective and given value for it 57 @type INF: L{fuzzy.norm.Norm.Norm} 58 @param ACC: norm for accumulation of set of adjectives 59 @type ACC: L{fuzzy.norm.Norm.Norm} 60 """ 61 self.ACC = ACC # accumulation 62 self.INF = INF # inference 63 self.activated_sets = {} 64 self.accumulated_set = None
65
66 - def getValue(self, variable):
67 """Defuzzification.""" 68 raise NotImplementedError("don't use the abstract base class")
69 70 # helper methods for sub classes 71
72 - def accumulate(self, variable, segment_size=None):
73 """combining adjective values into one set""" 74 self.activated_sets = {} 75 temp = None 76 for name, adjective in variable.adjectives.items(): 77 # get precomputed adjective set 78 temp2 = norm((self.INF or self._INF), adjective.set, adjective.getMembership(), segment_size) 79 self.activated_sets[name] = temp2 80 # accumulate all adjectives 81 if temp is None: 82 temp = temp2 83 else: 84 temp = merge((self.ACC or self._ACC), temp, temp2, segment_size) 85 self.accumulated_set = temp 86 return temp
87
88 - def value_table(self, set):
89 """get a value table of the polygon representation""" 90 # get polygon representation 91 return set.getValuesXY()
92
93 - def __repr__(self):
94 """Return representation of instance. 95 96 @return: representation of instance 97 @rtype: string 98 """ 99 params = [] 100 self._repr_params(params) 101 return "%s.%s(%s)" % (self.__class__.__module__, self.__class__.__name__, ", ".join(params))
102
103 - def _repr_params(self, params):
104 """Helper for representation of instance. 105 106 Add all own params to given list in params. 107 """ 108 if self.INF: params.append("INF=%s" % repr(self.INF)) 109 if self.ACC: params.append("ACC=%s" % repr(self.ACC))
110