Advertisement
toweber

defuzificacao

Mar 4th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. import ipdb # ipdb.set_trace()
  2. import numpy as np
  3.  
  4. def deffuzify_cog(y, uy):
  5.     """
  6.    center of gravity
  7.    """
  8.  
  9.     sum_uy = np.sum(uy)
  10.     middle_sum_uy = sum_uy/2
  11.  
  12.     error = np.abs(np.cumsum(uy)-middle_sum_uy)
  13.     index = np.where(error == np.min(error))
  14.     index = index[0][0]
  15.     y_star = y[index]
  16.  
  17.     return y_star
  18.  
  19.  
  20. def deffuzify_mom(y, uy):
  21.     """
  22.    Middle of Maximum
  23.    """
  24.     indices = np.where(uy==max(uy))
  25.     M = y[indices]
  26.     y_star = np.sum(M)/M.shape[0]
  27.     return y_star
  28.  
  29.  
  30. def deffuzify_lom(y, uy):
  31.     """
  32.    Larger of Maximum
  33.    """
  34.     indices = np.where(uy==max(uy))
  35.     M = y[indices]
  36.     y_star = max(M)
  37.     return y_star
  38.  
  39. def deffuzify_som(y, uy):
  40.     """
  41.    Smaller of Maximum
  42.    """
  43.     indices = np.where(uy==max(uy))
  44.     M = y[indices]
  45.     y_star = min(M)
  46.     return y_star
  47.  
  48.  
  49. def trimf(x, abc):
  50.     a, b, c = abc
  51.     y = np.zeros(len(x))
  52.     left_side_eq = (1/(b-a))*(x-a)
  53.     right_side_eq = (1/(c-b))*(-x+c)
  54.     y = np.minimum(left_side_eq,right_side_eq)    
  55.     y = np.maximum(y,0)
  56.     return y
  57.  
  58. def inference_mandani_nomatrix(P,ux,uy):
  59.     """
  60.    onde:
  61.    - P é a premissa
  62.    - ux é a função de pertinência do antencedente
  63.    - uy é a função de pertinência do consequente
  64.    - retornará Q
  65.    """
  66.     antecedente_max = np.max(np.minimum(P[:],ux[:]))
  67.     #ipdb.set_trace()
  68.     Q = np.minimum(antecedente_max,uy[:])
  69.     return Q
  70.  
  71. def inference_mandani_2_premissas_AND(P1,ux1,P2,ux2,uy):
  72.     """
  73.    onde:
  74.    - P1 é o antecedente 1
  75.    - P2 é o antecedente 2
  76.    - ux1 é a função de pertinência do antencedente 1
  77.    - ux2 é a função de pertinência do antencedente 2
  78.    - uy é a função de pertinência do consequente
  79.    - retornará Q
  80.    """
  81.     antecedente1_max = np.max(np.minimum(P1[:],ux1[:]))
  82.     antecedente2_max = np.max(np.minimum(P2[:],ux2[:]))
  83.     antecedente_max = min(antecedente1_max,antecedente2_max)
  84.     Q = np.minimum(antecedente_max,uy[:])
  85.     return Q
  86.  
  87.  
  88. # conjunto Universo  das variáveis base
  89. #X = [-40, -30 ... 40]
  90. X = np.arange(-40,41,10)
  91.  
  92. #Y = [-6, -2, ..., 6]
  93. Y = np.arange(-6, 6.1, 2)
  94.  
  95.  
  96. # Funções de Pertinência
  97. u_x_baixo = trimf(X,[-40,0,40])  # funcao de pertinencia para variável linguística x com valor baixo
  98. u_x_medio = trimf(X,[-10,10,60])  # funcao de pertinencia para variável linguística x1 com valor medio
  99.  
  100. u_y_medio  = trimf(Y,[-6,-2,2])     # funcao de pertinencia para variável linguística y com valor medio
  101. u_y_alto  = trimf(Y,[-2,2,6])      # funcao de pertinencia para variável linguística y com valor alto
  102.  
  103. #ipdb.set_trace()
  104. # Inferência Fuzzy
  105.  
  106. #Assuma que x = 20
  107. A_l = np.zeros(X.shape[0])   # inicializa A_l
  108. A_l[np.where(X==20)] = 1     # faz que no índice onde o conjunto universo = 20, este conjunto tenha valor 1
  109.  
  110.  
  111. Y_l1 = inference_mandani_nomatrix(A_l,u_x_baixo,u_y_alto)
  112. Y_l2 = inference_mandani_nomatrix(A_l,u_x_medio,u_y_medio)
  113.  
  114. Y_l = np.maximum(Y_l1,Y_l2)
  115.  
  116. #ipdb.set_trace()
  117. y_defuzified = deffuzify_cog(Y,Y_l)
  118. #y_defuzified = deffuzify_lom(Y,Y_l)
  119.  
  120. print("\n")
  121. print("Universo de discurso: %s" %Y)
  122. print("Grau de Pertinência da saída: %s" %Y_l)
  123. print("Saída defuzificada: %f" %y_defuzified)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement