Skip to content

Commit 8f64061

Browse files
committed
[INTEGRANDO]: Solucao final, solucao passo a passo e simplex min
1 parent 46d795e commit 8f64061

6 files changed

Lines changed: 421 additions & 79 deletions

File tree

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+
}

app/models/matriz-test.js

Lines changed: 108 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,136 @@ 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+
}
71+
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+
}
3380

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
}
56-
}
57118

58-
//module.exports = Matriz;
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+
}
153+
}

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ <h5>SIMPLEX PO20 - DLLN</h5>
4141
window.addEventListener("load", function(event) {
4242

4343
var btnMax = document.querySelector('#max');
44+
var btnMin = document.querySelector('#min');
4445
var variables = document.querySelector('#qtd_var');
4546
var restrictions = document.querySelector('#qtd_res');
4647
var iterations = document.querySelector('#qtd_it')
@@ -51,6 +52,18 @@ <h5>SIMPLEX PO20 - DLLN</h5>
5152
window.localStorage.setItem("iterations", iterations.value);
5253

5354
window.location.href = "maximizar.html";
55+
56+
window.localStorage.setItem('option', 'MAX');
57+
});
58+
59+
btnMin.addEventListener("click", function(event) {
60+
window.localStorage.setItem("variables", variables.value);
61+
window.localStorage.setItem("restrictions", restrictions.value);
62+
window.localStorage.setItem("iterations", iterations.value);
63+
64+
window.location.href = "maximizar.html";
65+
66+
window.localStorage.setItem('option', 'MIN');
5467
});
5568

5669
});

0 commit comments

Comments
 (0)