模糊层次分析法的python代码
时间: 2023-09-08 21:04:42 浏览: 510
### 回答1:
我可以为你提供以下Python代码来实现模糊层次分析法:# 载入库
import numpy as np# 设置参数
x = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])# 进行模糊层次分析
def fuzzy_hierarchical_analysis(x, alpha=0.5):
"""
模糊层次分析法
:param x: 矩阵
:param alpha: 权重参数
:return: 权重矩阵
"""
n_row, n_col = x.shape
# 初始化权重
w = np.ones((n_row, n_col))
# 计算权重
for i in range(n_row):
for j in range(n_col):
w[i,j] = w[i,j] * (x[i,j]**alpha)
# 返回权重
return w
### 回答2:
模糊层次分析法(Fuzzy Analytic Hierarchy Process)是一种用于多准则决策的决策分析方法。它能够处理主观的、模糊的和不确定的数据,并基于这些数据进行决策。
以下是一个使用Python实现模糊层次分析法的代码示例:
```python
import numpy as np
import math
# 定义模糊层次分析法类
class FuzzyAHP:
def __init__(self, criteria, alternatives):
self.criteria = criteria
self.alternatives = alternatives
self.c_matrix = np.zeros((len(criteria), len(criteria)))
self.a_matrices = []
self.weights = []
# 生成模糊矩阵
def generate_fuzzy_matrix(self, n):
matrix = np.zeros((n, n))
for i in range(n):
for j in range(i, n):
if i == j:
matrix[i][j] = 1
else:
value = float(input(f"请输入第{i+1}个准则和第{j+1}个准则的相对重要性(0-9之间):"))
matrix[i][j] = value
matrix[j][i] = 1/value
return matrix
# 对模糊矩阵归一化
def normalize_matrix(self, matrix):
normalized_matrix = np.zeros(matrix.shape)
for i in range(matrix.shape[0]):
s = np.sum(matrix[i])
for j in range(matrix.shape[1]):
normalized_matrix[i][j] = matrix[i][j] / s
return normalized_matrix
# 计算准则权重
def calculate_weights(self):
criteria_matrix = self.generate_fuzzy_matrix(len(self.criteria))
self.c_matrix = self.normalize_matrix(criteria_matrix)
for i in range(len(self.alternatives)):
alternative_matrix = self.generate_fuzzy_matrix(len(self.criteria))
self.a_matrices.append(self.normalize_matrix(alternative_matrix))
self.weights = np.zeros((len(self.alternatives), len(self.criteria)))
for i in range(len(self.criteria)):
for j in range(len(self.alternatives)):
for k in range(len(self.criteria)):
self.weights[j][i] += self.a_matrices[j][k][i] * self.c_matrix[i][k]
self.weights = self.normalize_matrix(self.weights)
# 打印准则权重和备选方案的权重
def print_weights(self):
print("准则权重:")
for i in range(len(self.criteria)):
print(f"{self.criteria[i]}: {self.weights[i]}")
print("\n备选方案的权重:")
for i in range(len(self.alternatives)):
print(f"{self.alternatives[i]}: {self.weights[:, i]}")
# 测试代码
criteria = ["价格", "品质", "功能"]
alternatives = ["产品A", "产品B", "产品C", "产品D"]
fuzzy_ahp = FuzzyAHP(criteria, alternatives)
fuzzy_ahp.calculate_weights()
fuzzy_ahp.print_weights()
```
这段代码实现了模糊层次分析法的关键步骤,包括生成模糊矩阵、归一化矩阵、计算准则权重和备选方案的权重。用户可以输入相对重要性的评分,然后根据输入的评分计算权重,并打印结果。
### 回答3:
模糊层次分析法(Fuzzy Analytic Hierarchy Process,FAHP)是一种用于多准则决策的方法,它将模糊逻辑和层次分析法相结合。下面是一个使用Python实现模糊层次分析法的简单示例代码:
```python
import numpy as np
class FAHP:
def __init__(self, criteria, alternatives):
self.criteria = criteria
self.alternatives = alternatives
def normalize_matrix(self, matrix):
normalized_matrix = np.zeros(matrix.shape)
for i in range(matrix.shape[0]):
sum_row = sum(matrix[i])
for j in range(matrix.shape[1]):
normalized_matrix[i][j] = matrix[i][j] / sum_row
return normalized_matrix
def calculate_weights(self, matrix):
weights = np.zeros(matrix.shape[0])
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
weights[i] += matrix[j][i]
weights[i] /= matrix.shape[1]
return weights
def calculate_fuzzy_weights(self, matrix):
fuzzy_weights = np.zeros(matrix.shape[1])
for i in range(matrix.shape[1]):
fuzzy_weights[i] = max(matrix[:,i])
return fuzzy_weights
def calculate_global_weights(self, criteria_weights, alternative_weights):
global_weights = np.zeros(len(self.alternatives))
for i in range(len(self.alternatives)):
for j in range(len(criteria_weights)):
global_weights[i] += criteria_weights[j] * alternative_weights[i][j]
return global_weights
def fuzzy_analytic_hierarchy_process(self):
criteria_matrix = np.array(self.criteria)
alternatives_matrix = np.array(self.alternatives)
# 归一化准则矩阵
normalized_criteria_matrix = self.normalize_matrix(criteria_matrix)
# 计算准则权重
criteria_weights = self.calculate_weights(normalized_criteria_matrix)
# 归一化替代方案矩阵
normalized_alternatives_matrix = self.normalize_matrix(alternatives_matrix)
# 计算替代方案模糊权重
alternatives_fuzzy_weights = self.calculate_fuzzy_weights(normalized_alternatives_matrix)
# 计算全局权重
global_weights = self.calculate_global_weights(criteria_weights, alternatives_fuzzy_weights)
return global_weights
# 示例用法
criteria = [[1, 3, 5],
[1/3, 1, 2],
[1/5, 1/2, 1]]
alternatives = [[1, 5, 3],
[1/5, 1, 1/3],
[1/3, 3, 1]]
fahp = FAHP(criteria, alternatives)
global_weights = fahp.fuzzy_analytic_hierarchy_process()
print("Global weights:", global_weights)
```
这段代码的功能是实现模糊层次分析法,输入包括准则矩阵和替代方案矩阵,输出为每个替代方案的全局权重。首先对准则矩阵和替代方案矩阵进行归一化处理,然后计算准则权重、替代方案模糊权重和全局权重。最后打印出全局权重。
阅读全文