diff --git a/src/Equation.java b/src/Equation.java index afb8867895c81aa59d136ad02c51867fee73be25..bb19874a2ce80a4a0f8ce701b0d1b823235b1c52 100644 --- a/src/Equation.java +++ b/src/Equation.java @@ -122,7 +122,7 @@ public class Equation { else this.rightVec.add(-res); // Matrice - this.mat.matrixRealloc(this.mat.getX() + 1, this.mat.getY()); + this.mat.matrixRealloc(this.mat.getLine() + 1, this.mat.getCol()); for (int i = 0; i < this.nbContraintes; i++) { double tmp = Double.parseDouble(elements[i]); this.mat.setData(line, i, tmp); @@ -167,8 +167,8 @@ public class Equation { System.out.println("Vecteur membre de droite: " + getRightVec()); System.out.println("Matrice Amxn:"); - for (int i = 0; i < this.mat.getX(); i++) { - for (int j = 0; j < this.mat.getY(); j++) { + for (int i = 0; i < this.mat.getLine(); i++) { + for (int j = 0; j < this.mat.getCol(); j++) { if (this.mat.getData(i, j) < 0.0) System.out.print(this.mat.getData(i, j) + " "); else diff --git a/src/Main.java b/src/Main.java index 676675f434e1139b21f2f6f090d645dcd7c2cc83..1d1efc68f877fc16ae2a9546924e56229733793c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,5 @@ import java.io.File; import java.io.FileNotFoundException; -import java.util.Arrays; import java.util.Scanner; import java.util.concurrent.TimeUnit; @@ -43,7 +42,7 @@ public class Main { 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 = "network1.txt"; + String nameFile = "network2.txt"; //String nameFile = "input.txt"; File f = new File("src/" + nameFile); Scanner sc = new Scanner(f); @@ -79,16 +78,16 @@ public class Main { eq.printEq(); // Tableau initial - Simplex spx = new Simplex(eq.getMat().getX(), eq.getMat().getX() + eq.getMat().getY() + 1, line, contraintes); + Simplex spx = new Simplex(eq.getMat().getLine(), eq.getMat().getLine() + eq.getMat().getCol() + 1, line, contraintes); spx.createSimplex(eq, contraintes); - spx.printSimplex(spx.getMatEcart(), "Tableau initial", 0); + spx.printSimplex(spx.getMatEcart(), "Tableau initial"); // true = phase 1 membres de droite pas admissible | false = phase 2 membres de droite admissible if (spx.which_phase()) { - spx.tabAux(0); // TODO changer ça + spx.tabAux(); } else { - spx.pivot(spx.getMatEcart()); - spx.printSimplex(spx.getMatEcart(), "Résultat", 3); + spx.pivot(spx.getMatEcart(), false); + spx.printSimplex(spx.getMatEcart(), "Résultat"); System.out.println("Nombre de pivot: " + spx.getNbPivot()); } diff --git a/src/Matrix.java b/src/Matrix.java index b4f2bd8f05d88e04d1878d5110160a404746ed28..d7f3202c9f51745a79d46c7d94dd3aab9427cb1c 100644 --- a/src/Matrix.java +++ b/src/Matrix.java @@ -1,74 +1,56 @@ public class Matrix { - private int x; - private int y; + private int line; // Lignes + private int col; // Colonnes private double[][] data; - public int getX() { - return x; + public int getLine() { + return line; } - public void setX(int x) { - this.x = x; + public int getCol() { + return col; } - public int getY() { - return y; - } - - public void setY(int y) { - this.y = y; - } - - public double getData(int x, int y) { - return data[x][y]; + public double getData(int line, int col) { + return data[line][col]; } public double[][] getDatas() { return data; } - public void setData(int x, int y, double d) { - this.data[x][y] = d; + public void setData(int line, int col, double d) { + this.data[line][col] = d; } - public Matrix(int x, int y) { - this.x = x; - this.y = y; - this.data = new double[x][y]; + public Matrix(int line, int col) { + this.line = line; + this.col = col; + this.data = new double[line][col]; } - public void matrixFill(int x, int y, double[][] tab) throws IndexOutOfBoundsException { - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { + public void matrixFill(int line, int col, double[][] tab) throws IndexOutOfBoundsException { + for (int i = 0; i < line; i++) { + for (int j = 0; j < col; j++) { if (i < tab.length && j < tab[i].length) this.data[i][j] = tab[i][j]; } } } - public void matrixInitFromArray(double[] tab) throws IndexOutOfBoundsException { - int id = 0; - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { - this.data[i][j] = tab[id]; - id++; - } - } - } - - public void matrixRealloc(int x, int y) { + public void matrixRealloc(int line, int col) { double[][] tmp = this.data; - this.x = x; - this.y = y; - this.data = new double[x][y]; - matrixFill(x, y, tmp); + this.line = line; + this.col = col; + this.data = new double[line][col]; + matrixFill(line, col, tmp); } - public void matrixPrint(String s, int precision) { + public void matrixPrint(String s) { System.out.println(); System.out.println(s + ": "); - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { + for (int i = 0; i < line; i++) { + for (int j = 0; j < col; j++) { System.out.format("%10.2f", this.data[i][j]); } System.out.println(); @@ -91,7 +73,7 @@ public class Matrix { System.out.format("Z[%d] ", i); } - System.out.format("S[%d] ", this.y / 2); + System.out.format("S[%d] ", this.col / 2); for (int i = 0; i < nbAux; i++) { if (i < 10) @@ -101,16 +83,16 @@ public class Matrix { } System.out.println(); - for (int i = 0; i < this.x; i++) { - if (i < this.x - 2) + for (int i = 0; i < this.line; i++) { + if (i < this.line - 2) if (i < 10) System.out.print("AUX_" + i + " | "); else System.out.print("AUX_" + i + " | "); - else if (i == this.x - 2) + else if (i == this.line - 2) System.out.print("OBJ. Init | "); else System.out.print("OBJ. | "); - for (int j = 0; j < this.y; j++) { + for (int j = 0; j < this.col; j++) { System.out.format("%10.3f", this.data[i][j]); } System.out.println(); diff --git a/src/Simplex.java b/src/Simplex.java index df821435900a8fc2d36f2a499a16cff3a1eb0d59..3168f60c068ccd4132ed1492df969d509d8a8529 100644 --- a/src/Simplex.java +++ b/src/Simplex.java @@ -4,16 +4,12 @@ public class Simplex { private Matrix matEcart; private Matrix tabAux; private int nbSousCondition; - private int x; - private int y; + private int ligne; // Ligne + private int colonne; // Colonne private int nbPivot; private static double EPSILON = 0E-7; private int nbContraintes; - public Matrix getTabAux() { - return tabAux; - } - public Matrix getMatEcart() { return matEcart; } @@ -22,20 +18,20 @@ public class Simplex { return nbPivot; } - public Simplex(int x, int y, int nbSousCondition, int nbContraintes) { - this.x = x; - this.y = y; - this.matEcart = new Matrix(x, y); + public Simplex(int ligne, int colonne, int nbSousCondition, int nbContraintes) { + this.ligne = ligne; + this.colonne = colonne; + this.matEcart = new Matrix(ligne, colonne); this.nbSousCondition = nbSousCondition; - this.tabAux = new Matrix(x + 2, y + nbSousCondition + 1); + this.tabAux = new Matrix(ligne + 2, colonne + nbSousCondition + 1); this.nbPivot = 0; this.nbContraintes = nbContraintes; } void createSimplex(Equation eq, int nbContraintes) { // Matrice Amxn - for (int i = 0; i < this.x; i++) { - for (int j = 0; j < this.y; j++) { + for (int i = 0; i < this.ligne; i++) { + for (int j = 0; j < this.colonne; j++) { if (j < nbContraintes) { this.matEcart.setData(i, j, eq.getMatAtId(i, j)); } else { @@ -45,13 +41,13 @@ public class Simplex { } // Membre de droite - for (int i = 0; i <= this.x - 1; i++) - this.matEcart.setData(i, this.y - 1, eq.getRightVec().get(i)); + for (int i = 0; i <= this.ligne - 1; i++) + this.matEcart.setData(i, this.colonne - 1, eq.getRightVec().get(i)); // Fonction obj - this.matEcart.matrixRealloc(this.matEcart.getX() + 1, this.matEcart.getY()); + this.matEcart.matrixRealloc(this.matEcart.getLine() + 1, this.matEcart.getCol()); for (int i = 0; i < nbContraintes; i++) - this.matEcart.setData(this.x, i, eq.getFuncObj().get(i)); + this.matEcart.setData(this.ligne, i, eq.getFuncObj().get(i)); } /** @@ -60,28 +56,28 @@ public class Simplex { * @return true = phase 1 | false = phase 2 */ boolean which_phase() { - for (int i = 0; i < this.x; i++) { - if (signe(this.matEcart.getData(i, this.y - 1))) return true; + for (int i = 0; i < this.ligne; i++) { + if (signe(this.matEcart.getData(i, this.colonne - 1))) return true; } return false; } - void tabAux(int line) { - System.out.println(""); - double[] tabRes = new double[this.y + this.nbSousCondition + 1]; + void tabAux() { + System.out.println(); + double[] tabRes = new double[this.colonne + this.nbSousCondition + 1]; Arrays.fill(tabRes, 1.0); - for (int i = 0; i < this.y; i++) { - for (int j = 0; j < this.x; j++) { - if (this.matEcart.getData(j, this.y - 1) < 0) { + for (int i = 0; i < this.colonne; i++) { + for (int j = 0; j < this.ligne; j++) { + if (this.matEcart.getData(j, this.colonne - 1) < 0) { if (this.matEcart.getData(j, i) != 0) { this.matEcart.setData(j, i, -this.matEcart.getData(j, i)); } } } } - for (int j = 0; j < this.y; j++) { + for (int j = 0; j < this.colonne; j++) { double tmpSum = 0; - for (int i = 0; i < this.x; i++) { + for (int i = 0; i < this.ligne; i++) { tmpSum += -1 * this.matEcart.getData(i, j); } tabRes[j] = tmpSum; @@ -90,88 +86,93 @@ public class Simplex { // -2 car => tabRes[(tabRes.length-1) - (this.nbSousCondition - 1)]; tabRes[tabRes.length - 1] = tabRes[tabRes.length - this.nbSousCondition - 2]; tabRes[tabRes.length - this.nbSousCondition - 2] = 0; - for (int i = 0; i < this.tabAux.getX(); i++) { - for (int j = 0; j < this.tabAux.getY(); j++) { - if (i < this.matEcart.getX() && j < this.matEcart.getY()) { + for (int i = 0; i < this.tabAux.getLine(); i++) { + for (int j = 0; j < this.tabAux.getCol(); j++) { + if (i < this.matEcart.getLine() && j < this.matEcart.getCol()) { this.tabAux.setData(i, j, this.matEcart.getData(i, j)); - } else if (i == this.tabAux.getX() - 1) { + } else if (i == this.tabAux.getLine() - 1) { this.tabAux.setData(i, j, tabRes[j]); } else // membre de droite à la fin du tab { - this.tabAux.setData(i, j, this.matEcart.getData(i, j - this.matEcart.getX())); + this.tabAux.setData(i, j, this.matEcart.getData(i, j - this.matEcart.getLine())); } } } - this.tabAux.printTabAux("Tableau auxiliaire", this.nbContraintes, this.nbSousCondition, this.tabAux.getY() - this.matEcart.getY() - 1); - pivot(this.tabAux); - double solutionOptimale = this.tabAux.getData(this.tabAux.getX() - 1, this.tabAux.getY() - 1); + 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) { System.out.println("Il n'y a pas de solutions admissibles pour ce problème."); } if (Math.abs(solutionOptimale) < EPSILON || solutionOptimale == 0) { // Il y a une solution optimale // Il faut enlever les variables auxilaires - Matrix res = new Matrix(matEcart.getX(), matEcart.getY()); - res.matrixFill(res.getX(), res.getY(), tabAux.getDatas()); - res.matrixPrint("Petit tableau", 2); + Matrix res = new Matrix(matEcart.getLine(), matEcart.getCol()); + res.matrixFill(res.getLine(), res.getCol(), tabAux.getDatas()); + res.matrixPrint("Petit tableau"); nbPivot = 0; - pivot(res); - res.matrixPrint("Résultat ", 3); + pivot(res, true); + res.matrixPrint("Résultat "); System.out.println("Nombre de pivot : " + nbPivot); } } int getFirstNeg(Matrix mat) { - for (int j = 0; j < mat.getY() - 1; j++) { - if (signe(mat.getData(mat.getX() - 1, j))) return j; + for (int j = 0; j < mat.getCol() - 1; j++) { + if (signe(mat.getData(mat.getLine() - 1, j))) return j; } return -1; } - void pivot(Matrix mat) { + /** + * @param mat + * @param phase true => phase 1 | false => phase 2 + */ + void pivot(Matrix mat, boolean phase) { this.nbPivot += 1; int firstNeg = getFirstNeg(mat); if (firstNeg == -1) return; // si pas de négatif boolean has_neg = false; - int id = ligneSortante(mat, firstNeg); + int id = ligneSortante(mat, firstNeg, phase); double val_pivot = mat.getData(id, firstNeg); - for (int i = 0; i < mat.getY(); i++) { + + for (int i = 0; i < mat.getCol(); i++) { mat.setData(id, i, mat.getData(id, i) / val_pivot); } - for (int i = 0; i < mat.getX(); i++) { + for (int i = 0; i < mat.getLine(); i++) { val_pivot = mat.getData(i, firstNeg); - for (int j = 0; j < mat.getY(); j++) { + for (int j = 0; j < mat.getCol(); j++) { if (i != id) { mat.setData(i, j, mat.getData(i, j) - val_pivot * mat.getData(id, j)); } } } - mat.matrixPrint("Pivot numéro " + this.nbPivot, 2); + 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.getY(); j++) - if (signe(mat.getData(mat.getX() - 1, j))) { + for (int j = 0; j < mat.getCol(); j++) + if (signe(mat.getData(mat.getLine() - 1, j))) { has_neg = true; break; } if (has_neg) - pivot(mat); + pivot(mat, phase); } - // TODO : A REFAIRE POUR SWITCH DE Y AU CAS OU - int ligneSortante(Matrix mat, int y) { + int ligneSortante(Matrix mat, int y, boolean phase) { + int depth = phase ? mat.getLine() - 2: mat.getLine() - 1; int id = 0; - while (!(mat.getData(id, y) > 0)) { + while (!(mat.getData(id, y) > 0) && mat.getData(id, depth) >= 0) { id++; } - double tmp = mat.getData(id, mat.getY() - 1) / mat.getData(id, y); + double tmp = mat.getData(id, mat.getCol() - 1) / mat.getData(id, y); double tmp_s; - for (int i = 1; i < mat.getX() - 1; i++) { + for (int i = 1; i < depth; i++) { if (mat.getData(i, y) > 0) { - tmp_s = mat.getData(i, mat.getY() - 1) / mat.getData(i, y); + tmp_s = mat.getData(i, mat.getCol() - 1) / mat.getData(i, y); if (tmp_s < tmp) { id = i; tmp = tmp_s; @@ -181,8 +182,8 @@ public class Simplex { return id; } - public void printSimplex(Matrix mat, String s, int precision) { - mat.matrixPrint(s, precision); + public void printSimplex(Matrix mat, String s) { + mat.matrixPrint(s); } boolean signe(Double x) {