[Mathematical modeling learning] Matlab realizes the evaluation model - Analytic Hierarchy Process (AHP)

Table of contents

1 Overview 

2 Algorithm implementation process

3 instances

 4matlab realizes the analytic hierarchy process

5 calculation results


1 Overview 

Analytical Hierarchy Process, referred to as AHP, is an algorithm in the evaluation model, which refers to a decision-making method that decomposes elements related to decision-making into goals, criteria, and programs, and conducts qualitative and quantitative analysis on this basis. The defect of AHP is that the judgment matrix is ​​determined subjectively, and an objective evaluation cannot be obtained, and the number of indicators should not be too large (not greater than 15). This article will demonstrate the use of AHP to realize the selection of tourist cities.

Matlab is the mainstream of modeling, followed by python, R, etc. Matlab is more convenient than python because it comes with many functions, while python needs to download the required libraries by itself. However, the defect of matlab is that the software is too large and the startup speed is slow. But as we all know, in addition to being unable to have children, matlab has other functions. Let's take a look at how to use matlab to implement the AHP.

2 Algorithm implementation process

First of all, it is necessary to determine the evaluation system, which is divided into target layer, criterion layer, and program layer, and then determine the evaluation indicators and the judgment matrix of the program according to the degree of importance (refer to the following table), and use the arithmetic mean method, geometric mean method, and eigenvalue method respectively. Solve the indicator weight (importance) and the score of each scheme separately. The weights and scores calculated by the three methods are averaged, which makes the competition paper more complete than only using the eigenvalue method to calculate the weights and scores.

Scaling meaning
1 equally important
3 slightly important
5 obviously important
7 strongly important
9 extremely important
2,4,6,8 The median value of the above two adjacent judgments
reciprocal A is 5 compared to B, and B is 1/5 compared to A

3 instances

Please select the appropriate indicator and choose a tourist city that suits Xiao Ming the most.

This question requires us to choose the best tourist city. First, we collect information on the Internet to determine the most important indicators in my subjective opinion, namely, cost, scenery, transportation, and select the cities we want to go to, namely: Wuhan, Xiamen, Nanchang . Subjectively determine the judgment matrix and check the consistency to judge whether the matrix is ​​consistent. Use the arithmetic average method, geometric average method, and eigenvalue method to calculate the index weight and the scores of each city. Finally, according to the weight of indicators and the scores of each city in each indicator, scoring is carried out.

Step1: Establish indicator judgment matrix

Determine the weight of xy according to your own judgment on these indicators. For example, the relative weight of scenery-expense (importance of scenery/importance of expense) is 1/2, then the relative weight of expense-view (importance of expense/importance of expense) Scenery importance) is 2

spend scenery transportation
spend 1 2 1/3
scenery 1/2 1 1/5
transportation 3 5 1

According to the above table, it is observed that the two weights of the diagonal position are multiplied to 1. This is because the relative cost of the scenery is 1/2, and in turn, the relative cost of the scenery is 2.

Step2: Establish city judgment matrix

In terms of cost, the judgment matrix of each city is established as follows:

spend Wuhan Xiamen Nanchang
Wuhan 1 1/3 4
Xiamen 3 1 5
Nanchang 1/4 1/5 1

In terms of scenery, the judgment matrix of each city is established as follows:

scenery Wuhan Xiamen Nanchang
Wuhan 1 1/3 2
Xiamen 3 1 5
Nanchang 1/2 1/5 1

In terms of transportation, the judgment matrix of each city is established as follows:

transportation Wuhan Xiamen Nanchang
Wuhan 1 2 5
Xiamen 1/2 1 3
Nanchang 1/5 1/3 1

Step3: Check the consistency of the judgment matrix

(1) Calculate the consistency index

Indicates the maximum eigenvalue of the judgment matrix, n represents the size of the matrix, that is, the judgment matrix is ​​a matrix with n rows and n columns 

 (2) Calculate the consistency index CR

 RI is the average random consistency index, which is specified in the following table, and the value of RI is determined according to the size of the judgment matrix

n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
RI 0 0 0.52 0.89 1.12 1.26 1.36 1.41 1.46 1.49 1.52 1.54 1.56 1.58 1.59

If the CR is less than <0.1, the consistency of the judgment matrix is ​​considered acceptable, otherwise the judgment matrix needs to be readjusted. If it does not pass the consistency test, it can be adjusted by a very simple method. First, check whether the ratio between each row and each column is proportional. If not, adjust the ratio between each row and each column.

An example of a consensus matrix is ​​as follows:

scenery hangzhou Xi'an Guilin
hangzhou 1 2 4
Xi'an 1/2 1 2
Guilin 1/4 1/2 1

It can be seen that the rows and columns of the above matrix are proportional. For example, the elements in the second column are twice the elements in the first column. If the consistency check does not pass, you need to adjust to the direction where each row and column are proportional.

Step4: Calculate the indicator weight and the score of the city on each indicator

  1. arithmetic mean

