Advertisement
toweber

tipping_controller

Sep 9th, 2021 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import os
  2. import ipdb; # ipdb.set_trace()  
  3. import numpy as np
  4. import skfuzzy as fuzz
  5. from skfuzzy import control as ctrl
  6. import matplotlib.pyplot as plt
  7.  
  8. # example from https://pythonhosted.org/scikit-fuzzy/auto_examples/plot_tipping_problem_newapi.html
  9.  
  10. # New Antecedent/Consequent objects hold universe variables and membership
  11. # functions
  12. quality = ctrl.Antecedent(np.arange(0, 11, 1), 'quality')
  13. service = ctrl.Antecedent(np.arange(0, 11, 1), 'service')
  14. tip = ctrl.Consequent(np.arange(0, 26, 1), 'tip')
  15.  
  16. # Auto-membership function population is possible with .automf(3, 5, or 7)
  17. quality.automf(3)
  18. service.automf(3)    
  19.  
  20. # Custom membership functions can be built interactively with a familiar,
  21. # Pythonic API
  22. tip['low'] = fuzz.trimf(tip.universe, [0, 0, 13])
  23. tip['medium'] = fuzz.trimf(tip.universe, [0, 13, 25])
  24. tip['high'] = fuzz.trimf(tip.universe, [13, 25, 25])
  25.  
  26.  
  27. quality['average'].view()
  28. #ipdb.set_trace()
  29. quality['good'].view()
  30. #ipdb.set_trace()
  31. #fig, ax = tip.view()
  32. #ipdb.set_trace()
  33. #ipdb.set_trace()
  34. rule1 = ctrl.Rule(quality['poor'] | service['poor'], tip['low'])
  35. rule2 = ctrl.Rule(service['average'], tip['medium'])
  36. rule3 = ctrl.Rule(service['good'] | quality['good'], tip['high'])
  37.  
  38. #fig, ax = rule1.view()
  39. #fig.show()
  40. #ipdb.set_trace()
  41.  
  42. # Control System Creation and Simulation
  43.  
  44. tipping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
  45. tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
  46.  
  47. # Pass inputs to the ControlSystem using Antecedent labels with Pythonic API
  48. # Note: if you like passing many inputs all at once, use .inputs(dict_of_data)
  49. tipping.input['quality'] = 5
  50. tipping.input['service'] = 4
  51.  
  52. # Crunch the numbers
  53. tipping.compute()
  54.  
  55. print(tipping.output['tip'])
  56. tip.view(sim=tipping)
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement