/tutorial/fuzzy-logic/defuzzification.py

https://code.google.com/p/peach/ · Python · 92 lines · 49 code · 12 blank · 31 comment · 2 complexity · 55b18605be3bf143c1fd50ef61fa41cb MD5 · raw file

  1. ################################################################################
  2. # Peach - Computational Intelligence for Python
  3. # Jose Alexandre Nalon
  4. #
  5. # This file: tutorial/defuzzification.py
  6. # Defuzzification methods
  7. ################################################################################
  8. # We import numpy for arrays and peach for the library. Actually, peach also
  9. # imports the numpy module, but we want numpy in a separate namespace:
  10. import numpy
  11. from peach.fuzzy import *
  12. # The main application of fuzzy logic is in the form of fuzzy controllers. The
  13. # last step on the control is the defuzzification, or returning from fuzzy sets
  14. # to crisp numbers. Peach has a number of ways of dealing with that operation.
  15. # Here we se how to do that.
  16. # Just to illustrate the method, we will create arbitrary fuzzy sets. In a
  17. # controller, these functions would be obtained by fuzzification and a set of
  18. # production rules. But our intent here is to show how to use the
  19. # defuzzification methods. Remember that instantiating Membership functions
  20. # gives us a function, so we must apply it over our domain.
  21. y = numpy.linspace(-30.0, 30.0, 500)
  22. gn = Triangle(-30.0, -20.0, -10.0)(y)
  23. pn = Triangle(-20.0, -10.0, 0.0)(y)
  24. z = Triangle(-10.0, 0.0, 10.0)(y)
  25. pp = Triangle(0.0, 10.0, 20.0)(y)
  26. gp = Triangle(10.0, 20.0, 30.0)(y)
  27. # Here we simulate the response of the production rules of a controller. In it,
  28. # a controller will associate a membership value with every membership function
  29. # of the output variable. Here we do that. You will notice that no membership
  30. # values are associated with pp and gp functions. That is because we are
  31. # supposing that they are 0, effectivelly eliminating those functions (we plot
  32. # them anyway.
  33. mf = gn & 0.33 | pn & 0.67 | z & 0.25
  34. # Here are the defuzzification methods. Defuzzification methods are functions.
  35. # They receive, as their first parameter, the membership function (or the fuzzy
  36. # set) and as second parameter the domain of the output variable. Every method
  37. # works that way -- and if you want to implement your own, use this signature.
  38. # Notice that it is a simple function, not a class that is instantiated.
  39. centroid = Centroid(mf, y) # Centroid method
  40. bisec = Bisector(mf, y) # Bissection method
  41. som = SmallestOfMaxima(mf, y) # Smallest of Maxima
  42. lom = LargestOfMaxima(mf, y) # Largest of Maxima
  43. mom = MeanOfMaxima(mf, y) # Mean of Maxima
  44. # We will use the matplotlib module to plot these functions. We save the plot in
  45. # a figure called 'defuzzification.png'.
  46. try:
  47. from matplotlib import *
  48. from matplotlib.pylab import *
  49. figure(1).set_size_inches(8., 4.)
  50. a1 = axes([ 0.125, 0.10, 0.775, 0.8 ])
  51. ll = [ 0.0, 1.0 ]
  52. a1.hold(True)
  53. a1.plot([ centroid, centroid ], ll, linewidth = 1)
  54. a1.plot([ bisec, bisec ], ll, linewidth = 1)
  55. a1.plot([ som, som ], ll, linewidth = 1)
  56. a1.plot([ lom, lom ], ll, linewidth = 1)
  57. a1.plot([ mom, mom ], ll, linewidth = 1)
  58. a1.plot(y, gn, 'k--')
  59. a1.plot(y, pn, 'k--')
  60. a1.plot(y, z, 'k--')
  61. a1.plot(y, pp, 'k--')
  62. a1.plot(y, gp, 'k--')
  63. a1.fill(y, mf, 'gray')
  64. a1.set_xlim([ -30, 30 ])
  65. a1.set_ylim([ -0.1, 1.1 ])
  66. a1.set_xticks(linspace(-30, 30, 7.0))
  67. a1.set_yticks([ 0.0, 1.0 ])
  68. a1.legend([ 'Centroid = %7.4f' % centroid,
  69. 'Bisector = %7.4f' % bisec,
  70. 'SOM = %7.4f' % som,
  71. 'LOM = %7.4f' % lom,
  72. 'MOM = %7.4f' % mom ])
  73. savefig("defuzzification.png")
  74. except ImportError:
  75. print "Defuzzification results:"
  76. print " Centroid = %7.4f" % centroid
  77. print " Bisector = %7.4f" % bisec
  78. print " SOM = %7.4f" % som
  79. print " LOM = %7.4f" % lom
  80. print " MOM = %7.4f" % mom