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

Source Code for Module main

  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 modeled 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  try: 
 19      import Gnuplot 
 20      have_Gnuplot = 1 
 21  except: 
 22      have_Gnuplot = 0 
 23   
24 -def generateDocs(FuzzyController):
25 system = FuzzyController.getFuzzySystem() 26 from fuzzy.doc.plot.gnuplot import doc 27 doc = doc.Doc("../doc") 28 doc.createDoc(system) 29 doc.create2DPlot(system,"input_temperature","output_temperature")
30
31 -def generateDot(FuzzyController):
32 system = FuzzyController.getFuzzySystem() 33 import fuzzy.doc.structure.dot.dot 34 import subprocess 35 for name,rule in system.rules.items(): 36 cmd = "dot -T png -o '%s/Rule %s.png'" % ("../doc",name) 37 f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin 38 #f = sys.stdout 39 fuzzy.doc.structure.dot.dot.print_header(f,"XXX") 40 fuzzy.doc.structure.dot.dot.print_dot(rule,f,system,"") 41 fuzzy.doc.structure.dot.dot.print_footer(f) 42 cmd = "dot -T png -o '%s/System.png'" % "../doc", 43 f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin 44 fuzzy.doc.structure.dot.dot.printDot(system,f)
45
46 -def loadController(filename):
47 from Controller import Controller 48 FuzzyController = Controller(0,0,0) 49 import cPickle 50 file = open(filename,"rb") 51 FuzzyController.system = cPickle.load(file) 52 file.close() 53 return FuzzyController
54
55 -def saveController(filename,FuzzyController):
56 import cPickle 57 file = open(filename,"wb") 58 cPickle.dump(FuzzyController.getFuzzySystem(),file,1) 59 file.close()
60 61
62 -def main():
63 """Main program.""" 64 import sys 65 66 # 67 # Definition of Fuzzy 68 # 69 FILENAME = "Mixer.pickle" 70 71 if "load" in sys.argv[1:]: 72 # load ready system from file 73 FuzzyController = loadController(FILENAME) 74 else: 75 # build it from scratch 76 # value to hold 77 Sollwert = 26.3 78 from Controller import Controller 79 FuzzyController = Controller(Sollwert,10,60) 80 81 if "save" in sys.argv[1:]: 82 # save system in file 83 saveController(FILENAME,FuzzyController) 84 85 # make docs 86 if "doc" in sys.argv[1:]: 87 generateDocs(FuzzyController) 88 sys.exit(0) 89 90 if "dot" in sys.argv[1:]: 91 generateDot(FuzzyController) 92 sys.exit(0) 93 94 from LoggingProcess import LoggingProcess 95 process = LoggingProcess(FuzzyController) 96 # 97 # loop 98 # 99 print "\ncalculations started:" 100 import os 101 starttime = os.times() 102 103 Schritte = 600 # steps -> time unit 1/20s 104 for _ in range(Schritte): 105 process.step(1./20.) 106 107 # result 108 endtime = os.times() 109 print "END: wanted value: %6.3f °C rltemp: %6.3f °C vltemp: %6.3f °C" % (Sollwert,process.rltemp,process.vltemp) 110 print "USER: %6.3f sec %d steps (%6.3fms/step)" % (endtime[0]-starttime[0],Schritte,(endtime[0]-starttime[0])/Schritte*1000.) 111 print "SYSTEM: %6.3f sec %d steps (%6.3fms/step)" % (endtime[1]-starttime[1],Schritte,(endtime[1]-starttime[1])/Schritte*1000.) 112 print "USER(C): %6.3f sec %d steps (%6.3fms/step)" % (endtime[2]-starttime[2],Schritte,(endtime[2]-starttime[2])/Schritte*1000.) 113 print "SYSTEM(C): %6.3f sec %d steps (%6.3fms/step)" % (endtime[3]-starttime[3],Schritte,(endtime[3]-starttime[3])/Schritte*1000.) 114 print "REAL: %6.3f sec %d steps (%6.3fms/step)" % (endtime[4]-starttime[4],Schritte,(endtime[4]-starttime[4])/Schritte*1000.) 115 116 process.plot()
117 118 119 if __name__ == "__main__": 120 main() 121