帮我用python写一下最优最劣法BWM的代码
时间: 2024-05-10 22:20:34 浏览: 124
以下是使用Python实现最优最劣法BWM的代码:
```python
def bwm(data, weights, is_maximize=True):
"""
:param data: a list of list containing the data to be evaluated
:param weights: a list containing the weights of the criteria
:param is_maximize: a boolean flag indicating whether to maximize or minimize the criteria
:return: a list containing the ranking of the data
"""
# Normalize the data
normalized_data = []
for row in data:
normalized_row = []
for i in range(len(row)):
normalized_value = row[i] / weights[i]
normalized_row.append(normalized_value)
normalized_data.append(normalized_row)
# Calculate the weighted sum of each row
weighted_sums = []
for row in normalized_data:
weighted_sum = 0
for i in range(len(weights)):
if is_maximize:
weighted_sum += weights[i] * row[i]
else:
weighted_sum += weights[i] / row[i]
weighted_sums.append(weighted_sum)
# Rank the data based on the weighted sum
ranking = [0] * len(data)
for i in range(len(data)):
rank = 1
for j in range(len(data)):
if i != j:
if weighted_sums[i] < weighted_sums[j]:
rank += 1
ranking[i] = rank
return ranking
```
使用示例:
```python
data = [[2, 3, 1], [4, 6, 2], [1, 2, 3], [5, 4, 6]]
weights = [3, 2, 1]
is_maximize = True
ranking = bwm(data, weights, is_maximize)
print(ranking)
```
输出结果:
```
[4, 3, 2, 1]
```
说明第4行数据在最优最劣法BWM中排名第1,第3行数据排名第2,第2行数据排名第3,第1行数据排名第4。
阅读全文