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

pivot ok

parent 0f86decb
No related branches found
No related tags found
No related merge requests found
...@@ -57,15 +57,14 @@ public class Main { ...@@ -57,15 +57,14 @@ public class Main {
elements = tmp.split(";"); elements = tmp.split(";");
switch (eq.getSens()) { switch (eq.getSens()) {
case "min": case "min", "max" ->
case "max":
// création de l'équation dans le sens min // création de l'équation dans le sens min
line = eq.createEq(elements, line); line = eq.createEq(elements, line);
break; default -> {
default:
System.err.println("Le sens n'a pas été entré."); System.err.println("Le sens n'a pas été entré.");
System.exit(1); System.exit(1);
} }
}
line++; line++;
} }
// Print // Print
...@@ -74,15 +73,13 @@ public class Main { ...@@ -74,15 +73,13 @@ public class Main {
// Tableau initiale // Tableau initiale
Simplex spx = new Simplex(4, 6); Simplex spx = new Simplex(4, 6);
spx.createMatEcart(eq); 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()) if (spx.which_phase())
spx.tabAux(); spx.tabAux();
else else
spx.pivot(); spx.pivot(0);
spx.printSimplex();
sc.close(); sc.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
......
public class Simplex { public class Simplex {
private final Double[][] matEcart; private final Double[][] matEcart;
private int width; private final int x;
private int height; private final int y;
public int getWidth() { public void setMatEcartById(Double d, int x, int y) {
return width; matEcart[x][y] = d;
} }
public void setWidth(int width) { public Simplex(int x, int y) {
this.width = width; this.x = x;
this.y = y;
this.matEcart = new Double[x][y];
} }
public int getHeight() { void printSimplex(String s) {
return height; System.out.println(s + ": ");
} for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
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++) {
if (matEcart[i][j] < 0.0) if (matEcart[i][j] < 0.0)
System.out.print(matEcart[i][j] + " "); System.out.print(matEcart[i][j] + " ");
else else
...@@ -59,8 +29,8 @@ public class Simplex { ...@@ -59,8 +29,8 @@ public class Simplex {
void createMatEcart(Equation eq) { void createMatEcart(Equation eq) {
int nbContraintes = eq.getNbContraintes(); int nbContraintes = eq.getNbContraintes();
// Matrice Amxn // Matrice Amxn
for (int i = 0; i < width; i++) { for (int i = 0; i < x; i++) {
for (int j = 0; j < height; j++) { for (int j = 0; j < y; j++) {
if (j < nbContraintes) { if (j < nbContraintes) {
setMatEcartById(eq.getMatAtId(i, j), i, j); setMatEcartById(eq.getMatAtId(i, j), i, j);
} else { } else {
...@@ -71,11 +41,11 @@ public class Simplex { ...@@ -71,11 +41,11 @@ public class Simplex {
// Membre de droite // Membre de droite
for (int i = 0; i <= nbContraintes; i++) 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 // Fonction obj
for (int i = 0; i < nbContraintes; i++) 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 // overwrite la mat en bas à droite qui est à 1
setMatEcartById(0.0, width - 1, height - 1); setMatEcartById(0.0, width - 1, height - 1);
...@@ -87,8 +57,8 @@ public class Simplex { ...@@ -87,8 +57,8 @@ public class Simplex {
*/ */
boolean which_phase(){ boolean which_phase(){
boolean res = true; boolean res = true;
for (int i = 0; i < width; i++) { for (int i = 0; i < x; i++) {
if(!signe(matEcart[i][height - 1])) if(signe(matEcart[i][y - 1]))
res = false; res = false;
} }
return res; return res;
...@@ -97,8 +67,42 @@ public class Simplex { ...@@ -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) { boolean signe(Double x) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment