Skip to content
Snippets Groups Projects
Select Git revision
  • e4e715597ecc3e126f3e5f1d84bbc53aba892eb6
  • main default protected
  • thibault.capt-main-patch-72132
3 results

Simplex.java

Blame
  • Simplex.java 3.02 KiB
    public class Simplex {
        private final Double[][] matEcart;
        private final int x;
        private final int y;
    
        public void setMatEcartById(Double d, int x, int y) {
            matEcart[x][y] = d;
        }
    
        public Simplex(int x, int y) {
            this.x = x;
            this.y = y;
            this.matEcart = new Double[x][y];
        }
    
        void printSimplex(String s) {
            System.out.println(s + ": ");
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    if (matEcart[i][j] < 0.0)
                        System.out.print(matEcart[i][j] + " ");
                    else
                        System.out.print(" " + 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 <= nbContraintes; 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, width - 1, height - 1);
        }
    
        /**
         * Si b[i] < 0 phase 1 sinon phase 2
         * @return true = phase 1 | false = phase 2
         */
        boolean which_phase(){
            boolean res = true;
            for (int i = 0; i < x; i++) {
                if(signe(matEcart[i][y - 1]))
                    res = false;
            }
            return res;
        }
        void tabAux() {
    
        }
    
        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) {
            return x >= 0;
        }
    }