文獻復現——A New Geometric Mean FMEA Method Based on Information Quality

Ai_Math發表於2020-11-06

實現了作者的模型,並且新增了簡單的資料預處理:

#作者提出了一個新的FMEA方法,主要是對原有的RPN計算方法改進
#1、構建模糊評價矩陣,如果有 N個失效模式,那麼就構建N×3的模糊評價矩陣
#N行有N個失效模式FM,3列3個不同的指標,S O D
#模糊評價矩陣的元素為(a,b,c,....)是專家對該指標的滿意度評價
#2、計算模糊矩陣中每個元素的廣義資訊質量,生成廣義資訊質量矩陣
#3、對廣義資訊質量矩陣進行修正(qu_matrix/max(qu_matrix)),得到幾何平均權重矩陣(0.3365,0.4158,0.2475)

#4、通過幾何平均權重矩陣計算每個效模式的RPN
import numpy as np
import pandas as pd
import math
#1、根據專家意見構建模糊評價矩陣
# fuzzy_list=[[(0.8193,0.0771,0.1033),(0.0545,0.3105,0.6346),(0.2191,0.4894,0.2914)],
#             [(0.7224,0.1373,0.1399),(0.0545,0.3104,0.6346),(0.8250,0.0776,0.1040)],
#             [(0.8709,0.1039,0.0253),(0.0233,0.0722,0.2042),(0.2191,0.4892,0.2914)],
#             [(0.3669,0.4475,0.1854),(0.0233,0.0722,0.9043) ,(0.0545,0.3104,0.6346)],
#             [(0.3669,0.4475,0.1854) ,(0.1504,0.4446,0.4045) ,(0.7546,0.1373,0.1075)],
#             [(0.3669,0.4475,0.1854) ,(0.1504,0.4446,0.4045) ,(0.7546,0.1373,0.1075)],
#             [(0.7227,0.1374,0.1400) ,(0.1504,0.4446,0.4045) ,(0.0545,0.3104,0.6346)],
#             [(0.8709,0.1039,0.0253) ,(0.0233,0.0722,0.9042),(0.2191,0.4894,0.2914)],
#             [(0.3669,0.4475,0.1854) ,(0.1504,0.4446,0.4045) ,(0.7546,0.1373,0.1075)],
#             [(0.7966,0.1070,0.1135) ,(0.3092,0.1103,0.5671),(0.3365,0.4158,0.2475)]]
# fuzzy_matrix=np.array(fuzzy_list);#轉換為矩陣
#x,y,z=fuzzy_matrix.shape;
#資料預處理
def checkdata(fuzzy_matrix):
    x,y,z=fuzzy_matrix.shape;
    m=0
    for i in range(0,x):
        for j in range(0,y):
            sum=0
            for k in range(0,z):
                sum=sum+fuzzy_matrix[i,j,k]
            if(round(sum,0)!=1):
                m=m+1
                print("(",i+1,",",j+1,")")
                for k_ in range(0,z):
                    fuzzy_matrix[i,j,k_]=fuzzy_matrix[i,j,k_]/sum
    print("保留0位小數後,一共有{0}個資料不符合條件,現在已經修改".format(m))
    return fuzzy_matrix;
#2、計算廣義資訊質量
#2.0向量得自身長度得平方,z是大小
def selfDis2(pi,z):
    sum=0.0
    for i in range(0,z):
        sum=sum+pi[i]*pi[i]
    return sum;


#2.1 計算pi與pj之間的距離:元組
def distance(p_i,p_j):
    #轉換為行向量
    vector_i=np.array(p_i);
    vector_j = np.array(p_j);
    #計算差值:
    vector_dif_T=vector_i-vector_j;#這是列項量
    line=vector_dif_T.shape[0];#獲取行
    vector_dif_T=vector_dif_T.reshape(line,1);#重新塑性,否則錯誤
    vector_dif_line=vector_dif_T.reshape(1,line);#轉置
    d=np.dot(vector_dif_line,vector_dif_T);#矩陣相乘
    d=d[0,0]
    d=pow(d,1/2);
    return round(d,4);
#2.2 計算相似度
def sim(p_i,p_j):
    return 1-distance(p_i,p_j);
#2.3構建每一個指標的sim矩陣,距離採用全部的
#返回矩陣
def simArray(fmatrix):
    x,y,z=fmatrix.shape;
    p=1
    simMatrix=[]
    for i in range(0,x):
        tmp = []
        for j in range(0,y):
                pi=fmatrix[i,j,:];
                sum=0.0
                for i_ in range(0,x):
                    for j_ in range(0,y):
                        if(i==i_ and j==j_):
                            continue;
                        else:
                             pj=fmatrix[i_,j_,:];
                             sum=sum+sim(pi,pj);
                tmp.append(round(sum,4))
        simMatrix.append(tmp);
    simM=np.array(simMatrix)
    return simM

