Module Controller
[hide private]
[frames] | no frames]

Source Code for Module Controller

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4   
  5  # This example shows how to set up a fuzzy controller for  
  6  # the temperature in a container in which we can put cold or warm water (through the mixer). 
  7  # The container is modelled as a PT1-System which means the final value is 
  8  # asymptotically with ongoing time. 
  9   
 10  # Weitere Arbeiten: 
 11  # 
 12  # 1. Darstellung der Eingangs- und Ausgangsvariablen 
 13  # 2. Fahren eines Fuzzy-Reglers über einen veränderbaren Sollwert 
 14  # 3. Randbedingungen für die Variablen bei der Start- und Endefunktion 
 15  # 4. Verarbeitung von beliebig gro0en Fuzzysets 
 16  # 5. Verarbeitungen von mehreren Eingangs- und Ausgangsvariablen 
 17   
18 -class Controller(object):
19
20 - def __init__(self,target,cold,hot):
21 """Create fuzzy system.""" 22 import fuzzy 23 import fuzzy.System 24 import fuzzy.InputVariable 25 import fuzzy.fuzzify.Plain 26 import fuzzy.OutputVariable 27 import fuzzy.defuzzify.COG 28 import fuzzy.Adjective 29 #import fuzzy.AdjectiveProxy 30 import fuzzy.Rule 31 import fuzzy.operator 32 import fuzzy.operator.Input 33 import fuzzy.norm 34 import fuzzy.norm.Min 35 import fuzzy.set 36 #import fuzzy.set.Polygon 37 #import fuzzy.set.Trapez 38 import fuzzy.set.Triangle 39 import fuzzy.set.SFunction 40 import fuzzy.set.ZFunction 41 #import fuzzy.set.PiFunction 42 43 # create system object 44 self.system = fuzzy.System.System() 45 46 # definition of input variable 47 input_temp = fuzzy.InputVariable.InputVariable( 48 fuzzify=fuzzy.fuzzify.Plain.Plain(), 49 description="temperature", 50 min=0.0,max=100.0, 51 unit="degrees" 52 ) 53 54 # add variable to system 55 self.system.variables["input_temperature"] = input_temp 56 57 # create fuzzy set 58 # make it to an adjective 59 # and add it to the input variable 60 input_temp.adjectives["cold"] = fuzzy.Adjective.Adjective(fuzzy.set.ZFunction.ZFunction(a=target,delta=10.)) 61 62 #ignore, not used 63 #input_temp.adjectives["warm"] = fuzzy.Adjective.Adjective(fuzzy.set.PiFunction.PiFunction(a=target,delta=10.)) 64 65 input_temp.adjectives["hot"] = fuzzy.Adjective.Adjective(fuzzy.set.SFunction.SFunction(a=target,delta=10.)) 66 67 # definition of output variable 68 # we want use later the center of gravity method 69 output_temp = fuzzy.OutputVariable.OutputVariable( 70 defuzzify=fuzzy.defuzzify.COG.COG(), 71 description="temperature", 72 min=0.0,max=100.0, 73 unit="degrees" 74 ) 75 # more properties could be 76 # - output as value or membership of adjectives in a dictionary 77 # (input could work this way too.) 78 79 # add to system 80 self.system.variables["output_temperature"] = output_temp 81 82 # make set -> adjective -> add to variable 83 output_temp.adjectives["cold"] = fuzzy.Adjective.Adjective(fuzzy.set.Triangle.Triangle(m=cold)) 84 85 output_temp.adjectives["hot"] = fuzzy.Adjective.Adjective(fuzzy.set.Triangle.Triangle(m=hot)) 86 87 # create a fuzzy rule 88 rule1 = fuzzy.Rule.Rule( 89 # it sets putput_temperature hot 90 adjective=self.system.variables["output_temperature"].adjectives["hot"], 91 # it gets it value from here 92 operator=fuzzy.operator.Input.Input( 93 # and this get it value from here 94 self.system.variables["input_temperature"].adjectives["cold"], 95 ), 96 certainty=1.0, 97 CER=fuzzy.norm.Min.Min() 98 ) 99 100 # using another style of referencing the adjectives (by looking up the names at runtime) 101 rule2 = fuzzy.Rule.Rule( 102 # adjective=fuzzy.AdjectiveProxy.AdjectiveProxy( 103 # system = self.system, 104 # variable="output_temperature", 105 # adjective="cold" 106 # ), 107 adjective=self.system.variables["output_temperature"].adjectives["cold"], 108 operator=fuzzy.operator.Input.Input( 109 # fuzzy.AdjectiveProxy.AdjectiveProxy( 110 # system = self.system, 111 # variable="input_temperature", 112 # adjective="hot" 113 # ), 114 self.system.variables["input_temperature"].adjectives["hot"] 115 ), 116 certainty=1.0, 117 CER=fuzzy.norm.Min.Min() 118 ) 119 120 # add rules to system 121 self.system.rules["too cold"] = rule1 122 self.system.rules["too warm"] = rule2
123 124 # system ready to use 125
126 - def getFuzzySystem(self):
127 return self.system
128
129 - def calculate(self,input):
130 # dictionaries as container for transfer of parameters 131 # a is for named output values! 132 input_values = {"input_temperature" : input} 133 output_values = {"output_temperature" : 0.0} 134 # calculation in fuzzy controller 135 self.system.calculate(input=input_values,output=output_values) 136 137 result = output_values["output_temperature"] 138 # if next turn not calculable use last value (= current in this turn) 139 self.system.variables["output_temperature"].failsafe = result 140 return result
141