1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
1)using System;
2)using System.Collections.Generic;
3)using System.Text;
4)using System.Windows.Forms;
namespace simplexessaie
{
public class Simplex
{
int L;
int var, contr;
int C;
decimal[] SIMPL;
public int Simplexe(decimal[] Donnees, int nombre_var, int nombre_contr, decimal[] Solution, decimal[] z, String mot)
{
int Resultat;
Element pivot=new Element();
int[] var_base;
L=nombre_contr+1;
var=nombre_var;
contr=nombre_contr;
C=nombre_var+2; //première initiablisation
mot+="*-*-*-*-*-*-*-**-*-*-*-*-*-* Fonction Simplexe canonique *-*-*-*-*-*-*-* \n*-*-*-*-*-*--*-*-*-*";
mot+="\nla matice du Debut :\n";
mot+=aff(Donnees,nombre_contr+1,nombre_var+2);
// nombre de s il sera egale au nombre des contraint
int nbr_s=nombre_contr;
// les variables de base seront stoquer dans un tableau nome var_base
var_base=new int[contr];
int i=0;
int s=nombre_var;
while(i<nombre_contr)
{
{
var_base[i]=s;
s++;
}
i++;
}
//Tableau initial
C=nombre_var+nbr_s+1;
SIMPL=new decimal[L*C];
//matrice des contraintes
for(i=0;i<nombre_contr;i++)
for(int j=0;j<nombre_var;j++)
SIMPL[i*C+j]=Donnees[i*(nombre_var+2)+j];
// matrice des variables ajoutes
s=nombre_var;
for(i=0;i<nombre_contr;i++)
{
SIMPL[i*C+s]=1;
s++;
}
//vecteur b
for(i=0;i<nombre_contr;i++)
SIMPL[i*C+C-1]=Donnees[i*(nombre_var+2)+nombre_var];
//delta
for(i=0;i<C;i++)
SIMPL[contr*C+i]=0;
for(i=nombre_var+nbr_s;i<C-1;i++)
SIMPL[contr*C+i]=1;
//Traitement
for(i=0;i<nombre_var;i++)
SIMPL[contr*C+i]=Donnees[contr*(var+2)+i];
annuler_delta_base(var_base);
//choix du pivot et pivotage
while((Resultat=Sol_opt(pivot))==0)
{
var_base[pivot.getLigne()]=pivot.getColonne();
pivotage(pivot);
}
//System.out.println("la valeur de z"+SIMPL[L*C-1]);
z[0]=SIMPL[L*C-1];
mot+="\nla martice finale :\n";
mot+=aff(SIMPL,L,C);
MessageBox.Show(mot);
//Console.WriteLine(mot);
// Solution
for(i=0;i<nombre_var;i++)
{
int j=0;
try{
while(j<nombre_var && var_base[j]!=i)
j++;
if(j==nombre_var)
Solution[i]=0;
else
Solution[i]=SIMPL[j*C+C-1];}
catch(Exception){Console.WriteLine("vous avez une erreur");}
}
if(Resultat>0)
return 1;
else
return 0;
//return (Resultat>0);
}
public void annuler_delta_base(int[] var_base)
{
decimal m;
for (int i = 0; i < contr; i++)
if (SIMPL[contr * C + var_base[i]] != 0)//
{
m = -SIMPL[contr * C + var_base[i]];
m /= SIMPL[i * C + var_base[i]];
for (int j = 0; j < C; j++)
{
SIMPL[contr * C + j] += m * SIMPL[i * C + j];
}
}
}
120) public int Sol_opt(Element pivot)//la ou il ya l'erreur
{ int i = 0;
while (i < C - 1 && (SIMPL[(L - 1) * C + i]) >= 0)
i++;
if (i < C - 1)
{
pivot.setColonne(i);
pivot.setLigne((int)min_div(i));
if (pivot.getLigne() >= 0)
{
pivot.setValeur(SIMPL[pivot.getLigne() * C + pivot.getColonne()]);
return 0;
}
else
return -1;
}
else
return 1;
}
public decimal min_div(int col_pivot)
{
int i_min = -1;
decimal min;
int last_col = C - 1;
decimal[] div = new decimal[contr];
int i = 0;
while (i < contr)
{
if ((SIMPL[i * C + col_pivot]) > 0)
div[i] = SIMPL[i * C + last_col] / SIMPL[i * C + col_pivot];
i++;
}
i = 0;
while (i < contr)
{
if (SIMPL[i * C + col_pivot] > 0)
{
min = div[i]; //on cherche
initialiser min par a 1re div correspondante
// un SIMPL[i][col_pivot]>0
i_min = i;
while (i < contr)
{
if (SIMPL[i * C + col_pivot] > 0)
if (div[i] < min)
{
min = div[i];
i_min = i;
}
i++;
}
}
i++;
}
return i_min;
}
void pivotage(Element pivot)
{
decimal m, Add;
int i = 0;
// la variable hors base entre dans la base
while (i < C)
{
SIMPL[pivot.getLigne() * C + i] /= pivot.getValeur();
i++;
}
// modification des autres lignes
for (i = 0; i <= contr; i++)
{
if (i != pivot.getLigne())
{
m = -SIMPL[i * C + pivot.getColonne()];
for (int j = 0; j < C; j++)
{
Add = m * SIMPL[pivot.getLigne() * C + j];
SIMPL[i * C + j] += Add;
}
}
}
}
void eliminer_colonne(int colonne)
{
decimal[] nouveau_SIMPL = new decimal[L * (C - 1)];//(L*(C-1)*sizeof(decimal));
for (int i = 0; i < L; i++)
for (int j = 0; j < colonne; j++)
nouveau_SIMPL[i * (C - 1) + j] = SIMPL[i * C + j];
for (int i = 0; i < L; i++)
for (int j = colonne + 1; j < C; j++)
nouveau_SIMPL[i * (C - 1) + j - 1] = SIMPL[i * C + j];
//SIMPL=null;
SIMPL = nouveau_SIMPL;
}
public String aff(decimal[] tab, int L, int C)
{
String mot1 = "";
for (int i = 0; i < L; i++)
{
for (int j = 0; j < C; j++)
mot1 += " " + toString(tab[i * C + j]) + " ";
mot1 += "\n";
}
//JOptionPane.showMessageDialog(null,"\t\t\t\t\t1");
return mot1;
}
public static String toString(decimal i) { return " " + i + " ";
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace simplexessaie
{
class Element
{
public int colonne;
public int ligne;
public decimal valeur;
public Element()
{
}
public int getColonne()
{
return colonne;
}
public int getLigne()
{
return ligne;
}
public decimal getValeur()
{
return valeur;
}
public void setColonne(int colonne)
{
this.colonne = colonne;
}
public void setLigne(int ligne)
{
this.ligne = ligne;
}
public void setValeur(decimal valeur)
{
this.valeur = valeur;
}
}
} |
Partager