-
Notifications
You must be signed in to change notification settings - Fork 35
/
topsis.py
152 lines (125 loc) · 5.94 KB
/
topsis.py
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
import numpy as np
import warnings
class Topsis():
evaluation_matrix = np.array([]) # Matrix
weighted_normalized = np.array([]) # Weight matrix
normalized_decision = np.array([]) # Normalisation matrix
M = 0 # Number of rows
N = 0 # Number of columns
'''
Create an evaluation matrix consisting of m alternatives and n criteria,
with the intersection of each alternative and criteria given as {\displaystyle x_{ij}}x_{ij},
we therefore have a matrix {\displaystyle (x_{ij})_{m\times n}}(x_{{ij}})_{{m\times n}}.
'''
def __init__(self, evaluation_matrix, weight_matrix, criteria):
# M×N matrix
self.evaluation_matrix = np.array(evaluation_matrix, dtype="float")
# M alternatives (options)
self.row_size = len(self.evaluation_matrix)
# N attributes/criteria
self.column_size = len(self.evaluation_matrix[0])
# N size weight matrix
self.weight_matrix = np.array(weight_matrix, dtype="float")
self.weight_matrix = self.weight_matrix/sum(self.weight_matrix)
self.criteria = np.array(criteria, dtype="float")
'''
# Step 2
The matrix {\displaystyle (x_{ij})_{m\times n}}(x_{{ij}})_{{m\times n}} is then normalised to form the matrix
'''
def step_2(self):
# normalized scores
self.normalized_decision = np.copy(self.evaluation_matrix)
sqrd_sum = np.zeros(self.column_size)
for i in range(self.row_size):
for j in range(self.column_size):
sqrd_sum[j] += self.evaluation_matrix[i, j]**2
for i in range(self.row_size):
for j in range(self.column_size):
self.normalized_decision[i,
j] = self.evaluation_matrix[i, j]/(sqrd_sum[j]**0.5)
'''
# Step 3
Calculate the weighted normalised decision matrix
'''
def step_3(self):
from pdb import set_trace
self.weighted_normalized = np.copy(self.normalized_decision)
for i in range(self.row_size):
for j in range(self.column_size):
self.weighted_normalized[i, j] *= self.weight_matrix[j]
'''
# Step 4
Determine the worst alternative {\displaystyle (A_{w})}(A_{w}) and the best alternative {\displaystyle (A_{b})}(A_{b}):
'''
def step_4(self):
self.worst_alternatives = np.zeros(self.column_size)
self.best_alternatives = np.zeros(self.column_size)
for i in range(self.column_size):
if self.criteria[i]:
self.worst_alternatives[i] = min(
self.weighted_normalized[:, i])
self.best_alternatives[i] = max(self.weighted_normalized[:, i])
else:
self.worst_alternatives[i] = max(
self.weighted_normalized[:, i])
self.best_alternatives[i] = min(self.weighted_normalized[:, i])
'''
# Step 5
Calculate the L2-distance between the target alternative {\displaystyle i}i and the worst condition {\displaystyle A_{w}}A_{w}
{\displaystyle d_{iw}={\sqrt {\sum _{j=1}^{n}(t_{ij}-t_{wj})^{2}}},\quad i=1,2,\ldots ,m,}
and the distance between the alternative {\displaystyle i}i and the best condition {\displaystyle A_{b}}A_b
{\displaystyle d_{ib}={\sqrt {\sum _{j=1}^{n}(t_{ij}-t_{bj})^{2}}},\quad i=1,2,\ldots ,m}
where {\displaystyle d_{iw}}d_{{iw}} and {\displaystyle d_{ib}}d_{{ib}} are L2-norm distances
from the target alternative {\displaystyle i}i to the worst and best conditions, respectively.
'''
def step_5(self):
self.worst_distance = np.zeros(self.row_size)
self.best_distance = np.zeros(self.row_size)
self.worst_distance_mat = np.copy(self.weighted_normalized)
self.best_distance_mat = np.copy(self.weighted_normalized)
for i in range(self.row_size):
for j in range(self.column_size):
self.worst_distance_mat[i][j] = (self.weighted_normalized[i][j]-self.worst_alternatives[j])**2
self.best_distance_mat[i][j] = (self.weighted_normalized[i][j]-self.best_alternatives[j])**2
self.worst_distance[i] += self.worst_distance_mat[i][j]
self.best_distance[i] += self.best_distance_mat[i][j]
for i in range(self.row_size):
self.worst_distance[i] = self.worst_distance[i]**0.5
self.best_distance[i] = self.best_distance[i]**0.5
'''
# Step 6
Calculate the similarity
'''
def step_6(self):
np.seterr(all='ignore')
self.worst_similarity = np.zeros(self.row_size)
self.best_similarity = np.zeros(self.row_size)
for i in range(self.row_size):
# calculate the similarity to the worst condition
self.worst_similarity[i] = self.worst_distance[i] / \
(self.worst_distance[i]+self.best_distance[i])
# calculate the similarity to the best condition
self.best_similarity[i] = self.best_distance[i] / \
(self.worst_distance[i]+self.best_distance[i])
def ranking(self, data):
return [i+1 for i in data.argsort()]
def rank_to_worst_similarity(self):
# return rankdata(self.worst_similarity, method="min").astype(int)
return self.ranking(self.worst_similarity)
def rank_to_best_similarity(self):
# return rankdata(self.best_similarity, method='min').astype(int)
return self.ranking(self.best_similarity)
def calc(self):
print("Step 1\n", self.evaluation_matrix, end="\n\n")
self.step_2()
print("Step 2\n", self.normalized_decision, end="\n\n")
self.step_3()
print("Step 3\n", self.weighted_normalized, end="\n\n")
self.step_4()
print("Step 4\n", self.worst_alternatives,
self.best_alternatives, end="\n\n")
self.step_5()
print("Step 5\n", self.worst_distance, self.best_distance, end="\n\n")
self.step_6()
print("Step 6\n", self.worst_similarity,
self.best_similarity, end="\n\n")