Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import os
- #import ipdb # ipdb.set_trace()
- import numpy as np
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- from matplotlib import cm
- def deffuzify_cog_alt(y, uy):
- """
- center of gravity
- """
- y_star = np.sum(uy*y)/np.sum(uy)
- return y_star
- def deffuzify_cog(y, uy):
- """
- center of gravity
- """
- sum_uy = np.sum(uy)
- middle_sum_uy = sum_uy/2
- error = np.abs(np.cumsum(uy)-middle_sum_uy)
- index = np.where(error == np.min(error))
- index = index[0][0]
- y_star = y[index]
- return y_star
- def deffuzify_mom(y, uy):
- """
- Middle of Maximum
- """
- indices = np.where(uy==max(uy))
- indices = indices[0]
- first_index = np.min(indices)
- last_index = np.max(indices)
- y_star = (y[first_index]+y[last_index])/2
- return y_star
- def deffuzify_lom(y, uy):
- """
- Larger of Maximum
- """
- indices = np.where(uy==max(uy))
- indices = indices[0]
- first_index = np.min(indices)
- last_index = np.max(indices)
- y_star = y[last_index]
- return y_star
- def deffuzify_som(y, uy):
- """
- Smaller of Maximum
- """
- indices = np.where(uy==max(uy))
- indices = indices[0]
- first_index = np.min(indices)
- last_index = np.max(indices)
- y_star = y[first_index]
- return y_star
- def trimf(x, abc):
- a, b, c = abc
- y = np.zeros(len(x))
- left_side_eq = (1/(b-a))*(x-a)
- right_side_eq = (1/(c-b))*(-x+c)
- y = np.minimum(left_side_eq,right_side_eq)
- y = np.maximum(y,0)
- return y
- def inference_mamdani_nomatrix(P,ux,uy):
- """
- onde:
- - P é a premissa
- - ux é a função de pertinência do antencedente
- - uy é a função de pertinência do consequente
- - retornará Q
- """
- antecedente_max = np.max(np.minimum(P[:],ux[:]))
- #ipdb.set_trace()
- Q = np.minimum(antecedente_max,uy[:])
- return Q
- def inference_mamdani_2_premissas_AND(P1,ux1,P2,ux2,uy):
- """
- onde:
- - P1 é o antecedente 1
- - P2 é o antecedente 2
- - ux1 é a função de pertinência do antencedente 1
- - ux2 é a função de pertinência do antencedente 2
- - uy é a função de pertinência do consequente
- - retornará Q
- """
- antecedente1_max = np.max(np.minimum(P1[:],ux1[:]))
- antecedente2_max = np.max(np.minimum(P2[:],ux2[:]))
- antecedente_max = min(antecedente1_max,antecedente2_max)
- Q = np.minimum(antecedente_max,uy[:])
- return Q
- def inference_mamdani_2_premissas_OR(P1,ux1,P2,ux2,uy):
- """
- onde:
- - P1 é o antecedente 1
- - P2 é o antecedente 2
- - ux1 é a função de pertinência do antencedente 1
- - ux2 é a função de pertinência do antencedente 2
- - uy é a função de pertinência do consequente
- - retornará Q
- """
- antecedente1_max = np.max(np.minimum(P1[:],ux1[:]))
- antecedente2_max = np.max(np.minimum(P2[:],ux2[:]))
- antecedente_max = max(antecedente1_max,antecedente2_max) #OR
- Q = np.minimum(antecedente_max,uy[:])
- return Q
- # conjunto Universo das variáveis base
- X = np.arange(0,10.1,0.1)
- Y = np.arange(0, 25, 0.1)
- # Funções de Pertinência
- u_servico_ruim = trimf(X,[-5,0,5])
- u_servico_aceitavel = trimf(X,[0,5,10])
- u_servico_otimo = trimf(X,[5,10,15])
- u_refeicao_ruim = trimf(X,[-5,0,5])
- u_refeicao_aceitavel = trimf(X,[2,5,8])
- u_refeicao_excelente = trimf(X,[6,10,14])
- u_gorjeta_baixa = trimf(Y,[-12.5,0,12.5])
- u_gorjeta_media = trimf(Y,[0,12.5,25])
- u_gorjeta_alta = trimf(Y,[12.5,25,37.5])
- xs = []
- ys = []
- zs = []
- X_sampled = X[2::3]
- for i in X_sampled:
- for j in X_sampled:
- refeicao_l = np.zeros(X.shape[0]) # inicializa A_l
- servico_l = np.zeros(X.shape[0]) # inicializa A_l
- refeicao_l[np.where(X==i)] = 1
- servico_l[np.where(X==j)] = 1
- Y_l1 = inference_mamdani_2_premissas_AND(servico_l, u_servico_otimo ,refeicao_l, u_refeicao_excelente ,u_gorjeta_alta)
- Y_l2 = inference_mamdani_nomatrix(servico_l, u_servico_aceitavel ,u_gorjeta_media)
- Y_l3 = inference_mamdani_2_premissas_AND(servico_l, u_servico_ruim ,refeicao_l, u_refeicao_ruim, u_gorjeta_baixa)
- Y_l = np.maximum(Y_l1,Y_l2)
- Y_l = np.maximum(Y_l,Y_l3)
- #ipdb.set_trace()
- #print("Estamos em i=%d,j=%d" %(i,j),end='')
- y_defuzified = deffuzify_cog(Y,Y_l)
- #print("\t Saída = %f" %y_defuzified)
- xs.append(i)
- ys.append(j)
- zs.append(y_defuzified)
- fig = plt.figure()
- ax = fig.add_subplot(111, projection='3d')
- ax.scatter(xs, ys, zs, s=1, depthshade = True)
- ax.set_xlabel('Refeicao')
- ax.set_ylabel('Servico')
- ax.set_zlabel('gorjeta')
- #ax.set_xticks(X)
- #ax.set_yticks(X)
- plt.show()
- # plot através de superfície
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement