Skip to content
Snippets Groups Projects
Commit 7091eb31 authored by juliano.souzaluz's avatar juliano.souzaluz
Browse files

correction de bug depuis la création de matrice

parent 4c8f325b
No related branches found
No related tags found
No related merge requests found
...@@ -4,9 +4,10 @@ import java.util.List; ...@@ -4,9 +4,10 @@ import java.util.List;
public class Equation { public class Equation {
private String sens; private String sens;
private int nbContraintes; private int nbContraintes;
private final List<Double> funcObj; private List<Double> funcObj;
private final List<Double> rightVec; private List<Double> rightVec;
private final Matrix mat; private Matrix mat;
/** /**
* getter sens * getter sens
...@@ -66,6 +67,10 @@ public class Equation { ...@@ -66,6 +67,10 @@ public class Equation {
return mat.getData(w, h); return mat.getData(w, h);
} }
public Matrix getMat() {
return this.mat;
}
/** /**
* Constructeur * Constructeur
* *
...@@ -117,6 +122,7 @@ public class Equation { ...@@ -117,6 +122,7 @@ public class Equation {
else this.rightVec.add(-res); else this.rightVec.add(-res);
// Matrice // Matrice
this.mat.matrixRealloc(this.mat.getX() + 1, this.mat.getY());
for (int i = 0; i < this.nbContraintes; i++) { for (int i = 0; i < this.nbContraintes; i++) {
double tmp = Double.parseDouble(elements[i]); double tmp = Double.parseDouble(elements[i]);
this.mat.setData(line, i, tmp); this.mat.setData(line, i, tmp);
...@@ -153,7 +159,7 @@ public class Equation { ...@@ -153,7 +159,7 @@ public class Equation {
/** /**
* Print les vecteurs et la matrice * Print les vecteurs et la matrice
*/ */
public void printEq(int w) { public void printEq() {
// Fonction obj // Fonction obj
System.out.println("Fonction obj: " + getFuncObj()); System.out.println("Fonction obj: " + getFuncObj());
...@@ -161,8 +167,8 @@ public class Equation { ...@@ -161,8 +167,8 @@ public class Equation {
System.out.println("Vecteur membre de droite: " + getRightVec()); System.out.println("Vecteur membre de droite: " + getRightVec());
System.out.println("Matrice Amxn:"); System.out.println("Matrice Amxn:");
for (int i = 0; i < w; i++) { for (int i = 0; i < this.mat.getX(); i++) {
for (int j = 0; j < this.nbContraintes; j++) { for (int j = 0; j < this.mat.getY(); j++) {
if (this.mat.getData(i, j) < 0.0) if (this.mat.getData(i, j) < 0.0)
System.out.print(this.mat.getData(i, j) + " "); System.out.print(this.mat.getData(i, j) + " ");
else else
......
...@@ -48,7 +48,7 @@ public class Main { ...@@ -48,7 +48,7 @@ public class Main {
sc = new Scanner(f); // remettre le scanner à la première ligne sc = new Scanner(f); // remettre le scanner à la première ligne
int contraintes = nbContraintes(sc); int contraintes = nbContraintes(sc);
sc = new Scanner(f); // remettre le scanner à la première ligne 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 // Max / Min + function obj
String firstLine = sc.nextLine(); String firstLine = sc.nextLine();
...@@ -72,12 +72,12 @@ public class Main { ...@@ -72,12 +72,12 @@ public class Main {
line++; line++;
} }
// Print // Print
eq.printEq(line); eq.printEq();
// Tableau initial // 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.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 // 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();
...@@ -87,7 +87,7 @@ public class Main { ...@@ -87,7 +87,7 @@ public class Main {
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", 3);
} }
sc.close(); sc.close();
......
...@@ -22,6 +22,7 @@ public class Matrix { ...@@ -22,6 +22,7 @@ public class Matrix {
public double getData(int x, int y) { public double getData(int x, int y) {
return data[x][y]; return data[x][y];
} }
public double[][] getDatas() { public double[][] getDatas() {
return data; return data;
} }
...@@ -36,18 +37,19 @@ public class Matrix { ...@@ -36,18 +37,19 @@ public class Matrix {
this.data = new double[x][y]; 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 i = 0; i < x; i++) {
for (int j = 0; j < y; j++) { 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 { public void matrixInitFromArray(double[] tab) throws IndexOutOfBoundsException {
int id = 0; int id = 0;
for (int i = 0; i < x; i ++) { for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j ++) { for (int j = 0; j < y; j++) {
this.data[i][j] = tab[id]; this.data[i][j] = tab[id];
id++; id++;
} }
...@@ -62,14 +64,14 @@ public class Matrix { ...@@ -62,14 +64,14 @@ public class Matrix {
matrixFill(x, y, tmp); matrixFill(x, y, tmp);
} }
public void matrixPrint(String s) { public void matrixPrint(String s, int precision) {
System.out.println(s + ": "); System.out.println(s + ": ");
for (int i = 0; i < x; i++) { for (int i = 0; i < x; i++) {
System.out.print("["); System.out.print("[");
for (int j = 0; j < y; j++) { for (int j = 0; j < y; j++) {
System.out.printf(this.data[i][j] < 0.0 ? System.out.printf(this.data[i][j] < 0.0 ?
String.format("%.2f ", this.data[i][j]) : String.format("%." + precision + "f ", this.data[i][j]) :
String.format(" %.2f ", this.data[i][j])); String.format(" %." + precision + "f ", this.data[i][j]));
} }
System.out.println("]"); System.out.println("]");
} }
......
import java.util.Arrays; import java.util.Arrays;
public class Simplex { public class Simplex {
private final Matrix matEcart; private Matrix matEcart;
private final Matrix tabAux; private Matrix tabAux;
private final int nbLines; private int nbSousCondition;
private final int x; private int x;
private final int y; private int y;
public double[][] getTabAux() { public double[][] getTabAux() {
return tabAux.getDatas(); return tabAux.getDatas();
} }
public Simplex(int x, int y, int nbLines) { public Simplex(int x, int y, int nbSousCondition) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.matEcart = new Matrix(x, y); this.matEcart = new Matrix(x, y);
this.nbLines = nbLines; this.nbSousCondition = nbSousCondition;
this.tabAux = new Matrix(x + nbLines + 1, y + 1); this.tabAux = new Matrix(x + 2, y + nbSousCondition + 1);
} }
void createSimplex(Equation eq, int nbContraintes) { void createSimplex(Equation eq, int nbContraintes) {
...@@ -32,15 +32,13 @@ public class Simplex { ...@@ -32,15 +32,13 @@ public class Simplex {
} }
// Membre de droite // 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)); this.matEcart.setData(i, this.y - 1, eq.getRightVec().get(i));
// Fonction obj // Fonction obj
this.matEcart.matrixRealloc(this.matEcart.getX() + 1, this.matEcart.getY());
for (int i = 0; i < nbContraintes; i++) for (int i = 0; i < nbContraintes; i++)
this.matEcart.setData(this.x - 1, i, eq.getFuncObj().get(i)); this.matEcart.setData(this.x, 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);
} }
/** /**
...@@ -57,17 +55,18 @@ public class Simplex { ...@@ -57,17 +55,18 @@ public class Simplex {
} }
void tabAux(int line) { void tabAux(int line) {
Double[] tabRes = new Double[this.y]; double[] tabRes = new double[this.y];
Arrays.fill(tabRes, 0.0); Arrays.fill(tabRes, 0.0);
for (int j = 0; j < this.y; j++) { for (int j = 0; j < this.y; j++) {
if (this.matEcart.getData(line, j) != 0.0) { if (this.matEcart.getData(line, j) != 0.0) {
double res = this.matEcart.getData(line, j) * -1; double res = this.matEcart.getData(line, j) * -1;
this.matEcart.setData(line, j, res); 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); 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++) { for (int j = 0; j < this.y + 1; j++) {
if (i < this.x && j < this.y) if (i < this.x && j < this.y)
this.tabAux.setData(i, j, this.matEcart.getData(i, j)); this.tabAux.setData(i, j, this.matEcart.getData(i, j));
...@@ -81,7 +80,7 @@ public class Simplex { ...@@ -81,7 +80,7 @@ public class Simplex {
int getFirstNeg() { int getFirstNeg() {
for (int j = 0; j < this.y; j++) { 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; return -1;
} }
...@@ -95,7 +94,7 @@ public class Simplex { ...@@ -95,7 +94,7 @@ public class Simplex {
for (int i = 0; i < this.y; i++) { for (int i = 0; i < this.y; i++) {
this.matEcart.setData(id, i, this.matEcart.getData(id, i) / pivot); 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); pivot = this.matEcart.getData(i, firstNeg);
for (int j = 0; j < this.y; j++) { for (int j = 0; j < this.y; j++) {
if (i != id) { if (i != id) {
...@@ -104,7 +103,7 @@ public class Simplex { ...@@ -104,7 +103,7 @@ public class Simplex {
} }
} }
for (int j = 0; j < this.y; j++) 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; has_neg = true;
} }
if (has_neg) pivot(); if (has_neg) pivot();
...@@ -124,8 +123,8 @@ public class Simplex { ...@@ -124,8 +123,8 @@ public class Simplex {
return id; return id;
} }
public void printSimplex(String s) { public void printSimplex(String s, int precision) {
this.matEcart.matrixPrint(s); this.matEcart.matrixPrint(s, precision);
} }
boolean signe(Double x) { boolean signe(Double x) {
......
...@@ -2,4 +2,4 @@ max;8;9 ...@@ -2,4 +2,4 @@ max;8;9
2;5;<=;12 2;5;<=;12
50;5;<=;150 50;5;<=;150
5;50;<=;100 5;50;<=;100
1;1;>=;1 1;2;>=;1
\ No newline at end of file \ 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