├── README.md ├── bashrc.sh ├── day1 ├── Class 1 (10.1.17).pdf ├── gauss-seidel.c └── gauss-seidel.c.out ├── day2 ├── Class 2 (17.1.17).pdf ├── bsf-optimal.pdf ├── bsf.c ├── bsf.c.out ├── bsf_optimal.c ├── bsf_optimal.c.out ├── day2.pdf ├── gauss-elimination.c └── gauss-elimination.c.out ├── day3 ├── Assignment 3 Lab.pdf ├── simplex.c ├── simplex.c.out └── simplex.pdf ├── day4 ├── Class 4 (31.1.17).pdf ├── big_m.c ├── big_m.c.out └── output.pdf ├── day5 ├── Big_M_menu.pdf ├── Class 5 (07.02.17).pdf ├── big_m_menu.c └── big_m_menu.c.out ├── day6 ├── Class 6 (28.02.17).pdf ├── dual_simplex.c ├── dual_simplex.c.out ├── revised_simplex.c └── revised_simplex.c.out ├── day7 ├── 2phase_simplex.c ├── 2phase_simplex.c.out └── Lab on Two phase Simplex.pdf ├── day8 ├── Class 8 (14.03.17).pdf ├── branch&bound.cpp ├── cutting_plane.cpp ├── cutting_plane.cpp.out └── day8.pdf ├── day9 ├── question.pdf ├── transport.c ├── transport.c.out └── transport.pdf ├── index.pdf └── or_lab.sh /README.md: -------------------------------------------------------------------------------- 1 | # OR Lab 2 | 3 | This repository that contains the code I've submitted for the course MA39014 - Operations Research Lab, IIT Kharagpur. 4 | 5 | ### Getting started 6 | 7 | - Clone this repository 8 | - Set this up in your shell configuration file (`~/.bashrc`, `~/.zshrc/` or `~/.fishrc`). This code snippet can also be found in [`bashrc.sh file`](https://github.com/athityakumar/or_lab/blob/master/bashrc.sh) 9 | 10 | ``` 11 | export OR_LAB_REPO_PATH=/path/to/or_lab/ 12 | source $OR_LAB_REPO_PATH/or_lab.sh 13 | ``` 14 | - Restart your terminal, or just source your shell configuration file (say, `source ~/.bashrc`for bash shell) 15 | 16 | ### Shell Commands 17 | 18 | All tweaks required can be made, by incorporating the changes in the [`or_lab.sh`](https://github.com/athityakumar/or_lab/blob/master/or_lab.sh) file 19 | 20 | - `or_c` : Single command to compile and execute the `*.c` file. For example, `or_c filename.c` 21 | - `or_cpp` : Single command to compile and execute the `*.cpp` file. For example, `or_cpp filename.cpp` 22 | - `or_push` : Command to commit and push your changes to the remote git repository. You may have to set your git remote `origin` properly to point to a repository you have push access to. For example, `or_push "Makes these changes"`. 23 | - `or_pull` : Single command to commit and push your changes to the remote git repository. You may have to set your git remote `origin` properly to point to a repository you have push access to. For example, simply use `or_pull` from the root directory of the git repository. 24 | - `or_cd` : Command to directly change the directory to this path. Made for fellow developers, who might be lazy to type 6 extra characters. For example, `or_cd`. 25 | 26 | ### License 27 | 28 | Feel free to use any code present in this repository, without any restrictions. -------------------------------------------------------------------------------- /bashrc.sh: -------------------------------------------------------------------------------- 1 | # Copy paste this code into ~/.bashrc file with proper repo path. This is just an example path. 2 | # IMPORTANT NOTE : Don't change the variable from "OR_LAB_REPO_PATH" to anything else. 3 | 4 | export OR_LAB_REPO_PATH=~/or_lab 5 | source $OR_LAB_REPO_PATH/or_lab.sh -------------------------------------------------------------------------------- /day1/Class 1 (10.1.17).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/athityakumar/or_lab/83b7a79292e0d545aff6b594f906397e2f197f27/day1/Class 1 (10.1.17).pdf -------------------------------------------------------------------------------- /day1/gauss-seidel.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | 6 | int n,i,j,key=1; 7 | double er,val,x[100],x0[100],sum,a[100][100],b[100]; 8 | printf("\n\n Enter stopping criteria (er) : "); 9 | scanf("%lf",&er); 10 | printf("\n Enter size of matrix (n) : "); 11 | scanf("%d",&n); 12 | for(i=0;ier) 56 | { 57 | key = 1; 58 | x0[i] = x[i]; 59 | } 60 | } 61 | 62 | }while(key==1); 63 | 64 | printf("\n\n Printing solved matrix x."); 65 | for(i=0;i 2 | 3 | int gauss_elimination(double a[][100], int m,int n, int new2darr[], int option,int r) 4 | { 5 | int i,j,k,key,flag,track=0; 6 | double er=0.001,val,x[100],x0[100],sum,a_new[100][100],c; 7 | for(i=0;ij) 16 | { 17 | c=a[i][j]/a[j][j]; 18 | for(k=0; k=0; i--) 27 | { 28 | sum=0; 29 | for(j=i+1; j 0) 59 | { 60 | check_degeneracy = 1; 61 | } 62 | } 63 | } 64 | if((check_infeasibility==1 && option==1)||(check_degeneracy==1 && option ==3)||(check_infeasibility==0 && check_degeneracy==0 && option==2)) 65 | { 66 | for(j=0;j= r-index; i++) 103 | { 104 | data[index] = arr[i]; 105 | s = number_combine(arr, data, i+1, end, index+1, r, s); 106 | } 107 | return s; 108 | } 109 | 110 | 111 | int * combine(int arr[], int data[], int start, int end,int index, int r, int* newarr, int *l) 112 | { 113 | int j,i; 114 | if (index == r) 115 | { 116 | for (j=0; j= r-index; i++) 125 | { 126 | data[index] = arr[i]; 127 | newarr = combine(arr, data, i+1, end, index+1, r, newarr, l); 128 | } 129 | return newarr; 130 | } 131 | 132 | 133 | void main () { 134 | 135 | int i,j,m,n,option; 136 | printf("\n Enter number of unknowns(n) : "); 137 | scanf("%d",&n); 138 | printf(" Enter number of equations (m) : "); 139 | scanf("%d",&m); 140 | double a[m][n],b[m]; 141 | printf("\n"); 142 | for(i=0;i 2 | 3 | double gauss_elimination(double a[][100], int m,int n, int new2darr[], double objective[]) 4 | { 5 | int i,j,k,key,flag,track=0; 6 | double er=0.001,val,x[100],x0[100],sum,a_new[100][100],c; 7 | for(i=0;ij) 16 | { 17 | c=a[i][j]/a[j][j]; 18 | for(k=0; k=0; i--) 27 | { 28 | sum=0; 29 | for(j=i+1; j 0) 66 | { 67 | check_degeneracy = 1; 68 | } 69 | } 70 | } 71 | if(check_degeneracy==1) 72 | { 73 | printf(" - Degenerate solution"); 74 | } 75 | if(check_infeasibility==0 && check_degeneracy==0) 76 | { 77 | printf(" - Basic feasible solution"); 78 | } 79 | if(check_infeasibility==1) 80 | { 81 | obj_val = 0.0; 82 | printf(" - Infeasible solution"); 83 | } 84 | else 85 | { 86 | printf(" - Objective function value = %lf",obj_val); 87 | } 88 | return obj_val; 89 | } 90 | 91 | 92 | int number_combine(int arr[], int data[], int start, int end,int index, int r, int s) 93 | { 94 | int i; 95 | if (index == r) 96 | { 97 | return(s+1); 98 | } 99 | 100 | for (i=start; i<=end && end-i+1 >= r-index; i++) 101 | { 102 | data[index] = arr[i]; 103 | s = number_combine(arr, data, i+1, end, index+1, r, s); 104 | } 105 | return s; 106 | } 107 | 108 | 109 | int * combine(int arr[], int data[], int start, int end,int index, int r, int* newarr, int *l) 110 | { 111 | int j,i; 112 | if (index == r) 113 | { 114 | for (j=0; j= r-index; i++) 123 | { 124 | data[index] = arr[i]; 125 | newarr = combine(arr, data, i+1, end, index+1, r, newarr, l); 126 | } 127 | return newarr; 128 | } 129 | 130 | 131 | void main () { 132 | int i,j,m,n,optimal; 133 | printf("\n Enter number of unknowns (n) : "); 134 | scanf("%d",&n); 135 | printf(" Enter number of equations (m) : "); 136 | scanf("%d",&m); 137 | double a[m][n],b[m],objective_function[n]; 138 | printf("\n"); 139 | for(i=0;i sum[j]) 246 | { 247 | replace = sum[i]; 248 | sum[i] = sum[j]; 249 | sum[j] = replace; 250 | } 251 | } 252 | } 253 | } 254 | 255 | if(sum[0]==sum[1]) 256 | { 257 | if(sum[0] == 0) 258 | { 259 | printf("\n\n No optimal solution."); 260 | } 261 | else 262 | { 263 | printf("\n\n Infinitely many optimal solutions, with optimal objective function value = %lf",sum[0]); 264 | } 265 | } 266 | else 267 | { 268 | printf("\n\n Exactly one optimal solution, with optimal objective function value = %lf",sum[0]); 269 | } 270 | printf("\n"); 271 | } 272 | -------------------------------------------------------------------------------- /day2/bsf_optimal.c.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/athityakumar/or_lab/83b7a79292e0d545aff6b594f906397e2f197f27/day2/bsf_optimal.c.out -------------------------------------------------------------------------------- /day2/day2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/athityakumar/or_lab/83b7a79292e0d545aff6b594f906397e2f197f27/day2/day2.pdf -------------------------------------------------------------------------------- /day2/gauss-elimination.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void gauss_elimination(double a[][100], int m,int n, int new2darr[]) 4 | { 5 | int i,j,k,key,flag,track=0; 6 | double er=0.001,val,x[100],x0[100],sum,a_new[100][100],c; 7 | for(i=0;ij) 16 | { 17 | c=a[i][j]/a[j][j]; 18 | for(k=0; k=0; i--) 27 | { 28 | sum=0; 29 | for(j=i+1; j= r-index; i++) 62 | { 63 | data[index] = arr[i]; 64 | s = number_combine(arr, data, i+1, end, index+1, r, s); 65 | } 66 | return s; 67 | } 68 | 69 | 70 | int * combine(int arr[], int data[], int start, int end,int index, int r, int* newarr, int *l) 71 | { 72 | int j,i; 73 | if (index == r) 74 | { 75 | for (j=0; j= r-index; i++) 84 | { 85 | data[index] = arr[i]; 86 | newarr = combine(arr, data, i+1, end, index+1, r, newarr, l); 87 | } 88 | return newarr; 89 | } 90 | 91 | 92 | void main () { 93 | 94 | int i,j,m,n; 95 | printf("\n Enter number of unknowns(n) : "); 96 | scanf("%d",&n); 97 | printf(" Enter number of equations (m) : "); 98 | scanf("%d",&m); 99 | double a[m][n],b[m]; 100 | printf("\n"); 101 | for(i=0;i 2 | #include 3 | 4 | int next_iteration_index(double *ptr, int m, int n) 5 | { 6 | int i,index=0; 7 | for(i=1;i 0) 15 | { 16 | return -1; 17 | } 18 | else 19 | { 20 | if(*(ptr+(m-1)*n+index) == 0) 21 | { 22 | return -2; 23 | } 24 | } 25 | return index; 26 | } 27 | 28 | int pivot_index(double *ptr, int m, int n) 29 | { 30 | int i,j=next_iteration_index(ptr,m,n),index=j; 31 | double vi = *(ptr+j),vn = *(ptr+n-1),min = vn/vi; 32 | for(i=1;i0 && (vn/vi) 2 | #include 3 | 4 | int next_iteration_index(double *ptr, int m, int n) 5 | { 6 | int i,index=0; 7 | for(i=1;i 0) 15 | { 16 | return -1; 17 | } 18 | else 19 | { 20 | if(*(ptr+(m-1)*n+index) == 0) 21 | { 22 | return -2; 23 | } 24 | } 25 | return index; 26 | } 27 | 28 | int pivot_index(double *ptr, int m, int n) 29 | { 30 | int i,j=next_iteration_index(ptr,m,n),index=j; 31 | double vi = -1.0,vn = -1.0,min = 10000.0; 32 | for(i=0;i0 && (vn/vi)= B \n 2. Ax = B \n 3. Ax <= B \n Enter your option : ",(i+1)); 219 | scanf("%d",&option[i]); 220 | if(option[i]==1) 221 | { 222 | n++; 223 | } 224 | } 225 | 226 | printf("\n"); 227 | printf(" Type of optimization \n 1. Minimize \n 2. Maximize \n Enter your option : "); 228 | scanf("%d",&obj_type); 229 | 230 | double bigm_arr[m*n]; 231 | for(i=0;i 2 | #include 3 | #include 4 | 5 | int get_slack(int ies[], int m) 6 | { 7 | int slack = 0; 8 | for(int i=0; i= 0) 39 | cbv[i] = -100000; 40 | else 41 | cbv[i] = 0; 42 | } 43 | int surplus = get_surplus(ies, m); 44 | for(int i=0; i=n) table[m][i] = inner_product; 61 | else table[m][i] = inner_product - c[i]; 62 | } 63 | } 64 | 65 | void copy_2d_array(float src[][100], float dest[][100], int m, int n) 66 | { 67 | for(int i=0; i= n) continue; 124 | sol_array[nbv[i]] = 0; 125 | } 126 | for(int i=0; i= n) continue; 129 | sol_array[bv[i]] = table[i][n+surplus]; 130 | } 131 | for(int i=0; i 0 || is_nearly_equal(value, 0, pow(2, -6))) continue; 156 | if(value < min || pivot_h == -1) 157 | { 158 | pivot_h = i; 159 | min = table[m][i]; 160 | } 161 | } 162 | return pivot_h; 163 | } 164 | 165 | int get_pivot_v(float table[][100], int pivot_h, int m, int n) 166 | { 167 | float min = 0; 168 | int pivot_v = -1; 169 | for(int i=0; i= 0 && ies[index] != -1) 253 | { 254 | status = 3; 255 | flag = 0; 256 | break; 257 | } 258 | } 259 | if(flag != 0) 260 | { 261 | if(zero > 0) 262 | { 263 | status = 1; 264 | flag = 0; 265 | } 266 | else 267 | { 268 | status = 0; 269 | flag = 0; 270 | } 271 | } 272 | } 273 | else 274 | { 275 | pivot_h = get_pivot_h(table, m, n+surplus); 276 | if(pivot_h < 0) 277 | { 278 | printf("Error occurred!\n"); 279 | exit(-1); 280 | } 281 | int pos_col=0, neg_col=0, zero_col=0; 282 | for(int i=0; i=B \n Enter your option : "); 375 | scanf("%d", &ies[i]); 376 | printf("Input for matrix B's constant for equation %d : ", i+1); 377 | scanf("%f", &b[i]); 378 | printf("\n"); 379 | } 380 | 381 | printf("\nEnter the coefficients of the maximizing equation (enter -ve coefficients for minimising function) :\n"); 382 | for(int i=0; i 2 | #define INFINITY -999 3 | #define N 10 4 | #define M 20 5 | /***************************************************/ 6 | /***** Solves the LPP by "DUAL SIMPLEX" method *****/ 7 | /***************************************************/ 8 | void minimum(float *arr,int *arrminpos,int n); 9 | /* Calculates the minimum valued position among the array arr having n elements. */ 10 | void maximum(float *arr,int *arrminpos,int n); 11 | /* Calculates the minimum valued position among the array arr having n elements. */ 12 | void display (float c[],float b[],float a[][M],int basic[]); 13 | /* Display the table */ 14 | void displayframe(float c[M]); 15 | /* Displays the frame of the table */ 16 | void calctemp(float *,float [][M],float [],int []); 17 | /* Calculates Zj-Cj */ 18 | 19 | int countmaxzterms; 20 | int constraint; 21 | 22 | void main() 23 | { 24 | float c[M]; 25 | /* Stores co-efficient of the objective function Max(z) */ 26 | float a[N][M]; /* Stores the co-efficent of the constraints */ 27 | float b[N]; /* Stores the values on RHS of constraints */ 28 | float temp[M]; /* Stores the values of Zj-Cj*/ 29 | int bminpos; /* Stores the minimum valued position 30 | of {Zj-Cj} i.e. coming in variable */ 31 | float maxratio[M]; /* Stores the value of the ratio Zj-Cj/a[i][j] */ 32 | int maxratiomaxpos; /* Stores the minimum valued position of 33 | b[i]/a[i][j] i.e. going out variable */ 34 | float key; /* Stores the key element */ 35 | int gooutcol; /* Stores the column number which goes out */ 36 | int incomingcol; 37 | float z; /* Stores the value of the objective function */ 38 | float x[M]; /* Stores the value of the variables */ 39 | int i,j; /* Loop variables */ 40 | int basic[N]; /* Stores the basic variable */ 41 | int flag=0; /* Terminating variable */ 42 | int obj_type=0; 43 | /*** Initializing basic variables ***/ 44 | 45 | for(i=0;i=, else <=) \n"); 100 | for(i=0;i0) 153 | { 154 | maxratio[i]=INFINITY; 155 | continue; 156 | } 157 | maxratio[i]=temp[i]/a[bminpos][i]; 158 | } 159 | maximum(maxratio,&maxratiomaxpos,2*constraint); 160 | incomingcol=maxratiomaxpos; 161 | for(i=0;iarrmax) 223 | { 224 | arrmax=arr[i]; 225 | *arrmaxpos=i; 226 | } 227 | } 228 | 229 | void minimum(float *arr,int *arrminpos, int n) 230 | { 231 | int i; 232 | int arrmin; 233 | arrmin=arr[0]; 234 | *arrminpos=0; 235 | for(i=0;i 2 | #include 3 | 4 | #define MAX 5 5 | #define FORN(i,n) for(int i=0;i> inequality; 182 | //if (inequality.at(0) == '<') 183 | //{ 184 | slackVar++; 185 | scanf("%f", &mat[i][j]); 186 | } 187 | //else if (inequality.at(0) == '>') 188 | //{ 189 | // Can be extended for Big-M Method 190 | //} 191 | 192 | } 193 | 194 | } 195 | */ 196 | int main(){ 197 | 198 | float b[MAX][MAX]={0}; 199 | float bi[MAX][MAX]; 200 | // float b1[MAX][MAX] = {{3,2,3,1},{3,0,1,2},{1,3,0,2}}; 201 | // float c[MAX] = {200,180,160,50}; 202 | float b1[MAX][MAX] = {{-2,3,0,5},{-2,-1,1,-2},{1,0,3,3}}; 203 | float c[MAX] = {-2,3,3,0}; 204 | 205 | int n = 3; 206 | 207 | initialize_unity(b); 208 | copy(bi,b); 209 | 210 | calculate(b,bi,b1,c,n); 211 | 212 | return 0; 213 | } -------------------------------------------------------------------------------- /day6/revised_simplex.c.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/athityakumar/or_lab/83b7a79292e0d545aff6b594f906397e2f197f27/day6/revised_simplex.c.out -------------------------------------------------------------------------------- /day7/2phase_simplex.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int min_index(float *lastrow, int length) 8 | { 9 | int index=0; 10 | for (int i=1; i=0) 16 | return -1; 17 | else 18 | return index; 19 | } 20 | 21 | int min_col_index(float *temp, int length) 22 | { 23 | int index=0; 24 | for (int i=1; i=0) 68 | sol_[index[i]]=activeA[i][n]; 69 | } 70 | for (i=0; i=0) 91 | sol_[index[i]]=activeA[i][n]; 92 | } 93 | for (i=0; i0) 118 | temp[i]=activeA[i][n]/activeA[i][pivot_col]; 119 | else 120 | temp[i]=1000000; 121 | } 122 | pivot_row=min_col_index(temp, m); 123 | cout<<"Row Column"<>m; 164 | 165 | cout<<"Enter No of Unknwons (n) : "; 166 | cin>>n; 167 | 168 | cout<<"Enter the number of >=, =, <= contraints : "<>geq>>eq>>leq; 170 | 171 | if (geq==0 && eq==0) 172 | M=0; 173 | 174 | A= new float* [m]; 175 | activeA = new float* [m+1+1+1]; 176 | 177 | for (i=0; i>A[i][j]; 189 | activeA[i][j]=A[i][j]; 190 | } 191 | } 192 | 193 | for (i=0; i>b[i]; 207 | activeA[i][n+geq]=b[i]; 208 | } 209 | 210 | for (i=0; i>max_min; 228 | 229 | cout<<"Enter the objective function z :\n"; 230 | z=new float [n]; 231 | for (i=0; i>z[i]; 233 | if (max_min==1) 234 | activeA[m+1][i]=-z[i]; 235 | if (max_min==2) 236 | activeA[m+1][i]=z[i]; 237 | } 238 | 239 | sol_index = new int[m]; 240 | 241 | for (i=0; i=50) 256 | { 257 | for (j=0; j 2 | #include 3 | #include 4 | 5 | int a[10][10],visited[10],n,cost=0; 6 | 7 | void get() 8 | { 9 | int i,j; 10 | printf("\n\nEnter Number of Cities: "); 11 | scanf("%d",&n); 12 | printf("\nEnter Cost Matrix: \n"); 13 | for( i=0;i ",city+1); 35 | ncity=least(city); 36 | 37 | if(ncity==999) 38 | { 39 | ncity=0; 40 | printf("%d",ncity+1); 41 | cost+=a[city][ncity]; 42 | return; 43 | } 44 | 45 | mincost(ncity); 46 | } 47 | 48 | int least(int c) 49 | { 50 | int i,nc=999; 51 | int min=999,kmin; 52 | for(i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int d[10]={0}; 6 | int dmap[10]={-1}; 7 | float mat[10][10], b[10], temp[10][10], constants[10]; 8 | float ans[10][10], z[10]; 9 | int R, C; 10 | 11 | int duald[10]={0}; 12 | int dualdmap[10]={-1}; 13 | float dualmat[10][10], dualb[10], dualtemp[10][10], dualconstants[10]; 14 | float dualans[10][10], dualz[10]; 15 | int dualR, dualC; 16 | 17 | int main() 18 | { 19 | int in_var, eqn, var; 20 | int flag = 0;int incos=0; 21 | string inequality; 22 | cout << "Enter no of variables\n"; 23 | cin >> in_var; 24 | var= in_var; 25 | cout << "Enter no. of equations\n"; 26 | cin >> eqn; 27 | for(int i = 0 ; i < eqn ; i++) 28 | { 29 | cout << "Enter coefficients and constant term of equation no " << i+1 << " seperated by spaces:\n"; 30 | for(int j = 0 ; j <= in_var ; j++) 31 | { 32 | cin >> mat[i][j]; 33 | } 34 | 35 | } 36 | 37 | for(int i=0;i < eqn;i++) 38 | { 39 | for(int j=0;j<=in_var;j++) 40 | { 41 | temp[i][j] = mat[i][j]; 42 | } 43 | } 44 | 45 | cout << "Enter the objective function to be maximised\n"; 46 | for(int i=0;i<=in_var;i++) 47 | { 48 | int a=0; 49 | cin >> a; 50 | if(a>0)flag=1; 51 | temp[eqn][i] = -a; 52 | } 53 | 54 | cout << "\nInitial simplex table\n "; 55 | for(int i=0;i <= eqn;i++) 56 | { 57 | for(int j=0;j<=in_var;j++) 58 | { 59 | cout << " " << temp[i][j]; 60 | } 61 | cout << endl; 62 | } 63 | int dummy=1; 64 | cout << "\n Flag is " << flag; 65 | while(flag){ 66 | 67 | float min = 10000; int prod=0; 68 | int minpos = -1; 69 | for(int i=0;i0) 94 | { 95 | minp = 1.0*temp[i][in_var]/temp[i][minpos]; 96 | pivot = i; 97 | } 98 | } 99 | 100 | if(pivot==-1){ 101 | flag=0; 102 | incos = 1; 103 | break; 104 | } 105 | cout << "\n Pivot : " << temp[pivot][minpos] << " at pos " << pivot; 106 | cout << "\n Most negative element " << min; 107 | 108 | d[minpos] = 1; 109 | dmap[minpos] = pivot; 110 | float p = temp[pivot][minpos]; 111 | 112 | for(int i=0;i<=eqn;i++) 113 | { 114 | for(int j=0;j<=in_var;j++) 115 | { 116 | if(i==pivot || j==minpos)continue; 117 | temp[i][j] = temp[i][j] - 1.0*temp[pivot][j]*temp[i][minpos]/p; 118 | } 119 | } 120 | 121 | for(int i=0;i<=eqn; i++) 122 | { 123 | if(i==pivot)continue; 124 | temp[i][minpos] = -temp[i][minpos]/p; 125 | } 126 | for(int j=0;j<=in_var;j++) 127 | { 128 | if(j==minpos)continue; 129 | temp[pivot][j] = temp[pivot][j]/p; 130 | } 131 | temp[pivot][minpos] = 1.0/temp[pivot][minpos]; 132 | 133 | 134 | 135 | cout << "\n Simplex table\n "; 136 | for(int i=0;i <= eqn;i++) 137 | { 138 | for(int j=0;j<=in_var;j++) 139 | { 140 | cout <1e-5){ 168 | ifc = false;break; 169 | } 170 | } 171 | if(ifc){flagdual=false;break;} 172 | float xbmax=-1; int xbpos; 173 | for(int i=0;i < eqn;i++) 174 | { 175 | for(int j=0;j<=in_var;j++) 176 | { 177 | dualtemp[i][j] = temp[i][j]; 178 | if(j==in_var && fabs(floor(temp[i][j])-temp[i][j])>1e-5){ 179 | if((temp[i][j]-floor(temp[i][j])) > xbmax){ 180 | xbmax = temp[i][j]-floor(temp[i][j]); 181 | xbpos = i; 182 | } 183 | } 184 | } 185 | } 186 | cout << "\nvalue of xbmax: " << xbmax << endl; 187 | if((int)xbmax==-1 || fabs(xbmax-1)<1e-5){cout << "\n"< 2 | 3 | int main() 4 | { 5 | int flag=0,flag1=0; 6 | int s[10],d[10],sn,eop=1,dm,a[10][10]; 7 | int i,j,sum=0,min,x[10][10],k,fa,fb; 8 | printf("Enter the number of supplies : "); 9 | scanf("%d",&sn); 10 | printf("Enter the number of demands : "); 11 | scanf("%d",&dm); 12 | printf("Enter the supply values : \n"); 13 | for(i=0;i=d[j]) 37 | { 38 | x[i][j]=a[i][j]*d[j]; 39 | s[i]=s[i]-d[j]; 40 | j++; 41 | } 42 | } 43 | printf("\n\n Given cost matrix is :"); 44 | for(fa=0;fa