Select Git revision
Simplex.java
Simplex.java 4.32 KiB
import java.util.Arrays;
public class Simplex {
private Double[][] matEcart;
private Double[][] tabAux;
private int nbLines;
private int x;
private int y;
public Double[][] getMatEcart() {
return matEcart;
}
public void setMatEcartById(Double d, int x, int y) {
matEcart[x][y] = d;
}
public Double[][] getTabAux() {
return tabAux;
}
public void setTabAux(Double[][] tabAux) {
this.tabAux = tabAux;
}
public Simplex(int x, int y, int nbLines) {
this.x = x;
this.y = y;
this.matEcart = new Double[x][y];
this.nbLines = nbLines;
this.tabAux = new Double[x + nbLines + 1][y + 1];
}
void printSimplex(String s) {
System.out.println(s + ": ");
for (int i = 0; i < x; i++) {
System.out.print("[");
for (int j = 0; j < y; j++) {
System.out.printf(matEcart[i][j] < 0.0 ?
String.format("%.2f ", matEcart[i][j]) :
String.format(" %.2f ", matEcart[i][j]));
}
System.out.println("]");
}
}
void createMatEcart(Equation eq) {
int nbContraintes = eq.getNbContraintes();
// Matrice Amxn
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (j < nbContraintes) {
setMatEcartById(eq.getMatAtId(i, j), i, j);
} else {
setMatEcartById(j - nbContraintes == i ? 1.0 : 0.0, i, j);
}
}
}
// Membre de droite
for (int i = 0; i <= x-2; i++)
setMatEcartById(eq.getRightVec().get(i), i, y-1);
// Fonction obj
for (int i = 0; i < nbContraintes; i++)
setMatEcartById(eq.getFuncObj().get(i), x-1, i);
// overwrite la mat en bas à droite qui est à 1
setMatEcartById(0.0, x - 1, y - 1);
}
/**
* Si b[i] < 0 phase 1 sinon phase 2
* @return true = phase 1 | false = phase 2
*/
int which_phase(){
int res = -1;
for (int i = 0; i < x; i++) {
if(signe(matEcart[i][y - 1]))
res = i;
}
return res;
}
void tabAux(int line) {
Double[] tabRes = new Double[y];
Arrays.fill(tabRes, 0.0);
for (int j = 0; j < y; j ++) {
if( matEcart[line][j] != 0.0) {
double res = matEcart[line][j] * -1;
setMatEcartById(res, line, j);
}
for (int i = 0; i < x-1; i ++)
tabRes[j] -= matEcart[i][j];
}
for (int i = 0; i < x + nbLines + 1; i ++) {
for (int j = 0; j < y + 1; j ++) {
if(i < x && j < y)
tabAux[i][j] = matEcart[i][j];
else if(i >= x)
tabAux[i][j] = matEcart[i - 5][j];
else if (j == y)
tabAux[i][j] = tabRes[i];
}
}
}
int getFirstNeg() {
for (int j = 0; j < this.y; j++) {
if(signe(matEcart[x - 1][j])) return j;
}
return -1;
}
void pivot() {
int firstNeg = getFirstNeg();
if (firstNeg == -1) return;
boolean has_neg = false;
int id = ligneSortante(firstNeg);
double pivot = this.matEcart[id][firstNeg];
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][firstNeg];
for (int j = 0; j < this.y; j++) {
if (i != id) {
this.matEcart[i][j] -= pivot * this.matEcart[id][j];
}
}
}
for (int j = 0; j < this.y; j++)
if (signe(this.matEcart[x - 1][j])) {
has_neg = true;
}
if (has_neg) pivot();
}
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) {
return x < 0;
}
}