Package fuzzy :: Module Rule
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.Rule

  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  """Represents a fuzzy rule.""" 
 19  __revision__ = "$Id: Rule.py,v 1.17 2010-02-17 19:57:13 rliebscher Exp $" 
 20   
 21  from fuzzy.norm.Min import Min 
 22   
23 -class Rule(object):
24 """This is realizes an important part of the inference engine. 25 It represents and calculates the value of a fuzzy rule 26 and sets the given adjective to the appropriate value. 27 28 @cvar _CER: the default value (=Min()) for the norm used to calculate the certainty of a rule. 29 @type _CER: L{fuzzy.norm.Norm.Norm} 30 @ivar adjective: fuzzy adjective to set 31 @type adjective: L{fuzzy.Adjective.Adjective} 32 @ivar operator: Operator which provides the value to set 33 @type operator: L{fuzzy.operator.Operator.Operator} 34 @ivar certainty: how sure are we about this rule 35 @type certainty: float 36 @ivar CER: fuzzy norm to use with certainty (normally a t-norm) 37 @type CER: L{fuzzy.norm.Norm.Norm} 38 """ 39 40 # default if not set in instance 41 _CER = Min() 42
43 - def __init__(self, adjective, operator, certainty=1.0, CER=None):
44 """Initialize instance. 45 @param adjective: fuzzy adjective to set 46 @type adjective: L{fuzzy.Adjective.Adjective} 47 @param operator: Operator which provides the value to set 48 @type operator: L{fuzzy.operator.Operator.Operator} 49 @param certainty: how sure are we about this rule 50 @type certainty: float 51 @param CER: fuzzy norm to use with certainty (normally a t-norm) 52 @type CER: L{fuzzy.norm.Norm.Norm} 53 """ 54 55 self.adjective = adjective 56 self.operator = operator 57 self.certainty = certainty 58 self.CER = CER
59
60 - def compute(self):
61 """Compute and set value for given fuzzy adjective.""" 62 63 import fuzzy.Adjective 64 if isinstance(self.adjective, fuzzy.Adjective.Adjective): 65 self.adjective.setMembership( 66 (self.CER or self._CER)( 67 self.certainty, # how sure are we about this rule 68 self.operator() # value from input 69 ) 70 ) 71 elif isinstance(self.adjective, list): 72 for adj in self.adjective: 73 adj.setMembership( 74 (self.CER or self._CER)( 75 self.certainty, # how sure are we about this rule 76 self.operator() # value from input 77 ) 78 ) 79 else: 80 from fuzzy.Exception import FuzzyException 81 raise FuzzyException("rule target not set.")
82
83 - def getName(self, system):
84 """Lookup the name given this rule in the given system""" 85 return system.findRuleName(self)
86
87 - def __repr__(self):
88 """Return representation of instance. 89 90 @return: representation of instance 91 @rtype: string 92 """ 93 params = [] 94 params.append("adjective=%s" % object.__repr__(self.adjective)) 95 params.append("operator=%s" % repr(self.operator)) 96 if self.certainty != 1.0: params.append("certainty=%s" % self.certainty) 97 if self.CER: params.append("CER=%s" % repr(self.CER)) 98 return "%s.%s(%s)" % (self.__class__.__module__, self.__class__.__name__, ", ".join(params))
99