diff --git a/src/Equation.java b/src/Equation.java index b2aefd9e71108b3e1b1ded709d406431a537dd67..afb8867895c81aa59d136ad02c51867fee73be25 100644 --- a/src/Equation.java +++ b/src/Equation.java @@ -4,9 +4,10 @@ import java.util.List; public class Equation { private String sens; private int nbContraintes; - private final List<Double> funcObj; - private final List<Double> rightVec; - private final Matrix mat; + private List<Double> funcObj; + private List<Double> rightVec; + private Matrix mat; + /** * getter sens @@ -66,6 +67,10 @@ public class Equation { return mat.getData(w, h); } + public Matrix getMat() { + return this.mat; + } + /** * Constructeur * @@ -117,6 +122,7 @@ public class Equation { else this.rightVec.add(-res); // Matrice + this.mat.matrixRealloc(this.mat.getX() + 1, this.mat.getY()); for (int i = 0; i < this.nbContraintes; i++) { double tmp = Double.parseDouble(elements[i]); this.mat.setData(line, i, tmp); @@ -153,7 +159,7 @@ public class Equation { /** * Print les vecteurs et la matrice */ - public void printEq(int w) { + public void printEq() { // Fonction obj System.out.println("Fonction obj: " + getFuncObj()); @@ -161,8 +167,8 @@ public class Equation { System.out.println("Vecteur membre de droite: " + getRightVec()); System.out.println("Matrice Amxn:"); - for (int i = 0; i < w; i++) { - for (int j = 0; j < this.nbContraintes; j++) { + for (int i = 0; i < this.mat.getX(); i++) { + for (int j = 0; j < this.mat.getY(); 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 82260776b615bdaeb38cb50f78b9bd68135df270..971c0b5bdce68577de875b1f743f980f92cfaad4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -48,7 +48,7 @@ public class Main { sc = new Scanner(f); // remettre le scanner à la première ligne int contraintes = nbContraintes(sc); sc = new Scanner(f); // remettre le scanner à la première ligne - Equation eq = new Equation(sousCondition * 2, contraintes); // lignes supp au cas où, il y a des "=" + Equation eq = new Equation(sousCondition, contraintes); // lignes supp au cas où, il y a des "=" // Max / Min + function obj String firstLine = sc.nextLine(); @@ -72,12 +72,12 @@ public class Main { line++; } // Print - eq.printEq(line); + eq.printEq(); // Tableau initial - Simplex spx = new Simplex(sousCondition + 1, line + contraintes, line + 3); + Simplex spx = new Simplex(eq.getMat().getX(), eq.getMat().getX() + eq.getMat().getY() + 1, line); spx.createSimplex(eq, contraintes); - spx.printSimplex("Tableau"); + spx.printSimplex("Tableau", 0); // true = phase 1 membres de droite pas admissible | false = phase 2 membres de droite admissible int phase = spx.which_phase(); @@ -87,7 +87,7 @@ public class Main { System.out.println(Arrays.deepToString(spx.getTabAux())); } else { spx.pivot(); - spx.printSimplex("pivot"); + spx.printSimplex("pivot", 3); } sc.close(); diff --git a/src/Matrix.java b/src/Matrix.java index 72d4fddf696937ebd6d6019dcf31a279728cc37a..9507d824fb164f3b9a0bb4a8deb1d92350d60dc0 100644 --- a/src/Matrix.java +++ b/src/Matrix.java @@ -22,6 +22,7 @@ public class Matrix { public double getData(int x, int y) { return data[x][y]; } + public double[][] getDatas() { return data; } @@ -36,18 +37,19 @@ public class Matrix { this.data = new double[x][y]; } - public void matrixFill(int x, int y, double[][] tab) throws IndexOutOfBoundsException{ + 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++) { - this.data[i][j] = tab[i][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 ++) { + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { this.data[i][j] = tab[id]; id++; } @@ -62,14 +64,14 @@ public class Matrix { matrixFill(x, y, tmp); } - public void matrixPrint(String s) { + public void matrixPrint(String s, int precision) { System.out.println(s + ": "); for (int i = 0; i < x; i++) { System.out.print("["); for (int j = 0; j < y; j++) { System.out.printf(this.data[i][j] < 0.0 ? - String.format("%.2f ", this.data[i][j]) : - String.format(" %.2f ", this.data[i][j])); + String.format("%." + precision + "f ", this.data[i][j]) : + String.format(" %." + precision + "f ", this.data[i][j])); } System.out.println("]"); } diff --git a/src/Simplex.java b/src/Simplex.java index 7bc9ed84ce1fe465b56506a53f3a9f92579c74a8..28858c4a5430c961db56d860c2acfd6c6943cc15 100644 --- a/src/Simplex.java +++ b/src/Simplex.java @@ -1,22 +1,22 @@ import java.util.Arrays; public class Simplex { - private final Matrix matEcart; - private final Matrix tabAux; - private final int nbLines; - private final int x; - private final int y; + private Matrix matEcart; + private Matrix tabAux; + private int nbSousCondition; + private int x; + private int y; public double[][] getTabAux() { return tabAux.getDatas(); } - public Simplex(int x, int y, int nbLines) { + public Simplex(int x, int y, int nbSousCondition) { this.x = x; this.y = y; this.matEcart = new Matrix(x, y); - this.nbLines = nbLines; - this.tabAux = new Matrix(x + nbLines + 1, y + 1); + this.nbSousCondition = nbSousCondition; + this.tabAux = new Matrix(x + 2, y + nbSousCondition + 1); } void createSimplex(Equation eq, int nbContraintes) { @@ -32,15 +32,13 @@ public class Simplex { } // Membre de droite - for (int i = 0; i <= this.x - 2; i++) + for (int i = 0; i <= this.x - 1; i++) this.matEcart.setData(i, this.y - 1, eq.getRightVec().get(i)); // Fonction obj + this.matEcart.matrixRealloc(this.matEcart.getX() + 1, this.matEcart.getY()); for (int i = 0; i < nbContraintes; i++) - this.matEcart.setData(this.x - 1, i, eq.getFuncObj().get(i)); - - // overwrite la matrice en bas à droite qui est à 1. - this.matEcart.setData(this.x - 1, this.y - 1, 0.0); + this.matEcart.setData(this.x, i, eq.getFuncObj().get(i)); } /** @@ -57,17 +55,18 @@ public class Simplex { } void tabAux(int line) { - Double[] tabRes = new Double[this.y]; + double[] tabRes = new double[this.y]; Arrays.fill(tabRes, 0.0); for (int j = 0; j < this.y; j++) { if (this.matEcart.getData(line, j) != 0.0) { double res = this.matEcart.getData(line, j) * -1; this.matEcart.setData(line, j, res); } - for (int i = 0; i < this.x - 1; i++) + for (int i = 0; i < this.x; i++) tabRes[j] -= this.matEcart.getData(i, j); } - for (int i = 0; i < this.x + nbLines + 1; i++) { + + for (int i = 0; i < this.x + nbSousCondition + 1; i++) { for (int j = 0; j < this.y + 1; j++) { if (i < this.x && j < this.y) this.tabAux.setData(i, j, this.matEcart.getData(i, j)); @@ -81,7 +80,7 @@ public class Simplex { int getFirstNeg() { for (int j = 0; j < this.y; j++) { - if (signe(this.matEcart.getData(this.x - 1, j))) return j; + if (signe(this.matEcart.getData(this.x, j))) return j; } return -1; } @@ -95,7 +94,7 @@ public class Simplex { for (int i = 0; i < this.y; i++) { this.matEcart.setData(id, i, this.matEcart.getData(id, i) / pivot); } - for (int i = 0; i < this.x; i++) { + for (int i = 0; i < this.x + 1; i++) { pivot = this.matEcart.getData(i, firstNeg); for (int j = 0; j < this.y; j++) { if (i != id) { @@ -104,7 +103,7 @@ public class Simplex { } } for (int j = 0; j < this.y; j++) - if (signe(this.matEcart.getData(this.x - 1, j))) { + if (signe(this.matEcart.getData(this.x, j))) { has_neg = true; } if (has_neg) pivot(); @@ -124,8 +123,8 @@ public class Simplex { return id; } - public void printSimplex(String s) { - this.matEcart.matrixPrint(s); + public void printSimplex(String s, int precision) { + this.matEcart.matrixPrint(s, precision); } boolean signe(Double x) { diff --git a/src/inputNonAdmissible.txt b/src/inputNonAdmissible.txt index df16df5c565b627916ed63168fa8e381e1e35d67..d828faa56855b8b25bb383479015c519290b7c11 100644 --- a/src/inputNonAdmissible.txt +++ b/src/inputNonAdmissible.txt @@ -2,4 +2,4 @@ max;8;9 2;5;<=;12 50;5;<=;150 5;50;<=;100 -1;1;>=;1 \ No newline at end of file +1;2;>=;1 \ No newline at end of file