-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
278 lines (227 loc) · 8.73 KB
/
main.py
File metadata and controls
278 lines (227 loc) · 8.73 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
Authors Oussama FORTAS
Aimene BAHRI
Ali Atmani
Abed Kebir
"""
import sys
import numpy
import csv
import numpy as np
import time
np.set_printoptions(threshold=sys.maxsize)
print("PROMETHEE 2 METHOD")
print("#######################################################")
print("We will be using AHP : Analytic Hierarchy Process.")
time.sleep(3)
Matrix = np.array(list(csv.reader(open("MP.csv", "r"), delimiter=",")))
print(Matrix)
time.sleep(3)
#to print matrix in a good format
#len(matix) gives us the number of rows
# Step1 Normalize the evaluation matrix (Decision Matrix)
print("STEP 1 : Normalize the Evaluation Matrix")
time.sleep(3)
# make the matrix as array to facilitate the Loop function
array_Matrix = np.array(Matrix)
# Delete first ligne and column and keep only the float variables
Alternative_matix = array_Matrix[1:,1:].astype(np.float)
print('Alternative_matix \n',Alternative_matix)
time.sleep(3)
# Save the Labels of the Ligne we deleted (we will need it later)
labels = array_Matrix[0,1:]
print('labels \n',labels)
time.sleep(3)
# Save the Names of the Column we deleted (we will need it later)
Alternatives = array_Matrix[1:,0]
print('Names \n',Alternatives)
time.sleep(3)
# Get min and max for each criteria
min_criteria_array = Alternative_matix.min(axis=0)
max_criteria_array = Alternative_matix.max(axis=0)
print("MIN and MAX Criteria Array")
print(min_criteria_array)
print(max_criteria_array)
time.sleep(3)
# Calculate the new matrix with beneficial non beneficial criteria:
# Beneficial Criteria == 1(python nebdou mel 0)
# NON ben Criteria == 2,3,4(python == 1,2,3)
for i in range(len(Alternative_matix)):
for j in range(len(Alternative_matix[i])):
if j == 0:
Alternative_matix[i][j] = (max_criteria_array[j]-Alternative_matix[i][j])/(max_criteria_array[j]-min_criteria_array[j])
else:
Alternative_matix[i][j] = (Alternative_matix[i][j]-min_criteria_array[j])/(max_criteria_array[j]-min_criteria_array[j])
print(Alternative_matix)
print("STEP 2 : Calculate Evaluative ieme per the othere {m1-m2 | m1-m3 | ....}")
time.sleep(3)
# Create the Alternatives Possibilities array[m1-m2,........]
def all_alternatives(Alternatives):
Alternative_possibilities = []
for i in range(len(Alternatives)):
for j in range(len(Alternatives)):
if i != j:
Alternative_possibilities.append(Alternatives[i]+'-'+Alternatives[j])
else:
pass
return np.array(Alternative_possibilities).reshape(len(Alternative_possibilities),1)
Alternative_possibilities = all_alternatives(Alternatives)
print('Alternative_possibilities \n', Alternative_possibilities)
time.sleep(3)
# create the matrix of all variables possibilities:
def all_variables(matrix):
new_matrix = []
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if i != j:
new_matrix.append(matrix[i]-matrix[j])
else:
pass
return np.array(new_matrix).reshape(len(matrix)*(len(matrix)-1),len(matrix))
variables_possibilities = all_variables(Alternative_matix)
print('variables_possibilities \n', variables_possibilities)
time.sleep(3)
# concatenate the Names and variables related
the_all_matrix = np.hstack([Alternative_possibilities, variables_possibilities])
print('The All Matrix \n', the_all_matrix)
time.sleep(3)
print("STEP 3 : Calculate the PREFERENCE Function")
# Create an updated matrix that return 0 if value is negative or equal to 0
# else keep value as it it
def changetozeros(matrix):
for i in range(len(matrix)) :
for j in range(len(matrix[i])) :
if matrix[i][j] <= 0 :
matrix[i][j] = 0
return matrix
Preference_matrix = changetozeros(variables_possibilities)
print('PREFERENCE_matrix \n', Preference_matrix)
time.sleep(3)
# concatenate the Names and preferences related
the_Preference_matrix = np.hstack([Alternative_possibilities, Preference_matrix])
print('the_Preference_matrix \n', the_Preference_matrix)
time.sleep(3)
# calculate the aggregated preferenbce function
# hna nedourbou f les poids(weights)
# lets call the weights from a csv file
weights =list(csv.reader(open("weights.csv", "r"), delimiter=","))
print('weights \n', weights)
time.sleep(2)
array_weights = np.asarray(weights[0], dtype='float64')
print('array_weights \n', array_weights)
time.sleep(3)
# lets create a fucntion to mult the weights with the matrix of preferences variables
def mult_matrix_vect(matrix, weight):
for i in range(len(matrix)) :
for j in range(len(matrix[i])) :
matrix[i][j] = matrix[i][j]* weight[j]
return matrix
# TODO: Check this multyplie function
def show_mult_matrix_vect(matrix, weight):
data = []
for i in range(len(matrix)) :
for j in range(len(matrix[i])) :
data.append('{}*{}'.format(weight[j],matrix[i][j]))
return np.array(data)
Agregate_preference_matrix = mult_matrix_vect(Preference_matrix, array_weights)
show_calculation = show_mult_matrix_vect(Preference_matrix, array_weights)
print('show_calculation \n', show_calculation)
time.sleep(3)
print('Agregate_preference_matrix \n', Agregate_preference_matrix)
time.sleep(3)
# lets add a column to sum these aggregated preferences
def add_aggregated_preferences_line(matrix):
average_line_weight = []
for i in range(len(matrix)) :
sum = 0
for j in range(len(matrix[i])) :
sum = sum + matrix[i][j]
average_line_weight.append(sum)
matrix = np.vstack([matrix.transpose(), average_line_weight]).transpose()
return matrix
Agregate_preference_matrix_with_sum = add_aggregated_preferences_line(Agregate_preference_matrix)
print('Agregate_preference_matrix_with_sum \n', Agregate_preference_matrix_with_sum)
time.sleep(3)
aggrsums = Agregate_preference_matrix_with_sum[:,-1]
print(aggrsums)
# take only the aggragated sum values(LAST column) and create aggregated preference Function
def create_aggregated_matrix(matrix, aggr):
# retrieve only the aggregated column(list)
aggregate_column = np.array(matrix[:, -1].transpose())
agrs = aggr.tolist()
print(aggregate_column)
# aggregated_matrix = [[len(Alternatives), len(Alternatives) ]]
#hada el hmar ghadi ylez madam les valeurs yethattou
# print(np.array(aggregated_matrix).shape)
for i in range(len(aggregated_matrix)) :
for j in range(len(aggregated_matrix[i])) :
if i == j:
aggregated_matrix[i][j] = 0
else:
aggregated_matrix[i][j]= agrs[0]
agrs.pop(0)
# aggregated_matrix.append(aggregate_column[j])
# print('lol',aggregated_matrix)
print(np.array(aggregated_matrix).shape)
return aggregated_matrix
aggregated_matrix = np.zeros((len(Alternatives), len(Alternatives)))
hamoud = create_aggregated_matrix(aggregated_matrix, aggrsums)
print("HADA HAMOUD")
print(hamoud)
time.sleep(3)
linesha9eh = hamoud
#flot entrant w sortant
def sumColumn(m):
return [sum(col) for col in zip(*m)]
sommeeecolonne= sumColumn(hamoud)
sumrows = np.sum(hamoud, axis = 1)
#we need to deivde those calculated vvalues on the number of alternatives -1
newsommecolonne = []
newsumrow= []
for x in sommeeecolonne:
newsommecolonne.append(x /(len(hamoud) - 1))
for x in sumrows:
newsumrow.append(x /(len(hamoud) - 1))
print(sommeeecolonne)
print(sumrows)
print("flots entrants \n" , newsommecolonne)
print("flots sortants \n" , newsumrow)
time.sleep(3)
hamoud = np.vstack([hamoud, newsumrow])
print("b rows ")
print(hamoud)
newsommecolonne.append(0)
hamoud= np.vstack([hamoud.transpose(), newsommecolonne]).transpose()
print("hamoud kamel\n", hamoud)
time.sleep(3)
#here i'll be using a function to calculate the flots
def calculateflows(matrix):
diffs=[]
for i in range(len(matrix)):
diffs.append(matrix[i,-1] - matrix[-1, i])
return diffs
print("flowshamoud")
differencesflots = calculateflows(hamoud)
print(differencesflots)
time.sleep(3)
alt = np.append(Alternatives, " ")
linesha9eh = np.vstack([alt, hamoud.transpose()])
#so far hamoud is transposed
# def remove_last_element(arr):
# return arr[np.arange(arr.size - 1)]
# fachnhat = remove_last_element(fachnhat)
talyabachtetsetef = np.vstack([linesha9eh, differencesflots]).transpose()
print("sma3")
print("##############")
with numpy.printoptions(threshold=numpy.inf):
print(talyabachtetsetef[:-1,:])
time.sleep(3)
# Sort 2D numpy array by first column
sortedArr = talyabachtetsetef[talyabachtetsetef[:,-1].argsort()]
print('Sorted 2D Numpy Array')
print("##############")
with numpy.printoptions(threshold=numpy.inf):
print(np.flipud(sortedArr))
time.sleep(3)
print("Final Sort is : ")
print(sortedArr[:,0])