-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig_m_method.java
502 lines (342 loc) · 13 KB
/
big_m_method.java
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import java.util.*;
import java.io.*;
/**
* Code by Bijendar Prasad | IIITD'23
*/
/**
* This is a necessary condition for solving the problem:
* the numbers on the right parts of the constraint system must be non-negative.
*/
/**
* Since, Simplex method for LPPs with ≥ and = constraints needs a modified approach.
* So We use Big-M method, but Big M method raises difficulty when the problem is solved on a digital computer.
* Convert minimization into maximization problem.
*/
public class big_m_method {
static int M = 999999;
static List<Integer> C_j = new ArrayList<>();
static List<Float> Z_j = new ArrayList<>();
static List<Integer> C_B = new ArrayList<>();
static List<String> B = new ArrayList<>();
static List<Float> X_B = new ArrayList<>();
static List<String> variables = new ArrayList<>();
static List<String> artificial_variables = new ArrayList<>();
static List<Float> difference = new ArrayList<>();
static float[][] table;
static String[] row_variables; // all variables in a row
static String[] basic_variables; // contain basic variables
static boolean problemType = false; // Problem Type
static boolean canPrint = true;
public static void initialize() {
Scanner input = new Scanner(System.in);
System.out.println("Choose Problem Type:\n" +"\t\t 1) Maximization Problem \n" + "\t\t 2) Minimization Problem");
System.out.print("Enter chosen type: ");
int type = input.nextInt();
/**
String prob_type = Input.next();
int type = 0;
try {
type = Integer.parseInt(prob_type);
} catch (NumberFormatException ex) {
System.out.println("Wrong input!!!");
input.close();
}
*/
while (type > 2 || type <= 0) {
System.out.println("Invalid Input");
System.out.print("Enter chosen type again: ");
type = input.nextInt();
}
problemType = type == 1; // Assign max/min type...
System.out.print("Enter No. of variables: ");
int n = input.nextInt();
System.out.print("Enter No. of constraints: ");
int m = input.nextInt();
System.out.println("Enter coefficients of Objective Function:");
int[] objective_arr = new int[n];
for (int i=0; i<n; i++) {
System.out.print("Enter the value of " + "x" + (i+1) + ": ");
objective_arr[i] = problemType ? input.nextInt() : - input.nextInt();
String str = "x" + (i+1);
variables.add(str);
}
for (int i=0; i<objective_arr.length; i++) {
C_j.add(objective_arr[i]);
}
List<List<Integer>> list = new ArrayList<>();
int point = n;
int var = 1;
int art = 1;
for (int j=0; j<m; j++) {
System.out.println("Enter LHS coefficients of constraints(" + (j+1) + ") : ");
List<Integer> l = new ArrayList<>();
for (int i=0; i<n; i++) {
System.out.print("Enter the value of " + "x" + (i+1) + ": ");
int num = input.nextInt();
l.add(num);
}
System.out.println("Choose Inequality option: \n" + "\t\t 1) ≤ \n" + "\t\t 2) ≥ \n" + "\t\t 3) = ");
System.out.print("Enter chosen option: ");
int choice = input.nextInt();
while (choice > 3 || choice <= 0) {
System.out.println("Invalid Input");
System.out.print("Enter chosen option: ");
choice = input.nextInt();
}
System.out.print("Enter RHS coefficient of constraints(" + (j+1) + ") : ");
int b_val = input.nextInt();
X_B.add((float) Math.abs(b_val));
for (int i=0; i<point; i++) {
l.add(0);
}
if(b_val < 0) { // negative value...
for (int i=0; i<l.size(); i++) {
l.set(i, -l.get(i));
}
if(choice == 1) {
// slack variable added
l.set(point++, -1);
C_j.add(0);
l.set(point++, 1);
C_j.add(-M);
String s = "s" + (var++);
variables.add(s);
String a = "a" + (art++);
variables.add(a);
} else if(choice == 2) {
// surplus variable added
l.set(point++, 1);
C_j.add(0);
String s = "s" + (var++);
variables.add(s);
} else {
l.set(point++, 1);
C_j.add(-M);
String a = "a" + (art++);
variables.add(a);
}
} else {
if(choice == 1) {
// slack variable added
l.set(point++, 1);
C_j.add(0);
String s = "s" + (var++);
variables.add(s);
} else if(choice == 2) {
// surplus variable added
l.set(point++, -1);
C_j.add(0);
l.set(point++, 1);
C_j.add(-M);
String s = "s" + (var++);
variables.add(s);
String a = "a" + (art++);
variables.add(a);
} else {
l.set(point++, 1);
C_j.add(-M);
String a = "a" + (art++);
variables.add(a);
}
}
list.add(l);
}
System.out.println();
// System.out.println(C_j);
// for (int i=0; i<list.size(); i++) {
// System.out.println(list.get(i));
// }
table = new float[m][list.get(list.size()-1).size()];
for (int i=0; i<list.size(); i++) {
for (int j=0; j<list.get(i).size(); j++) {
table[i][j] = list.get(i).get(j);
}
}
System.out.println(Arrays.deepToString(table));
for (int i=0; i<table.length; i++) {
for (int j=n; j<C_j.size(); j++) {
if(table[i][j] == 1.0) {
B.add(variables.get(j));
C_B.add(C_j.get(j));
}
}
}
// System.out.println(variables);
// System.out.println(C_B);
// System.out.println(B);
// System.out.println(X_B);
for (int i=0; i<C_j.size(); i++) {
if(C_j.get(i) == -M) {
artificial_variables.add(variables.get(i));
}
}
// System.out.println(artificial_variables);
// fill_variables(n, m);
//
// System.out.println(Arrays.toString(row_variables));
// System.out.println(Arrays.toString(basic_variables));
//
System.out.println();
//
optimize_table();
System.out.println();
System.out.println("Final table: " + Arrays.deepToString(table));
System.out.println();
if(canPrint) {
print_solution();
}
}
public static void optimize_table() {
int iter = 1;
while (ifminExists() && iter < 4) {
int index = min_index();
float min_ratio = Float.MAX_VALUE;
int min_index = 0;
boolean state = false;
for (int j=0; j<table.length; j++) {
if (table[j][index] > 0) { // must be > 0
state = true;
float ratio = X_B.get(j) / table[j][index];
if(ratio < min_ratio) {
min_ratio = ratio;
min_index = j;
}
}
}
// System.out.println("Statehdhddh:" + state);
if(!state) {
System.out.println("******* This system has unbounded solution *******");
canPrint = false;
break;
} else {
System.out.println("************ Iteration - " + iter + " ************");
System.out.println("Incoming Variable is: " + variables.get(index));
System.out.println("Outgoing Variable is: " + B.get(min_index));
System.out.println();
B.set(min_index, variables.get(index)); // swap basic variables...
C_B.set(min_index, C_j.get(index));
// System.out.println("Cb:" + C_B);
// System.out.println("B:" + B);
row_operation(index, min_index); // row operation in table...
// System.out.println("X_b: " + X_B);
iter++;
}
Z_j = new ArrayList<>();
difference = new ArrayList<>();
}
}
public static void row_operation(int index, int min_index) {
float num = table[min_index][index];
X_B.set(min_index, X_B.get(min_index)/num);
for (int i=0; i<C_j.size(); i++) {
table[min_index][i] = table[min_index][i] / num;
}
// System.out.println(Arrays.deepToString(table));
for (int i=0; i<table.length; i++) {
if (i != min_index) {
float cal = -table[i][index];
for (int j=0; j<C_j.size(); j++) {
table[i][j] = cal*table[min_index][j] + table[i][j];
}
// System.out.println("cal: " + cal);
// System.out.println("min_index: " + X_B.get(min_index));
// System.out.println("min: " + X_B.get(index));
// System.out.println("i: " + X_B.get(i));
X_B.set(i, cal*X_B.get(min_index) + X_B.get(i));
}
}
// System.out.println(Arrays.deepToString(table));
}
public static void print_solution() {
boolean yes = true;
for (int i=0; i<artificial_variables.size(); i++) {
for (int j=0; j<B.size(); j++) {
if(artificial_variables.get(i).equals(B.get(j)) && X_B.get(j)>0) {
yes = false;
break;
}
}
}
if (!yes) {
System.out.println("*************** No Feasible Solution: *********************");
} else {
System.out.println("*************** Basic Feasible Solution: *********************");
for (int i=0; i<variables.size(); i++) {
boolean state = false;
for (int j=0; j<B.size(); j++) {
if(variables.get(i).equals(B.get(j))) {
System.out.println("The value of " + variables.get(i) + " is: " + X_B.get(j));
state = true;
break;
}
}
if(!state) {
System.out.println("The value of " + variables.get(i) + " is: " + 0);
}
}
float total = 0;
for (int i=0; i<X_B.size(); i++) {
total += C_B.get(i)*X_B.get(i);
}
if (problemType) {
System.out.println("The value of Z_max is: " + total);
} else {
System.out.println("The value of Z_min is: " + -total);
}
}
}
public static void diff() {
for (int i=0; i<C_j.size(); i++) {
float val = 0;
for (int j=0; j<table.length; j++) {
val += C_B.get(j)*table[j][i];
}
Z_j.add(val);
difference.add(Z_j.get(i) - C_j.get(i));
}
// System.out.println(Z_j);
// System.out.println(difference);
}
public static boolean ifminExists() {
diff();
boolean state = false;
for (int i=0; i<difference.size(); i++) {
if (difference.get(i) < 0) {
state = true;
break;
}
}
return state;
}
public static int min_index() {
int index = 0;
float min = Float.MAX_VALUE;
for (int i=0; i<difference.size(); i++) {
if (difference.get(i) < min) {
index = i;
min = difference.get(i);
}
}
// System.out.println(min);
return index;
}
public static void fill_variables(int n, int m) {
basic_variables = new String[m+1];
basic_variables[0] = "c";
for (int i=0; i<m; i++) {
basic_variables[i+1] = "s" + (i+1);
}
row_variables = new String[n+m+2];
row_variables[0] = "z";
for (int i=0; i<n; i++) {
row_variables[i+1] = "x" + (i+1);
}
for (int i=0; i<m; i++) {
row_variables[n+i+1] = "s" + (i+1);
}
row_variables[n+m+1] = "b";
}
public static void main(String[] args) {
initialize();
}
}