Skip to content

Commit

Permalink
version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoudmy authored and gaborcsardi committed Sep 24, 2013
0 parents commit 4d399ae
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,13 @@
Package: topsis
Type: Package
Title: TOPSIS method for multiple-criteria decision making (MCDM)
Version: 1.0
Date: 2013-09-24
Author: Mahmoud Mosalman Yazdi
Maintainer: Mahmoud Mosalman Yazdi <m.mosalman@gmail.com>
Description: Evaluation of alternatives based on multiple criteria using TOPSIS method.
License: GPL-2
Packaged: 2013-09-30 17:28:07 UTC; mahmoud
NeedsCompilation: no
Repository: CRAN
Date/Publication: 2013-09-30 20:15:19
4 changes: 4 additions & 0 deletions MD5
@@ -0,0 +1,4 @@
35189690043fed76ff83ef5843e6ea5e *DESCRIPTION
df390c53434517b304ac5db487184641 *NAMESPACE
224ef24e709b9d691e3c8cc9fd3b1d01 *R/topsis.R
c9ccb46828d2fcb835b8ce067ddfc1ed *man/topsis.Rd
1 change: 1 addition & 0 deletions NAMESPACE
@@ -0,0 +1 @@
exportPattern("^[[:alpha:]]+")
41 changes: 41 additions & 0 deletions R/topsis.R
@@ -0,0 +1,41 @@
topsis=function(decision = NULL, weights = NULL, impacts = NULL){
if(missing(weights))
stop("'weights' must be a numeric vector")
if(missing(impacts))
stop("'impacts' must be a character vector")
if(! is.matrix(decision) | is.data.frame(decision))
stop("'decision' must be a matirx or data frame")
if(length(weights) != ncol(decision))
stop("length of 'weights' is not equal to number of columns")
if(length(impacts) != ncol(decision))
stop("length of 'impacts' is not equal to number of columns")
if(! all(weights > 0))
stop("weights must be positive numbers")
if(! is.character(impacts))
stop("impacts must be a character vector of '+' and '-' signs")
if(! all(impacts == "+" | impacts == "-"))
stop("impacts must be only '+' or '-' sign")
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)
return(data.frame(alt.row = 1:nrow(decision), score = score, rank = rank(-score)))
}
29 changes: 29 additions & 0 deletions man/topsis.Rd
@@ -0,0 +1,29 @@
\name{topsis}
\alias{topsis}
\title{TOPSIS method for multiple-criteria decision making (MCDM)}
\description{The Technique for Order of Preference by Similarity to Ideal Solution (TOPSIS) is a multiple-criteria decision making (MCDM) method.}
\usage{
topsis(decision = NULL, weights = NULL, impacts = NULL)
}
\arguments{
\item{decision}{A numeric matrix with m rows for m alternatives and n columns for n criterions.}
\item{weights}{A numeric vector with length equal to number of columns in decision matrix for weights of criterions.}
\item{impacts}{A character vector of "+" and "-" signs for the way that each criterion influences on the alternatives.}
}
\value{
A data frame including elements
\item{alt.row}{ Row number of alternatives in decision matrix. }
\item{score}{ TOPSIS score of alternatives. }
\item{rank}{ Rank of alternatives based on TOPSIS scores. }
}
\author{Mahmoud Mosalman Yazdi <m.mosalman@gmail.com>}
\references{
Yoon, K.P.; Hwang, C. (1995). _Multiple Attribute Decision Making: An Introduction_. California: SAGE publications.
}
\examples{
d <- matrix(rpois(12, 5), nrow = 4)
w <- c(1, 1, 2)
i <- c("+", "-", "+")
topsis(d, w, i)
}
\keyword{topsis}

0 comments on commit 4d399ae

Please sign in to comment.