#!/usr/bin/env python # -*- coding: utf-8 -*- import sys sys.path.append('../../../pyfuzzy') # This example shows how to set up a fuzzy controller for # the temperature in a container in which we can put cold or warm water (through the mixer). # The container is modeled as a PT1-System which means the final value is # asymptotically with ongoing time. # Weitere Arbeiten: # # 1. Darstellung der Eingangs- und Ausgangsvariablen # 2. Fahren eines Fuzzy-Reglers über einen veränderbaren Sollwert # 3. Randbedingungen für die Variablen bei der Start- und Endefunktion # 4. Verarbeitung von beliebig gro0en Fuzzysets # 5. Verarbeitungen von mehreren Eingangs- und Ausgangsvariablen try: import Gnuplot have_Gnuplot = 1 except: have_Gnuplot = 0 def generateDocs(FuzzyController): system = FuzzyController.getFuzzySystem() from fuzzy.doc.plot.gnuplot import doc doc = doc.Doc("../doc") doc.createDoc(system) doc.create2DPlot(system,"input_temperature","output_temperature") def generateDot(FuzzyController): system = FuzzyController.getFuzzySystem() import fuzzy.doc.structure.dot.dot import subprocess for name,rule in system.rules.items(): cmd = "dot -T png -o '%s/Rule %s.png'" % ("../doc",name) f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin #f = sys.stdout fuzzy.doc.structure.dot.dot.print_header(f,"XXX") fuzzy.doc.structure.dot.dot.print_dot(rule,f,system,"") fuzzy.doc.structure.dot.dot.print_footer(f) cmd = "dot -T png -o '%s/System.png'" % "../doc", f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin fuzzy.doc.structure.dot.dot.printDot(system,f) def loadController(filename): from Controller import Controller FuzzyController = Controller(0,0,0) import cPickle file = open(filename,"rb") FuzzyController.system = cPickle.load(file) file.close() return FuzzyController def saveController(filename,FuzzyController): import cPickle file = open(filename,"wb") cPickle.dump(FuzzyController.getFuzzySystem(),file,1) file.close() def main(): """Main program.""" import sys # # Definition of Fuzzy # FILENAME = "Mixer.pickle" if "load" in sys.argv[1:]: # load ready system from file FuzzyController = loadController(FILENAME) else: # build it from scratch # value to hold Sollwert = 45.#26.3 from Controller import Controller FuzzyController = Controller(Sollwert,10,60) if "save" in sys.argv[1:]: # save system in file saveController(FILENAME,FuzzyController) # make docs if "doc" in sys.argv[1:]: generateDocs(FuzzyController) sys.exit(0) if "dot" in sys.argv[1:]: generateDot(FuzzyController) sys.exit(0) from LoggingProcess import LoggingProcess process = LoggingProcess(FuzzyController) # # loop # print ("\ncalculations started:") import os starttime = os.times() Schritte = 600 # steps -> time unit 1/20s for _ in range(Schritte): process.step(1./20.) # result endtime = os.times() print ("END: wanted value: %6.3f °C rltemp: %6.3f °C vltemp: %6.3f °C" % (Sollwert,process.rltemp,process.vltemp)) print ("USER: %6.3f sec %d steps (%6.3fms/step)" % (endtime[0]-starttime[0],Schritte,(endtime[0]-starttime[0])/Schritte*1000.)) print ("SYSTEM: %6.3f sec %d steps (%6.3fms/step)" % (endtime[1]-starttime[1],Schritte,(endtime[1]-starttime[1])/Schritte*1000.)) print ("USER(C): %6.3f sec %d steps (%6.3fms/step)" % (endtime[2]-starttime[2],Schritte,(endtime[2]-starttime[2])/Schritte*1000.)) print ("SYSTEM(C): %6.3f sec %d steps (%6.3fms/step)" % (endtime[3]-starttime[3],Schritte,(endtime[3]-starttime[3])/Schritte*1000.)) print ("REAL: %6.3f sec %d steps (%6.3fms/step)" % (endtime[4]-starttime[4],Schritte,(endtime[4]-starttime[4])/Schritte*1000.)) process.plot() if __name__ == "__main__": main()