层次分析法在matlab上的实现

版权声明:转载需写明出处 https://blog.csdn.net/qq_23860475/article/details/80660841

原理和计算步骤

点击打开链接

点击打开链接

matlab代码

%%目标层A,准则层B1、B2、B3、B4、B5、B6,方案层C1、C2、C3
clc,clear
fid=fopen('C:\Users\Administrator\Desktop\txt3.txt','r');%文本数据文件存放路径
n1=6;%准则层因素个数
n2=3;%方案层方案个数
a=[];
for i=1:n1
    tmp=str2num(fgetl(fid));
    a=[a;tmp]; %读准则层对目标层的判断矩阵a
end
for i=1:n1
    str1=char(['b',int2str(i),'=[];']);
    str2=char(['b',int2str(i),'=[b',int2str(i),';tmp];']);
    eval(str1);
    for j=1:n2
        tmp=str2num(fgetl(fid));
        eval(str2); %读方案层对准则层的判断矩阵bi
    end
end
ri=[0,0,0.58,0.90,1.12,1.24,1.32,1.41,1.45]; %一致性指标
[x,y]=eig(a);
lamda=max(diag(y));
num=find(diag(y)==lamda);
w0=x(:,num)/sum(x(:,num));%准则层对目标层的权重
cr0=(lamda-n1)/(n1-1)/ri(n1);%准则层判断矩阵的一致性检验,调整至小于0.1
for i=1:n1
    [x,y]=eig(eval(char(['b',int2str(i)])));
    lamda=max(diag(y));
    num=find(diag(y)==lamda);
    w1(:,i)=x(:,num)/sum(x(:,num));%各方案对准则层的权重
    cr1(i)=(lamda-n2)/(n2-1)/ri(n2);%方案层判断矩阵的一致性检验,调整至小于0.1
end
w=w1*w0;
disp('各方案对目标层的综合权重:')
disp(w)

上面代码是只有目标层、准则层、方案层,下面的代码多了子准则层,并且准则层对应不同的子准则层

%%目标层A,准则层B1、B2、B3,子准则层C1、C2、C3、C4、C5、C6、C7、C8、C9,方案层D1、D2、D3、D4、D5
clc,clear
fid=fopen('C:\Users\Administrator\Desktop\txt4.txt','r');%文本数据文件存放路径
n1=3;%方案层个数
n2=9;%子准则层个数
n3=5;%准则层个数
a=[];
w1=zeros(n2,n1);
for i=1:n1
    tmp=str2num(fgetl(fid));
    a=[a;tmp]; %读准则层判断矩阵a
end
for i=1:n1
    str1=char(['b',int2str(i),'=[];']);
    str2=char(['b',int2str(i),'=[b',int2str(i),';tmp];']);
    eval(str1);
    if i==1
        for j=1:2%经济效益对应子准则层1-2项,可根据实际情况调整
            tmp=str2num(fgetl(fid));
            eval(str2); %读子准则层的判断矩阵
        end
    elseif i==2
        for j=3:6%技术性能对应子准则层3-6项,可根据实际情况调整
            tmp=str2num(fgetl(fid));
            eval(str2); %读子准则层的判断矩阵
        end 
     else
        for j=7:n2%施工工艺对应子准则层7-9项,可根据实际情况调整
            tmp=str2num(fgetl(fid));
            eval(str2); %读子准则层的判断矩阵
        end 
    end
end
for i=1:n2
    str1=char(['c',int2str(i),'=[];']);
    str2=char(['c',int2str(i),'=[c',int2str(i),';tmp];']);
    eval(str1);
    for j=1:n3
        tmp=str2num(fgetl(fid));
        eval(str2); %读方案层的判断矩阵
    end
