fu.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import numpy as np
  2. import skfuzzy as fuzz
  3. import matplotlib.pyplot as plt
  4. #from mpl_toolkit.mplot3d import Axes3D
  5. from skfuzzy import control as ctrl
  6. import math
  7. x_qual = np.arange(0,11,1)
  8. x_serv = np.arange(0,11,1)
  9. x_tip = np.arange(0,26,1)
  10. quality = ctrl.Antecedent(x_qual,'quality')
  11. service = ctrl.Antecedent(x_serv,'service')
  12. tip = ctrl.Consequent(x_tip,'tip')
  13. quality['L']=fuzz.trimf(x_qual,[0,0,5])
  14. quality['M']=fuzz.trimf(x_qual,[0,5,10])
  15. quality['H']=fuzz.trimf(x_qual,[5,10,10])
  16. service['L']=fuzz.trimf(x_serv,[0,0,5])
  17. service['M']=fuzz.trimf(x_serv,[0,5,10])
  18. service['H']=fuzz.trimf(x_serv,[5,10,10])
  19. tip['L']=fuzz.trimf(x_tip,[0,0,13])
  20. tip['M']=fuzz.trimf(x_tip,[0,13,25])
  21. tip['H']=fuzz.trimf(x_tip,[13,25,25])
  22. quality.view()
  23. service.view()
  24. tip.view()
  25. tip.defuzzify_method='centroid'
  26. rule1=ctrl.Rule(antecedent=((quality['L']&service['L'])|(quality['L']&service['M'])|(quality['M']&service['L'])),consequent=tip['L'],label='Low')
  27. rule2=ctrl.Rule(antecedent=((quality['M']&service['M'])|(quality['L']&service['H'])|(quality['H']&service['L'])),consequent=tip['M'],label='Medium')
  28. rule3=ctrl.Rule(antecedent=((quality['M']&service['H'])|(quality['H']&service['M'])|(quality['H']&service['H'])),consequent=tip['H'],label='High')
  29. #rule2.view()
  30. tipping_ctrl = ctrl.ControlSystem([rule1,rule2,rule3])
  31. tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
  32. tipping.input['quality']=2
  33. tipping.input['service']=2
  34. tipping.compute()
  35. p=tipping.output['tip']
  36. tip.view(sim=tipping)
  37. print(p)
  38. #upsampled = np.linspace(0,11,21)
  39. #x,y=np.meshgrid(upsampled,upsampled)
  40. #z=np.zeros_like(x)
  41. #pp=[]
  42. #for i in range(0,21):
  43. # for j in range(0,21):
  44. # tipping.input['quality']=x[i,j]
  45. # tipping.input['service']=y[i,j]
  46. # tipping.compute()
  47. # z[i,j]=tipping.output['tip']
  48. # pp.append(z[i,j])
  49. #print('max:',max(pp))
  50. #print('min:',min(pp))
  51. #fig = plt.figure(figsize=(8,8))
  52. #ax = fig.add_subplot(111,projection='3d')
  53. #surf = ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap='viridis',linewidth=0.4,antialiased=True)
  54. #ax.view_init(30,200)
  55. plt.show()