#2.4構建信用矩陣crdi
def crdi(fmatrix):
    x,y,z=fmatrix.shape;
    sup=simArray(fmatrix);
    sum=0.0
    for i in range(0,x):
        for j in range(0,y):
            sum=sum+sup[i,j]
    for i in range(0,x):
        for j in range(0,y):
            sup[i,j]=sup[i,j]/sum
    return sup;
#2.5構建廣義資訊質量矩陣
def qu(fmatrix):
    crdiMatrix_=crdi(fmatrix);#信任矩陣
    print(type(crdiMatrix_))
    x,y,z=fmatrix.shape;
    list_line=[]
    for i in range(0,x):
        list_column=[]
        for j in range(0,y):
            pi=fmatrix[i,j,:];#pi
            doublePi=selfDis2(pi,z);
            exp=crdiMatrix_[i,j]
            expCrdi=pow(math.e,exp)
            sum=expCrdi*doublePi;
            list_column.append(sum)
        list_line.append(list_column)
    quMatrix=np.array(list_line)
    # print("廣義資訊質量矩陣")
    # print(quMatrix)
    return quMatrix
#對廣義資訊質量矩陣進行修正,得到幾何平均權重矩陣
def weightMatrix(fmatrix):
    quMatrix=qu(fmatrix);#廣義矩陣
    maxValue=quMatrix.max();
    x,y=quMatrix.shape;
    for i in range(0,x):
        for j in range(0,y):
            quMatrix[i,j]=quMatrix[i,j]/maxValue;
    #print("Geometric mean Matrix")
    #print(quMatrix)
    return quMatrix
#利用幾何均值法生成新的RPN矩陣
def geometricMean(fmatrix):
    fmatrix = checkdata(fmatrix);#資料預處理
    geoMatrix=weightMatrix(fmatrix);#生成geometric mean metric
    x,y,z=fmatrix.shape;
    finalResult=[];#最終的結果矩陣,預設大小為3*10,最後需要轉置
    for q in range(0,z):
        #q代表good、average、poor
        tmp_q=[];#臨時矩陣
        for i in range(0,x):
           rqn=0.0;
           w_line=0.0;#在權重矩陣中計算每一行的權重之和
           m_line = 1;  # 作為連乘的結果
           for j in range(0,y):
               w_line=w_line+geoMatrix[i,j];
               m_line=m_line*pow(fmatrix[i,j,q],geoMatrix[i,j]);
           w_line = 1 / w_line;  # 作為指數
           rqn=pow(m_line,w_line);
           tmp_q.append(rqn);
        finalResult.append(tmp_q);
    finalR=np.array(finalResult);#轉為矩陣
    x,y=finalR.shape
    #轉置,不要用reshape,reshape是按照順序以及設定的x,y,一行一行的進行切割,不是真正意義上的轉置
    finalResult=[]
    for j in range(0,y):
        tmp = []
        for i in range(0,x):
            tmp.append(finalR[i,j]);
        finalResult.append(tmp);
    return np.array(finalResult);
#生成等級RNK等級
def rankMake(fmatrix):
    geoMetrix=geometricMean(fmatrix);
    print("平均幾何均值矩陣 Geometric Mean Matrix")
    print(geoMetrix)
    x,y=geoMetrix.shape;#權重值未0.1 0.3 0.6
    weight=[0.1,0.3,0.6];
    results=[]
    for i in range(0,x):
        str_="A"+str(i+1)
        tmp=[]
        sum=0.0
        tmp.append(str_)
        for j in range(0,y):
            sum=sum+geoMetrix[i,j]*weight[j]
        tmp.append(sum);
        results.append(tmp)
    results=np.array(results)#這就是新合成的RPN
    # print("新合成的RPN")
    # print(results)
    #應該加上rank等級這一列
    results=results[results[:,1].argsort()]#根據每一列的值進行排序,調整行的順序
    x, y = results.shape;
    finals=[]
    for i in range(0,x):
        tmp=[]
        for j in range(0,y):
            tmp.append(results[i,j])
        tmp.append(10-i)
        finals.append(tmp)
    results=np.array(finals)
    results=results[results[:,0].argsort()]
    print(results)
    return results;
if __name__ == '__main__':
    #fuzzy_list=[[(0.8193,0.0771,0.1033),(0.0545,0.3105,0.6346),(0.2191,0.4894,0.2914)]]
    #(good,average,poor)
    fuzzy_list=[[(0,0,1),(0,0,1),(0,0,1)],
                [(0,0,1),(0,0,1),(0,0,1)],
                [(0,0,1),(0,0,1),(0,0,1)]]
    fuzzy_matrix = np.array(fuzzy_list);
    # weightMatrix(fuzzy_matrix)
    #print(geometricMean(fuzzy_matrix))
    #simArray(fuzzy_matrix)
    # crdi(fuzzy_matrix)
    rankMake(fuzzy_matrix)

相關文章