end
ri=[0,0,0.58,0.90,1.12,1.24,1.32,1.41,1.45]; %一致性指标
[x,y]=eig(a);
lamda=max(diag(y));
num=find(diag(y)==lamda);
w0=x(:,num)/sum(x(:,num));%准则层对目标层的权重
cr0=(lamda-n1)/(n1-1)/ri(n1)%准则层对目标层判断矩阵的一致性检验,调整至小于0.1
for i=1:n1
    [x,y]=eig(eval(char(['b',int2str(i)])));
    lamda=max(diag(y));
    num=find(diag(y)==lamda);
    W=x(:,num)/sum(x(:,num));
    [r1,cc]=size(W);
    if i==1
        for i1=1:r1
            w1(i1,i)=W(i1);
            cr1(i)=(lamda-2)/(2-1)/ri(2);
        end
    elseif i==2
        for i1=1:r1
            w1(i1+2,i)=W(i1);
            cr1(i)=(lamda-4)/(4-1)/ri(4);
        end
    else
       for i1=1:r1
            w1(i1+6,i)=W(i1);%子准则层对准则层权重
            cr1(i)=(lamda-3)/(3-1)/ri(3);%子准则层对准则层判断矩阵的一致性检验,调整至小于0.1
        end 
    end
end
for i=1:n2
    [x,y]=eig(eval(char(['c',int2str(i)])));
    lamda=max(diag(y));
    num=find(diag(y)==lamda);
    w2(:,i)=x(:,num)/sum(x(:,num));%方案层对子准则层的权重
    cr2(i)=(lamda-n3)/(n3-1)/ri(n3);%方案层对子准则层判断矩阵的一致性检验,调整至小于0.1
end
w=w2*w1*w0;
disp('各方案对目标层的综合权重:')
disp(w)

文本数据格式

txt3.txt

1 1 1 4 1 1/2
1 1 2 4 1 1/2
1 1/2 1 5 3 1/2
1/4 1/4 1/5 1 1/3 1/3
1 1 1/3 3 1 1
2 2 2 3 3 1
1 1/4 1/2
4 1 3
2 1/3 1
1 1/4 1/5
4 1 1/2
5 2 1
1 3 1/3
1/3 1 1/7
3 7 1
1 1/3 5
3 1 7
1/5 1/7 1
1 1 7
1 1 7
1/7 1/7 1
1 7 9
1/7 1 1

1/9 1 1

txt4.txt

1 1/3 3
3 1 5
1/3 1/5 1
1 3
1/3 1
1 1/2 1/4 1/5
2 1 1/5 1/6
4 5 1 2
5 6 1/2 1
1 3 5
1/3 1 3
1/5 1/3 1
1 1/5 1/2 1/9 1/3
5 1 3 1/2 2
2 1/3 1 1/6 1/2
9 2 6 1 4
3 1/2 2 1/4 1
1 5 3 7 3
1/5 1 1/3 3 1/2
1/3 3 1 5 1
1/7 1/3 1/5 1 1/5
1/3 2 1 5 1
1 5 1 9 3
1/5 1 1/2 5 1/3
1 2 1 9 3
1/9 1/5 1/9 1 1/9
1/3 3 1/3 9 1
1 1 1 9 3
1 1 1 9 3
1 1 1 9 3
1/9 1/9 1/9 1 1/7
1/3 1/3 1/3 7 1
1 3 1 9 1
1/3 1 1/3 7 1/3
1 3 1 9 1
1/9 1/7 1/9 1 1/9
1 3 1 9 1
1 2 1 5 1
1/2 1 1/2 4 1/2
1 2 1 5 1
1/5 1/4 1/5 1 1/5
1 2 1 5 1
1 2 1 3 3
1/2 1 1/2 2 2
1 2 1 3 3
1/3 1/2 1/3 1 1
1/3 1/2 1/3 1 1
1 1/2 1 1/2 3
2 1 1 1/2 3
1 1 1 1/2 3
2 2 2 1 5
1/3 1/3 1/3 1/5 1
1 1/3 1/3 1/4 1/5
3 1 1 1/2 1/3
3 1 1 1/2 1/3
4 2 2 1 1/2
5 3 3 2 1

备注:承接模型、算法代码实现(支持python、matlab),有意请联系QQ947943645 ,非诚勿扰! 

猜你喜欢

转载自blog.csdn.net/qq_23860475/article/details/80660841
今日推荐