多准则决策模型-TOPSIS评价方法-源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #多准则决策模型-TOPSIS评价方法##R语言实现-代码MCDM=function (decision = NULL, weights = NULL, impacts = NULL) #决策矩阵,权重向量,影响因子{ if (missing(weights)) stop("缺少'权重向量-weights'") if (missing(impacts)) stop("缺少'影响因子-impacts'") if (!is.matrix(decision) | is.data.frame(decision)) stop("'决策矩阵-decision'必须是矩阵或数据框") if (length(weights) != ncol(decision)) stop("权重向量长度错误") if (length(impacts) != ncol(decision)) stop("影响因子长度错误") if (!all(weights > 0)) stop("权重必须大于零") if (!is.character(impacts)) stop("影响因子必须是字符型 '+'或'-' 符号") if (!all(impacts == "+" | impacts == "-")) stop("影响因子只能是字符型 '+'或'-' 符号") weights <- weights/sum(weights) N <- matrix(nrow = nrow(decision), ncol = ncol(decision)) #建一个空矩阵 for (i in 1:nrow(decision)) { for (j in 1:ncol(decision)) { N[i, j] <- decision[i, j]/sqrt(sum(decision[, j]^2)) } } #决策矩阵标准化 W = diag(weights) #建权重对角矩阵 V = N %*% W #构造加权规范化矩阵#确定理想方案和负理想方案 u <- as.integer(impacts == "+") * apply(V, 2, max) + as.integer(impacts == "-") * apply(V, 2, min) l <- as.integer(impacts == "-") * apply(V, 2, max) + as.integer(impacts == "+") * apply(V, 2, min)#构建理想方案和负理想方案距离公式 distance_u = function(x) { sqrt(sum((x - u)^2)) } distance_l = function(x) { sqrt(sum((x - l)^2)) }#计算相对接近度并排序 du <- apply(V, 1, distance_u) dl <- apply(V, 1, distance_l) score <- dl/(dl + du) outcome <- data.frame("方案"= 1:nrow(decision), 得分 = score, 排名 = rank(-score))return(outcome)}Author(s)Mahmoud Mosalman Yazdi <m.mosalman@gmail.com> |

浙公网安备 33010602011771号