Code java du simplexe (méthode des pénalités

Fermé
cixion Messages postés 12 Date d'inscription samedi 27 juin 2009 Statut Membre Dernière intervention 29 novembre 2011 - 27 mai 2011 à 19:38
cixion Messages postés 12 Date d'inscription samedi 27 juin 2009 Statut Membre Dernière intervention 29 novembre 2011 - 27 mai 2011 à 19:40
Bonjour,

je suis en train de coder la méthode des pénalités en java mais je n'obtiens pas les bons résultats , est-ce que l'on pourrait m'aider svp je vous fournis l'algo que j'ai codé et mon code
si vous avez des conseils ou astuces à me donner pour optimiser mon code , don't hesitate !
cordialement
ma classe simplexe :public class Simplexe {

	public Simplexe(int nbVarDec, int nbContraintes, double[] fctObj,
			double[][] cont, double[] c, String[] typeContrainte) {

		for (int i = 0; i < nbContraintes; i++) {
			if (c[i] < 0) {

				for (int j = 0; j < nbVarDec; j++) { // pour chaque colonne de
					// la matrice cont on
					// inverse

					cont[i][j] = -cont[i][j];

				}
				c[i] = -c[i];
				if (typeContrainte[i].equals("<=")) {
					typeContrainte[i] = ">=";
				} else if (typeContrainte[i].equals(">=")) {
					typeContrainte[i] = "<=";
				}
			}
		}

		// on compte le nombre total de variables
		int nbVarTot = nbVarDec;
		for (int i = 0; i < nbContraintes; i++) {
			if (typeContrainte[i].equals("<="))

			{
				nbVarTot = nbVarTot + 1;
			}// variable d'ecart

			else if (typeContrainte[i].equals(">=")) {
				nbVarTot = nbVarTot + 2;
			}// variable d'ecart+variable artificielle

			else {
				nbVarTot = nbVarTot + 1;
			}// variable artificielle
		}

		// Allocation du tableau pour la fonction objectif

		double[] fctObjEA = new double[nbVarTot];

		// Allocation du tableau pour repérer les variables de bases

		boolean[] VB = new boolean[nbVarTot];

		// Allocation de la matrice pour la standardisation des contraintes
		double[][] contEA = new double[nbContraintes][nbVarTot];

		// Allocation du tableau pour la correspondance entre les contraintes et
		// les variables de base

		int[] noVBcont = new int[nbContraintes];

		// allocation du tableau pour la correspondance entre les variables
		// d'ecart et les contraintes
		int[] noVEcont = new int[nbContraintes];

		// Initialisation des coefficients de la fonction objectif

		for (int j = 0; j < nbVarDec; j++) {
			fctObjEA[j] = fctObj[j];

		}

		// Initialisation des coefficients de la fonction objectif

		int col = nbVarDec;

		// Initialisation du coefficient pour les variables artificielles

		double M = 1000000000;// ??? vaut un très grand nombre positif

		// Ajout des variables d'écart et des variables artificielles

		for (int i = 0; i < nbContraintes; i++) {
			for (int j = 0; j < nbVarDec; j++) {
				contEA[i][j] = cont[i][j];
			}
			for (int j = nbVarDec + 1; j < col - 1; j++) {
				contEA[i][j] = 0;
			}

			if (typeContrainte[i].equals("<=")) {
				contEA[i][col] = 1;
				fctObjEA[col] = 0;
				VB[col] = true;
				noVBcont[i] = col;
				noVEcont[i] = col;
				col = col + 1;
			} else if (typeContrainte[i].equals(">=")) // On ajoute une variable
				// d'écart
			{
				contEA[i][col] = -1;
				fctObjEA[col] = 0;
				VB[col] = false;
				col = col + 1;

				contEA[i][col] = 1;
				fctObjEA[col] = -M;
				VB[col] = true;
				noVBcont[i] = col;
				col = col + 1;
			} else {
				contEA[i][col] = 1;
				fctObjEA[col] = -M;
				VB[col] = true;
				noVBcont[i] = col;
				col = col + 1;

			}
		}
		// les différentes itérations du simplexe
		// Initialisation du nombre epsilon pour éviter les bouclages

		double epsilon = 0.00000001;
		boolean algotermine = false;

		while (!algotermine) {
			// On cherche la variable entrante parmi les variables hors base
			int noVarEntrante = 0;
			double deltaMax = 0;
			String typeSolution = "UNIQUE";
			for (int j = 0; j < nbVarTot; j++) {
				if (!VB[j]) {
					double delta = fctObjEA[j];

					for (int i = 0; i < nbContraintes; i++) {
						delta = delta - contEA[i][j] * fctObjEA[noVBcont[i]];
						if (noVarEntrante == 0) {
							deltaMax = delta;
							noVarEntrante = j;

						} else if (delta > deltaMax) {
							deltaMax = delta;
							noVarEntrante = j;

						}

					}

				}

			}

			if (deltaMax <= 0) {
				algotermine = true;
				System.out.println(algotermine);
				int i = 0;
				while (typeSolution == "UNIQUE" && i < nbContraintes) {
					System.out.println(typeSolution);

					if (fctObjEA[noVBcont[i]] == -M) {
						typeSolution = "VIDE";
						System.out.println(typeSolution);
					}
					i = i + 1;

				}
			} else // On cherche la variable sortante
			{
				int noLignePivot = 0;// numéro de ligne indéfinie
				for (int i = 0; i < nbContraintes; i++) {
					for (int h = 0; h < contEA.length; h++) {
						for (int k = 0; k < contEA.length; k++) {
							System.out.println(contEA[k][h]);

						}
					}

					double rapport;
					if (contEA[i][noVarEntrante] > 0) {
						rapport = c[i] / contEA[i][noVarEntrante];
						double rapportMin = 0;
						if (noLignePivot == 0) {
							rapportMin = rapport;
							noLignePivot = i;

						} else if (rapport < rapportMin) {
							rapportMin = rapport;
							noLignePivot = i;

						}
					}
				}
				if (noLignePivot == 0) {
					algotermine = true;
					typeSolution = "INFINI";
					System.out.println(typeSolution);

				} else
					algotermine = false;
				VB[noVarEntrante] = true;
				int pos=noVBcont[noLignePivot];
				VB[pos]= false;
				noVBcont[noLignePivot] = noVarEntrante;
				// Dans la matrice contEA, on normalise la ligne pivot
				double coeff = contEA[noLignePivot][noVarEntrante];

				System.out.println(algotermine);





				for (int j = 1; j < nbVarTot; j++) {
					contEA[noLignePivot][j] = contEA[noLignePivot][j] / coeff;
				}
				c[noLignePivot] = c[noLignePivot] / coeff;

				// Dans la matrice contEA, on fait apparaître des 0 dans la
				// colonne noVarEntrante

				for (int i = 0; i < nbContraintes; i++) {
					if (i != noLignePivot) {
						coeff = contEA[i][noVarEntrante];
						for (int j = 0; j < nbVarTot; j++) {
							contEA[i][j] = contEA[i][j] - coeff
							* contEA[noLignePivot][j];
							for (int h = 0; h < contEA.length; h++) {
								for (int k = 0; k < contEA.length; k++) {
									System.out.println(contEA[k][h]);

								}
							}

						}

					}
					c[i] = c[i] - c[noLignePivot] * coeff;

					if (c[i] == 0) {
						c[i] = epsilon; // pour eviter le bouclage
					}

				}

			}
		}

		for (int k = 0; k < nbVarDec+1; k++) {
			System.out.println(noVBcont[k]);

		}

	}

}


 



A voir également:

1 réponse

cixion Messages postés 12 Date d'inscription samedi 27 juin 2009 Statut Membre Dernière intervention 29 novembre 2011
27 mai 2011 à 19:40
comment puis-je joindre des fichiers sur ce forum ?
0