Allenfenqu/final_partiton_topsis.py

71 lines
2.5 KiB
Python

import numpy as np
def topsis_scores(data, weights, beneficial):
# 归一化决策矩阵
norm_data = data / np.linalg.norm(data, axis=0)
# 计算权重后的归一化决策矩阵
weighted_data = norm_data * weights
# 确定正负理想解
ideal_best = np.where(beneficial, np.max(weighted_data, axis=0), np.min(weighted_data, axis=0))
ideal_worst = np.where(beneficial, np.min(weighted_data, axis=0), np.max(weighted_data, axis=0))
# 计算到正负理想解的欧氏距离
dist_pos_ideal = np.linalg.norm(weighted_data - ideal_best, axis=1)
dist_neg_ideal = np.linalg.norm(weighted_data - ideal_worst, axis=1)
# 计算接近度
similarity = dist_neg_ideal / (dist_pos_ideal + dist_neg_ideal)
# 返回每个备选方案的接近度值
return similarity
data = np.array([
[161.6950962, 1862.511111, 89.44444444],
[153.969874, 1991.972222, 105.9047619],
[149.3103732, 2117.583333, 298.0833333],
[149.1761437, 1895.887302, 252.3703703],
[149.8792445, 1828.444445, 405.2],
[147.7267296, 1903.372222, 336.6666667],
[148.336893, 1881.8, 337.5],
[147.5684715, 2049.088889, 385.948718],
[144.7657088, 2072.377778, 504.3809524],
[143.2383359, 2083.472222, 516.1333333],
[140.1784555, 2042.816667, 637.1666667]
])
# data = np.array([
# [970.1705774, 1862.511111, 89.44444444],
# [1077.789118, 1991.972222, 105.9047619],
# [1194.482986, 2117.583333, 298.0833333],
# [1342.585293, 1895.887302, 252.3703703],
# [1498.792445, 1828.444445, 405.2],
# [1624.994025, 1903.372222, 336.6666667],
# [1780.042716, 1881.8, 337.5],
# [1918.390129, 2049.088889, 385.948718],
# [2026.719924, 2072.377778, 504.3809524],
# [2148.575039, 2083.472222, 516.1333333],
# [2242.855288, 2042.816667, 637.1666667]
# ])
data = np.array([
[156.3679873, 2191.222727, 429.3333333],
[151.5332934, 2138.160606, 364.8051948],
[146.6145815, 2131.957576, 610.1590909],
[146.7999613, 2099.460173, 470.2626262],
[144.2009152, 2019.037987, 653.5818182],
[144.1279229, 2030.688528, 590.9090909],
[144.3696568, 2061.98052, 552.7727273],
[142.3387265, 2066.03355, 636.8951049],
[139.4676336, 2044.895671, 743.9350649],
[139.3547153, 2023.510498, 769.4181818],
[139.3603277, 2017.13355, 771.3977273]
])
weights = np.array([0.5, 0.3, 0.2])
beneficial = [False, False, False] # 所有准则的值都越小越好
scores = topsis_scores(data, weights, beneficial)
print(scores)