评价类模型

AHP+模糊综合评价python

1、AHP层次分析法

import numpy as np  
  
# 辅助函数:计算一致性指标CI  
def consistency_index(n, lambda_max):  
    return (lambda_max - n) / (n - 1)  
  
# 辅助函数:查找随机一致性指标RI  
def random_index(n):    
    # 以下是一个常用的RI值表  
    RI_values = {  
        1: 0.00, 2: 0.00, 3: 0.58, 4: 0.90, 5: 1.12,  
        6: 1.24, 7: 1.32, 8: 1.41, 9: 1.45, 10: 1.49  
    }  
    return RI_values[n]  
  
# 辅助函数:计算一致性比例CR  
def consistency_ratio(CI, RI):  
    return CI / RI  
  
# 计算判断矩阵的特征向量和权重  
def compute_weights(A):  
    # 计算判断矩阵的特征值和特征向量  
    eigenvalues, eigenvectors = np.linalg.eig(A)  
    # 最大的特征值对应的特征向量即为权重向量  
    max_eigenvalue_index = np.argmax(eigenvalues)  
    weights = eigenvectors[:, max_eigenvalue_index].real  
    # 归一化权重向量  
    weights = weights / np.sum(weights)  
    return weights  
  
# 计算一致性检验  
def check_consistency(A, weights):  
    n = A.shape[0]  
    # 计算判断矩阵的最大特征值  
    lambda_max = np.dot(np.dot(A, weights.T), weights) / np.sum(weights**2)  
    # 计算一致性指标CI  
    CI = consistency_index(n, lambda_max)  
    # 查找随机一致性指标RI  
    RI = random_index(n)  
    # 计算一致性比例CR  
    CR = consistency_ratio(CI, RI)  
    return CR  
  
# 示例:一个3x3的判断矩阵  
A = np.array([[1, 2, 3], [1/2, 1, 2], [1/3, 1/2, 1]])  
  
# 计算权重  
weights = compute_weights(A)  
print("Weights:", weights)  
  
# 一致性检验  
CR = check_consistency(A, weights)  
print("Consistency Ratio (CR):", CR)  
if CR < 0.1:  
    print("矩阵通过了一致性检验")  
else:  
    print("矩阵没有通过一致性检验")

2、模糊综合评价

import numpy as np

# Function: Fuzzy AHP
def fuzzy_ahp_method(dataset):
    '''
    dataset: 专家判断矩阵,n*n*3,n表示指标数
    '''
    row_sum = []
    s_row   = []
    f_w     = []  #模糊权重
    d_w     = []  #去模糊权重
    # 一致性检测指标
    inc_rat  = np.array([0, 0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51, 1.48, 1.56, 1.57, 1.59])
    # 模糊判断矩阵转为清晰判断矩阵
    X       = [(item[0] + 4*item[1] + item[2])/6 for i in range(0, len(dataset)) for item in dataset[i]]  #三角数转为模糊数
    X       = np.asarray(X)  #列表转数组
    X       = np.reshape(X, (len(dataset), len(dataset)))  #转为n*n矩阵
    
    # S4. 计算每个准则的模糊比较值的几何平均值r_i(公式4)
    for i in range(0, len(dataset)):
        a, b, c = 1, 1, 1
        for j in range(0, len(dataset[i])):
            d, e, f = dataset[i][j]
            # 计算公式(4)的括号内部分: r_s = \prod_{j=1}^n d_ij
            a, b, c = a*d, b*e, c*f
        row_sum.append( (a, b, c) )
    L, M, U = 0, 0, 0
    for i in range(0, len(row_sum)):
        a, b, c = row_sum[i]
        # 计算公式(4)的括号外部分: s_r = (r_s)^(1/n)
        a, b, c = a**(1/len(dataset)), b**(1/len(dataset)), c**(1/len(dataset))
        s_row.append( ( a, b, c ) )
        # 计算公式(5)中⨁运算部分:R5 = r1⨁r2⨁...⨁rn
        L = L + a
        M = M + b
        U = U + c

    for i in range(0, len(s_row)):
        a, b, c = s_row[i]
        # 计算公式公式(5)中⨂运算部分:wi = ri⨂R5
        a, b, c = a*(U**-1), b*(M**-1), c*(L**-1)
        # 模糊权重
        f_w.append( ( a, b, c ) )
        # 计算公式(6):去模糊权重
        d_w.append( (a + b + c)/3 )
    # 计算公式(7): 归一化权重
    n_w      = [item/sum(d_w) for item in d_w]
    # 计算特征根向量
    vector   = np.sum(X*n_w, axis = 1)/n_w
    # 获得平均特征根
    lamb_max = np.mean(vector)
    # 计算一致性指标
    cons_ind = (lamb_max - X.shape[1])/(X.shape[1] - 1)
    # 一致性判断
    rc       = cons_ind/inc_rat[X.shape[1]]
    return f_w, d_w, n_w, rc


dataset = list([
    [ (  1,   1,   1), (  4,   5,   6), (  3,   4,   5), (  6,   7,   8) ],   #g1
    [ (1/6, 1/5, 1/4), (  1,   1,   1), (1/3, 1/2, 1/1), (  2,   3,   4) ],   #g2
    [ (1/5, 1/4, 1/3), (  1,   2,   3), (  1,   1,   1), (  2,   3,   4) ],   #g3
    [ (1/8, 1/7, 1/6), (1/4, 1/3, 1/2), (1/4, 1/3, 1/2), (  1,   1,   1) ]    #g4
    ])
fuzzy_weights, defuzzified_weights, normalized_weights, rc = fuzzy_ahp_method(dataset)
print('模糊权重')
for i in range(0, len(fuzzy_weights)):
    print('g'+str(i+1)+': ', np.around(fuzzy_weights[i], 3))
print('清晰权重')
for i in range(0, len(defuzzified_weights)):
    print('g'+str(i+1)+': ', round(defuzzified_weights[i], 3))
print('归一化权重')
for i in range(0, len(normalized_weights)):
    print('g'+str(i+1)+': ', round(normalized_weights[i], 3))
    
print('一致性判断')
print('RC: ' + str(round(rc, 2)))
if (rc > 0.10):
    print('The solution is inconsistent, the pairwise comparisons must be reviewed')
else:
    print('The solution is consistent')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值