matlab implements analytic hierarchy process

Steps of Analytic Hierarchy Process:

  1. List the target layer, criterion layer, and measure layer
  . 2. Complete the judgment matrix between two adjacent layers.
  3. Use AHP to check the consistency of each judgment matrix
  (record the AHP result Q as the weight, and at the same time, the two Record the CI at the time of the layer)
  4. All the results are tested by AHP using AHP1
  5. List the table and give the weight ranking table

The AHP code is as follows:

%本程序使用了特征值法,其余方法还有几何平均法、算数平均法
%最后可输出权值矩阵,多决策对一准则
function [Q]=AHP(B)
%Q 为权值,B 为对比矩阵
%导入判别矩阵 B
[n,m]=size(B);
%判别矩阵具有完全一致性
for i=1:n
    for j=1:n
        if B(i,j)*B(j,i)~=1
            fprintf('i=%d,j=%d,B(i,j)=%d,B(j,i)=%d\n',i,j,B(i,j),B(j,i))    
        end
    end
end
%求特征值特征向量,找到最大特征值对应的特征向量
[V,D]=eig(B); %特征值组成对角阵 DV 是特征向量()
max_eig_hang=max(D); %max_eig_hang 代表最大特征值所在行向量
max_eig_value=max(max_eig_hang); %max_eig_value 代表最大特征值
c1=find(D(1,:)==max(max_eig_hang));%c1 代表最大特征值对应的列向量编号
tzx=V(:,c1); %特征向量
%权
quan=zeros(n,1);
for i=1:n
    quan(i,1)=tzx(i,1)/sum(tzx);
end
Q=quan;
%一致性检验
CI=(max_eig_value-n)/(n-1)
RI=[0,0,0.58,0.9,1.12,1.24,1.32,1.41,1.45,1.49,1.52,1.54,1.56,1.58,1.59];%RI 的值是固定的有表可查
size(RI);
%判断是否通过一致性检验
CR=CI/RI(1,n);
if CR>=0.1
    fprintf('没有通过一致性检验\n');
else
    fprintf('通过一致性检验\n');
end
end

The AHP1 code is as follows:

 %A 代表 A (目标-决策)形成的权重,是对A层每次运行AHP后得到的结果,单次作为列向量
 %B 的列向量是 B 层(决策-方案)各个权重,同A
function AHP1(A,B,CI,n)
    %size(B)是n x 决策层个数
    %ending即为方案层相较于目标层的权重
    ending = ones(n,1);
    for i=1:n
        ending(i)=B(i,1)*A(1,1)+B(i,2)*A(2,1);
    end
    fprintf("结果权重为:");
    ending
    RI=[0,0,0.58,0.9,1.12,1.24,1.32,1.41,1.45,1.49,1.52,1.54,1.56,1.58,1.59];
    CR1 = 0;
    CR2 = 0;
    size(A)
    for i=1:size(A)
        CR1 = CR1 + CI(i)* A(i);
        CR2 = CR2 + RI(1,n)* A(i);
    end
    CR = CR1/CR2;
    if CR>=0.1
        fprintf('未通过一致性检验\n');
    else
        fprintf('通过一致性检验\n');
end

Guess you like

Origin blog.csdn.net/gls_nuaa/article/details/113127728