Skip to content

Commit 659c585

Browse files
committed
Merge branch 'dev'
2 parents c5a3b5e + 8f64061 commit 659c585

10 files changed

Lines changed: 762 additions & 89 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
class matrizMin {
2+
constructor(rows, cols) {
3+
this.rows = rows;
4+
this.cols = cols;
5+
6+
this.data = [];
7+
8+
for (var i = 0; i < rows; i++) {
9+
var arr = [];
10+
for (var j = 0; j < cols; j++) {
11+
arr.push(0);
12+
}
13+
this.data.push(arr);
14+
}
15+
}
16+
17+
print() {
18+
console.table(this.data);
19+
}
20+
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+
//console.log(colBigNegative);
31+
return colBigNegative;
32+
}
33+
34+
getExitRow(typeSimplex) {
35+
var arr = [];
36+
var rowSmallPositive = -1;
37+
var column = this.getEnterColumn();
38+
if (typeSimplex == 'max'){
39+
if (column == -1) {
40+
return -1
41+
}
42+
}else if (typeSimplex == 'min'){
43+
if (column == 1) {
44+
return 1
45+
}
46+
}
47+
for (var i = 0; i < this.rows - 2; i++) {
48+
arr.push(this.data[i][this.cols - 2] / this.data[i][column]);
49+
}
50+
if (arr.length <= 0) {
51+
return -1;
52+
}
53+
var smallPositive = arr[0];
54+
for (var i = 0; i < arr.length; i++) {
55+
if (smallPositive > arr[i]) {
56+
smallPositive = arr[i];
57+
rowSmallPositive = i;
58+
}
59+
if (i == arr.length - 1 && rowSmallPositive == -1 && arr[0] == smallPositive)
60+
rowSmallPositive = 0;
61+
}
62+
return rowSmallPositive;
63+
}
64+
65+
getPivo(typeSimplex) {
66+
return this.getExitRow(typeSimplex) != -1 && this.getEnterColumn() != -1 ? this.data[this.getExitRow(typeSimplex)][this.getEnterColumn()] : -1;
67+
}
68+
69+
addInput(arr, column) {
70+
for (var i = 0; i < arr.length; i++)
71+
this.data[column][i] = arr[i];
72+
}
73+
74+
addRow(arr, row) {
75+
for (var i = 0; i < arr.length; i++)
76+
this.data[row][i] = arr[i];
77+
}
78+
79+
checkRowZ() {
80+
for (var i = 0; i < this.cols - 1; i++) {
81+
if (this.data[this.rows - 2][i] < 0) {
82+
return true;
83+
}
84+
}
85+
return false;
86+
}
87+
88+
static generateSimplexFrame(matriz) {
89+
var frame = new matrizMin(matriz.rows + 1, matriz.rows + matriz.cols);
90+
// Adiciona os valores iniciais
91+
for (var i = 0; i < frame.rows - 1; i++) {
92+
for (var j = 0; j < matriz.cols - 1; j++) {
93+
frame.data[i][j] = matriz.data[i][j];
94+
}
95+
}
96+
// Adiciona as variáveis de folga
97+
for (var i = 0; i < frame.rows - 2; i++) {
98+
for (var j = matriz.cols - 1; j < frame.cols - 1; j++) {
99+
if (i == (j - (matriz.cols - 1))) {
100+
frame.data[i][j] = 1;
101+
}
102+
}
103+
}
104+
// Adiciona as retrições
105+
for (var i = 0; i < matriz.rows - 1; i++)
106+
frame.data[i][frame.cols - 2] = matriz.data[i][matriz.cols - 1];
107+
// Adiciona tags para identificação da tabela
108+
for (var i = 0; i < frame.rows - 1; i++) {
109+
if (i < matriz.rows - 1)
110+
{
111+
frame.data[i][frame.cols - 1] = "F" + (i + 1)
112+
}
113+
else
114+
frame.data[i][frame.cols - 1] = "-Z";
115+
}
116+
for (var i = 0; i < frame.cols - 1; i++) {
117+
if (i < matriz.cols - 1)
118+
frame.data[frame.rows - 1][i] = "X" + (i + 1);
119+
else if (i < frame.cols - 2)
120+
frame.data[frame.rows - 1][i] = "F" + ((i + 1) - 2);
121+
else
122+
frame.data[frame.rows - 1][i] = "B";
123+
}
124+
frame.data[frame.rows - 1][frame.cols - 1] = "DLLN";
125+
return frame;
126+
}
127+
128+
static generateNewSimplexFrame(simplex, typeSimplex) {
129+
var frame = new matrizMin(simplex.rows, simplex.cols);
130+
var pivoRow = simplex.getExitRow(typeSimplex);
131+
if (pivoRow == -1){
132+
return frame;
133+
}
134+
var pivoColumn = simplex.getEnterColumn();
135+
if (pivoColumn == -1){
136+
return frame;
137+
}
138+
var pivoValue = simplex.getPivo(typeSimplex);
139+
if (pivoValue == -1){
140+
return frame;
141+
}
142+
143+
var baseRow = [];
144+
for (var i = 0; i < simplex.cols; i++) {
145+
baseRow.push(simplex.data[pivoRow][i]);
146+
}
147+
148+
for (var i = 0; i < baseRow.length - 1; i++) {
149+
baseRow[i] = baseRow[i] / pivoValue;
150+
}
151+
baseRow[baseRow.length - 1] = simplex.data[simplex.rows - 1][pivoColumn];
152+
frame.addRow(baseRow, pivoRow);
153+
154+
for (var i = 0; i < simplex.rows - 1; i++) {
155+
if (i != pivoRow) {
156+
var newRow = [];
157+
for (var j = 0; j < simplex.cols - 1; j++) {
158+
newRow.push(baseRow[j] * -simplex.data[i][pivoColumn] + simplex.data[i][j]);
159+
}
160+
newRow.push(simplex.data[i][simplex.cols - 1]);
161+
frame.addRow(newRow, i);
162+
}
163+
}
164+
165+
for (var i = 0; i < simplex.cols; i++)
166+
frame.data[frame.rows - 1][i] = simplex.data[simplex.rows - 1][i];
167+
return frame;
168+
}
169+
}
170+
171+
module.exports = matrizMin;

