Advertisement
toweber

fuzzy_inference_without_relation_matrix_multiple_rules_defuzzification

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