-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabulate.js
401 lines (365 loc) · 13.6 KB
/
tabulate.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* Write A and b rows to tempStr.
*
* @param A Array of constraint coefficients.
* @param b Array containing solution column elements.
* @param xB Basis variable array.
* @param cB Array of cB column elements.
* @param ratio Ratio of b to pivot column elements.
* @param pivRowIdx Pivot row index.
* @param pivColIdx Pivot column index.
* @param bools Object containing relevant Booleans.
* @return Nothing, writes data to tempStr.
*/
function AbRows(A, b, xB, cB, ratio, pivRowIdx, pivColIdx, bools) {
// Get required Booleans
var {isFeas, isOptim, isPermInf, isUnbounded, isAltSol, befAltSol} = bools;
// Initialize dimensionality variables
var {m, mn} = getDims(A);
// Start row
for (let i = 0; i < m; i++) {
tempStr += "<tr>";
tempStr += "<td>" + decimalToFrac(cB[i]) + "</td>";
if (( pivRowIdx != i) || (isNaN(pivColIdx)) || isPermInf || isUnbounded
|| isAltSol) {
tempStr += subscripts(xB[i], {isBold: false, isLeftArrow: false,
isDownArrow: false, notRow: false});
} else {
tempStr += subscripts(xB[i], {isBold: true, isLeftArrow: true,
isDownArrow: false, notRow: false});
}
for (let j = 0; j < mn; j++) {
tempStr += "<td>" + decimalToFrac(A[i][j]) + "</td>";
}
tempStr += "<td>" + decimalToFrac(b[i]) + "</td>";
// Ratio column
if (isFeas && !isOptim || befAltSol) {
if (ratio[i] != Number.POSITIVE_INFINITY && ratio[i] >= 0) {
tempStr += "<td>" + decimalToFrac(ratio[i]) + "</td>";
} else {
tempStr += "<td>NA</td>";
}
}
tempStr += "</tr>";
}
}
/**
* Generate simplex tableau based on specified data.
*
* @param A Constraint coefficient in a 2d array.
* @param b RHS of the constraints in a 1d array.
* @param cj 1d array of objective function coefficients.
* @param x 1d array of decision variable names.
* @param xB 1d array of basis variable names.
* @param bools Relevant Booleans that determine some of the
* characteristics of the tableau.
* @param pivCol Pivot column.
* @param ratio Ratio array used to decide entering/departing variables.
* @param pivEl Pivot element.
* @param pivRowIdx Pivot row index.
* @param pivColIdx Pivot column index.
* @return Nothing, simply writes the tableaux to HTML.
*/
function genTableau(A, b, cj, x, xB, bools, pivCol, ratio, pivEl,
pivRowIdx, pivColIdx) {
// Initialize relevant globals
var {cB, zj, zc} = calcEntries(A, b, cj, x, xB);
var {isFeas, isOptim, isUnbounded, isPermInf} = bools;
// The following is to prevent departing/entering variable
// indications from appearing in a final tableau
if (xB[pivRowIdx] == x[pivColIdx]) {
pivRowIdx = NaN;
pivColIdx = NaN;
}
// Start tableau
document.getElementById("tableau").innerHTML = "";
tempStr += "<table>";
// Objective function coefficient row
objectiveRow(cj);
// Header row
headerRow(x, pivColIdx, bools);
// A & b rows
AbRows(A, b, xB, cB, ratio, pivRowIdx, pivColIdx, bools);
// zj row
zRow(pivEl, isFeas, ratio, zj);
// zj-cj row
zcRow(zc);
// Ratio row
ratRow(pivEl, ratio, bools)
// End tableau
tempStr += "</table>";
// Show row operations
!isOptim && !isUnbounded && !isNaN(pivRowIdx) && !isNaN(pivEl) &&
!isPermInf && rowOperations(pivRowIdx, pivCol, pivEl);
}
/**
* Write header row to tempStr.
*
* @param x x array containing decision variable names.
* @param pivColIdx Pivot column index.
* @param bools Relevant Boolean values.
* @return Nothing, changes are written to the tempStr global.
*/
function headerRow(x, pivColIdx, bools) {
// Extract relevant Booleans
var {isFeas, isOptim, isPermInf, isAltSol, befAltSol} = bools;
// Add non-variable initial elements of row
tempStr += "<tr>";
tempStr += katexRow("c_{\\mathbf{B}}");
tempStr += katexRow("x_{\\mathbf{B}}");
// Generate columns for each decision variable including slack variables
xCols(x, pivColIdx, isPermInf, isAltSol);
// b column
tempStr += katexRow("\\mathbf{b}");
// Ratio col
if (isFeas && !isOptim || befAltSol) {
tempStr += "<td>" + katex.renderToString("\\mathrm{Ratio}") + "</td>";
}
tempStr += "</tr>";
}
/**
* Generate a row with the input string rendered with KaTeX.
*
* @param str String to render.
* @return Row HTML with KaTeX-rendered string.
*/
function katexRow(str) {
return "<td>" + katex.renderToString(str) + "</td>";
}
/**
* Write objective function row to tempStr.
*
* @param cj c array.
* @return Nothing. Writes objective function row to tempStr.
*/
function objectiveRow(cj) {
tempStr += "<tr>";
tempStr += "<td></td>";
tempStr += katexRow("c_j");
for (let i = 0; i < cj.length; i++) {
tempStr += "<td>" + decimalToFrac(cj[i]) + "</td>";
}
tempStr += "</tr>";
}
/**
* Adds ratio row to tempStr.
*
* @param pivEl Pivot element.
* @param ratio Ratio of zj-cj to the elements of the pivot row.
* @param bools Object containing relevant Booleans.
* @return Nothing, the row is just written to tempStr.
*/
function ratRow(pivEl, ratio, bools) {
var {isFeas, isPermInf} = bools;
if (ratio != undefined && !isNaN(pivEl) && !isFeas && !isPermInf) {
// Gathering dimensionality data
var mn = ratio.length;
// Start row
tempStr += "<tr>";
tempStr += "<td>" + katex.renderToString("\\mathrm{Ratio}") + "</td>";
for (let i = 0; i < mn; i++) {
if (ratio[i] != Number.POSITIVE_INFINITY) {
tempStr += "<td>" + decimalToFrac(ratio[i]) + "</td>";
} else {
tempStr += "<td></td>";
}
}
tempStr += "</tr>";
}
}
/**
* Remove simplex tableaux
* @params None.
* @return Nothing.
*/
function removeTableaux() {
document.getElementById("tableau").innerHTML = "";
tempStr = "";
}
/**
* Show row operations.
*
* @param pivRowIdx Pivot row index.
* @param pivCol Pivot column.
* @param pivEl Pivot element.
* @return Nothing, adds the row operations to tempStr.
*/
function rowOperations(pivRowIdx, pivCol, pivEl) {
// Row numbers start at 1 not 0
pivRowIdx++;
// Initialize dimensionality variable
var m = pivCol.length;
// Loop over rows and write the operations to be performed to them to
// tempStr
for (let i = 0; i < m; i++) {
// Pivot row operation
if (pivRowIdx - 1 == i) {
if (pivEl == 1) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + pivRowIdx + "} \\rightarrow R_{"
+ pivRowIdx + "}^{'}") + "</div>";
} else if (pivEl == -1) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("-R_{" + pivRowIdx + "} \\rightarrow R_{"
+ pivRowIdx + "}^{'}") + "</div>";
} else {
var fraction = math.fraction(1/pivEl);
if (fraction.d != 1) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString(sign(fraction.s) + "\\dfrac{" +
fraction.n + "}{" + fraction.d + "}" + " R_{" +
pivRowIdx + "} \\rightarrow R_{" + pivRowIdx + "}^{'}") +
"</div>";
} else {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString(sign(fraction.s) + fraction.n +
" R_{" + pivRowIdx + "} \\rightarrow R_{" + pivRowIdx
+ "}^{'}") + "</div>";
}
}
}
// Row operations for non-pivot rows, adjusted according to the value
// of the pivot column element for the row
else {
if (floatCor(pivCol[i]) == -1) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + (i + 1) + "} + " + "R_{" +
pivRowIdx + "}^{'} \\rightarrow R_{" + (i + 1) + "}^{'}") +
"</div>";
} else if ( floatCor(pivCol[i]) < 0) {
var fraction = math.fraction(-pivCol[i]);
if (fraction.d != 1) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + (i + 1) + "} + \\dfrac{" +
fraction.n + "}{" + fraction.d + "} R_{" + pivRowIdx +
"}^{'} \\rightarrow R_{" + (i + 1) + "}^{'}") + "</div>";
} else {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + (i + 1) + "} + " +
fraction.n + "R_{" + pivRowIdx + "}^{'} \\rightarrow R_{" +
(i + 1) + "}^{'}") + "</div>";
}
} else if (floatCor(pivCol[i]) == 0) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + (i + 1) + "} \\rightarrow R_{" +
(i + 1) + "}^{'}") + "</div>";
} else if (floatCor(pivCol[i]) == 1) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + (i + 1) + "} - " + "R_{" +
pivRowIdx + "}^{'} \\rightarrow R_{" + (i + 1) + "}^{'}")
+ "</div>";
}
// pivCol[i] > 0 and not equal to 1
else {
var fraction = math.fraction(pivCol[i]);
if (fraction.d != 1) {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + (i + 1) + "} - \\dfrac{" +
fraction.n + "}{" + fraction.d + "}R_{" + pivRowIdx +
"}^{'} \\rightarrow R_{" + (i + 1) + "}^{'}") + "</div>";
} else {
tempStr += "<div style='padding: 7px;'>" +
katex.renderToString("R_{" + (i + 1) + "} - " +
fraction.n + "R_{" + pivRowIdx +
"}^{'} \\rightarrow R_{" + (i + 1) + "}^{'}") + "</div>";
}
}
}
}
}
/**
* Place numbers in specified decision variable in a subscript.
*
* @param decVar Decision variable to be formatted.
* @param format An object containing formatting-related Booleans.
* @return decVar in a row with numbers as subscripts and specified
* formatting.
*/
function subscripts(decVar, format) {
// Move numbers in variables with a subscript
var corrected = decVar.replace(/\d+/, function(x) {
return "_{" + x + "}";
});
// Gather Booleans from format
var {isBold, isLeftArrow, isDownArrow, notRow} = format;
// Adjust formatting according to value of Booleans.
if (isBold) {
if (isLeftArrow) {
return katexRow("\\leftarrow \\mathbf{" + corrected + "}");
} else if (isDownArrow) {
return katexRow("\\mathbf{" + corrected + "} \\downarrow");
} else {
return katexRow("\\mathbf{" + corrected + "}");
}
} else if (notRow) {
return katex.renderToString(corrected);
} else {
return katexRow(corrected);
}
}
/**
* Generate columns for each decision variable, including slacks.
*
* @param x Decision variable array.
* @param pivColIdx Pivot column index.
* @param isPermInf Is it permanently infeasible?
* @param isAltSol Is an alternate solution?
* @return Nothing.
*/
function xCols(x, pivColIdx, isPermInf, isAltSol) {
// Loop over elements of x, display them with subscripts
// If pivot column and isPermInf = isAltSol = false show down arrow
for (let i = 0; i < x.length; i++) {
if (i != pivColIdx || isPermInf || isAltSol) {
tempStr += subscripts(x[i], {isBold: false, isLeftArrow: false,
isDownArrow: false, notRow: false});
} else {
tempStr += subscripts(x[i], {isBold: true, isLeftArrow: false,
isDownArrow: true, notRow: false});
}
}
}
/**
* Write zj-cj row to tempStr.
*
* @param zc Array containing zj-cj data.
* @return Nothing, just modifies the tempStr global.
*/
function zcRow(zc) {
// Initialize dimensionality variables
var mn = zc.length;
// Add row
tempStr += "<tr>";
tempStr += katexRow("z_j - c_j");
for (let i = 0; i < mn; i++) {
tempStr += "<td>" + decimalToFrac(zc[i]) + "</td>";
}
tempStr += "</tr>";
}
/**
* Write zj row to tempStr.
*
* @param pivEl Pivot element.
* @param isFeas Boolean indicating whether the problem is feasible.
* @param ratio Array of the ratio of b to the pivot column.
* @param zj Array of zj values.
* @return Nothing, simply writes the row to the tempStr global.
*/
function zRow(pivEl, isFeas, ratio, zj) {
// Calculate mn from z
var mn = zj.length - 1;
// Start row
tempStr += "<tr>";
if (ratio != undefined && !isNaN(pivEl) && !isFeas) {
tempStr += "<td rowspan='3'></td>";
} else {
tempStr += "<td rowspan='2'></td>";
}
tempStr += katexRow("z_j");
for (let i = 0; i < mn; i++) {
tempStr += "<td>" + decimalToFrac(zj[i]) + "</td>";
}
// Objective function value
tempStr += "<td rowspan='2'>" + decimalToFrac(zj[mn]) + "</td>";
tempStr += "</tr>";
}