Advertisement
toweber

fuzzy_inference_without_relation_matrix_multiple_rules_defuzzification_tipping_problem

Sep 9th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  1. #import os
  2. #import ipdb # ipdb.set_trace()
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. from matplotlib import cm
  7.  
  8. def deffuzify_cog_alt(y, uy):
  9.     """
  10.    center of gravity
  11.    """
  12.     y_star = np.sum(uy*y)/np.sum(uy)
  13.     return y_star
  14.  
  15. def deffuzify_cog(y, uy):
  16.     """
  17.    center of gravity
  18.    """
  19.     sum_uy = np.sum(uy)
  20.     middle_sum_uy = sum_uy/2
  21.     error = np.abs(np.cumsum(uy)-middle_sum_uy)
  22.     index = np.where(error == np.min(error))
  23.     index = index[0][0]
  24.     y_star = y[index]
  25.  
  26.     return y_star
  27.  
  28. def deffuzify_mom(y, uy):
  29.     """
  30.    Middle of Maximum
  31.    """
  32.     indices = np.where(uy==max(uy))
  33.     indices = indices[0]
  34.     first_index = np.min(indices)
  35.     last_index = np.max(indices)
  36.     y_star = (y[first_index]+y[last_index])/2
  37.  
  38.     return y_star
  39.  
  40.  
  41. def deffuzify_lom(y, uy):
  42.     """
  43.    Larger of Maximum
  44.    """
  45.     indices = np.where(uy==max(uy))
  46.     indices = indices[0]
  47.     first_index = np.min(indices)
  48.     last_index = np.max(indices)
  49.     y_star = y[last_index]
  50.  
  51.     return y_star
  52.  
  53. def deffuzify_som(y, uy):
  54.     """
  55.    Smaller of Maximum
  56.    """
  57.     indices = np.where(uy==max(uy))
  58.     indices = indices[0]
  59.     first_index = np.min(indices)
  60.     last_index = np.max(indices)
  61.     y_star = y[first_index]
  62.  
  63.     return y_star
  64.  
  65.  
  66. def trimf(x, abc):
  67.     a, b, c = abc
  68.     y = np.zeros(len(x))
  69.     left_side_eq = (1/(b-a))*(x-a)
  70.     right_side_eq = (1/(c-b))*(-x+c)
  71.     y = np.minimum(left_side_eq,right_side_eq)    
  72.     y = np.maximum(y,0)
  73.     return y
  74.  
  75. def inference_mamdani_nomatrix(P,ux,uy):
  76.     """
  77.    onde:
  78.    - P é a premissa
  79.    - ux é a função de pertinência do antencedente
  80.    - uy é a função de pertinência do consequente
  81.    - retornará Q
  82.    """
  83.     antecedente_max = np.max(np.minimum(P[:],ux[:]))
  84.     #ipdb.set_trace()
  85.     Q = np.minimum(antecedente_max,uy[:])
  86.     return Q
  87.  
  88. def inference_mamdani_2_premissas_AND(P1,ux1,P2,ux2,uy):
  89.     """
  90.    onde:
  91.    - P1 é o antecedente 1
  92.    - P2 é o antecedente 2
  93.    - ux1 é a função de pertinência do antencedente 1
  94.    - ux2 é a função de pertinência do antencedente 2
  95.    - uy é a função de pertinência do consequente
  96.    - retornará Q
  97.    """
  98.     antecedente1_max = np.max(np.minimum(P1[:],ux1[:]))
  99.     antecedente2_max = np.max(np.minimum(P2[:],ux2[:]))
  100.     antecedente_max = min(antecedente1_max,antecedente2_max)
  101.     Q = np.minimum(antecedente_max,uy[:])
  102.     return Q
  103.  
  104. def inference_mamdani_2_premissas_OR(P1,ux1,P2,ux2,uy):
  105.     """
  106.    onde:
  107.    - P1 é o antecedente 1
  108.    - P2 é o antecedente 2
  109.    - ux1 é a função de pertinência do antencedente 1
  110.    - ux2 é a função de pertinência do antencedente 2
  111.    - uy é a função de pertinência do consequente
  112.    - retornará Q
  113.    """
  114.     antecedente1_max = np.max(np.minimum(P1[:],ux1[:]))
  115.     antecedente2_max = np.max(np.minimum(P2[:],ux2[:]))
  116.     antecedente_max = max(antecedente1_max,antecedente2_max) #OR
  117.     Q = np.minimum(antecedente_max,uy[:])
  118.     return Q
  119.  
  120.  
  121. # conjunto Universo  das variáveis base
  122. X = np.arange(0,10.1,0.1)
  123. Y = np.arange(0, 25, 0.1)
  124.  
  125.  
  126. # Funções de Pertinência
  127. u_servico_ruim = trimf(X,[-5,0,5])
  128. u_servico_aceitavel = trimf(X,[0,5,10])
  129. u_servico_otimo = trimf(X,[5,10,15])
  130.  
  131. u_refeicao_ruim = trimf(X,[-5,0,5])
  132. u_refeicao_aceitavel = trimf(X,[2,5,8])
  133. u_refeicao_excelente = trimf(X,[6,10,14])
  134.  
  135. u_gorjeta_baixa = trimf(Y,[-12.5,0,12.5])
  136. u_gorjeta_media = trimf(Y,[0,12.5,25])
  137. u_gorjeta_alta = trimf(Y,[12.5,25,37.5])
  138.  
  139.  
  140. xs = []
  141. ys = []
  142. zs = []
  143.  
  144. X_sampled = X[2::3]
  145.  
  146.  
  147. for i in X_sampled:
  148.     for j in X_sampled:        
  149.         refeicao_l = np.zeros(X.shape[0])   # inicializa A_l
  150.         servico_l = np.zeros(X.shape[0])   # inicializa A_l
  151.  
  152.         refeicao_l[np.where(X==i)] = 1
  153.         servico_l[np.where(X==j)] = 1
  154.  
  155.         Y_l1 = inference_mamdani_2_premissas_AND(servico_l,    u_servico_otimo     ,refeicao_l,    u_refeicao_excelente       ,u_gorjeta_alta)
  156.         Y_l2 = inference_mamdani_nomatrix(servico_l,    u_servico_aceitavel      ,u_gorjeta_media)
  157.         Y_l3 = inference_mamdani_2_premissas_AND(servico_l,     u_servico_ruim         ,refeicao_l,      u_refeicao_ruim,          u_gorjeta_baixa)
  158.  
  159.         Y_l = np.maximum(Y_l1,Y_l2)
  160.         Y_l = np.maximum(Y_l,Y_l3)        
  161.         #ipdb.set_trace()
  162.         #print("Estamos em i=%d,j=%d" %(i,j),end='')
  163.         y_defuzified = deffuzify_cog(Y,Y_l)
  164.         #print("\t Saída = %f" %y_defuzified)
  165.         xs.append(i)
  166.         ys.append(j)
  167.         zs.append(y_defuzified)
  168.  
  169.  
  170.  
  171. fig = plt.figure()
  172. ax = fig.add_subplot(111, projection='3d')
  173. ax.scatter(xs, ys, zs, s=1, depthshade = True)
  174. ax.set_xlabel('Refeicao')
  175. ax.set_ylabel('Servico')
  176. ax.set_zlabel('gorjeta')
  177. #ax.set_xticks(X)
  178. #ax.set_yticks(X)
  179. plt.show()
  180.  
  181.  
  182. # plot através de superfície
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement