-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcEntries.js
82 lines (70 loc) · 2.21 KB
/
calcEntries.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
/**
* Create an array of where each element in x appears in xB. Useful for
* calculating cB later.
*
* @param x 1d array of decision variable names.
* @param xB 1d array of basis variable names.
* @return Array of size xB.length containing indices of where in x each
* component of xB appears.
*/
function basisIndex(x, xB) {
// Initialize array that will store the locations
var loc = new Array(xB.length);
// Loop over basis variables
for (let i = 0; i < xB.length; i++) {
// Loop over decision variables
for (let j = 0; j < x.length; j++) {
// Record where in x each entry in xB is found
if (xB[i] == x[j]) {
loc[i] = j;
break;
}
}
}
// Return array of locations
return loc;
}
/**
* Calculate cB, z and zc from other data
*
* @param A Array of constraint coefficients.
* @param b RHS of the constraints.
* @param cj Objective function coefficients.
* @param x 1d array of decision variable names.
* @param xB 1d array of basis variable names.
* @return [cB, zj, zj-cj]
*/
function calcEntries(A, b, cj, x, xB) {
// Initialize globals
var {m, mn} = getDims(A);
var loc = basisIndex(x, xB);
var cB = new Array(m);
var zj = new Array(mn);
var zc = new Array(mn);
// Input validation
if (m != xB.length) {
alert("Length of A does not match the length of xB in calcEntries.");
return;
}
// Loop over columns of the tableau in which z appears
for (let i = 0; i < mn + 1; i++) {
// Initialize z at zero
zj[i] = 0;
// i = mn is the objective function value
if (i != mn) {
// Loop over rows calculating cB and adding up contributions to z
for (let j = 0; j < m; j++) {
cB[j] = cj[loc[j]];
zj[i] += cB[j] * A[j][i];
}
// Calculate zj-cj row entries
zc[i] = zj[i] - cj[i];
} else {
// Calculate objective function value
for (let j = 0; j < m; j++) {
zj[i] += cB[j] * b[j];
}
}
}
return {cB: cB, zj: zj, zc: zc};
}