Skip to content
Snippets Groups Projects
Commit 734a0c98 authored by thibault.capt's avatar thibault.capt
Browse files

change code for list

parent 52fcbd6b
No related branches found
No related tags found
No related merge requests found
...@@ -72,22 +72,22 @@ public class Main { ...@@ -72,22 +72,22 @@ public class Main {
eq.printEq(line); eq.printEq(line);
// Tableau initiale // Tableau initiale
Simplex spx = new Simplex(line+1, line + 3, widthMat); Simplex2 spx = new Simplex2(widthMat, line+1, line + 3);
spx.createMatEcart(eq); spx.createSimplex(eq);
spx.printSimplex("Tableau"); 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 // true = phase 1 membres de droite pas admissible | false = phase 2 membres de droite admissible
int phase = spx.which_phase(); int phase = spx.which_phase();
if (phase != -1) { if (phase != -1) {
spx.tabAux(phase); /*spx.tabAux(phase);
System.out.println("Tableau Auxiliaire:"); System.out.println("Tableau Auxiliaire:");
System.out.println(Arrays.deepToString(spx.getTabAux())); System.out.println(Arrays.deepToString(spx.getTabAux()));*/
} }
else { else {
spx.pivot(); spx.pivot();
spx.printSimplex("pivot"); spx.printSimplex("pivot");
} }
sc.close(); sc.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
...@@ -2,15 +2,122 @@ import java.util.ArrayList; ...@@ -2,15 +2,122 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Simplex2 { public class Simplex2 {
private int nbLines; private final int nbLines;
private int x; private int x;
private int y; private int y;
private ArrayList<ArrayList<Double>> simplex; 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.nbLines = nbLines;
this.x = x; this.x = x;
this.y = y; 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();
} }
} }
...@@ -2,4 +2,3 @@ min;-8;-9; ...@@ -2,4 +2,3 @@ min;-8;-9;
2;5;<=;12; 2;5;<=;12;
50;5;<=;150; 50;5;<=;150;
5;50;<=;100; 5;50;<=;100;
-1;-2;<=;-1;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment