30 lines
1.3 KiB
Python
30 lines
1.3 KiB
Python
import numpy as np
|
||
|
||
|
||
def three_dimensional_TOPSIS(chromosome_results, data=None):
|
||
# 如果data不为空,则堆叠数据
|
||
if data is not None:
|
||
chromosome_results = np.vstack((chromosome_results, data))
|
||
|
||
# 获取每个特征的范数
|
||
norms = np.linalg.norm(chromosome_results, axis=0)
|
||
|
||
# 检查是否有零值并替换它们,以避免除以零
|
||
norms[norms == 0] = 1.0 # 或使用一个非常小的数来替换零
|
||
|
||
# 归一化处理
|
||
normalized_data = chromosome_results / norms
|
||
|
||
# 计算负理想解 (这里我们选择最小值,你也可以根据需求选择其他标准)
|
||
negative_ideal_solution = np.amin(normalized_data, axis=0)
|
||
dist_to_pis_dimension1 = (0.6*normalized_data[:, 0] - 0.6*negative_ideal_solution[0]) ** 2
|
||
dist_to_pis_dimension2 = (0.3*normalized_data[:, 1] - 0.3*negative_ideal_solution[1]) ** 2
|
||
dist_to_pis_dimension3 = (0.1*normalized_data[:, 2] - 0.1*negative_ideal_solution[2]) ** 2
|
||
dist_to_nis = np.sqrt(dist_to_pis_dimension1+dist_to_pis_dimension2+dist_to_pis_dimension3)
|
||
# dist_to_nis = np.linalg.norm(normalized_data - negative_ideal_solution, axis=1)
|
||
# 如果data不为空,则删除最后一行
|
||
if data is not None:
|
||
dist_to_nis = dist_to_nis[:-1]
|
||
|
||
return dist_to_nis
|