diff --git a/src/Main.java b/src/Main.java index 1d1efc68f877fc16ae2a9546924e56229733793c..87b167a0466784f299fd7caf7ea2ff8fd9f15f38 100644 --- a/src/Main.java +++ b/src/Main.java @@ -38,13 +38,21 @@ public class Main { * @param args arguments en entrées */ public static void main(String[] args) throws FileNotFoundException, RuntimeException { - long start = System.nanoTime(); Scanner readFile = new Scanner(System.in); - System.out.println("Entrez le nom du fichier à tester, il doit se situer dans le dossier src."); - //String nameFile = readFile.nextLine(); - String nameFile = "network2.txt"; - //String nameFile = "input.txt"; - File f = new File("src/" + nameFile); + System.out.println("Entrez le nom du fichier à tester SANS EXTENSION, il doit se situer dans le dossier src. (Ex: input)"); + String nameFile = readFile.nextLine(); + File f = new File("src/" + nameFile + ".txt"); + System.out.println("Souhaitez vous activer le débogage ? y / n"); + String readingDebug = readFile.nextLine(); + boolean debugging = false; + + switch (readingDebug) { + case "y" -> debugging = true; + case "n" -> debugging = false; + default -> System.err.println("Une erreur a été détectée lors de la saisie du débogage"); + } + + long start = System.nanoTime(); Scanner sc = new Scanner(f); String[] elements; int sousCondition = nbSc(sc), line = 0; @@ -78,7 +86,8 @@ public class Main { eq.printEq(); // Tableau initial - Simplex spx = new Simplex(eq.getMat().getLine(), eq.getMat().getLine() + eq.getMat().getCol() + 1, line, contraintes); + Simplex spx = new Simplex(eq.getMat().getLine(), eq.getMat().getLine() + eq.getMat().getCol() + 1, + line, contraintes, debugging); spx.createSimplex(eq, contraintes); spx.printSimplex(spx.getMatEcart(), "Tableau initial"); @@ -87,8 +96,10 @@ public class Main { spx.tabAux(); } else { spx.pivot(spx.getMatEcart(), false); - spx.printSimplex(spx.getMatEcart(), "Résultat"); - System.out.println("Nombre de pivot: " + spx.getNbPivot()); + if(debugging) { + spx.printSimplex(spx.getMatEcart(), "Résultat"); + System.out.println("Nombre de pivot: " + spx.getNbPivot()); + } } sc.close(); diff --git a/src/Simplex.java b/src/Simplex.java index 3168f60c068ccd4132ed1492df969d509d8a8529..e76eb234eaeb551352011f61fcb2ce47a2893587 100644 --- a/src/Simplex.java +++ b/src/Simplex.java @@ -1,6 +1,7 @@ import java.util.Arrays; public class Simplex { + private boolean debugging; private Matrix matEcart; private Matrix tabAux; private int nbSousCondition; @@ -18,7 +19,7 @@ public class Simplex { return nbPivot; } - public Simplex(int ligne, int colonne, int nbSousCondition, int nbContraintes) { + public Simplex(int ligne, int colonne, int nbSousCondition, int nbContraintes, boolean debugging) { this.ligne = ligne; this.colonne = colonne; this.matEcart = new Matrix(ligne, colonne); @@ -26,6 +27,7 @@ public class Simplex { this.tabAux = new Matrix(ligne + 2, colonne + nbSousCondition + 1); this.nbPivot = 0; this.nbContraintes = nbContraintes; + this.debugging = debugging; } void createSimplex(Equation eq, int nbContraintes) { @@ -63,7 +65,7 @@ public class Simplex { } void tabAux() { - System.out.println(); + if (debugging) System.out.println(); double[] tabRes = new double[this.colonne + this.nbSousCondition + 1]; Arrays.fill(tabRes, 1.0); for (int i = 0; i < this.colonne; i++) { @@ -98,7 +100,9 @@ public class Simplex { } } } - this.tabAux.printTabAux("Tableau auxiliaire", this.nbContraintes, this.nbSousCondition, this.tabAux.getCol() - this.matEcart.getCol() - 1); + if(debugging) this.tabAux.printTabAux("Tableau auxiliaire", this.nbContraintes, this.nbSousCondition, + this.tabAux.getCol() - this.matEcart.getCol() - 1); + pivot(this.tabAux, true); double solutionOptimale = this.tabAux.getData(this.tabAux.getLine() - 1, this.tabAux.getCol() - 1); if (solutionOptimale > 0 + EPSILON) { @@ -109,11 +113,13 @@ public class Simplex { // Il faut enlever les variables auxilaires Matrix res = new Matrix(matEcart.getLine(), matEcart.getCol()); res.matrixFill(res.getLine(), res.getCol(), tabAux.getDatas()); - res.matrixPrint("Petit tableau"); + if (debugging) res.matrixPrint("Petit tableau"); nbPivot = 0; pivot(res, true); - res.matrixPrint("Résultat "); - System.out.println("Nombre de pivot : " + nbPivot); + if (debugging) { + res.matrixPrint("Matrice Résultat "); + System.out.println("Nombre de pivot : " + nbPivot); + } } } @@ -125,8 +131,8 @@ public class Simplex { } /** - * @param mat - * @param phase true => phase 1 | false => phase 2 + * @param mat Matrix + * @param phase true => phase 1 | false => phase 2 */ void pivot(Matrix mat, boolean phase) { this.nbPivot += 1; @@ -148,10 +154,12 @@ public class Simplex { } } } - mat.matrixPrint("Pivot numéro " + this.nbPivot); - System.out.println("colonne du pivot: " + firstNeg); - System.out.println("ligne du pivot: " + id); - System.out.println("Valeur du pivot: " + val_pivot); + if (debugging) { + mat.matrixPrint("Pivot numéro " + this.nbPivot); + System.out.println("colonne du pivot: " + firstNeg); + System.out.println("ligne du pivot: " + id); + System.out.println("Valeur du pivot: " + val_pivot); + } for (int j = 0; j < mat.getCol(); j++) if (signe(mat.getData(mat.getLine() - 1, j))) { has_neg = true; @@ -159,11 +167,10 @@ public class Simplex { } if (has_neg) pivot(mat, phase); - } int ligneSortante(Matrix mat, int y, boolean phase) { - int depth = phase ? mat.getLine() - 2: mat.getLine() - 1; + int depth = phase ? mat.getLine() - 2 : mat.getLine() - 1; int id = 0; while (!(mat.getData(id, y) > 0) && mat.getData(id, depth) >= 0) { id++;