diff --git a/src/Main.java b/src/Main.java index b6198d3efd625721593bd6a579b1e3d9e0ba9962..368b2da61a5cd0a9eea6124fff518f525ceef94b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -56,15 +56,14 @@ public class Main { String tmp = sc.nextLine(); elements = tmp.split(";"); - switch(eq.getSens()) { - case "min": - case "max": + switch (eq.getSens()) { + case "min", "max" -> // création de l'équation dans le sens min - line = eq.createEq(elements, line); - break; - default: + line = eq.createEq(elements, line); + default -> { System.err.println("Le sens n'a pas été entré."); System.exit(1); + } } line++; } @@ -74,15 +73,13 @@ public class Main { // Tableau initiale Simplex spx = new Simplex(4, 6); spx.createMatEcart(eq); + spx.printSimplex("Tableau"); - // true = phase 1 | false = phase 2 + // true = phase 1 membres de droite pas admissible | false = phase 2 membres de droite admissible if (spx.which_phase()) spx.tabAux(); else - spx.pivot(); - - spx.printSimplex(); - + spx.pivot(0); sc.close(); } catch (FileNotFoundException e) { diff --git a/src/Simplex.java b/src/Simplex.java index 299708937700abcb5bbb24802d84852907639e85..75d03f26fe86fdf15a506c9235a3499849013152 100644 --- a/src/Simplex.java +++ b/src/Simplex.java @@ -1,52 +1,22 @@ public class Simplex { private final Double[][] matEcart; - private int width; - private int height; + private final int x; + private final int y; - public int getWidth() { - return width; + public void setMatEcartById(Double d, int x, int y) { + matEcart[x][y] = d; } - public void setWidth(int width) { - this.width = width; + public Simplex(int x, int y) { + this.x = x; + this.y = y; + this.matEcart = new Double[x][y]; } - public int getHeight() { - return height; - } - - public void setHeight(int height) { - this.height = height; - } - - public Double getMatEcartById(int i, int j) { - return matEcart[i][j]; - } - - public Double[][] getMatEcart() { - return matEcart; - } - - public void setMatEcartById(Double d, int i, int j) { - matEcart[i][j] = d; - } - - public Simplex(int w, int h) { - this.width = w; - this.height = h; - this.matEcart = new Double[w][h]; - } - - public Simplex() { - this.width = 0; - this.height = 0; - this.matEcart = new Double[0][0]; - } - - void printSimplex() { - System.out.println("Tableau: "); - for (int i = 0; i < width; i++) { - for (int j = 0; j < height; j++) { + void printSimplex(String s) { + System.out.println(s + ": "); + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { if (matEcart[i][j] < 0.0) System.out.print(matEcart[i][j] + " "); else @@ -59,8 +29,8 @@ public class Simplex { void createMatEcart(Equation eq) { int nbContraintes = eq.getNbContraintes(); // Matrice Amxn - for (int i = 0; i < width; i++) { - for (int j = 0; j < height; j++) { + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { if (j < nbContraintes) { setMatEcartById(eq.getMatAtId(i, j), i, j); } else { @@ -71,11 +41,11 @@ public class Simplex { // Membre de droite for (int i = 0; i <= nbContraintes; i++) - setMatEcartById(eq.getRightVec().get(i), i, height-1); + setMatEcartById(eq.getRightVec().get(i), i, y-1); // Fonction obj for (int i = 0; i < nbContraintes; i++) - setMatEcartById(eq.getFuncObj().get(i), width-1, i); + setMatEcartById(eq.getFuncObj().get(i), x-1, i); // overwrite la mat en bas à droite qui est à 1 setMatEcartById(0.0, width - 1, height - 1); @@ -87,8 +57,8 @@ public class Simplex { */ boolean which_phase(){ boolean res = true; - for (int i = 0; i < width; i++) { - if(!signe(matEcart[i][height - 1])) + for (int i = 0; i < x; i++) { + if(signe(matEcart[i][y - 1])) res = false; } return res; @@ -97,8 +67,42 @@ public class Simplex { } - void pivot() { + void pivot(int y) { + boolean has_neg = false; + int id = ligneSortante(y); + double pivot = this.matEcart[id][y]; + for (int i = 0; i < this.y; i++) { + this.matEcart[id][i] /= pivot; + } + for (int i = 0; i < this.x; i++) { + pivot = this.matEcart[i][y]; + for (int j = 0; j < this.y; j++) { + if (i != id) { + this.matEcart[i][j] -= pivot * this.matEcart[id][j]; + } + } + } + printSimplex("pivot"); + for (int j = 0; j < this.y; j++) + if (!signe(this.matEcart[x - 1][j])) { + has_neg = true; + y = j; + } + if (has_neg) pivot(y); + } + + int ligneSortante(int y) { + int id = 0; + double tmp = this.matEcart[id][this.y - 1] / this.matEcart[id][y]; + for (int i = 1; i < this.x - 1; i++) { + double tmp_s = this.matEcart[i][this.y - 1] / this.matEcart[i][y]; + if(tmp_s < tmp) { + id = i; + tmp = tmp_s; + } + } + return id; } boolean signe(Double x) {