#include <iostream>
#include<iomanip>
using namespace std;
//--- VARIABLES ---
int p,q,Max,n; //n=p+q
float Tab[30][60];
float Max_CM;
float Min_Rapport;
int ColPiv,LigPiv;
bool TrouveCol,TrouveLig;
//--- FONCTIONS --- void Initialisation();
void Saisir();
void Afficher(); // affiche le tableau "nu" sans opérations à faire void ChercheCoutMarginalMax();
void ChercherPlusPetitRapport();
void AfficherTabOp(); // affiche le tableau avec les opérations à // faire sur chaque ligne
void Operations(); // opérations pour le pivot trouvé void Simplexe();
void Conclusions();
//************ MAIN (corps du programme exécutable) **************
int main(int argc, char* argv[]) {
Saisir();
Afficher();
RecherchePivots();
Conclusions();
cin.sync();
cin.get();
return 0;
}//********** FIN DU MAIN (corps du programme exécutable)***********
//--- FONCTIONS détails ---
//- NOTE dans les fonctions les variables locales i et j désigneront respectivement :
// pour i, un n° de ligne
// et pour j, un n° de colonne dans le tableau
void Initialisation() //--- DÉBUT INITIALISATION { for (int i=0;i<=p;i++)
{
for (int j=0;j<=n+1;j++) Tab[i][j]=0;
}
for (int i=0;i<=;i++) {
Tab[i][0]=q+i;
Tab[i][q+i]=1 }
}//--- FIN INITIALISATION --- void Saisir() //--- DÉBUT SAISIE
{
cout<<"Maximisation ou Minimisation d'une fonction linéaire")<<endl;
cout<<" de q variables X1,X2, ... ,Xq positives ou nulles")<<endl;
cout<<" soumises à p contraintes d'inégalité")<<endl;
cout<<endl; cout<<"Ai2X2 + Ai3X3 + ... + AiqXq<= Di ")<<endl;
cout<<"pour i=1,2,...,p")<<endl;
cout<<"avec D1,D2, ... ,Dp >=0 ")<<endl;
cout<<"Nombre q de variables X1,X2, ... ,Xq : ";
cin>>q;
cout<<endl;
cout<<"Nombre p de contraintes : ";
cin>>p;
cout<<endl;
n=p+q;
//Initialisation du tableau Initialisation();
Max=2;
while ((Max!=0) && (Max!=1)) {
cout<<"Pour maximiser taper:1";
cout<<endl,
cout<<"pour minimiser taper:0 ";
cin>>Max;
cout<<endl;
}
cout<<"Coefficients des variables X1,X2, .. dans la fonction :";
cout<<endl;
for (int j=1;j<=q;j++) {
cout<<" Coefficient de X"<<j<<" : ";
cin>>Tab[i][j];
cout<<endl;
if (Max==0)
Tab[i][j]=-Tab[i][j];
}
for (int i=1;i<=p;i++) {
cout<<"Contrainte No "<<i<<endl;
for (int j=1;j<=q;j++) {
cout<<" Coefficient de X"<<j<<" : ";
cin>>Tab[i][j];
cout<<endl;
}
//Saisie du second membre
cout<<" Second membre (positif ou nul) : ";
cin>>Tab[i][n+1];
cout<<endl;
}
}//--- FIN SAISIE --- void Afficher() //----DÉBUT AFFICHAGE DU TABLEAU
{
for(int i=0; i<=p; i++) {
if(i==0)
cout<<" ";
else
cout<<setw(2)<<setprecision(0)<<fixed<<Tab[i][j];
cout<<"|";
for(int j=1; j<=n+1; j++) {
cout<<setw(10)<<setprecision(4)<<fixed<<Tab[i][j];
if(j==n) cout<<"|";
}
cout<<endl;
if (i==0) {
for int k=1;k<=(n+1)*10+4;k++);
cout<<"-";
cout<<endl;
} }
}//--- FIN AFFICHAGE DU TABLEAU --- void ChercheCoutMarginalMax() //DÉBUT RECHERCHE du plus grand coût marginal > 0 { Max_CM=0;
TrouveCol=false;
for (int j=1;j<=n;j++) {
if (Tab[0][j]>Max_CM) {
Max_CM=Tab[0][j];
ColPiv=j;
TrouveCol=true;
} }
} //--- FIN RECHERCHE DU PLUS GRAND COÛT MARGINAL POSITIF --- void ChercherPlusPetitRapport() //DÉBUT RECHERCHE DU PLUS PETIT RAPPORT {
float Min_Rapport;
float Rapport;
bool Premier=true;
TrouveLig=false;
for(int i=1; i<=p; i++) {
if(Tab[i][ColPiv]>0) {
Rapport=Tab[i][n+1]/Tab[i][ColPiv];
if (Premier) {
Min_Rapport=Rapport;
LigPiv=i;
Premier=false;
TrouveLig=true;
} else {
if (Rapport<Min_Rapport) {
Min_Rapport=Rapport;
LigPiv=i;
} } } }
} //--- FIN RECHERCHE DU PLUS PETIT RAPPORT --- void AfficherTabOp() //DÉBUT AFFICHAGE du tableau et des opérations à faire { float Pivot=Tab[LigPiv][ColPiv]; //Pivot trouvé
for(int i=0; i<=p; i++) {
if (i==0) cout<<" ";
else
cout<<setw(2)<<setprecision(0)<<fixed<<Tab[i][j];
cout<<"|";
for(int j=1; j<=n+1; j++) {
cout<<setw(10)<<setprecision(4)<<fixed<<Tab[i][j];
if(j==n) cout<<"|";
}
cout<<"| ";
// on a affiché la ligne, on affiche maintenant l'opération à faire sur cette ligne if (LigPiv == i)
cout<<setw(10)<<setprecision(4)<<fixed<<"L"<<i<<"/"<<Pivot;
else {
cout<<<<"L"<<i<<"-(";
On n e p eut initialiser
Min_Rapportav ec un va le ur arb itraire
Mmê me tr ès gr and e car le p lus pe tit d es ra pp orts
Tab[i][n+1]/Tab[i][ColPiv]
(pour Tab[i][ColPiv]>0)
p e ut ê tre b ien p lu s g ra n d qu e
Mcout<<setw(10)<<setprecision(4)<<fixed<<Tab[i][ColPiv];
cout<<")/(";
cout<<setw(10)<<setprecision(4)<<fixed<<Pivot;
cout<<")*L"<<LigPiv;
cout<<endl;
}
if (i==0) {
for int k=1;k<=(n+1)*10+4;k++);
cout<<"-";
cout<<endl;
} }
} //--- FIN AFFICHAGE DU TABLEAU ET DES OPÉRATIONS À FAIRE -- void Operations() //---- DÉBUT DES OPÉRATIONS À FAIRE POUR LE PIVOT TROUVÉ { // en ligne LigPiv et en colonne ColPiv
float Pivot=Tab[LigPiv][ColPiv]; //c'est le pivot trouvé for (int j=1; j<=n+1; j++)
Tab[LigPiv][j] /= Pivot; //pour la ligne du pivot for (int i=0;i<=p;i++)
{
if (i!=LigPiv) //pour toutes les autres lignes {
for (int j=1; j<=n+1; j++)
Tab[i][j]-=Tab[i][ColPiv]*Tab[LigPiv][j];
} }
// Pour retrouver la colonne du pivot dans chaque ligne : Tab[LigPiv][0]=ColPiv;
} //--- FIN DES OPÉRATIONS À FAIRE POUR LE PIVOT TROUVÉ --- void Simplexe() //--- DÉBUT RECHERCHE DES PIVOTS SUCCESSIFS
{ // par la méthode du SIMPLEXE
TrouveCol=true;
TrouveLig=true;
while (TrouveCol && TrouveLig) {
ChercheCoutMarginalMax();
if (TrouveCol) {
ChercherPlusPetitRapport();
if (TrouveLig) {
AfficherTabOp();
Operations();
} }
}
Afficher();
}
//--- FIN DE LA MÉTHODE DU SIMPLEXE --- void Conclusions(); //--- DÉBUT DES CONCLUSIONS
{
if (!TrouveCol) // les coûts marginaux sont tous <= 0 {
if (Max=1) {
cout<<"La fonction admet un MAXIMUM : "<<-Tab[0][n+1];
cout<<endl;
} else {
cout<<"La fonction admet un MINIMUM : "<<Tab[0][n+1];
cout<<endl;
}
cout<<"Valeurs des variables à l'optimum :"<<endl;
// toute variable correspondant à un pivot est égale au second membre // correspondant à la ligne de ce pivot et pour chaque ligne i=1 à p le // numéro de la colonne du pivot de cette ligne est Tab[i][0]
for (int i=1;i<=p;i++) {
cout<<"X"<<setw(2)<<setprecision(0)<<Tab[i][0]<<" = ";
cout<<setw(10)<<setprecision(4)<<Tab[i][n+1];
cout<<endl;
}
cout<<"Les autres variables sont nulles à l'optimum"<<endl;
} else {
if (!TrouveLig) {
if (Max=1)
cout<<"La fonction n'admet pas de MAXIMUM"<<endl;
else
cout<<"La fonction n'admet pas de MINIMUM"<<endl;
} }
}//--- FIN DES CONCLUSIONS --- //============== FIN DU FICHIER SOURCE ==============================