-
Notifications
You must be signed in to change notification settings - Fork 2
/
revised.c
241 lines (227 loc) · 4.72 KB
/
revised.c
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
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
void gauss_elim(int m,float A[][2*m+1]){
int i,j,k,t;
float c,sum=0,AA[m+1][2*m+1];
for (i=1; i<=m; i++) {
// Search for maximum in this column
float maxEl = (A[i][i]);
int maxRow = i;
for (k=i+1; k<=m; k++) {
if ((A[k][i]) > maxEl) {
maxEl = (A[k][i]);
maxRow = k;
}
}
// Swap maximum row with current row (column by column)
for (k=i; k<=m;k++) {
float tmp = A[maxRow][k];
A[maxRow][k] = A[i][k];
A[i][k] = tmp;
}
// Make all rows below this one 0 in current column
for (k=i+1; k<=m; k++) {
c = -A[k][i]/A[i][i];
for (j=i; j<=2*m; j++) {
if (i==j) {
A[k][j] = 0;
} else {
A[k][j] += c * A[i][j];
}
}
}
}
// for(j=1;j<=m;j++){
// for(i=1;i<=m;i++){
// if(i>j){
// c=A[i][j]/A[j][j];
// for(k=1;k<=2*m;k++){
// A[i][k]-=c*A[j][k];
// }
// }
// }
// }
for(i=1;i<=m;i++)
for(j=1;j<=2*m;j++)
AA[i][j]=A[i][j];
for(i=1;i<=m;i++){
for(j=1;j<=2*m;j++){
if(i!=j) A[i][j]/=A[i][i];
}
A[i][i]=1;
}
printf("\n");
for(i=m-1;i>=1;i--){
for(j=2;j<=2*m;j++){
t=i+1;
sum=0;
for(k=i+1;k<=m;k++){
sum+=A[k][j]*AA[i][t++];
}
A[i][j]-=sum;
}
}
}
void compute_Inv(int m,float B[][m+1],float Binv[][m+1]){
float A[m+1][2*m+1];
int i,j;
for(i=1;i<=m;i++){
for(j=1;j<=m;j++)
A[i][j]=B[i][j];
for(j=m+1;j<=2*m;j++)
A[i][j]=j-m==i?1:0;
}
gauss_elim(m,A);
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
Binv[i][j]= A[i][j+m];
}
void compute_Y(int m,float cB[],float Binv[][m+1],float Y[]){
int i,j;
float sum;
for(i=1;i<=m;i++){
sum=0;
for(j=1;j<=m;j++)
sum+=cB[j]*Binv[j][i];
Y[i]=sum;
}
}
void compute_cB(int m,float z[],int basis[],float cB[]){
int i;
for(i=1;i<=m;i++)
cB[i]=z[basis[i]];
}
void compute_xB(int m,float Binv[][m+1],float b[],float xB[m+1]){
int i,j;
float sum=0;
for(i=1;i<=m;i++){
sum=0;
for(j=1;j<=m;j++)
sum+=Binv[i][j]*b[j];
xB[i]=sum;
}
}
bool x_not_In(int m,int x,int basis[]){
int i;
for(i=1;i<=m;i++)
if(x==basis[i]) return false;
return true;
}
void RevisedSimplex(int n,int m,float a[][n+1],float b[],float z[],float B[][m+1],int basis[]){
printf("Entered\n");
int i,j,k,min_index,min_index2;
for(i=1;i<=m;i++){
for(j=1;j<=m;j++)
printf("%0.4f ",B[i][j]);
printf("\n");
}
printf("Basis ");
for(i=1;i<=m;i++)
printf("%d ",basis[i]);
printf("\n");
float xB[m+1],cB[m+1],Binv[m+1][m+1],Y[m+1],sum,min=0;
compute_Inv(m,B,Binv);
printf("Binv\n");
for(i=1;i<=m;i++){
for(j=1;j<=m;j++)
printf("%0.4f ",Binv[i][j]);
printf("\n");
}
compute_cB(m,z,basis,cB);
compute_xB(m,Binv,b,xB);
//step 1
compute_Y(m,cB,Binv,Y);
printf("\n");
for(i=1;i<=m;i++)
printf("%0.4f ",cB[i]);
printf("\n");
for(i=1;i<=m;i++)
printf("%0.4f ",xB[i]);
printf("\n");
for(i=1;i<=n;i++){
if(x_not_In(m,i,basis)){
sum=0;
for(j=1;j<=m;j++){
sum+=Y[j]*a[j][i];
}
sum-=z[i];
if(min>sum){
min=sum;
min_index=i;// computing entering variable
}
}
}
if(min==0){
sum=0;
for(i=1;i<=m;i++){
printf("X[%d] = %f\n",basis[i],xB[i]);
sum+=cB[i]*xB[i];
}
printf("Z = %f\n", sum);
return;
}
//step 2
else{
printf("Entered step 2\n");
float alpha[m+1];
for(i=1;i<=m;i++){
sum=0;
for(j=1;j<=m;j++){
sum+=Binv[i][j]*a[j][min_index];
}
alpha[i]=sum;
}
for(i=1;i<=m;i++)
printf("%0.4f ", alpha[i]);
printf("\n");
min=10000000;
printf("Theta ");
for(i=1;i<=m;i++){
if(alpha[i]!=0){
if(min>xB[i]/alpha[i] && xB[i]/alpha[i]>0){
printf("%0.4f ",xB[i]/alpha[i]);
min=xB[i]/alpha[i];
min_index2=basis[i];// computing leaving variable
}
}
}
//step 3
printf("\nEntered step 3\n");
for(i=1;i<=m;i++){
if(basis[i]==min_index2){
basis[i]=min_index;
for(j=1;j<=m;j++)
B[j][i]=a[j][min_index];
break;
}
}
RevisedSimplex(n,m,a,b,z,B,basis);
}
}
int main(){
int n,m,i,j;
printf("Enter n and m\n");
scanf("%d%d",&n,&m);
printf("Enter the coefficient matrix\n");
float a[m+1][n+1],b[m+1],z[n+1],B[m+1][m+1];
int basis[m+1];
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%f",&a[i][j]);
printf("Enter the constant matrix\n");
for(i=1;i<=m;i++)
scanf("%f",&b[i]);
printf("Enter the objective function\n");
for(i=1;i<=n;i++)
scanf("%f",&z[i]);
for(i=1;i<=m;i++)
basis[i]=n-m+i;
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
B[i][j]=i==j?1:0;
RevisedSimplex(n,m,a,b,z,B,basis);
// RevisedSimplex(n,m,a,b,z,B,basis);
// RevisedSimplex(n,m,a,b,z,B,basis);
// RevisedSimplex(n,m,a,b,z,B,basis);
}