-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (234 loc) · 8.57 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
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
279
function solveMatrix() {
let data = {}
const result = Papa.parse(
document.getElementById('matrix').value.split(' ').join('')
)
data.matrix = result.data
if (invalidMatrix(data.matrix) || result.errors.length) {
M.toast({ html: 'Invalid matrix syntax', classes: 'red' })
return
}
data.cantEstadosNaturales = data.matrix[0].length
data.ganancia = document.getElementById('ganancia').checked
data.solutions = {}
data.matrixT = data.matrix[0].map((_, colIndex) =>
data.matrix.map((row) => row[colIndex])
)
document.getElementById('matrixTable').innerHTML = makeTableHTML(
data.matrix,
data.ganancia ? 'Matriz de ganancia' : 'Matriz de pérdida'
)
solveViaLaplace(data)
solveViaWald(data)
solveViaMaxMax(data)
solveViaSavage(data)
solveViaHurwicz(data)
console.log(data)
}
function solveViaLaplace(data) {
const prob = 1 / data.cantEstadosNaturales
let VEs = data.matrix
.map((row) => row.reduce((a, b) => parseFloat(a) + parseFloat(b), 0))
.map((value) => prob * value)
let solutionIndex,
tableHTML =
'<table class="striped centered"><caption>Valores Esperados</caption><thead><tr><th></th><th>VEs</th></tr></thead><tbody>'
if (data.ganancia)
solutionIndex = VEs.findIndex((ve) => ve == Math.max.apply(null, VEs))
else solutionIndex = VEs.findIndex((ve) => ve == Math.min.apply(null, VEs))
for (var i = 0; i < VEs.length; i++) {
tableHTML +=
'<tr><td>' +
`<strong>A${i + 1}</strong>` +
`</td><td ${i == solutionIndex ? 'class="yellow ligthen-2"' : ''}>` +
VEs[i] +
'</td></tr>'
}
tableHTML += '</tbody></table>'
data.solutions.laplace = `A${solutionIndex + 1}`
document.getElementById('laplace').innerHTML = tableHTML
Array.from(document.getElementsByClassName('solutionLaplace')).forEach(
(el) => (el.innerText = data.solutions.laplace)
)
}
function solveViaWald(data) {
let solutionIndex,
tableHTML = ''
if (data.ganancia) {
const mins = data.matrix.map((row) => Math.min.apply(null, row))
const max = Math.max.apply(null, mins)
solutionIndex = mins.findIndex((min) => min == max)
tableHTML =
'<table class="striped centered"><caption>Mínimos</caption><thead><tr><th></th><th>MINS</th></tr></thead><tbody>'
for (var i = 0; i < mins.length; i++) {
tableHTML +=
'<tr><td>' +
`<strong>A${i + 1}</strong>` +
`</td><td ${i == solutionIndex ? 'class="yellow ligthen-2"' : ''}>` +
mins[i] +
'</td></tr>'
}
tableHTML += '</tbody></table>'
} else {
const maxs = data.matrix.map((row) => Math.max.apply(null, row))
const min = Math.min.apply(null, maxs)
solutionIndex = maxs.findIndex((max) => max == min)
tableHTML =
'<table class="striped centered"><caption>Máximos</caption><thead><tr><th></th><th>MAXS</th></tr></thead><tbody>'
for (var i = 0; i < maxs.length; i++) {
tableHTML +=
'<tr><td>' +
`<strong>A${i + 1}</strong>` +
`</td><td ${i == solutionIndex ? 'class="yellow ligthen-2"' : ''}>` +
maxs[i] +
'</td></tr>'
}
tableHTML += '</tbody></table>'
}
data.solutions.wald = `A${solutionIndex + 1}`
document.getElementById('wald').innerHTML = tableHTML
Array.from(document.getElementsByClassName('solutionWald')).forEach(
(el) => (el.innerText = data.solutions.wald)
)
}
function solveViaMaxMax(data) {
let solutionIndex,
tableHTML = ''
if (data.ganancia) {
const maxs = data.matrix.map((row) => Math.max.apply(null, row))
const max = Math.max.apply(null, maxs)
solutionIndex = maxs.findIndex((m) => m == max)
tableHTML =
'<table class="striped centered"><caption>Máximos</caption><thead><tr><th></th><th>MAXS</th></tr></thead><tbody>'
for (var i = 0; i < maxs.length; i++) {
tableHTML +=
'<tr><td>' +
`<strong>A${i + 1}</strong>` +
`</td><td ${i == solutionIndex ? 'class="yellow ligthen-2"' : ''}>` +
maxs[i] +
'</td></tr>'
}
tableHTML += '</tbody></table>'
} else {
const mins = data.matrix.map((row) => Math.min.apply(null, row))
const min = Math.min.apply(null, mins)
solutionIndex = mins.findIndex((m) => m == min)
tableHTML =
'<table class="striped centered"><caption>Mínimos</caption><thead><tr><th></th><th>MINS</th></tr></thead><tbody>'
for (var i = 0; i < mins.length; i++) {
tableHTML +=
'<tr><td>' +
`<strong>A${i + 1}</strong>` +
`</td><td ${i == solutionIndex ? 'class="yellow ligthen-2"' : ''}>` +
mins[i] +
'</td></tr>'
}
tableHTML += '</tbody></table>'
}
data.solutions.maxmax = `A${solutionIndex + 1}`
document.getElementById('maxmax').innerHTML = tableHTML
Array.from(document.getElementsByClassName('solutionMaxMax')).forEach(
(el) => (el.innerText = data.solutions.maxmax)
)
}
function solveViaSavage(data) {
let COMatrix = []
let tableHTML = ''
if (data.ganancia) {
const maxs = data.matrixT.map((row) => Math.max.apply(null, row))
COMatrix = data.matrix.map((row) =>
row.map((value, index) => maxs[index] - value)
)
tableHTML +=
'<table class="striped centered"><caption>Máximos</caption><thead><tr><th></th>'
for (let i = 0; i < data.cantEstadosNaturales; i++) {
tableHTML += `<th>F${i + 1}</th>`
}
tableHTML += '</tr></thead><tbody><tr><td><strong>MAXS</strong></td>'
for (var i = 0; i < maxs.length; i++) {
tableHTML += '<td>' + maxs[i] + '</td>'
}
tableHTML += '</tr></tbody></table></br>'
tableHTML += makeTableHTML(COMatrix, 'Costos de oportunidades')
const COMaxs = COMatrix.map((row) => Math.max.apply(null, row))
const min = Math.min.apply(null, COMaxs)
solutionIndex = COMaxs.findIndex((max) => max == min)
tableHTML +=
'</br><table class="striped centered"><caption>Máximos COs</caption><thead><tr><th></th><th>MAXS</th></tr></thead><tbody>'
for (var i = 0; i < COMaxs.length; i++) {
tableHTML +=
'<tr><td>' +
`<strong>A${i + 1}</strong>` +
`</td><td ${i == solutionIndex ? 'class="yellow ligthen-2"' : ''}>` +
COMaxs[i] +
'</td></tr>'
}
tableHTML += '</tbody></table>'
} else {
const mins = data.matrixT.map((row) => Math.min.apply(null, row))
//TODO: check if its right the substraction
COMatrix = data.matrix.map((row) =>
row.map((value, index) => mins[index] - value)
)
tableHTML +=
'<table class="striped centered"><caption>Mínimos</caption><thead><tr><th></th>'
for (let i = 0; i < data.cantEstadosNaturales; i++) {
tableHTML += `<th>F${i + 1}</th>`
}
tableHTML += '</tr></thead><tbody><tr><td><strong>MINS</strong></td>'
for (var i = 0; i < mins.length; i++) {
tableHTML += '<td>' + mins[i] + '</td>'
}
tableHTML += '</tr></tbody></table></br>'
tableHTML += makeTableHTML(COMatrix, 'Costos de oportunidades')
const COMins = COMatrix.map((row) => Math.min.apply(null, row))
const max = Math.max.apply(null, COMins)
solutionIndex = COMins.findIndex((min) => min == max)
tableHTML +=
'</br><table class="striped centered"><caption>Mínimos COs</caption><thead><tr><th></th><th>MAXS</th></tr></thead><tbody>'
for (var i = 0; i < COMins.length; i++) {
tableHTML +=
'<tr><td>' +
`<strong>A${i + 1}</strong>` +
`</td><td ${i == solutionIndex ? 'class="yellow ligthen-2"' : ''}>` +
COMins[i] +
'</td></tr>'
}
tableHTML += '</tbody></table>'
}
data.solutions.savage = `A${solutionIndex + 1}`
document.getElementById('savage').innerHTML = tableHTML
Array.from(document.getElementsByClassName('solutionSavage')).forEach(
(el) => (el.innerText = data.solutions.savage)
)
}
function solveViaHurwicz(data) {}
function invalidMatrix(matrix) {
let cantEstadosNaturales = matrix[0].length
for (const alternativa of matrix) {
if (alternativa.length != cantEstadosNaturales) return true
for (const value of alternativa) {
if (!value || isNaN(value)) return true
}
}
return false
}
function makeTableHTML(matrix, caption) {
let result =
'<table class="striped centered"><caption>' +
caption +
'</caption><thead><tr><th></th>'
for (let i = 0; i < matrix[0].length; i++) {
result += `<th>F${i + 1}</th>`
}
result += '</tr></thead><tbody>'
for (var i = 0; i < matrix.length; i++) {
result += '<tr><td>' + `<strong>A${i + 1}</strong>` + '</td>'
for (var j = 0; j < matrix[i].length; j++) {
result += '<td>' + matrix[i][j] + '</td>'
}
result += '</tr>'
}
result += '</tbody></table>'
return result
}