Step 1: Add the judgment matrix by row to get a 1*n row vector

Step 2: Divide all the elements in the first row of the judgment matrix by the corresponding elements of the above 1*n row vector until the last row completes the operation of dividing by 1*n row vector, and finally obtain an n*n matrix

The third step: add the n*n matrix obtained in the second step column by column to obtain an n*1 matrix, and then divide each element by n to obtain the weight vector calculated by the arithmetic mean method.

Let me give you an example:

The weight to calculate the cost is calculated using this formula as

The weight obtained by dividing the ratio of spending to other indicators by the sum of the columns where other indicators are located

Calculate the weights using the first column elements:

1/(1+1/2+3)

使用第二列元素计算权重:
2/(2+1+5)

使用第三列元素计算权重:
1/3/(1/3+1/5+1)

再将这三个结果取平均值就得到归一化后的结果

(1/(1+1/2+3)+ 2/(2+1+5)+ 1/3/(1/3+1/5+1))/3

(2)几何平均法

第一步:将判断矩阵的元素按照行相乘得到一个新的列向量

第二步:将新的向量的每个分量开n次方

第三步:对该列向量进行归一化即可得到权重向量

 表示将判断矩阵的元素按行相乘得到一个新的列向量并开n次方,随后除以新的列向量所有元素之和得到归一化后的权重向量

(3)特征值法

第一步:求出矩阵A的最大特征值以及其对应的特征向量

第二步:对求出的特征向量进行归一化即可得到我们的权重

使用matlab 的eig()函数可以求特征向量矩阵

 4matlab实现层次分析法

注意,当只有两个指标或两个方案需要选择时不可以使用层次分析法。

%输入判断矩阵A
A=[1 1/3 4;
    3 1 5;
    1/4 1/5 1]
%判断判断矩阵A是否通过一致性检验,否则需要修改判断矩阵直到通过一致性检验
[n,n]=size(A)
    [v,d]=eig(A)
    %求最大特征向量
    %v表特征向量,d表示特征值构成的对角矩阵
    max_v=max(max(d))
    %求判断矩阵A是几行几列的矩阵
    %计算一致性指标CI
    CI=(max_v-n)/(n-1)
    RI=[0 0 0.52 0.89 1.12 1.26 1.36 1.41 1.46 1.49 1.52 1.54 1.56 1.58 1.59]
    %计算一致性比例CR
    disp("一致性比例为")
    CR=CI/RI(n)
    disp(CR)
    if CR<0.10
        disp("因为一致性比例CR<0.1,因此判断矩阵A通过一致性检验")
    else
        disp("因为一致性比例CR>0.1,因此判断矩阵A不通过一致性检验,请重新调整判断矩阵,直至通过一致性检验")
    end
%算数平均法求权重
%将判断矩阵按列求和
%n表示判断矩阵A有n行n列
%[n,n]=size(A)
if CR<0.1
    sum_a=sum(A,1)
    sum_A=repmat(sum_a,n,1)
    %计算未归一化的权重
    weight=A./sum_A
    %计算归一化后的权重
    sum_weight=sum(weight,2)
    weight=sum_weight/n
    disp("算数平均法求的权重或得分为:")
    disp(weight)
    
    %几何平均法求权重
    %判断矩阵按行相乘
    prod_A=prod(A,2)
    %将新的向量开n次方得到未归一化的权重
    prod_A=prod_A.^(1/n)
    %权重归一化
    disp("几何平均法求得权重或得分为:")
    weight_jihe=prod_A./sum(prod_A)
    
    %特征值法求权重
    %v表特征向量,d表示特征值构成的对角矩阵
    %[v,d]=eig(A)
    %max_v=max(max(d))
    %将特征向量归一化
    %find函数用于寻找最大特征值所在的位置
    %max_v表示最大特征值
    [max_r,max_c]=find(max_v,1)
    %v(:,max_c)找到最大特征值对应的列
    e=v(:,max_c)/sum(v(:,max_c))
    disp("特征值法求的权重或得分为:")
    disp(e)
end
%三种方法得出的平均权重
disp("三种方法得出的平均权重或得分为")
disp((e+ weight_jihe+weight)./3)

5计算结果

指标权重及各个城市在各方面的得分为:

指标权重 武汉 厦门 南昌
花费 0.2297 0.2812 0.6424 0.0946
景色 0.1221 0.2297 0.6482 0.1221
交通 0.6482 0.5815 0.3090 0.1095

各个城市的最终得分为:

得分
武汉 0.47
厦门 0.43
南昌 0.11

可以看到武汉得分最高,我主观认为最适合小明去的城市为武汉。

非常感谢各位小伙伴看到这里,有不对的地方欢迎各位小伙伴指正。我写到这里老辛苦了,可怜一下我给我个赞吧,谢谢各位大漂亮大帅逼~

Guess you like

Origin blog.csdn.net/denghls/article/details/126842244