#include<stdio.h>
#include<conio.h>
void main()
{
float c[10],val[10][10],basis[10]={0},z[10]={0},del[10],b[10];
int i,j,k,eno,vno,og,in,bname[10];
float min,max,multiplier,z1=0,ba[10],key_el;
clrscr();
printf("Enter no. of variables");
scanf("%d",&vno);
printf("\n Enter coeffiients with variables in objective function Z::");
for(i=0;i<vno;i++)
scanf("%f",&c[i]);
printf("\n How many equations??");
scanf("%d",&eno);
printf("\n Enter coefficents of variables in each equation WITH SLACK VARIABLES");
for(i=0;i<eno;i++)
{ printf("\n Equaion %d::",i+1);
for(j=0;j<vno;j++)
{printf("coeff with var%d=",j+1);
scanf("%f",&val[i][j]);
}
for(j=vno;j<eno+vno;j++)
{ printf("coeff with slack%d=",j-vno+1);
scanf("%f",&val[i][j]);
}
}
printf("\n Enter RHS values for each equation");
for(i=0;i<eno;i++)
{ printf("\n Equaion %d::",i+1);
scanf("%f",&b[i]);
}
//now for slack we have to do cj=0 in Z;
for(i=vno;i<eno+vno;i++)
{c[i]=0;}
//basis are already set to zero bt to know which variable is in basis,bname
//we will slack=number of equation i.e if 3 equations then 3 slack variables.
//so last number will be vno+eno=total variables in each equation containg slack.
for(i=0,j=vno;j<(vno+eno);i++,j++)
{bname[i]=j;
}
//start processing simplex
for(k=0;k<10;k++)
{
max=0;
//after each iteration make zj=0;
for(i=0;i<vno+eno;i++)
z[i]=0;
//find z to calculate delta
for(i=0;i<vno+eno;i++)
{ for(j=0;j<eno;j++)
z[i]=z[i]+(basis[j]*val[j][i]);
}
// find delta and maximum among them
for(j=0;j<vno+eno;j++)
{ del[j]=0;
del[j]=c[j]-z[j];
if(del[j]>max)
{max=del[j];
in=j;
}
printf("del %f",del[j]);
}
//if max=0 it means all neagtive valuesin delta so stop.
if(max==0)
{break;
}
//printf("here1");
for(i=0;i<eno;i++)
{
if(val[i][in]!=0)
ba[i]=b[i]/val[i][in];
//first of all take first value of bi/aij as smallest.
if(i==0)
{min=ba[i];
}
//we are finding outgoing variable so it will be minimum amongst bi/aij;
if(ba[i]<=min)
{min=ba[i];
og=i;
}
//d printf("%f",ba[i]);
}
//we got o/g variable so rename it with i/g.
bname[og]=in;
printf("incomming=%d",in);
//basis will be changed according to cj value.
printf("outgoing=%d",og);
basis[og]=c[in];
//key element will be intersection of o/g variable's row & i/g variable's coloumn.
key_el=val[og][in];
printf("key %f",key_el);
//NOTE: here i/g shows key coloumn &
// o/g shows key row.
//now do pivoting.
//firstly for key row which will be of 'o/g'.
// printf("here2");
for(j=0;j<eno+vno;j++)
{val[og][j]=val[og][j]/key_el;
//at the end of loop do it for rhs also;
// printf("h3");
if(j==(eno+vno)-1)
b[og]=b[og]/key_el;
}
//for rest of the rows;
//we have to set one multiplier which is above and below values of key_element.
for(i=0;i<eno;i++)
{
//only process rows except key_row;
if(i!=og)
{multiplier=val[i][in];
// multiply whole key row with negative multiplier
// and now add it in previous row and overwite value.
for(j=0;j<eno+vno;j++)
{ val[i][j]=val[i][j]+(-1*multiplier*val[og][j]);
}
//after doing for variables do it for rhs;
b[i]=b[i]+(-1*multiplier*b[og]);
}
}
//done
//updated basis,basis_name,b(rhs),all coefficients.
//so next loop will use updated value and do same;
} //finish loop with 'k';
printf("\n Solution");
for(i=0;i<eno;i++)
{ if(bname[i]>=vno)
printf("\n slack%d=%f",(bname[i]%vno)+1,b[i]);
else
printf("\n x%d=%f",bname[i]+1,b[i]);
}
//calculating z by multiplying rhs of variables and its corredsponding basis;
for(j=0;j<eno;j++)
{z1=z1+(basis[j]*b[j]);
}
printf("\n Z=%f",z1);
getch();
}
#include<conio.h>
void main()
{
float c[10],val[10][10],basis[10]={0},z[10]={0},del[10],b[10];
int i,j,k,eno,vno,og,in,bname[10];
float min,max,multiplier,z1=0,ba[10],key_el;
clrscr();
printf("Enter no. of variables");
scanf("%d",&vno);
printf("\n Enter coeffiients with variables in objective function Z::");
for(i=0;i<vno;i++)
scanf("%f",&c[i]);
printf("\n How many equations??");
scanf("%d",&eno);
printf("\n Enter coefficents of variables in each equation WITH SLACK VARIABLES");
for(i=0;i<eno;i++)
{ printf("\n Equaion %d::",i+1);
for(j=0;j<vno;j++)
{printf("coeff with var%d=",j+1);
scanf("%f",&val[i][j]);
}
for(j=vno;j<eno+vno;j++)
{ printf("coeff with slack%d=",j-vno+1);
scanf("%f",&val[i][j]);
}
}
printf("\n Enter RHS values for each equation");
for(i=0;i<eno;i++)
{ printf("\n Equaion %d::",i+1);
scanf("%f",&b[i]);
}
//now for slack we have to do cj=0 in Z;
for(i=vno;i<eno+vno;i++)
{c[i]=0;}
//basis are already set to zero bt to know which variable is in basis,bname
//we will slack=number of equation i.e if 3 equations then 3 slack variables.
//so last number will be vno+eno=total variables in each equation containg slack.
for(i=0,j=vno;j<(vno+eno);i++,j++)
{bname[i]=j;
}
//start processing simplex
for(k=0;k<10;k++)
{
max=0;
//after each iteration make zj=0;
for(i=0;i<vno+eno;i++)
z[i]=0;
//find z to calculate delta
for(i=0;i<vno+eno;i++)
{ for(j=0;j<eno;j++)
z[i]=z[i]+(basis[j]*val[j][i]);
}
// find delta and maximum among them
for(j=0;j<vno+eno;j++)
{ del[j]=0;
del[j]=c[j]-z[j];
if(del[j]>max)
{max=del[j];
in=j;
}
printf("del %f",del[j]);
}
//if max=0 it means all neagtive valuesin delta so stop.
if(max==0)
{break;
}
//printf("here1");
for(i=0;i<eno;i++)
{
if(val[i][in]!=0)
ba[i]=b[i]/val[i][in];
//first of all take first value of bi/aij as smallest.
if(i==0)
{min=ba[i];
}
//we are finding outgoing variable so it will be minimum amongst bi/aij;
if(ba[i]<=min)
{min=ba[i];
og=i;
}
//d printf("%f",ba[i]);
}
//we got o/g variable so rename it with i/g.
bname[og]=in;
printf("incomming=%d",in);
//basis will be changed according to cj value.
printf("outgoing=%d",og);
basis[og]=c[in];
//key element will be intersection of o/g variable's row & i/g variable's coloumn.
key_el=val[og][in];
printf("key %f",key_el);
//NOTE: here i/g shows key coloumn &
// o/g shows key row.
//now do pivoting.
//firstly for key row which will be of 'o/g'.
// printf("here2");
for(j=0;j<eno+vno;j++)
{val[og][j]=val[og][j]/key_el;
//at the end of loop do it for rhs also;
// printf("h3");
if(j==(eno+vno)-1)
b[og]=b[og]/key_el;
}
//for rest of the rows;
//we have to set one multiplier which is above and below values of key_element.
for(i=0;i<eno;i++)
{
//only process rows except key_row;
if(i!=og)
{multiplier=val[i][in];
// multiply whole key row with negative multiplier
// and now add it in previous row and overwite value.
for(j=0;j<eno+vno;j++)
{ val[i][j]=val[i][j]+(-1*multiplier*val[og][j]);
}
//after doing for variables do it for rhs;
b[i]=b[i]+(-1*multiplier*b[og]);
}
}
//done
//updated basis,basis_name,b(rhs),all coefficients.
//so next loop will use updated value and do same;
} //finish loop with 'k';
printf("\n Solution");
for(i=0;i<eno;i++)
{ if(bname[i]>=vno)
printf("\n slack%d=%f",(bname[i]%vno)+1,b[i]);
else
printf("\n x%d=%f",bname[i]+1,b[i]);
}
//calculating z by multiplying rhs of variables and its corredsponding basis;
for(j=0;j<eno;j++)
{z1=z1+(basis[j]*b[j]);
}
printf("\n Z=%f",z1);
getch();
}
Comments
Post a Comment