-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAW.ts
264 lines (221 loc) 路 6.49 KB
/
SAW.ts
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
/*! *****************************************************************************
Copyright (c) the Trodoon authors. All rights reserved. MIT License.
***************************************************************************** */
import { Attribute } from "./../enums.ts";
class SAW {
private alternatives: Array<string> = [];
private criterias: Array<Array<number>> = [];
private weights: Array<number> = [];
private attributes: Array<Attribute> = [];
private process: boolean = false;
constructor(
alternatives: Array<string>,
criterias: Array<Array<number>>,
weights: Array<number>,
attributes: Array<Attribute>,
process: boolean = false
) {
this.weights = weights;
this.criterias = criterias;
this.alternatives = alternatives;
this.attributes = attributes;
this.process = process;
}
/**
* @return void
* function for error validation
*/
private log(msg: string): void {
console.log(msg);
console.log(
"Please check your argument, make sure the arguments follows the rules of use."
);
Deno.exit(0);
}
/**
* @return void
* check alternatives
*/
private checkAlternatives(): void {
if (!(this.alternatives.length > 0)) this.log("Alternatives empty");
this.alternatives.forEach((value) => {
if (value === "" || value.trim() === "") {
this.log("value of Alternatives at least must be one character");
}
});
}
/**
* @return void
* check criterias
*/
private checkCriterias(): void {
if (!(this.criterias.length > 0)) this.log("row of Criterias are empty");
if (this.criterias.length !== this.alternatives.length) {
this.log(
"row length of Criterias is not equal with The Alternative length"
);
}
this.criterias.forEach((values, indexes) => {
if (!(values.length > 0)) {
this.log(`column ${indexes} of Criterias are empty`);
}
if (values.length !== this.weights.length) {
this.log(
`column ${indexes} length of Criterias is not equal with The Weights length`
);
}
if (values.length !== this.attributes.length) {
this.log(
`The Attributes length of Criterias is not equal with The Weights length`
);
}
});
}
/**
* @return void
* check weights
*/
private checkWeights(): void {
if (!(this.weights.length > 0)) this.log("Weights are empty");
let count: number = 0;
this.weights.forEach((value) => {
count += value;
if (!Number.isInteger(value)) {
this.log(`value ${value} of Weights is not integer`);
}
});
if (count != 100) this.log("total value of weights must be 100");
}
/**
* @return void
* switch Matrix (row to column)
*/
private switchMatrix(): Array<Array<number>> {
const result: Array<Array<number>> = [];
this.weights.forEach((_, index) => {
let temp: Array<number> = [];
this.criterias.forEach((_, indexes) => {
temp.push(this.criterias[indexes][index]);
});
result.push(temp);
});
return result;
}
/**
* @return number[]
* new array column to row
*/
private attributeOfColumn(): Array<number> {
const attributes = this.switchMatrix();
const result: Array<number> = [];
this.attributes.forEach((value, index) => {
if (value === Attribute.BENEFIT) {
result.push(Math.max(...attributes[index]));
} else {
result.push(Math.min(...attributes[index]));
}
});
return result;
}
/**
* @return number[][]
* normalization of criterias
*/
private normalization(): Array<Array<number>> {
const result: Array<Array<number>> = [];
const columns = this.attributeOfColumn();
this.criterias.forEach((values) => {
let map = values.map((value, index) => {
if (this.attributes[index] === Attribute.BENEFIT) {
return value / columns[index];
} else {
return columns[index] / value;
}
});
result.push(map);
});
return result;
}
/**
* @return number[]
* convert each number to percent
*/
private weightToPercent(): Array<number> {
return [...this.weights.map((value) => value / 100)];
}
/**
* @return number[]
* weighted normalization
*/
private weightedNormalization(): Array<number> {
const result: Array<number> = [];
const weights = this.weightToPercent();
const criterias = this.normalization();
criterias.forEach((values) => {
let map: number = 0;
values.map((value, index) => (map += weights[index] * value));
result.push(map);
});
return result;
}
/**
* @return string[]
* sorting value from weightedNormalization (high to low)
*/
private ranking(): Array<string> {
const alternatives = this.alternatives;
const weightedNormalization = this.weightedNormalization();
const result = Object.fromEntries(
alternatives.map((_, i) => [alternatives[i], weightedNormalization[i]])
);
const sorted = Object.keys(result)
.sort((a, b) => result[a] - result[b])
.reverse();
return sorted;
}
/**
* @return void
* show class process if process true
*/
private showProcess(vararg: any, name: string = ""): void {
if (this.process) {
console.log(`START: ${name}`);
console.table(vararg);
console.log(`END : ${name}`);
console.log("");
}
}
/**
* @return void
* call all process function in this class
*/
public result(): any {
this.checkAlternatives();
this.checkCriterias();
this.checkWeights();
const alternatives = this.alternatives;
this.showProcess(alternatives, "Alternatives");
const criterias = this.criterias;
this.showProcess(criterias, "Criterias");
const weights = this.weights;
this.showProcess(weights, "Weights");
const attributes = this.attributes;
this.showProcess(attributes, "Attributes");
const aoc = this.attributeOfColumn();
this.showProcess(aoc, "Attribute Of Column");
const n = this.normalization();
this.showProcess(n, "Normalization");
const wn = this.weightedNormalization();
this.showProcess(wn, "Weighted Normalization");
const ranking = this.ranking();
this.showProcess(ranking, "Ranking of Alternatives");
return this.process ? "" : ranking;
}
}
export const saw = (
a: Array<string>,
c: Array<Array<number>>,
w: Array<number>,
attr: Array<Attribute>,
process: boolean = false
) => new SAW(a, c, w, attr, process).result();