diff --git a/src/Simplex.java b/src/Simplex.java index 4b88f387acb40f5c0afce32da6b1f8af63ecd202..6add24167c37c980680b3e110d8a897a08bc24ed 100644 --- a/src/Simplex.java +++ b/src/Simplex.java @@ -7,6 +7,7 @@ public class Simplex { private int x; private int y; private int nbPivot; + private static double EPSILON = 0E-7; public Matrix getTabAux() { return tabAux; @@ -88,24 +89,40 @@ public class Simplex { } } this.tabAux.matrixPrint("Tableau auxiliaire ", 2); - double valLastCase = this.tabAux.getData(this.tabAux.getX() - 1, this.tabAux.getY() - 1); + pivot(this.tabAux); + double solutionOptimale = this.tabAux.getData(this.tabAux.getX() - 1, this.tabAux.getY() - 1); // si la dernière case est négative, il faut envoyer le tableau "basique" dans la méthode du pivot - if (valLastCase < 0) { - pivot(tabAux); - System.out.println("Nombre de pivot: " + getNbPivot()); + /*if (solutionOptimale < 0) { + Matrix newMat = this.matEcart; // pour récupérer les bonnes tailles initiales + newMat.matrixFill(newMat.getX(), newMat.getY(), tabAux.getDatas()); + pivot(newMat); - } else if (valLastCase > 0) { + } else if (solutionOptimale > 0) { // si elle est positive , il n'y a pas de solutions admissibles System.out.println("Il n'y a pas de solutions admissibles pour ce problème"); } else // = 0 { System.out.println("Le min/max est 0"); + }*/ + + if (solutionOptimale > 0) { + System.out.println("Il n'y a pas de solutions admissibles pour ce problème"); + } else { + Matrix newMat = this.matEcart; // pour récupérer les bonnes tailles initiales + newMat.matrixFill(newMat.getX(), newMat.getY(), tabAux.getDatas()); + //newMat.matrixPrint("Nouvelle matrice ", 3); + this.nbPivot = 0; + pivot(newMat); } + /*if (solutionOptimale <= EPSILON + 0 || solutionOptimale >= 0 - EPSILON) { + // il faut s'assurer que la base ne contient aucune variable auxiliaire + System.out.println("il faut s'assurer que la base ne contient aucune variable auxiliaire"); + }*/ } int getFirstNeg(Matrix mat) { - for (int j = 0; j < this.y; j++) { - if (signe(mat.getData(this.x, j))) return j; + for (int j = 0; j < mat.getY(); j++) { + if (signe(mat.getData(mat.getX() - 1, j))) return j; } return -1; } @@ -116,36 +133,46 @@ public class Simplex { if (firstNeg == -1) return; // si pas de négatif boolean has_neg = false; int id = ligneSortante(mat, firstNeg); - double pivot = mat.getData(id, firstNeg); - for (int i = 0; i < this.y; i++) { - mat.setData(id, i, mat.getData(id, i) / pivot); + double val_pivot = mat.getData(id, firstNeg); + for (int i = 0; i < mat.getY(); i++) { + mat.setData(id, i, mat.getData(id, i) / val_pivot); } - for (int i = 0; i < this.x + 1; i++) { - pivot = mat.getData(i, firstNeg); - for (int j = 0; j < this.y; j++) { + for (int i = 0; i < mat.getX(); i++) { + val_pivot = mat.getData(i, firstNeg); + for (int j = 0; j < mat.getY(); j++) { if (i != id) { - mat.setData(i, j, mat.getData(i, j) - pivot * mat.getData(id, j)); + mat.setData(i, j, mat.getData(i, j) - val_pivot * mat.getData(id, j)); } } } - mat.matrixPrint("Pivot numéro " + this.nbPivot, 2); - for (int j = 0; j < this.y; j++) - if (signe(mat.getData(this.x, j))) { + //mat.matrixPrint("Pivot numéro " + this.nbPivot, 2); + for (int j = 0; j < mat.getY(); j++) + if (signe(mat.getData(mat.getX() - 1, j))) { has_neg = true; + if (has_neg) { + break; + } } if (has_neg) pivot(mat); + } int ligneSortante(Matrix mat, int y) { int id = 0; - double tmp = mat.getData(id, this.y - 1) / mat.getData(id, y); - for (int i = 1; i < this.x - 1; i++) { - double tmp_s = mat.getData(i, this.y - 1) / mat.getData(i, y); - if (tmp_s < tmp) { - id = i; - tmp = tmp_s; + while (mat.getData(id, y) < 0) { + id++; + } + double tmp = mat.getData(id, mat.getY() - 1) / mat.getData(id, y); + double tmp_s; + for (int i = 1; i < mat.getX() - 1; i++) { + if (mat.getData(i, y) > 0) { + tmp_s = mat.getData(i, mat.getY() - 1) / mat.getData(i, y); + if (tmp_s < tmp) { + id = i; + tmp = tmp_s; + } } } return id;