层次分析法c++实现代码
时间: 2023-08-06 21:13:38 浏览: 108
层次分析法的C++简单代码
5星 · 资源好评率100%
层次分析法(AHP)是一种决策分析方法,可以帮助人们在多个准则和多个选项之间做出相对好的选择。以下是一个简单的层次分析法的C++实现示例:
```
#include <iostream>
#include <vector>
using namespace std;
// 计算矩阵行的和
vector<double> row_sum(vector<vector<double>> matrix) {
vector<double> result;
int rows = matrix.size();
int cols = matrix[0].size();
for (int i = 0; i < rows; i++) {
double sum = 0;
for (int j = 0; j < cols; j++) {
sum += matrix[i][j];
}
result.push_back(sum);
}
return result;
}
// 计算矩阵列的和
vector<double> col_sum(vector<vector<double>> matrix) {
vector<double> result;
int rows = matrix.size();
int cols = matrix[0].size();
for (int i = 0; i < cols; i++) {
double sum = 0;
for (int j = 0; j < rows; j++) {
sum += matrix[j][i];
}
result.push_back(sum);
}
return result;
}
// 计算矩阵的乘积
vector<vector<double>> matrix_product(vector<vector<double>> matrix1, vector<vector<double>> matrix2) {
int rows1 = matrix1.size();
int cols1 = matrix1[0].size();
int rows2 = matrix2.size();
int cols2 = matrix2[0].size();
vector<vector<double>> result(rows1, vector<double>(cols2, 0));
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
// 计算矩阵的逆
vector<vector<double>> matrix_inverse(vector<vector<double>> matrix) {
int n = matrix.size();
vector<vector<double>> result(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
result[i][i] = 1;
}
for (int i = 0; i < n; i++) {
double max_val = matrix[i][i];
int max_index = i;
for (int j = i + 1; j < n; j++) {
if (matrix[j][i] > max_val) {
max_val = matrix[j][i];
max_index = j;
}
}
if (max_index != i) {
swap(matrix[i], matrix[max_index]);
swap(result[i], result[max_index]);
}
double pivot = matrix[i][i];
for (int j = 0; j < n; j++) {
matrix[i][j] /= pivot;
result[i][j] /= pivot;
}
for (int j = 0; j < n; j++) {
if (j != i) {
double factor = matrix[j][i];
for (int k = 0; k < n; k++) {
matrix[j][k] -= factor * matrix[i][k];
result[j][k] -= factor * result[i][k];
}
}
}
}
return result;
}
// 层次分析法
vector<double> ahp(vector<vector<double>> matrix) {
int n = matrix.size();
vector<double> row_sums = row_sum(matrix);
vector<vector<double>> normalized_matrix(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
normalized_matrix[i][j] = matrix[i][j] / row_sums[i];
}
}
vector<double> col_sums = col_sum(normalized_matrix);
vector<vector<double>> weighted_matrix(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
weighted_matrix[i][j] = normalized_matrix[i][j] / col_sums[j];
}
}
vector<double> priorities(n, 0);
vector<vector<double>> identity(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
identity[i][i] = 1;
}
vector<vector<double>> tmp = matrix_product(weighted_matrix, normalized_matrix);
vector<vector<double>> tmp2 = matrix_product(normalized_matrix, weighted_matrix);
double lambda = 0;
for (int i = 0; i < n; i++) {
lambda += tmp[i][i] - tmp2[i][i];
}
lambda /= (n - 1);
vector<vector<double>> tmp3 = matrix_product(weighted_matrix, weighted_matrix);
vector<vector<double>> tmp4 = matrix_product(normalized_matrix, normalized_matrix);
double ci = (lambda - n) / (n - 1);
double cr = ci / 0.58;
vector<double> result(n, 0);
if (cr < 0.1) {
result = matrix_product(matrix_inverse(identity - weighted_matrix), col_sums);
}
return result;
}
int main() {
vector<vector<double>> matrix = {
{1, 3, 5},
{1.0 / 3, 1, 2},
{1.0 / 5, 1.0 / 2, 1}
};
vector<double> result = ahp(matrix);
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
cout << endl;
return 0;
}
```
以上代码演示了如何实现一个简单的AHP方法。在这个例子中,我们将一个3x3的矩阵作为输入,并输出每个选项的权重。您可以根据需要对代码进行修改以满足您的需求。
阅读全文