topsis

AHP层次分析法基础内容及python代码实现

2020-03-02  本文已影响0人  王一二_biu

概述

基本步骤

1.建立层次结构模型

2.构造成对比较矩阵

标度 含义
a_{ij}=1 i与同样重要性
a_{ij}=3 i比j稍微重要
a_{ij}=5 i比j明显重要
a_{ij}=7 i比j强烈重要
a_{ij}=9 i比j极端重要
2,4,6,8 上述两相邻判断的中值
倒数 A和B相比如果标度为3,则B和A相比就是1/3

3.一致性检验

4.权重的计算

5.层次总排序及决策

权重指标 y1 y2 y3
x1 0.4734 0.0819 0.2363 0.6817
x2 0.2624 0.5954 0.2764 0.1283
x3 0.0572 0.4286 0.4286 0.1429
x4 0.0989 0.6337 0.1919 0.1743
x5 0.1091 0.3047 0.1667 0.5286
总分 0.3148 0.2459 0.4393

其中,总分的计算为\sum_{j=1}^5 x_j y_{ij},其中x_i代表第j个指标的权重,y_{ij}代表第i个候选人的第j个指标的得分。

总结

使用层次分析法,

参考文献

https://wiki.mbalib.com/wiki/%E5%B1%82%E6%AC%A1%E5%88%86%E6%9E%90%E6%B3%95

附python代码

import numpy as np
'''
@Description:
    求解矩阵的权向量
@para:
    成对比较矩阵
@return:
    权向量
'''
def abhWeightVector(Mat):
    sizeMat = Mat.shape[0]
    #print(Mat)
    #print(sizeMat)

    # 计算矩阵A的特征值,特征向量
    eigenvalueMat, eigenvectorMat = np.linalg.eig(Mat)
    #print("特征值:", eigenvalueMat)
    #print("特征向量:", eigenvectorMat)
    
    # 将所有特征值取绝对值
    absEigenvalueMat = map(abs, eigenvalueMat)
    absEigenvalueMat = list(absEigenvalueMat)  
    #print(absEigenvalueMat)
    
    # 绝对值最大的特征值
    maxEigenvalueMat = max(absEigenvalueMat)
    #print("绝对值最大的特征值:", maxEigenvalueMat)
    
    # 绝对值最大的特征值的索引
    maxEigenvalueIndexMat = absEigenvalueMat.index(maxEigenvalueMat)
    #print(maxEigenvalueIndexMat)
    
    # 绝对值最大的特征值对应的特征向量
    maxEigenvectorMat = eigenvectorMat[:, maxEigenvalueIndexMat]
    #print("绝对值最大的特征值对应的特征向量:", maxEigenvectorMat)
    
    # 将上述特征向量标准化,即权向量   
    standardizeVectorMat = list(map(abs, maxEigenvectorMat)) / sum(list(map(abs, maxEigenvectorMat)))
    #print(standardizeVectorMat)
    
    # 计算不一致程度CI
    CI = (maxEigenvalueMat - sizeMat) / (sizeMat - 1)
    #print(CI)
    
    # 平均随机一致性指标RI
    listRI = [0, 0, 0.58, 0.90, 1.12, 1.24, 1.32, 1.41, 1.45]
    
    #计算随机一致性比率
    CR = CI / listRI[sizeMat - 1]
    #print(CR)
    
    return standardizeVectorMat
   

MatA = np.array([[1, 2, 7, 5 ,5],
                 [1/2, 1, 4, 3, 3],
                 [1/7, 1/4, 1, 1/2, 1/2],
                 [1/5, 1/3, 2, 1, 1],
                 [1/5, 1/3, 3, 1, 1]])
standardizeVectorMatA = abhWeightVector(MatA)
print(standardizeVectorMatA)

MatB1 = np.array([[1, 1/3, 1/8],
                  [3, 1, 1/3],
                  [8, 3, 1]])
standardizeVectorMatB1 = abhWeightVector(MatB1)
print(standardizeVectorMatB1)

MatB2 = np.array([[1, 2, 5],
                  [1/2, 1, 2],
                  [1/5, 1/2, 1]])
standardizeVectorMatB2 = abhWeightVector(MatB2)
print(standardizeVectorMatB2)

MatB3 = np.array([[1, 1, 3],
                  [1, 1, 3],
                  [1/3, 1/3, 1]])
standardizeVectorMatB3 = abhWeightVector(MatB3)
print(standardizeVectorMatB3)

MatB4 = np.array([[1, 3, 4],
                  [1/3, 1, 1],
                  [1/4, 1, 1]])
standardizeVectorMatB4 = abhWeightVector(MatB4)
print(standardizeVectorMatB4)

MatB5 = np.array([[1, 4, 1/4],
                  [1, 1, 1/4],
                  [4, 1, 1]])
standardizeVectorMatB5 = abhWeightVector(MatB5)
print(standardizeVectorMatB5)

MatB = np.array([standardizeVectorMatB1, 
                 standardizeVectorMatB2, 
                 standardizeVectorMatB3, 
                 standardizeVectorMatB4, 
                 standardizeVectorMatB5])
print(MatB)

sumY1 = 0
sumY2 = 0
sumY3 = 0
for i in range(0, MatA.shape[0]):
    sumY1 += standardizeVectorMatA[i] * MatB[i][0]
    sumY2 += standardizeVectorMatA[i] * MatB[i][1]
    sumY3 += standardizeVectorMatA[i] * MatB[i][2]
    
sumY = [sumY1, sumY2, sumY3]
print(sumY)
maxY = max(sumY)
theBestIndex = sumY.index(maxY)
print(theBestIndex)
上一篇 下一篇

猜你喜欢

热点阅读