topsis熵权法伪代码
时间: 2024-10-19 13:10:36 浏览: 71
Topsis (Technique for Order Preference by Similarity to Ideal Solution) 熵权法是一种决策分析工具,它结合了模糊集理论和灰色关联度评价方法。以下是使用熵权法进行TOPSIS算法的一个简单伪代码:
```python
// 定义函数
def calculateEntropy(data, column):
total = sum(data)
prob = [d / total for d in data]
entropy = -sum([p * math.log(p, 2) for p in prob])
return entropy
def normalizeData(data):
min_val = min(data)
max_val = max(data)
return [(d - min_val) / (max_val - min_val) for d in data]
def entropyWeighting(matrix, criteria):
# 对每个评价指标计算信息熵
entropies = [calculateEntropy(column, matrix[i]) for i, column in enumerate(matrix.T)]
# 计算标准差作为熵权
stds = [math.sqrt(sum([(ei - ei_mean)**2 for ei in row])) for ei_mean, row in zip(entropies, matrix.T)]
weights = [1 / e if s > 0 else 0 for e, s in zip(entropies, stds)] # 如果标准差为0,则赋予最大权重
# 归一化权重
weight_sum = sum(weights)
normalized_weights = [w / weight_sum for w in weights]
return normalized_weights
def topsisEntropy(matrix, ideal_solution, anti_ideal_solution, weights):
normalized_matrix = [[row_i * w for row_i, w in zip(row, weights)] for row in matrix]
# 计算距离向量
distance_to_ideal = [sum((vector - ideal_solution) ** 2) for vector in normalized_matrix]
distance_to_anti_ideal = [sum((vector - anti_ideal_solution) ** 2) for vector in normalized_matrix]
# 最优解和最劣解排序
best_alternatives = sorted(range(len(distance_to_ideal)), key=lambda x: distance_to_ideal[x], reverse=True)
worst_alternatives = sorted(range(len(distance_to_ideal)), key=lambda x: distance_to_anti_ideal[x])
# 返回最佳和次佳方案
return best_alternatives[0], best_alternatives[1]
// 调用函数并处理数据
matrix_data = ... // 评估矩阵
criteria = ... // 权重向量或准则列
ideal_solution = ... // 理想解向量
anti_ideal_solution = ... // 反理想解向量
weights = entropyWeighting(matrix_data, criteria)
best, second_best = topsisEntropy(matrix_data, ideal_solution, anti_ideal_solution, weights)
阅读全文