1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
46 """Reset all memberships for the next run of calculate"""
47 for variable in self.variables.values():
48 variable.reset()
49
51 """Fuzzify the inputs.
52 The input dictionary contains the input values for the named variables."""
53
54
55 for (name, value) in input.items():
56 if name in self.variables:
57 self.variables[name].setValue(value)
58
59
60
61
63 """Calculate the fuzzy inference given by the rules."""
64
65
66 for rule in self.rules.values():
67 rule.compute()
68
69
71 """Defuzzyfy the variables.
72 The output dictionary serves as container and provides the names of the
73 variables to read."""
74
75
76 for name in output.keys():
77 output[name] = self.variables[name].getValue()
78
79 return output
80
81
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
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
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
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
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