-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
359 lines (349 loc) · 14.5 KB
/
Main.java
File metadata and controls
359 lines (349 loc) · 14.5 KB
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
public class Main {
public static void main(String[] args) {
// initial data
int[] supply = {160, 140, 170};
int[][] costs = {
{7, 8, 1, 2},
{4, 5, 9, 8},
{9, 2, 3, 6},
};
int[] demand = {120, 50, 190, 110};
//check the applickability (if all values are > 0)
for (int i = 0; i < supply.length; i++) {
if (supply[i] < 0) {
System.out.println("The method is not applicable!");
System.exit(0);
}
for (int j = 0; j < demand.length; j++) {
if (costs[i][j] < 0) {
System.out.println("The method is not applicable!");
System.exit(0);
}
}
}
for (int i = 0; i < demand.length; i++) {
if (demand[i] < 0) {
System.out.println("The method is not applicable!");
System.exit(0);
}
}
//check the balancing of supply and demand
int sumSupply = 0;
for (int i = 0; i < supply.length; i++) {
sumSupply += supply[i];
}
int sumDemand = 0;
for (int i = 0; i < demand.length; i++) {
sumDemand += demand[i];
}
if (sumSupply != sumDemand) {
System.out.println("The problem is not balanced!");
System.exit(0);
}
//creating an optimal matrix for solving the problem
int[][] matrix = createMatrix(supply, costs, demand);
//printing the table
int counter = 1;
System.out.printf("+--------+--------+--------+--------+--------+--------+ \n");
System.out.printf("|%7s |%7s |%7s |%7s |%7s |%7s | \n", " ", "D1", "D2", "D3", "D4", "Supply");
System.out.printf("+--------+--------+--------+--------+--------+--------+ \n");
for (int i = 0; i < supply.length; i++) {
System.out.printf("|%7s ", "S" + counter++);
for (int j = 0; j < demand.length + 1; j++) {
System.out.printf("|%7d ", matrix[i][j]);
}
System.out.printf("| \n");
System.out.printf("+--------+--------+--------+--------+--------+--------+ \n");
}
System.out.printf("|%7s ", "Demand");
for (int i = 0; i < demand.length; i++) {
System.out.printf("|%7d ", matrix[supply.length][i]);
}
System.out.printf("|%7s |\n", "");
System.out.printf("+--------+--------+--------+--------+--------+--------+ \n");
//North-West corner method
int [][] matrixNW = createMatrix(supply, costs, demand);
int[][] feasibleSols = new int[supply.length][demand.length];
int answer = 0;
int curSupply = 0;
int curDemand = 0;
int minVariant = 0;
while (true) {
//select the minimum variant of supply/demand
if(matrixNW[curSupply][demand.length] > matrixNW[supply.length][curDemand]) {
minVariant = matrixNW[supply.length][curDemand];
//update supply/demand
matrixNW[curSupply][demand.length] -= minVariant;
matrixNW[supply.length][curDemand] -= minVariant;
//update feasible solutions and answer
feasibleSols[curSupply][curDemand] = minVariant;
answer += minVariant * costs[curSupply][curDemand];
//move right
curDemand++;
} else {
minVariant = matrixNW[curSupply][demand.length];
//update supply/demand
matrixNW[curSupply][demand.length] -= minVariant;
matrixNW[supply.length][curDemand] -= minVariant;
//update feasible solutions and answer
feasibleSols[curSupply][curDemand] = minVariant;
answer += minVariant * costs[curSupply][curDemand];
//move down
curSupply++;
}
//check that we can finish
if (curSupply == supply.length || curDemand == demand.length) {
break;
}
}
//printing the answer
System.out.println("\n- - - North-West corner method - - -");
printAnswer(feasibleSols, answer);
//Vogel's approximation method
matrix = createMatrix(supply, costs, demand);
feasibleSols = new int[supply.length][demand.length];
answer = 0;
int sum;
int maxApprox;
int approxX;
int approxY;
int minCost1;
int minCostX;
int minCostY;
int minCost2;
while (true) {
//check that we can finish
sum = 0;
for (int i = 0; i < supply.length; i++) {
sum += matrix[i][demand.length];
}
if (sum == 0) {
break;
}
//find approximations for each row
for (int i = 0; i < supply.length; i++) {
minCost1 = matrix[i][0];
minCost2 = 10000;
for (int j = 0; j < demand.length; j++) {
if (matrix[i][j] < minCost1) {
minCost2 = minCost1;
minCost1 = matrix[i][j];
}
if (matrix[i][j] < minCost2 && matrix[i][j] != minCost1) {
minCost2 = matrix[i][j];
}
}
matrix[i][demand.length + 1] = minCost2 - minCost1;
}
//find approximations for each column
for (int i = 0; i < demand.length; i++) {
minCost1 = matrix[0][i];
minCost2 = 10000;
for (int j = 0; j < supply.length; j++) {
if (matrix[j][i] < minCost1) {
minCost2 = minCost1;
minCost1 = matrix[j][i];
}
if (matrix[j][i] < minCost2 && matrix[j][i] != minCost1) {
minCost2 = matrix[j][i];
}
}
matrix[supply.length + 1][i] = minCost2 - minCost1;
}
//find the maximal approximation
maxApprox = matrix[supply.length + 1][0];
approxX = supply.length + 1;
approxY = 0;
//firstly check the columns
for (int i = 0; i < demand.length; i++) {
if (matrix[supply.length + 1][i] > maxApprox) {
maxApprox = matrix[supply.length + 1][i];
approxX = supply.length + 1;
approxY = i;
}
}
//then check the rows
for (int i = 0; i < supply.length; i++) {
if (matrix[i][demand.length + 1] > maxApprox) {
maxApprox = matrix[i][demand.length + 1];
approxX = i;
approxY = demand.length + 1;
}
}
//find the min value in the maxApprox column/row
if (approxX == supply.length + 1) {
//means that we should find the min value in the column
minCost1 = matrix[0][approxY];
minCostX = 0;
minCostY = approxY;
for (int i = 0; i < supply.length; i++) {
if (matrix[i][approxY] < minCost1) {
minCost1 = matrix[i][approxY];
minCostX = i;
minCostY = approxY;
}
}
} else {
//means that we should find the min value in the row
minCost1 = matrix[approxX][0];
minCostX = approxX;
minCostY = 0;
for (int i = 0; i < demand.length; i++) {
if (matrix[approxX][i] < minCost1) {
minCost1 = matrix[approxX][i];
minCostX = approxX;
minCostY = i;
}
}
}
//finding the minimal supply/demand
if (matrix[minCostX][demand.length] < matrix[supply.length][minCostY]) {
minVariant = matrix[minCostX][demand.length];
//erase the row
for (int i = 0; i < demand.length; i++) {
matrix[minCostX][i] = 10000;
}
//update supplies/demands
matrix[minCostX][demand.length] -= minVariant;
matrix[supply.length][minCostY] -= minVariant;
//update feasible solutions and answer
feasibleSols[minCostX][minCostY] = minVariant;
answer += minVariant * costs[minCostX][minCostY];
} else {
minVariant = matrix[supply.length][minCostY];
//erase the column
for (int i = 0; i < supply.length; i++) {
matrix[i][minCostY] = 10000;
}
//update supplies/demands
matrix[minCostX][demand.length] -= minVariant;
matrix[supply.length][minCostY] -= minVariant;
//update feasible solutions and answer
feasibleSols[minCostX][minCostY] = minVariant;
answer += minVariant * costs[minCostX][minCostY];
}
}
//printing the answer
System.out.println("- - - Vogel's approximation method - - -");
printAnswer(feasibleSols, answer);
//Russell’s approximation method
matrix = createMatrix(supply, costs, demand);
feasibleSols = new int[supply.length][demand.length];
answer = 0;
int approxMatrix[][] = new int[supply.length][demand.length];
boolean[][] isNotErasedMatrix = new boolean[supply.length][demand.length];
int minApprox;
//create isNotErasedMatrix (for checking that cell is not erased)
for (int i = 0; i < supply.length; i++) {
for (int j = 0; j < demand.length; j++) {
isNotErasedMatrix[i][j] = true;
}
}
while (true) {
//check that we can finish
sum = 0;
for (int i = 0; i < supply.length; i++) {
sum += matrix[i][demand.length];
}
if (sum == 0) {
break;
}
//find max cost for each column
for (int i = 0; i < demand.length; i++) {
maxApprox = matrix[0][i];
for (int j = 0; j < supply.length; j++) {
if (matrix[j][i] > maxApprox) {
maxApprox = matrix[j][i];
}
}
matrix[supply.length + 1][i] = maxApprox;
}
//find max cost for each row
for (int i = 0; i < supply.length; i++) {
maxApprox = matrix[i][0];
for (int j = 0; j < demand.length; j++) {
if (matrix[i][j] > maxApprox) {
maxApprox = matrix[i][j];
}
}
matrix[i][demand.length + 1] = maxApprox;
}
//filling the approximation matrix
for (int i = 0; i < supply.length; i++) {
for (int j = 0; j < demand.length; j++) {
approxMatrix[i][j] = matrix[i][j] - matrix[i][demand.length + 1] - matrix[supply.length + 1][j];
}
}
//finding the minimal approximation
minApprox = approxMatrix[0][0];
approxX = 0;
approxY = 0;
for (int i = 0; i < supply.length; i++) {
for (int j = 0; j < demand.length; j++) {
if (approxMatrix[i][j] < minApprox && isNotErasedMatrix[i][j]) {
minApprox = approxMatrix[i][j];
approxX = i;
approxY = j;
}
}
}
//finding the minimal supply/demand
if (matrix[approxX][demand.length] < matrix[supply.length][approxY]) {
minVariant = matrix[approxX][demand.length];
//erasing the row
for (int i = 0; i < demand.length; i++) {
matrix[approxX][i] = 0;
isNotErasedMatrix[approxX][i] = false;
}
//update supplies/demands
matrix[approxX][demand.length] -= minVariant;
matrix[supply.length][approxY] -= minVariant;
//update feasible solutions and answer
feasibleSols[approxX][approxY] = minVariant;
answer += minVariant * costs[approxX][approxY];
} else {
minVariant = matrix[supply.length][approxY];
//erasing the column
for (int i = 0; i < supply.length; i++) {
matrix[i][approxY] = 0;
isNotErasedMatrix[i][approxY] = false;
}
//update supplies/demands
matrix[approxX][demand.length] -= minVariant;
matrix[supply.length][approxY] -= minVariant;
//update feasible solutions and answer
feasibleSols[approxX][approxY] = minVariant;
answer += minVariant * costs[approxX][approxY];
}
}
//printing the answer
System.out.println("- - - Russell’s approximation method - - -");
printAnswer(feasibleSols, answer);
}
static int[][] createMatrix(int[] supply, int[][] costs, int[] demand) {
int[][] matrix = new int[supply.length + 2][demand.length + 2];
for (int i = 0; i < supply.length; i++) {
for (int j = 0; j < demand.length; j++) {
matrix[i][j] = costs[i][j];
}
matrix[i][demand.length] = supply[i];
matrix[i][demand.length + 1] = 0;
}
for (int i = 0; i < demand.length; i++) {
matrix[supply.length][i] = demand[i];
matrix[supply.length + 1][i] = 0;
}
return matrix;
}
static void printAnswer(int[][] feasibleSols, int answer) {
int counter = 1;
for (int i = 0; i < feasibleSols.length; i++) {
System.out.print("S" + counter++ + ": [");
for (int j = 0; j < feasibleSols[i].length - 1; j++) {
System.out.print(feasibleSols[i][j] + ", ");
}
System.out.print(feasibleSols[i][feasibleSols[i].length - 1] + "]\n");
}
System.out.println("Answer: " + answer + "\n");
}
}