app/models/matriz-min-test.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
class matrizMin {
2+
constructor(rows, cols) {
3+
this.rows = rows;
4+
this.cols = cols;
5+
6+
this.data = [];
7+
8+
for (var i = 0; i < rows; i++) {
9+
var arr = [];
10+
for (var j = 0; j < cols; j++) {
11+
arr.push(0);
12+
}
13+
this.data.push(arr);
14+
}
15+
}
16+
17+
print() {
18+
console.table(this.data);
19+
}
20+
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+
//console.log(colBigNegative);
31+
return colBigNegative;
32+
}
33+
34+
getExitRow(typeSimplex) {
35+
var arr = [];
36+
var rowSmallPositive = -1;
37+
var column = this.getEnterColumn();
38+
if (typeSimplex == 'max'){
39+
if (column == -1) {
40+
return -1
41+
}
42+
}else if (typeSimplex == 'min'){
43+
if (column == 1) {
44+
return 1
45+
}
46+
}
47+
for (var i = 0; i < this.rows - 2; i++) {
48+
arr.push(this.data[i][this.cols - 2] / this.data[i][column]);
49+
}
50+
if (arr.length <= 0) {
51+
return -1;
52+
}
53+
var smallPositive = arr[0];
54+
for (var i = 0; i < arr.length; i++) {
55+
if (smallPositive > arr[i]) {
56+
smallPositive = arr[i];
57+
rowSmallPositive = i;
58+
}
59+
if (i == arr.length - 1 && rowSmallPositive == -1 && arr[0] == smallPositive)
60+
rowSmallPositive = 0;
61+
}
62+
return rowSmallPositive;
63+
}
64+
65+
getPivo(typeSimplex) {
66+
return this.getExitRow(typeSimplex) != -1 && this.getEnterColumn() != -1 ? this.data[this.getExitRow(typeSimplex)][this.getEnterColumn()] : -1;
67+
}
68+
69+
addInput(arr, column) {
70+
for (var i = 0; i < arr.length; i++)
71+
this.data[column][i] = arr[i];
72+
}
73+
74+
addRow(arr, row) {
75+
for (var i = 0; i < arr.length; i++)
76+
this.data[row][i] = arr[i];
77+
}
78+
79+
checkRowZ() {
80+
for (var i = 0; i < this.cols - 1; i++) {
81+
if (this.data[this.rows - 2][i] < 0) {
82+
return true;
83+
}
84+
}
85+
return false;
86+
}
87+
88+
static generateSimplexFrame(matriz) {
89+
var frame = new matrizMin(matriz.rows + 1, matriz.rows + matriz.cols);
90+
// Adiciona os valores iniciais
91+
for (var i = 0; i < frame.rows - 1; i++) {
92+
for (var j = 0; j < matriz.cols - 1; j++) {
93+
frame.data[i][j] = matriz.data[i][j];
94+
}
95+
}
96+
// Adiciona as variáveis de folga
97+
for (var i = 0; i < frame.rows - 2; i++) {
98+
for (var j = matriz.cols - 1; j < frame.cols - 1; j++) {
99+
if (i == (j - (matriz.cols - 1))) {
100+
frame.data[i][j] = 1;
101+
}
102+
}
103+
}
104+
// Adiciona as retrições
105+
for (var i = 0; i < matriz.rows - 1; i++)
106+
frame.data[i][frame.cols - 2] = matriz.data[i][matriz.cols - 1];
107+
// Adiciona tags para identificação da tabela
108+
for (var i = 0; i < frame.rows - 1; i++) {
109+
if (i < matriz.rows - 1)
110+
{
111+
frame.data[i][frame.cols - 1] = "F" + (i + 1)
112+
}
113+
else
114+
frame.data[i][frame.cols - 1] = "-Z";
115+
}
116+
for (var i = 0; i < frame.cols - 1; i++) {
117+
if (i < matriz.cols - 1)
118+
frame.data[frame.rows - 1][i] = "X" + (i + 1);
119+
else if (i < frame.cols - 2)
120+
frame.data[frame.rows - 1][i] = "F" + ((i + 1) - 2);
121+
else
122+
frame.data[frame.rows - 1][i] = "B";
123+
}
124+
frame.data[frame.rows - 1][frame.cols - 1] = "DLLN";
125+
return frame;
126+
}
127+
128+
static generateNewSimplexFrame(simplex, typeSimplex) {
129+
var frame = new matrizMin(simplex.rows, simplex.cols);
130+
var pivoRow = simplex.getExitRow(typeSimplex);
131+
if (pivoRow == -1){
132+
return frame;
133+
}
134+
var pivoColumn = simplex.getEnterColumn();
135+
if (pivoColumn == -1){
136+
return frame;
137+
}
138+
var pivoValue = simplex.getPivo(typeSimplex);
139+
if (pivoValue == -1){
140+
return frame;
141+
}
142+
143+
var baseRow = [];
144+
for (var i = 0; i < simplex.cols; i++) {
145+
baseRow.push(simplex.data[pivoRow][i]);
146+
}
147+
148+
for (var i = 0; i < baseRow.length - 1; i++) {
149+
baseRow[i] = baseRow[i] / pivoValue;
150+
}
151+
baseRow[baseRow.length - 1] = simplex.data[simplex.rows - 1][pivoColumn];
152+
frame.addRow(baseRow, pivoRow);
153+
154+
for (var i = 0; i < simplex.rows - 1; i++) {
155+
if (i != pivoRow) {
156+
var newRow = [];
157+
for (var j = 0; j < simplex.cols - 1; j++) {
158+
newRow.push(baseRow[j] * -simplex.data[i][pivoColumn] + simplex.data[i][j]);
159+
}
160+
newRow.push(simplex.data[i][simplex.cols - 1]);
161+
frame.addRow(newRow, i);
162+
}
163+
}
164+
165+
for (var i = 0; i < simplex.cols; i++)
166+
frame.data[frame.rows - 1][i] = simplex.data[simplex.rows - 1][i];
167+
return frame;
168+
}
169+
}

0 commit comments

Comments
 (0)