기준 비교 행렬 i1을 최초 입력받는다.
temp1 <- read.csv("D:\\_work_category\\개인 프로젝트\\ahp\\input1.csv", header = FALSE)
i1 <- as.matrix(temp1)
i1
## V1 V2 V3 V4 V5
## [1,] 1.0000000 3.0 7.0000000 3.0000000 1.0000000
## [2,] 0.3333333 1.0 5.0000000 0.3333333 1.0000000
## [3,] 0.1428571 0.2 1.0000000 3.0000000 0.2000000
## [4,] 0.3333333 3.0 0.3333333 1.0000000 0.3333333
## [5,] 1.0000000 1.0 5.0000000 3.0000000 1.0000000
AHP 함수를 정의한다.
ahp_base <- function(i1)
{
mn <- ncol(i1)
lamda_max <- max(Re(eigen(i1)$value))
lm_index <- which(lamda_max == Re(eigen(i1)$value))
ci1 <- (lamda_max - mn)/(mn - 1)
ci1
temp_w <- abs(Re(eigen(i1)$vectors[,lm_index]))
w1 <- temp_w/sum(temp_w)
r_values <- list("weight" = w1, "ci" = ci1, "lamda_max" = lamda_max)
return(r_values)
}
N개의 기준, K개의 대안이 있는 일반화된 경우의 2-LEVEL AHP 함수를 정의한다.
ahp_d2 <- function(criterion)
{
print("기준 비교 행렬")
print(criterion)
w1 <- ahp_base(criterion)
print("기준 비교 행렬의 분석")
print(w1)
num_cr <- length(w1$weight)
for(i in 1:num_cr)
{
temp_file <- paste("cr", i, sep = "")
add <- paste("D:\\_work_category\\개인 프로젝트\\ahp\\", temp_file, sep ="")
add <- paste(add, ".csv", sep = "")
temp2 <- read.csv(add, header = FALSE)
assign(temp_file, temp2)
}
cr_0 <- c()
print("-----------------------")
print("<대안 비교 행렬의 분석>")
print("-----------------------")
for (i in 1:num_cr)
{
temp3 <- ahp_base(get(paste("cr", i, sep="")))
al_out <- "기준"
al_out <- paste(al_out, i)
al_out <- paste(al_out, "행렬", sep = "")
print(al_out)
print(get(paste("cr", i, sep= "")))
al_out <- paste(al_out, "에 대한 대안 행렬의 분석", sep = "")
print(al_out)
print(temp3)
cr_0 <- cbind(cr_0, temp3$weight)
cr_0 <- as.matrix(cr_0)
}
num_al <- nrow(cr_0)
dcs <- c()
dcs_name <- c()
for (j in 1:num_al)
{
temp4 <- sum(w1$weight * cr_0[j,])
dcs <- rbind(dcs, temp4)
dcs_name <- append(dcs_name, paste("alter", j, sep = ""))
}
rownames(dcs) <- dcs_name
print("최종 대안의 가중치")
return(dcs)
}
i1기준행렬, 특정 경로에 저장된 엑셀파일을 끌어와서 2 LEVEL AHP를 수행한다.
ahp_d2(i1)
## [1] "기준 비교 행렬"
## V1 V2 V3 V4 V5
## [1,] 1.0000000 3.0 7.0000000 3.0000000 1.0000000
## [2,] 0.3333333 1.0 5.0000000 0.3333333 1.0000000
## [3,] 0.1428571 0.2 1.0000000 3.0000000 0.2000000
## [4,] 0.3333333 3.0 0.3333333 1.0000000 0.3333333
## [5,] 1.0000000 1.0 5.0000000 3.0000000 1.0000000
## [1] "기준 비교 행렬의 분석"
## $weight
## [1] 0.34081738 0.16725123 0.09882908 0.13483287 0.25826945
##
## $ci
## [1] 0.3616715
##
## $lamda_max
## [1] 6.446686
##
## [1] "-----------------------"
## [1] "<대안 비교 행렬의 분석>"
## [1] "-----------------------"
## [1] "기준 1행렬"
## V1 V2
## 1 1.0000000 3
## 2 0.3333333 1
## [1] "기준 1행렬에 대한 대안 행렬의 분석"
## $weight
## [1] 0.75 0.25
##
## $ci
## [1] -5e-10
##
## $lamda_max
## [1] 2
##
## [1] "기준 2행렬"
## V1 V2
## 1 1.0 5
## 2 0.2 1
## [1] "기준 2행렬에 대한 대안 행렬의 분석"
## $weight
## [1] 0.8333333 0.1666667
##
## $ci
## [1] 0
##
## $lamda_max
## [1] 2
##
## [1] "기준 3행렬"
## V1 V2
## 1 1 0.3333333
## 2 3 1.0000000
## [1] "기준 3행렬에 대한 대안 행렬의 분석"
## $weight
## [1] 0.25 0.75
##
## $ci
## [1] -5e-10
##
## $lamda_max
## [1] 2
##
## [1] "기준 4행렬"
## V1 V2
## 1 1.0000000 7
## 2 0.1428571 1
## [1] "기준 4행렬에 대한 대안 행렬의 분석"
## $weight
## [1] 0.875 0.125
##
## $ci
## [1] 5e-10
##
## $lamda_max
## [1] 2
##
## [1] "기준 5행렬"
## V1 V2
## 1 1 1
## 2 1 1
## [1] "기준 5행렬에 대한 대안 행렬의 분석"
## $weight
## [1] 0.5 0.5
##
## $ci
## [1] 0
##
## $lamda_max
## [1] 2
##
## [1] "최종 대안의 가중치"
## [,1]
## alter1 0.6668098
## alter2 0.3331902