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

Source Code for Module fuzzy.System

  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  """Main coordinator class of a whole fuzzy system""" 
 20   
 21  __revision__ = "$Id: System.py,v 1.19 2010-02-17 19:57:13 rliebscher Exp $" 
 22   
23 -class System(object):
24 """Holds all stuff together. (variables, rules, ...) 25 Provides methods to do calculation with it. 26 27 @ivar variables: dictionary to hold all variables. 28 @type variables: {string:L{fuzzy.Variable.Variable}} 29 @ivar rules: dictionary to hold all rules. 30 @type rules: {string:L{fuzzy.Rule.Rule}} 31 @ivar description: description 32 @type description: string 33 """ 34
35 - def __init__(self, description="", variables = None, rules = None):
36 """Constructor. 37 38 @param description: description 39 @type description: string 40 """ 41 self.variables = variables or {} 42 self.rules = variables or {} 43 self.description = description
44
45 - def reset(self):
46 """Reset all memberships for the next run of calculate""" 47 for variable in self.variables.values(): 48 variable.reset()
49
50 - def fuzzify(self, input):
51 """Fuzzify the inputs. 52 The input dictionary contains the input values for the named variables.""" 53 54 # feed input values in variables and so in adjectives 55 for (name, value) in input.items(): 56 if name in self.variables: 57 self.variables[name].setValue(value)
58 #else: 59 # print "ignored input ",name 60 61
62 - def inference(self):
63 """Calculate the fuzzy inference given by the rules.""" 64 65 # compute fuzzy rules 66 for rule in self.rules.values(): 67 rule.compute()
68 69
70 - def defuzzify(self, output):
71 """Defuzzyfy the variables. 72 The output dictionary serves as container and provides the names of the 73 variables to read.""" 74 75 # get all wanted output variables 76 for name in output.keys(): 77 output[name] = self.variables[name].getValue() 78 79 return output
80 81
82 - def calculate(self, input, output):
83 """Do a complete fuzzy calculation step. 84 The input dictionary contains the input values for the named variables. 85 The output dictionary serves as container and provides the names of the 86 variables to read.""" 87 88 self.reset() 89 90 self.fuzzify(input) 91 92 self.inference() 93 94 self.defuzzify(output) 95 96 return output
97 98
99 - def findVariableName(self, var):
100 """Find name of variable in this system""" 101 for name, variable in self.variables.items(): 102 if var is variable: 103 return name 104 return None
105
106 - def findAdjectiveName(self, adj):
107 """Find name of adjective (and variable) in this system""" 108 for name, variable in self.variables.items(): 109 for namea, adjective in variable.adjectives.items(): 110 if adj is adjective: 111 return [namea, name] 112 return None
113
114 - def findRuleName(self, _rule):
115 """Find name of rule in this system""" 116 for name, rule in self.rules.items(): 117 if _rule is rule: 118 return name 119 return None
120
121 - def __repr__(self):
122 """Return representation of instance. 123 124 @return: representation of instance 125 @rtype: string 126 """ 127 params = [] 128 if self.description: params.append("description=%s" % repr(self.description)) 129 if self.variables: params.append("variables=%s" % repr(self.variables)) 130 if self.rules: params.append("rules=%s" % repr(self.rules)) 131 return "%s.%s(%s)" % (self.__class__.__module__, self.__class__.__name__, ", ".join(params))
132