diff --git a/src/Main.java b/src/Main.java index 2e2086af14822928cf8c31d692bc21beaa5e08c4..0a163e39f20b6a777bb4f2d121bc5b3d269dfdbd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -72,22 +72,22 @@ public class Main { eq.printEq(line); // Tableau initiale - Simplex spx = new Simplex(line+1, line + 3, widthMat); - spx.createMatEcart(eq); + Simplex2 spx = new Simplex2(widthMat, line+1, line + 3); + spx.createSimplex(eq); spx.printSimplex("Tableau"); - //System.out.println(Arrays.deepToString(spx.getMatEcart())); // true = phase 1 membres de droite pas admissible | false = phase 2 membres de droite admissible int phase = spx.which_phase(); if (phase != -1) { - spx.tabAux(phase); + /*spx.tabAux(phase); System.out.println("Tableau Auxiliaire:"); - System.out.println(Arrays.deepToString(spx.getTabAux())); + System.out.println(Arrays.deepToString(spx.getTabAux()));*/ } else { spx.pivot(); spx.printSimplex("pivot"); } + sc.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e); diff --git a/src/Simplex2.java b/src/Simplex2.java index 97906e8a6522f87d9abe75ae78ae1c2273193807..c84b2870f944d5ccf33b2946e81206b466dcf097 100644 --- a/src/Simplex2.java +++ b/src/Simplex2.java @@ -2,15 +2,122 @@ import java.util.ArrayList; import java.util.List; public class Simplex2 { - private int nbLines; + private final int nbLines; private int x; private int y; private ArrayList<ArrayList<Double>> simplex; - public Simplex2(int nbLines, int x, int y, ArrayList<ArrayList<Double>> simplex) { + public ArrayList<ArrayList<Double>> getSimplex() { + return simplex; + } + + public void setSimplex(ArrayList<ArrayList<Double>> simplex) { + this.simplex = simplex; + } + + public Simplex2(int nbLines, int x, int y) { this.nbLines = nbLines; this.x = x; this.y = y; - this.simplex = new ArrayList<>(); + this.simplex = new ArrayList<ArrayList<Double>>(); + } + + public void printSimplex(String title) { + System.out.println(title + ": "); + for (ArrayList<Double> doubles : simplex) { + System.out.print("["); + for (Double d : doubles) { + if(d != null) { + System.out.printf(d < 0.0 ? + String.format("%.2f ", d) : + String.format(" %.2f ", d)); + } + } + System.out.println("]"); + } + } + + void createSimplex(Equation eq) { + int nbContraintes = eq.getNbContraintes(); + for (int i = 0; i < x; i++) { + ArrayList<Double> tmp = new ArrayList<>(); + for (int j = 0; j < y; j++) { + if(j < nbContraintes) + tmp.add(eq.getMatAtId(i, j)); + else + tmp.add(j - nbContraintes == i ? 1.0 : 0.0); + } + simplex.add(tmp); + } + + // Membre de droite + for (int i = 0; i < x - 1; i ++) + simplex.get(i).set(y - 1, eq.getRightVec().get(i)); + + // Fonction obj + ArrayList<Double> tmp = (ArrayList<Double>) eq.getFuncObj(); + for (int i = 0; i < y - 2; i++) tmp.add(0.0); + simplex.set(x-1, tmp); + } + + boolean signe(Double d) { + return d < 0; + } + + int which_phase() { + int res = -1; + int id = 0; + for (ArrayList<Double> doubles : simplex) { + if(signe(doubles.get(doubles.size() - 1))) { + res = id; + } + id ++; + } + return res; + } + + private int getFirstNeg() { + for (Double d : simplex.get(x-1)) + if(signe(d)) return simplex.get(x-1).indexOf(d); + return -1; + } + + private int ligneSortante(int y) { + int id = 0; + int i = 0; + double tmp = simplex.get(id).get(this.y-1) / simplex.get(id).get(y); + for (ArrayList<Double> doubles : simplex) { + double tmp_s = doubles.get(this.y-1) / doubles.get(y-1); + if(tmp_s < tmp) { + id = i; + tmp = tmp_s; + } + i++; + } + return id; + } + + void pivot() { + int firstNeg = getFirstNeg(); + if(firstNeg == -1) return; + boolean has_neg = false; + int id = ligneSortante(firstNeg); + double pivot = simplex.get(id).get(firstNeg); + for (Double d : simplex.get(id)) + d /= pivot; + for (ArrayList<Double> doubles : simplex) { + pivot = doubles.get(firstNeg); + for (Double d : doubles) { + if(doubles.indexOf(d) != id) { + d -= pivot * simplex.get(id).get(doubles.indexOf(d)); + } + } + } + for (Double d : simplex.get(x-1)) { + if(signe(d)) { + has_neg = true; + } + } + if(has_neg) pivot(); } } diff --git a/src/input.txt b/src/input.txt index afe5e0bfc678a40ccf1f84ba4ec34edf3dd5ead4..b51d0e527993a73118d37ad00fb3712b055a80e5 100644 --- a/src/input.txt +++ b/src/input.txt @@ -1,5 +1,4 @@ min;-8;-9; 2;5;<=;12; 50;5;<=;150; -5;50;<=;100; --1;-2;<=;-1; \ No newline at end of file +5;50;<=;100; \ No newline at end of file