You are on page 1of 4

# Abhishek Borar 10MA20003

#include<stdio.h> //initializing global variables float giventable[20][20], zerotable[20][200]; int eqcount, varcount, tablerowcount, zerocount, currentrow=0; int pos[20]; //holds the positions for variables to be set zero float calcmat[20][20]; //function to calculate factorial int factorial(int a){ if(a==1)return 1; else return a*factorial(a-1); } //function that generates the permutations of the variables to be set 0 int permutezero(int start, int zerosleft){ int i,j; //if zeros left are null then assign the positions of zeros as 0 in the zerotable if(zerosleft==0){ for(j=0;j<zerocount;j++) zerotable[currentrow][pos[j]]=0; currentrow++; //increment the row count to set the next permutation return 1; } for(j=start;j<=varcount-zerosleft;j++){ //store the position for zero pos[zerocount-zerosleft]=j; //start the recursive call permutezero(j+1, zerosleft-1); } return 1; } //function that calculates the diagonal matrix of the non-zero variables and solves for the corresponding value of z float processmatrix(int zerotablerow){ int i,j,k,l,count; float ratio,z=0; //copying the non-zero variables to calculation matrix for(i=0,k=0;i<varcount;i++){ if(zerotable[zerotablerow][i]){ for(j=0;j<eqcount;j++){ calcmat[j][k]=giventable[j+1][i]; } k++; } } //copying the constants for(j=0;j<eqcount;j++){ calcmat[j][k]=giventable[j+1][varcount]; } count=k; //loop to convert into upper triangular matrix for(k=0;k<count;k++){ for(i=k+1;i<count;i++){ if(calcmat[k][k]==0){

## for(j=k+1;j<count;j++){ if(calcmat[j][k]!=0){ for(l=k;l<=count;l++) calcmat[k][l]+=calcmat[j][l]; } }

//loop to convert from upper triangular to diagonal matrix for(k=count-1;k>0;k--){ for(i=k-1;i>=0;i--){ if(calcmat[k][k]==0)continue; ratio=calcmat[i][k]/calcmat[k][k]; calcmat[i][k+1]=calcmat[i][k+1]-(calcmat[k][k+1]*ratio); calcmat[i][k]=0; } } //calculating the values of variables and the corresponding z-value zerotable[zerotablerow][varcount+1]=1; //this value stores whether the sol is basic or non-basic for(i=0,k=0;i<varcount;i++){ if(zerotable[zerotablerow][i]){ //check to stop division by zero if(calcmat[k][k]==0){ zerotable[zerotablerow][varcount+1]=0; continue; } zerotable[zerotablerow][i]=calcmat[k][count]/calcmat[k][k]; //checking for non-basic solution if(zerotable[zerotablerow][i]<0)zerotable[zerotablerow][varcount+1]=0; z+=zerotable[zerotablerow][i]*giventable[0][i]; k++; } } //returning the calculated value of z return z;

int main(){ int i,j,k,l,findmax,first,second; float z_res; printf("Enter 1 for maximization, 0 for minimisation: "); scanf("%d",&findmax); if(findmax)z_res=0; else z_res=1000; printf("Enter total variables: "); scanf("%d",&varcount); printf("\nEnter the objective equation:\n"); for(i=0;i<varcount;i++){

## printf("x%d : ",i+1); scanf("%f",&giventable[0][i]);

printf("Enter no. of contraint equations: "); scanf("%d",&eqcount); for(i=1;i<=eqcount;i++){ printf("\nEnter the constraint equation %d:\n",i); for(j=0;j<varcount;j++){ printf("x%d : ",j+1); scanf("%f",&giventable[i][j]); } printf("Constant term: "); scanf("%f",&giventable[i][j]); } //Calculating the no. of variables to be set zero and the number of permutations possible zerocount=varcount-eqcount; tablerowcount=factorial(varcount)/(factorial(zerocount)*factorial(varcount-zerocount)); for(i=0;i<tablerowcount;i++) for(j=0;j<varcount;j++) zerotable[i][j]=1; //calling the permutation function permutezero(0,zerocount); for(i=0;i<tablerowcount;i++){ zerotable[i][varcount]=processmatrix(i); } printf("\n\nNon Basic solutions:\n\n"); for(i=0;i<tablerowcount;i++){ if(zerotable[i][varcount+1]==0){ for(j=0;j<=varcount;j++) printf("%f ",zerotable[i][j]); printf("\n"); } } printf("\n\nBasic feasible solutions:\n\n"); for(i=0;i<tablerowcount;i++){ if(zerotable[i][varcount+1]==1){ for(j=0;j<=varcount;j++) printf("%f ",zerotable[i][j]); printf("\n"); } } if(findmax){ for(i=0;i<tablerowcount;i++){ if(zerotable[i][varcount+1]==1 && zerotable[i][varcount]>z_res){ second=-1; first=i; z_res=zerotable[i][varcount]; } //check for infinite solution else if(zerotable[i][varcount+1]==1 && zerotable[i][varcount]==z_res){ second=i; } } if(second==-1){ printf("\nOptimal solution: (");

} else {

for(i=0;i<varcount;i++) printf("%f, ",zerotable[first][i]); printf(")\n\nZmax = %f\n\n",z_res); printf("Infinite solutions: "); printf("A( "); for(i=0;i<varcount;i++) printf("%f, ",zerotable[first][i]); printf(") + (1-A)( "); for(i=0;i<varcount;i++) printf("%f, ",zerotable[second][i]); printf(")\n\nZmax = %f\n\n",z_res);

} else{

} } return 0;

for(i=0;i<tablerowcount;i++){ if(zerotable[i][varcount+1]==1 && zerotable[i][varcount]<z_res){ second=-1; first=i; z_res=zerotable[i][varcount]; } //check for infinite solution else if(zerotable[i][varcount+1]==1 && zerotable[i][varcount]==z_res){ second=i; } } if(second==-1){ printf("\nOptimal solution: ("); for(i=0;i<varcount;i++) printf("%f, ",zerotable[first][i]); printf(")\n\nZmin = %f\n\n",z_res); } else { printf("Infinite solutions: "); printf("A( "); for(i=0;i<varcount;i++) printf("%f, ",zerotable[first][i]); printf(") + (1-A)( "); for(i=0;i<varcount;i++) printf("%f, ",zerotable[second][i]); printf(")\n\nZmin = %f\n\n",z_res); }