P. 1
Algoritmul Simplex

Algoritmul Simplex

|Views: 372|Likes:
Published by Vlad Jerca

More info:

Published by: Vlad Jerca on Jan 01, 2012
Copyright:Traditional Copyright: All rights reserved

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

02/25/2012

text

original

ALGORITMUL

SIMPLEX
Universitatea Bucuresti
Facultatea de Matematica-Informatica
Specializarea Tehnologia Informatiei
Jerca Vlad
anul I gr. 152
Cuprins
1. Defnitie ......................................................................................................... 3
2. Exemple ........................................................................................................ 4
3. Codarea in limbajul C ..................................................................................... 6
4. Executarea programului ............................................................................... 10
3
1. Defnitie
A. Formulare
max f(x
1
...x
n
) = <c,x>
f
j
(x
1
...x
n
) ≤ b
j
x
1
,...,x
n
≥ 0
f - functia obiectiv
f
j
- restrictiile problemei
x
i
- nivele la care se desfasoara activitatea
Daca f si f
j
( j = 1,m ) sunt forme liniare se obtin probleme de programare liniara:

f(x) = ∑ c
i
x
i
= <c,x>
Daca vectorii a
i1
,a
i2
,...,a
im
apartin R
m
formeaza o baza B a lui R
m
spunem ca variabilele x
i1
,x
i2
,...,x
im

sunt variabile bazice (celelalte find nebazice).
Notam X
B
= B
-1
* b
Daca X
B
≥ 0 spunem ca B este baza primal admisibila. In acest caz notam:

I = { i
1
,...,i
m
}
C
B
= { c
i1
,c
i2
,...,c
im
}
f
B
= C
B
X
B

f
j
= C
B
a
j
B. TABELUL SIMPLEX:
C
B
B X
B
c
1
c
2
..................

c
m
..................

c
j
..................

c
n
a
1
a
2
..................

a
m
..................

a
j
..................

a
n
c
i1
c
i2
.
.
.
c
ie
.
.
.
c
im
a
i1
a
i2
.
.
.
a
ie
.
.
.
a
im
x
i1
x
i2
.
.
.
x
ie
.
.
.
x
im
B
-1
A
f
B
f
1
f
2
..................

f
m
..................

f
j
..................

f
n
C
j
-f
j
c
1
-f
1
c
2
-f
2
...............

c
m
-f
m
...............

c
j
-f
j
...............

c
n
-f
n
{
i=1
n
4
Se aplica testul de optimalitate asupra solutiei curente X
B
care consta in urmatoarea rezolvare:
a) Daca pentru orice j apartie lui J, c
j
- f
j
≤ 0 problema are oftim finit, X
B
solutie optima si max f = f
B
.
b) Daca pentru orice j apartie lui J, c
j
- f
j
> 0 atunci:

b
1
)

daca coloana a
j
are toate elementele

≤ 0

problema nu are optim finit
b
2
)

daca pe coloana a
j
exista cel putin un element

> 0

atunci solutia curenta se poate
imbunatati conducand-o spre o solutie optima astfel:
- se determina indicele k al vectorului care intra in baza cu urmatoarea formula:
C
k
-f
k
= max { c
j
- f
j
| oricare j apartie lui J pentru care c
j
- f
j
> 0 }
- se determina indicele l al vectorului care iese din baza B dupa formula:
x
il
= min { | oricare j apartine lui I pentru care a
jk
> 0 }
2. Exemple:
a) max f = 3x
1
+ 4x
2
+ 1x
3

1x
1
+ 2x
2
+ 3x
3
≤ 10
1x
1
- 1x
2
+ 0x
3
≤ 3
1x
1
+ 0x
2
+ 2x
3
≤ 6
3 4 1 0 0 0
C
B
B X
B
a
1
a
2
a
3
a
4
a
5
a
6
0 a
4
10 1 2 3 1 0 0
0 a
5
3 1 -1 0 0 1 0
0 a
6
6 1 0 2 0 0 1
0 0 0 0 0 0 0
3 4 1 0 0 0
4 a
2
5 1/2 1 3/2 1/2 0 0
0 a
5
8 3/2 0 3/2 1/2 1 0
0 a
6
6 1 0 2 0 0 1
20 2 4 6 2 0 0
1 0 -5 -2 0 0
a
2
7/3 0 1 1 1/3 -1/3 0
a
1
16/3 1 0 1 1/3 2/3 0
a
6
2/3 0 0 1 -1/3 -2/3 1
76/3 3 4 7 7/3 2/3 0
0 0 -6 -7/3 -2/3 0

x
il
a
ik
x
ij
a
jk
{
5
b) max f = 107x
4
+ 1x
5
+ 2x
6

