Skip to content

Commit 1245b90

Browse files
author
Luiz Murakami
committed
Adicionado método de geração de novos simplex a partir de um simplex. Adicionado método para checar a linha Z.
1 parent 8ee1e58 commit 1245b90

1 file changed

Lines changed: 108 additions & 11 deletions

File tree

app/models/matriz.js

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,138 @@ class Matriz {
1818
console.table(this.data);
1919
}
2020

21-
getEnterLine()
22-
{
23-
21+
getEnterColumn() {
22+
var colBigNegative = -1;
23+
var bigNegative = 0;
24+
for (var i = 0; i < this.cols; i++) {
25+
if (bigNegative > this.data[this.rows - 2][i]) {
26+
bigNegative = this.data[this.rows - 2][i];
27+
colBigNegative = i;
28+
}
29+
}
30+
return colBigNegative;
31+
}
32+
33+
getExitRow() {
34+
var arr = [];
35+
var rowSmallPositive = -1;
36+
var column = this.getEnterColumn();
37+
if (column == -1) {
38+
return -1;
39+
}
40+
for (var i = 0; i < this.rows - 2; i++) {
41+
arr.push(this.data[i][this.cols - 2] / this.data[i][column]);
42+
}
43+
if (arr.length <= 0) {
44+
return -1;
45+
}
46+
var smallPositive = arr[0];
47+
for (var i = 0; i < arr.length; i++) {
48+
if (smallPositive > arr[i]) {
49+
smallPositive = arr[i];
50+
rowSmallPositive = i;
51+
}
52+
if (i == arr.length - 1 && rowSmallPositive == -1 && arr[0] == smallPositive)
53+
rowSmallPositive = 0;
54+
}
55+
return rowSmallPositive;
56+
}
57+
58+
getPivo() {
59+
return this.getExitRow() != -1 && this.getEnterColumn() != -1 ? this.data[this.getExitRow()][this.getEnterColumn()] : -1;
2460
}
2561

2662
addInput(arr, column) {
2763
for (var i = 0; i < arr.length; i++)
2864
this.data[column][i] = arr[i];
2965
}
3066

31-
static generateSimplexFrame(matriz) {
32-
var frame = new Matriz(matriz.rows, (matriz.rows + matriz.cols) - 1);
67+
addRow(arr, row) {
68+
for (var i = 0; i < arr.length; i++)
69+
this.data[row][i] = arr[i];
70+
}
3371

72+
checkRowZ() {
73+
for (var i = 0; i < this.cols - 1; i++) {
74+
if (this.data[this.rows - 2][i] < 0) {
75+
return true;
76+
}
77+
}
78+
return false;
79+
}
80+
81+
static generateSimplexFrame(matriz) {
82+
var frame = new Matriz(matriz.rows + 1, matriz.rows + matriz.cols);
3483
// Adiciona os valores iniciais
35-
for (var i = 0; i < frame.rows; i++) {
84+
for (var i = 0; i < frame.rows - 1; i++) {
3685
for (var j = 0; j < matriz.cols - 1; j++) {
3786
frame.data[i][j] = matriz.data[i][j];
3887
}
3988
}
40-
4189
// Adiciona as variáveis de folga
42-
for (var i = 0; i < frame.rows - 1; i++) {
90+
for (var i = 0; i < frame.rows - 2; i++) {
4391
for (var j = matriz.cols - 1; j < frame.cols - 1; j++) {
4492
if (i == (j - (matriz.cols - 1))) {
4593
frame.data[i][j] = 1;
4694
}
4795
}
4896
}
49-
5097
// Adiciona as retrições
5198
for (var i = 0; i < matriz.rows - 1; i++)
52-
frame.data[i][frame.cols - 1] = matriz.data[i][matriz.cols - 1];
53-
99+
frame.data[i][frame.cols - 2] = matriz.data[i][matriz.cols - 1];
100+
// Adiciona tags para identificação da tabela
101+
for (var i = 0; i < frame.rows - 1; i++) {
102+
if (i < matriz.rows - 1)
103+
frame.data[i][frame.cols - 1] = "F" + (i + 1);
104+
else
105+
frame.data[i][frame.cols - 1] = "Z";
106+
}
107+
for (var i = 0; i < frame.cols - 1; i++) {
108+
if (i < matriz.cols - 1)
109+
frame.data[frame.rows - 1][i] = "X" + (i + 1);
110+
else if (i < frame.cols - 2)
111+
frame.data[frame.rows - 1][i] = "F" + ((i + 1) - 2);
112+
else
113+
frame.data[frame.rows - 1][i] = "B";
114+
}
115+
frame.data[frame.rows - 1][frame.cols - 1] = "DLLN";
54116
return frame;
55117
}
118+
119+
static generateNewSimplexFrame(simplex) {
120+
var frame = new Matriz(simplex.rows, simplex.cols);
121+
var pivoRow = simplex.getExitRow();
122+
if (pivoRow == -1)
123+
return frame;
124+
var pivoColumn = simplex.getEnterColumn();
125+
if (pivoColumn == -1)
126+
return frame;
127+
var pivoValue = simplex.getPivo();
128+
if (pivoValue == -1)
129+
return frame;
130+
var baseRow = [];
131+
for (var i = 0; i < simplex.cols; i++) {
132+
baseRow.push(simplex.data[pivoRow][i]);
133+
}
134+
for (var i = 0; i < baseRow.length - 1; i++) {
135+
baseRow[i] = baseRow[i] / pivoValue;
136+
}
137+
baseRow[baseRow.length - 1] = simplex.data[simplex.rows - 1][pivoColumn];
138+
frame.addRow(baseRow, pivoRow);
139+
for (var i = 0; i < simplex.rows - 1; i++) {
140+
if (i != pivoRow) {
141+
var newRow = [];
142+
for (var j = 0; j < simplex.cols - 1; j++) {
143+
newRow.push(baseRow[j] * -simplex.data[i][pivoColumn] + simplex.data[i][j]);
144+
}
145+
newRow.push(simplex.data[i][simplex.cols - 1]);
146+
frame.addRow(newRow, i);
147+
}
148+
}
149+
for (var i = 0; i < simplex.cols; i++)
150+
frame.data[frame.rows - 1][i] = simplex.data[simplex.rows - 1][i];
151+
return frame;
152+
}
56153
}
57154

58155
module.exports = Matriz;

0 commit comments

Comments
 (0)