-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
134 lines (96 loc) · 2.21 KB
/
index.js
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
const linearAlgebra = require('linear-algebra')();
const Matrix = linearAlgebra.Matrix;
const ri = [0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41];
exports.getWeights = function getWeights(c) {
// ERROR HANDLERS
if (!(c.data)) {
console.log('ERROR. Matrix argument MUST be a linear-algebra module matrix.');
return 'ERROR';
}
if (c.cols > 8) {
console.log('ERROR. There are too many criteria to analyse. Try with less than 8.');
return 'ERROR.';
}
// Calculating colum sum
let j; // Cols
let i = 0; // Rows
let colsum = 0;
let num = 0;
const colsumArray = [];
for (j = 0; j < c.cols; j += 1) {
for (i = 0; i < c.rows; i += 1) {
num = c.data[i][j];
colsum = num + colsum;
}
colsumArray.push(colsum);
colsum = 0;
}
let mcolsumArray = [];
i = 0;
for (i = 0; i < c.rows; i += 1) {
mcolsumArray.push(colsumArray);
}
mcolsumArray = new Matrix(mcolsumArray);
// Normalised Criteria Matrix
let nc = [];
nc = c.div(mcolsumArray);
// EigenVector
const ev = [];
num = 0;
i = 0;
j = 0;
for (i = 0; i < c.rows; i += 1) {
for (j = 0; j < c.cols; j += 1) {
num = nc.data[i][j] + num;
}
num /= c.cols;
ev.push(num);
num = 0;
}
j = 0;
let em = [];
for (j = 0; j < c.cols; j += 1) {
em.push(ev);
}
em = new Matrix(em);
// Computing consistency matrix
const cm = c.mul(em);
// Weighted sum value
const wsm = [];
num = 0;
i = 0;
i = 0;
for (i = 0; i < c.rows; i += 1) {
for (j = 0; j < c.cols; j += 1) {
num = cm.data[i][j] + num;
}
wsm.push(num);
num = 0;
}
// Lamda
const lamda = [];
j = 0;
for (j = 0; j < c.cols; j += 1) {
lamda.push(wsm[j] / ev[j]);
}
// LamdaMax
let lamdaMax = 0;
j = 0;
for (j = 0; j < c.cols; j += 1) {
num = lamda[j] + num;
}
lamdaMax = num / c.cols;
// Consistency Index
let ci = (lamdaMax - c.cols) / (c.cols - 1);
j = c.cols - 1;
let cr = ci / ri[j];
// Rounded values
j = 0;
for (j = 0; j < c.cols; j += 1) {
ev[j] = Math.round(ev[j] * 100) / 100;
}
ci = Math.round(ci * 100) / 100;
cr = Math.round(cr * 100) / 100;
const resp = { ev, ci, cr };
return resp;
}; // TERMINA FUNCION