Skip to content

Commit bece34f

Browse files
committed
Inserindo variaveis artificiais e primeira etapa do simplex revisado 2
1 parent 3174730 commit bece34f

7 files changed

+62
-178
lines changed

build/built-jar.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Thu, 10 Dec 2015 00:44:10 -0200
1+
#Thu, 10 Dec 2015 18:01:45 -0200
22

33

4-
C\:\\Users\\Jean_H_xD\\workspace\\LSR=
4+
/home/gusmao/NetBeansProjects/SPX=

build/classes/Loader.class

0 Bytes
Binary file not shown.

build/classes/Matriz.class

-3.5 KB
Binary file not shown.

build/classes/SimplexNormal.class

0 Bytes
Binary file not shown.

src/Matriz.java

Lines changed: 0 additions & 159 deletions
This file was deleted.

src/SimplexNormal.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
public class SimplexNormal {
66

7-
double[][] Matriz = {{-696, -399, 100, 0, 0, 0, -900},
7+
double[][] matriz = {{-696, -399, 100, 0, 0, 0, -900},
88
{1, 2, 0, 1, 0, 0, 4},
99
{3, 1, 0, 0, 1, 0, 3},
1010
{4, 3, -1, 0, 0, 1, 6}};
@@ -17,8 +17,8 @@ private int entraBase() {
1717
double menorValor = 0;
1818
int posicao = 0;
1919
for (int i = 0; i < (colunas-1); i++) {
20-
if (Matriz[0][i] < menorValor) {
21-
menorValor = Matriz[0][i];
20+
if (matriz[0][i] < menorValor) {
21+
menorValor = matriz[0][i];
2222
posicao = i;
2323
}
2424

@@ -34,8 +34,8 @@ private int saiBase(int entraBase) {
3434
double valorMenor = -1, valor = 0;
3535
int posicao = -1;
3636
for (int i = 1; i < linhas; i++) {
37-
if (Matriz[i][entraBase] > 0 && Matriz[i][colunas - 1] > 0) {
38-
valor = Matriz[i][colunas - 1] / Matriz[i][entraBase];
37+
if (matriz[i][entraBase] > 0 && matriz[i][colunas - 1] > 0) {
38+
valor = matriz[i][colunas - 1] / matriz[i][entraBase];
3939
if (valorMenor == -1 || valor < valorMenor) {
4040
valorMenor = valor;
4141
posicao = i;
@@ -47,21 +47,21 @@ private int saiBase(int entraBase) {
4747

4848
private void calcularMatriz(int entraBase, int saiBase) {
4949

50-
double pivo = Matriz[saiBase][entraBase];
50+
double pivo = matriz[saiBase][entraBase];
5151

5252
// dividir linha da Matriz do pivo pelo pivo
5353
if (pivo != 1) {
5454
for (int i = 0; i < colunas; i++) {
55-
Matriz[saiBase][i] = Matriz[saiBase][i] / pivo;
55+
matriz[saiBase][i] = matriz[saiBase][i] / pivo;
5656
}
5757
}
5858

5959
// Aplicar Gauss-Jordan
6060
for (int i = 0; i < linhas; i++) {
61-
double valor = Matriz[i][entraBase] * (-1);
61+
double valor = matriz[i][entraBase] * (-1);
6262
for (int j = 0; j < colunas; j++) {
6363
if (saiBase != i) {
64-
Matriz[i][j] = Matriz[i][j] + (Matriz[saiBase][j] * valor);
64+
matriz[i][j] = matriz[i][j] + (matriz[saiBase][j] * valor);
6565
}
6666
}
6767
}
@@ -71,7 +71,7 @@ private void calcularMatriz(int entraBase, int saiBase) {
7171
public void printarMatriz() {
7272
for (int i = 0; i < linhas; i++) {
7373
for (int j = 0; j < colunas; j++) {
74-
System.out.printf("%.2f ",Matriz[i][j]);
74+
System.out.printf("%.2f ",matriz[i][j]);
7575
}
7676
System.out.println("");
7777
}

src/SimplexRevisado.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,34 @@
88
public class SimplexRevisado {
99

1010
private final double FATOR_TOLERANCIA = 1.2; //20% maior
11-
public static int nIteracoes;
1211
private int qtdeVarArificiais;
12+
private int qtdeIteracoes;
1313

1414
private PPL problema; //instancia de um problema de programação linear
1515
private PPL simplex; //tabelaSimplex
1616
private int[] base; //variáveis da base
1717
private Matrix matriz;
1818
private double[] funcaoObjetivo; //funcaoObjetivo atual
1919
private double[] colunaPivot; //coluna do pivot
20+
private double[] ladoDireito; //lado direito do simplex
2021

2122
public SimplexRevisado(PPL problema) {
22-
qtdeVarArificiais = 0;
23+
this.qtdeVarArificiais = 0;
24+
this.qtdeIteracoes = 0;
2325
this.problema = problema;
24-
nIteracoes = 0;
2526
if (problema.getQtdeRestricoes() > problema.getQtdeVariaveis() * FATOR_TOLERANCIA) {
26-
//this.problema = problema.gerarDual();
27+
this.problema = problema.gerarDual();
2728
}
2829
this.simplex = this.problema;
2930
//Etapa 01: construção da solução inicial viável
3031
this.insertVariaveisArtificiais();
3132
this.insereElementosDaMatriz();
3233
this.insereBase();
34+
this.insereCoeficientesDaFuncaoObjetivo();
35+
//Etapa 02: calcular a matriz inversa na iteração i
36+
for (int i = 0; i < simplex.getNUMERO_MAXIMO_ITERACOES(); i++) {
37+
38+
}
3339
}
3440

3541
//Etapa 01: construção da solução inicial viável
@@ -72,19 +78,56 @@ private void insereElementosDaMatriz() {
7278

7379
private void insereBase() {
7480
//guardando os índices das variáveis da base inicial
75-
for (int i = 0; i < simplex.getQtdeVariaveis() - qtdeVarArificiais; i++) {
81+
int qtdeBase = simplex.getQtdeVariaveis() - qtdeVarArificiais;
82+
base = new int[qtdeBase];
83+
for (int i = 0; i < qtdeBase; i++) {
7684
base[i] = qtdeVarArificiais + i - 1;
7785
}
7886
}
7987

8088
private void insereCoeficientesDaFuncaoObjetivo() {
89+
//guardando os valores dos coeficientes da funcao objetivo inicial
90+
funcaoObjetivo = new double[simplex.getQtdeVariaveis()];
8191
for (int i = 0; i < simplex.getQtdeVariaveis(); i++) {
82-
92+
funcaoObjetivo[i] = simplex.getFuncaoObjetivo()[i];
93+
}
94+
}
95+
96+
private void insereLadoDireiro() {
97+
//guardando os valores do lado direito iniciais
98+
ladoDireito = new double[base.length];
99+
for (int i = 0; i < base.length; i++) {
100+
ladoDireito[i] = simplex.getQtdeVariaveis();
83101
}
84102
}
85103

86-
//Etapa 02: calcular a inversa de B utilizando decomposição
104+
//Etapa 02: calcular a inversa na iteracao i
87105
private void calculaInversa() {
106+
//encontra valor mais negativo
107+
boolean hasNegativo = false;
108+
double menor = 0.0;
109+
int maisNegativo = -1;
110+
for (int i = 0; i < simplex.getQtdeVariaveis(); i++) {
111+
if (funcaoObjetivo[i] < 0.0) {
112+
hasNegativo = true;
113+
menor = funcaoObjetivo[i];
114+
maisNegativo = i;
115+
}
116+
}
117+
if (!hasNegativo) { //se não houver valores negativos, chegou a solução ótima
118+
printSucesso();
119+
} else {
120+
//terminar
121+
}
122+
}
123+
124+
private void printSucesso() {
125+
System.out.println("=================================================================");
126+
System.out.println("Solução viável encontrada após " + qtdeIteracoes + " iterações.\n");
127+
int variaveisDoProblema = simplex.getQtdeVariaveis() - qtdeVarArificiais;
128+
for (int i = 0; i < variaveisDoProblema; i++) {
129+
System.out.print("X" + i + ": " + ladoDireito[i] + "\n");
130+
}
88131
}
89132

90133
private void insertFolga(double coeficiente, int i) {

0 commit comments

Comments
 (0)