14/3x
4
+ 1/3x
5
- 1/3x
6
≤ 7/3
16x
4
- 1/2x
5
-2x
6
≤ 5
3x
4
+ 0x
5
+ 0x
6
≤ 0
107 1 2 0 0 0
C
B
B X
B
a
1
a
2
a
3
a
4
a
5
a
6
0 a
4
7/3 14/3 1/3 -1/3 1 0 0
0 a
5
5 16 1/2 -2 0 1 0
0 a
6
0 3 0 0 0 0 1
0 0 0 0 0 0 0
107 4 1 0 0 0
0 a
4
7/3 1 0 -14/9 0 1/3 -1/3
0 a
5
5 0 1 -16/3 0 1/2 -2
107 a
1
0 0 0 1/3 1 0 0
0 0 0 107/3 107 0 0
0 0 -107/3 0 1 2

{
6
3. Codarea in limbajul C
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//intarzie
void timp(int n)
{
for(int t=1;t<=n*100000;t++);
}
void afisez(char c[])
{
int i=0;
while(c[i])
{
putchar(c[i]);
timp(75);
i++;
}
}
//face pasul pentru inversa matricii
void pas(float a[255][255], int n,int n1, int poz1, int poz2)
{
int i, j;
float pivot;
pivot=a[poz1][poz2];

for(i=1;i<=n1;i++)
if(i!=poz1)
for(j=0 ;j <= n+n1; j++)
if(pivot!=0&&j!=poz2)
a[i][j]=a[i][j]-(a[poz1][j]*a[i][poz2])/pivot; // are loc
pasul

for (i=0 ; i <= n+n1; i++)
if(pivot!=0)
a[poz1][i]=a[poz1][i]/pivot; // impartim linia pivotului la pivot

for(i=1; i <= n1; i++)
if(i!=poz1)
a[i][poz2]=0; // elementele pe coloana pivotului iau valoarea 0

}
//calculeaza produsul scalar
void produsScalar(float a[255][255], float c[255], float f[255], int n,int n1)
{
for(int j=0;j<=n+n1;j++)
{
f[j]=0;
for(int i=0;i<=n;i++)
f[j]=f[j]+(a[i][j]*c[i]);
}
}
7
//citeste datele de intrare
void incarcDate(float a[255][255], float c[255], int b[255], int n,int n1)
{
int i, j;
// afisez forma max f
printf(“max f = “);
for(i=1;i<=n;i++)
{
printf(“c%dx%d”,i, i);
if(i!=n)
printf(“ + “);
else printf(“\n”);
}
// afisez forma ecuatiilor
for(i=1;i<=n1;i++)
for(j=1;j<=n;j++)
{
printf(“a%d%dx%d”,i, j, j);
if(j!=n)
printf(“ + “);
else printf(“ <= b%d\n”, i);
}
// citesc max f
for( j=1; j<=n; j++)
{
printf(“c%d = \t”, j);
scanf(“%f”, &a[0][j]);
}
// citesc ecuatiile
for(i=1;i<=n1;i++)
{
for(j=1;j<=n;j++)
{
printf(“a%d%d = \t”,i,j);
scanf(“%f”, &a[i][j]);
}
printf(“b%d = \t”, i);
scanf(“%f”, &a[i][0]);
}
for(i=1;i<=n1;i++)
for(j=n+1;j<=n+n1;j++)
{
if(i+n==j)
a[i][j]=1;
}
// incarc vectorul C(B)
for(i=1;i<=n;i++)
c[i]=a[0][i+n];
//incarc vectorul B
for(i=1;i<=n1;i++)
b[i]=i+n;

}
8
//afiseaza tabelul simplex
void afisezTabel(float a[255][255], float c[255], float f[255], int b[255], int n,int n1)
{
system(“cls”);
printf(“\n\t\t\t”);
for(int i=1;i<=n+n1;i++)
printf(“%.2f\t”, a[0][i]);
printf(“\nC(B)\t B\tX(B)\t”);
for(int i=1;i<=n+n1;i++)
printf(“ a%d\t”, i);
printf(“\n\n”);
for(int i=1;i<=n1;i++)
{
printf(“%.2f\ta%d\t%.2f\t”,c[i], b[i], a[i][0]);
for(int j=1;j<=n+n1;j++)
printf(“%.2f\t”, a[i][j]);
printf(“\n”);
}
printf(“\n\t\t”);
for(int i=0;i<=n+n1;i++)
printf(“%.2f\t”, f[i] );
printf(“\n\t\t\t”);
for(int i=1;i<=n+n1;i++)
printf(“%.2f\t”, a[0][i]-f[i]);
}
//verifica si face pasul
void verificIntrareIesire(float a[255][255], float c[255], float f[255], int b[255], int n, int n1, int &daca)
{
int intra=-1, iese, i, ;
float tempvar=0;
//verific daca Cj-fj <=0
for(i=1;i<=n+n1;i++)
if((a[0][i]-f[i])<=0)
tempvar++;

if(tempvar==n+n1)
{
daca=1;
afisez(“\n\n\t\t\tS-a gasit optimul !”);
}
else
{
//afla maximul din Cj-fj si retine pozitia de intrare
tempvar=0;
for(i=1;i<=n+n1;i++)
if((a[0][i]-f[i])>0 && tempvar<a[0][i]-f[i])
{
tempvar=a[0][i]-f[i];
intra=i;
}
//afla minimul dintre C(B)i si aij si retine pozitia de iesire
tempvar=999;
for(i=1;i<=n;i++)
if(a[i][intra]>0 && a[i][0]/a[i][intra]<tempvar)
{
tempvar=a[i][0]/a[i][intra];
iese=i;
}

9
//afiseaza tabelul simplex
void afisezTabel(float a[255][255], float c[255], float f[255], int b[255], int n,int n1)
{
system(“cls”);
printf(“\n\t\t\t”);
for(int i=1;i<=n+n1;i++)
printf(“%.2f\t”, a[0][i]);
printf(“\nC(B)\t B\tX(B)\t”);
for(int i=1;i<=n+n1;i++)
printf(“ a%d\t”, i);
printf(“\n\n”);
for(int i=1;i<=n1;i++)
{
printf(“%.2f\ta%d\t%.2f\t”,c[i], b[i], a[i][0]);
for(int j=1;j<=n+n1;j++)
printf(“%.2f\t”, a[i][j]);
printf(“\n”);
}
printf(“\n\t\t”);
for(int i=0;i<=n+n1;i++)
printf(“%.2f\t”, f[i] );
printf(“\n\t\t\t”);
for(int i=1;i<=n+n1;i++)
printf(“%.2f\t”, a[0][i]-f[i]);
}
//verifica si face pasul
void verificIntrareIesire(float a[255][255], float c[255], float f[255], int b[255], int n, int n1, int &daca)
{
int intra=-1, iese, i, ;
float tempvar=0;
//verific daca Cj-fj <=0
for(i=1;i<=n+n1;i++)
if((a[0][i]-f[i])<=0)
tempvar++;

if(tempvar==n+n1)
{
daca=1;
afisez(“\n\n\t\t\tS-a gasit optimul !”);
}
else
{
//afla maximul din Cj-fj si retine pozitia de intrare
tempvar=0;
for(i=1;i<=n+n1;i++)
if((a[0][i]-f[i])>0 && tempvar<a[0][i]-f[i])
{
tempvar=a[0][i]-f[i];
intra=i;
}
//afla minimul dintre C(B)i si aij si retine pozitia de iesire
tempvar=999;
for(i=1;i<=n;i++)
if(a[i][intra]>0 && a[i][0]/a[i][intra]<tempvar)
{
tempvar=a[i][0]/a[i][intra];
iese=i;
}

if(iese>n+n1)
// daca nu exista nici un termen > 0 pe coloana de iesire ->
//programul se opreste fiindca nu are optim finit
{
daca=1;
afisez(“\n\n\t\t\tNu exista optim finit !”);
}
else
{
c[iese]=a[0][intra];
printf(“\n\n\t\t\tIese a%d ; Intra a%d”, b[iese], intra);
b[iese]=intra;
printf(“\tPozitia pivotului %d %d”, iese, intra);
afisez(“\n\t\t\tApasati orice tasta pentru urmatorul pas...”);
getch();
}
}
pas(a,n,n1,iese,intra);
}
int main ()
{
float matA[255][255], vectCb[255], vectF[255];
int nr_term, nr_termf, vectB[255], daca=0;
printf(“Numarul de termeni in max f: “); scanf(“%d”, &nr_term);
printf(“Numarul de ecuatii: “); scanf(“%d”, &nr_termf);
//se incarca datele
incarcDate(matA, vectCb, vectB,nr_term, nr_termf);
while(!daca)
{
produsScalar(matA, vectCb, vectF, nr_term, nr_termf);
afisezTabel(matA, vectCb, vectF, vectB, nr_term, nr_termf);
verificIntrareIesire(matA, vectCb, vectF,vectB,nr_term, nr_termf, daca);
}
afisez(“\n\n\t\t\tCodat de Jerca Vlad gr. 152 sectia TI”);
getch();
}
10
4. Executarea programului
Exemplul a.
11
Exemplul b.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->