-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtopsis.jl
More file actions
executable file
·163 lines (120 loc) · 4.76 KB
/
topsis.jl
File metadata and controls
executable file
·163 lines (120 loc) · 4.76 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
module Topsis
import ..MCDMMethod, ..MCDMResult, ..MCDMSetting
import ..Normalizations
using ..Utilities
export TopsisMethod, TopsisResult, topsis
struct TopsisMethod <: MCDMMethod
normalization::G where {G <: Function}
end
TopsisMethod() = TopsisMethod(Normalizations.vectornormnormalization)
struct TopsisResult <: MCDMResult
decisionMatrix::Matrix
weights::Array{Float64,1}
normalizedDecisionMatrix::Matrix
normalizedWeightedDecisionMatrix::Matrix
distanceToIdeal::Vector
distanceToNegative::Vector
bestIndex::Int64
scores::Vector
idealDesired::Vector
idealUndesired::Vector
end
"""
topsis(decisionMat, weights, fns; normalization)
Apply TOPSIS (Technique for Order of Preference by Similarity to Ideal Solution) method
for a given matrix and weights.
# Arguments:
- `decisionMat::Matrix`: n × m matrix of objective values for n candidate (or strategy) and m criteria
- `weights::Array{Float64, 1}`: m-vector of weights that sum up to 1.0. If the sum of weights is not 1.0, it is automatically normalized.
- `fns::Array{<:Function, 1}`: m-vector of function that are either minimize or maximize.
- `normalization{<:Function}`: Optional normalization function.
# Description
topsis() applies the TOPSIS method to rank n strategies subject to m criteria which are supposed to be either maximized or minimized.
# Output
- `::TopsisResult`: TopsisResult object that holds multiple outputs including scores and best index.
# Examples
```julia-repl
julia> df = DataFrame();
julia> df[:, :x] = Float64[9, 8, 7];
julia> df[:, :y] = Float64[7, 7, 8];
julia> df[:, :z] = Float64[6, 9, 6];
julia> df[:, :q] = Float64[7, 6, 6];
julia> w = Float64[4, 2, 6, 8];
julia> df
3×4 DataFrame
Row │ x y z q
│ Float64 Float64 Float64 Float64
─────┼────────────────────────────────────
1 │ 9.0 7.0 6.0 7.0
2 │ 8.0 7.0 9.0 6.0
3 │ 7.0 8.0 6.0 6.0
julia> fns = [maximum, maximum, maximum, maximum];
julia> result = topsis(Matrix(df), w, fns);
julia> result.bestIndex
2
julia> result.scores
3-element Array{Float64,1}:
0.38768695492211824
0.6503238218850163
0.08347670030339041
```
# References
Hwang, C.L.; Yoon, K. (1981). Multiple Attribute Decision Making: Methods and Applications. New York: Springer-Verlag
Celikbilek Yakup, Cok Kriterli Karar Verme Yontemleri, Aciklamali ve Karsilastirmali
Saglik Bilimleri Uygulamalari ile. Editor: Muhlis Ozdemir, Nobel Kitabevi, Ankara, 2018
İşletmeciler, Mühendisler ve Yöneticiler için Operasyonel, Yönetsel ve Stratejik Problemlerin
Çözümünde Çok Kriterli Karar verme Yöntemleri, Editörler: Bahadır Fatih Yıldırım ve Emrah Önder,
Dora, 2. Basım, 2015, ISBN: 978-605-9929-44-8
"""
function topsis(
decisionMat::Matrix,
weights::Array{Float64,1},
fns::Array{F,1};
normalization::G = Normalizations.vectornormnormalization
)::TopsisResult where {F<:Function, G<:Function}
w = unitize(weights)
nalternatives, ncriteria = size(decisionMat)
normalizedMat = normalization(decisionMat, fns)
# weightednormalizedMat = w * normalizedMat
weightednormalizedMat = Utilities.weightise(normalizedMat, w)
desired = apply_columns(fns, weightednormalizedMat)
undesired = apply_columns(reverseminmax(fns), weightednormalizedMat)
distances_plus = Vector{Any}(undef, nalternatives)
distances_minus = Vector{Any}(undef, nalternatives)
scores = Vector{Any}(undef, nalternatives)
@inbounds for i = 1:nalternatives
ithrow = weightednormalizedMat[i, :]
distances_plus[i] = euclidean(desired, ithrow)
distances_minus[i] = euclidean(undesired, ithrow)
scores[i] = distances_minus[i] / (distances_minus[i] + distances_plus[i])
end
best_index = sortperm(scores) |> last
topsisresult = TopsisResult(
decisionMat,
w,
normalizedMat,
weightednormalizedMat,
distances_plus,
distances_minus,
best_index,
scores,
desired,
undesired
)
return topsisresult
end
"""
topsis(setting)
Apply TOPSIS (Technique for Order of Preference by Similarity to Ideal Solution) method
for a given matrix and weights.
# Arguments:
- `setting::MCDMSetting`: MCDMSetting object.
# Description
topsis() applies the TOPSIS method to rank n strategies subject to m criteria which are supposed to be either maximized or minimized.
# Output
- `::TopsisResult`: TopsisResult object that holds multiple outputs including scores and best index.
"""
function topsis(setting::MCDMSetting)::TopsisResult
topsis(setting.df, setting.weights, setting.fns)
end
end # End of module Topsis