-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackend.java
More file actions
422 lines (385 loc) · 13 KB
/
Copy pathBackend.java
File metadata and controls
422 lines (385 loc) · 13 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
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
public class Backend {
// declaring all the global variables
static int m, n, m1, n1;
static int p1[][], p2[][];
static int nashP1[][], nashP2[][];
static int diff1[][];
static int p1oddments[];
static int diff2[][];
static int p2oddments[];
static int hcfforpl1[];
static int hcfforpl2[];
static int oddmenttotal1 = 0;
static int oddmenttotal2 = 0;
static String resultsP1[] = new String[3];
static String resultsP2[] = new String[3];
static String disply1[] = new String[3];
static String disply2[] = new String[3];
// constructor that takes the input and initializes all the data structures
// appropriately
Backend() {
// Scanner ob = new Scanner(System.in);
// System.out.println("Enter the order of the payoff matrix");
// m = ob.nextInt();
// n = ob.nextInt();
m1 = m;
n1 = n;
p1 = new int[m][n];
p2 = new int[m][n];
// diff1 = new int[m][n - 1];
// diff2 = new int[m - 1][n];
p1oddments = new int[m];
p2oddments = new int[n];
hcfforpl1 = new int[m];
hcfforpl2 = new int[n];
resultsP1 = new String[m];
resultsP2 = new String[n];
int i, j;
// System.out.println("Enter the payoff matrix for player 1: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (i == 0) {
p1[i][j] = j;
continue;
}
if (j == 0) {
p1[i][j] = i;
continue;
}
// p1[i][j] = ob.nextInt();
}
}
// System.out.println("Enter the payoff matrix for player 2: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (i == 0) {
p2[i][j] = j;
continue;
}
if (j == 0) {
p2[i][j] = i;
continue;
}
// p2[i][j] = ob.nextInt();
}
}
}
// function to check for any cases of pure dominance in the payoff matrices
public void checkDominance() {
// checking Dominance for Player 1
int i, j, k;
int flag;
for (i = 1; i < m; i++) {
for (j = 1; j < m; j++) {
if (i == j) {
continue;
}
flag = 0;
for (k = 1; k < n; k++) {
if (p1[i][k] <= p1[j][k]) {
flag *= 1;
} else {
flag += 1;
}
}
if (flag == 0) {
if (m == 3 && n == 3) {
return;
}
ejectStrat(i, 'r');
i = 1;
j = 1;
}
}
}
if (m == 3 && n == 3) {
return;
}
// checking Dominance for Player 2
boolean changes = false;
for (i = 1; i < n; i++) {
for (j = 1; j < n; j++) {
flag = 0;
if (i == j) {
continue;
}
for (k = 1; k < m; k++) {
if (p2[k][i] <= p2[k][j]) {
flag *= 1;
} else {
flag += 1;
}
}
if (flag == 0) {
if (m == 3 && n == 3) {
return;
}
ejectStrat(j, 'c');
i = 1;
j = 1;
changes = true;
}
}
}
if (changes == true) {
checkDominance();
}
}
// function to discard a dominated strategy form the payoff matrices
public void ejectStrat(int i, char ch) {
if (ch == 'r') {
if (i == m) {
m--;
return;
}
for (; i < m - 1; i++) {
for (int j = 0; j < n; j++) {
p1[i][j] = p1[i + 1][j];
p2[i][j] = p2[i + 1][j];
}
}
m -= 1;
}
if (ch == 'c') {
if (i == n) {
n--;
return;
}
for (; i < n - 1; i++) {
for (int j = 0; j < m; j++) {
p1[j][i] = p1[j][i + 1];
p2[j][i] = p2[j][i + 1];
}
}
n -= 1;
}
}
// function that calculates the nash eqm of the payoff matrices after they have
// been reduced to order 2
public void calcNashEqmO2() {
nashP1 = new int[2][2];
nashP2 = new int[2][2];
nashP1[0][0] = Math.abs(p2[2][2] - p2[2][1]);
nashP1[1][0] = Math.abs(p2[1][1] - p2[2][1] - p2[1][2] + p2[2][2]);
nashP1[0][1] = nashP1[1][0] - nashP1[0][0];
nashP1[1][1] = nashP1[1][0];
nashP2[0][0] = Math.abs(p1[2][2] - p1[1][2]);
nashP2[1][0] = Math.abs(p1[1][1] - p1[1][2] - p1[2][1] + p1[2][2]);
nashP2[0][1] = nashP2[1][0] - nashP2[0][0];
nashP2[1][1] = nashP2[1][0];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
nashP1[i][j] = Math.abs(nashP1[i][j]);
nashP2[i][j] = Math.abs(nashP2[i][j]);
}
}
reduceFractions();
}
// function that calculates the nash eqm of the payoff matrices after they have
// been reduced to order 3
public void calcNashEqmO3() {
diff1 = new int[m][n - 1];
diff2 = new int[m - 1][n];
// SOlVING FOR PLAYER1
for (int i = 1; i < m; i++) {
for (int j = 1; j < n - 1; j++) {
diff1[i][j] = p2[i][j] - p2[i][j + 1];
}
}
p1oddments[0] = detoddments(diff1[2][1], diff1[2][2], diff1[3][1], diff1[3][2]);
p1oddments[1] = detoddments(diff1[1][1], diff1[1][2], diff1[3][1], diff1[3][2]);
p1oddments[2] = detoddments(diff1[1][1], diff1[1][2], diff1[2][1], diff1[2][2]);
for (int i = 0; i < m - 1; i++) {
oddmenttotal1 += p1oddments[i];
}
for (int i = 0; i < m - 1; i++) {
hcfforpl1[i] = hcf(p1oddments[i], oddmenttotal1);
}
// SOLVING FOR PLAYER 2
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n; j++) {
diff2[i][j] = p2[i][j] - p2[i + 1][j];
}
}
p2oddments[0] = detoddments(diff2[1][2], diff2[1][3], diff2[2][2], diff2[2][3]);
p2oddments[1] = detoddments(diff2[1][1], diff2[1][3], diff2[2][1], diff2[2][3]);
p2oddments[2] = detoddments(diff2[1][1], diff2[1][2], diff2[2][1], diff2[2][2]);
for (int i = 0; i < n - 1; i++) {
oddmenttotal2 += p2oddments[i];
}
for (int i = 0; i < n - 1; i++) {
hcfforpl2[i] = hcf(p2oddments[i], oddmenttotal2);
}
}
// function that simplifies the probabilities to their smallest fraction
public void reduceFractions() {
int hcf;
hcf = hcf(nashP1[0][0], nashP1[1][0]);
while (hcf != 1) {
nashP1[0][0] /= hcf;
nashP1[1][0] /= hcf;
hcf = hcf(nashP1[0][0], nashP1[1][0]);
}
hcf = hcf(nashP1[0][1], nashP1[1][1]);
while (hcf != 1) {
nashP1[0][1] /= hcf;
nashP1[1][1] /= hcf;
hcf = hcf(nashP1[0][1], nashP1[1][1]);
}
hcf = hcf(nashP2[0][0], nashP2[1][0]);
while (hcf != 1) {
nashP2[0][0] /= hcf;
nashP2[1][0] /= hcf;
hcf = hcf(nashP2[0][0], nashP2[1][0]);
}
hcf = hcf(nashP2[0][1], nashP2[1][1]);
while (hcf != 1) {
nashP2[0][1] /= hcf;
nashP2[1][1] /= hcf;
hcf = hcf(nashP2[0][1], nashP2[1][1]);
}
}
// function that calculates the hcf of two numbers
public int hcf(int a, int b) {
if (a == 0) {
return Math.abs(b);
}
if (b == 0) {
return Math.abs(a);
}
int i, hcf = 1;
for (i = 1; i < a || i < b; i++) {
if ((a % i == 0) && (b % i == 0)) {
hcf = i;
}
}
return hcf;
}
// function to find the determinant of a matrix of order 2
public int detoddments(int A, int B, int C, int D) {
return Math.abs((A * D) - (B * C));
}
// function that displays the nash eqm of a payoff matrix of order 2
public static void displayNashEqmO2() {
int count0 = 0;
int res = 0;
float flag = 0;
if (m1 == 4) {
for (int i = 1, j = 0; i < m1; i++) {
if (p1[i][0] != i) {
disply1[res++] = "0";
count0++;
i++;
}
if (j < 2) {
flag += (float)nashP1[0][j] / nashP1[1][j];
disply1[res++] = nashP1[0][j] + "/" + nashP1[1][j];
j++;
}
}
for (int i = (2 + count0); i < m1 - 1; i++) {
disply1[res++] = "0";
}
if( flag != 1.0)
throw new ArithmeticException();
res = 0;
flag = 0;
for (int i = 1, j = 0; i < n1; i++) {
if (p1[0][i] != i) {
disply2[res++] = "0";
count0++;
i++;
}
if (j < 2) {
flag += (float)nashP2[0][j] / nashP2[1][j];
disply2[res++] = nashP2[0][j] + "/" + nashP2[1][j];
j++;
}
}
for (int i = (2 + count0); i < n1 - 1; i++) {
disply2[res++] = "0";
}
if( flag != 1.0)
throw new ArithmeticException();
return;
}
res = 0;
count0 = 0;
flag = 0;
for (int i = 1, j = 0; i < m1; i++) {
if (p1[i][0] != i) {
resultsP1[res++] = "0";
count0++;
i++;
}
if (j < 2) {
flag += (float)nashP1[0][j] / nashP1[1][j];
resultsP1[res++] = nashP1[0][j] + "/" + nashP1[1][j];
j++;
}
}
for (int i = (2 + count0); i < m1 - 1; i++) {
resultsP1[res++] = "0";
}
if( flag != 1.0)
throw new ArithmeticException();
res = 0;
flag = 0;
for (int i = 1, j = 0; i < n1; i++) {
if (p1[0][i] != i) {
resultsP2[res++] = "0";
count0++;
i++;
}
if (j < 2) {
flag += (float)nashP2[0][j] / nashP2[1][j];
resultsP2[res++] = nashP2[0][j] + "/" + nashP2[1][j];
j++;
}
}
for (int i = (2 + count0); i < n1 - 1; i++) {
resultsP2[res++] = "0";
}
if( flag != 1.0)
throw new ArithmeticException();
}
// function that displays the nash eqm of a payoff matrix of order 3
public static void displayNashEqmO3() {
float flag = 0;
for (int i = 0; i < m - 1; i++) {
flag += ((float)(p1oddments[i] / hcfforpl1[i]) )/ ((float) (oddmenttotal1 / hcfforpl1[i]));
if(oddmenttotal1 / hcfforpl1[i] == 0 || p1oddments[i] / hcfforpl1[i] == 0)
{
disply1[i] = "0";
}
else
{
disply1[i] = p1oddments[i] / hcfforpl1[i] + "/" + oddmenttotal1 / hcfforpl1[i];
}
if(disply1[i] == null)
{
disply1[i] = "0";
}
}
if( flag != 1.0)
throw new ArithmeticException();
flag = 0;
for (int i = 0; i < m - 1; i++) {
flag += ((float)(p2oddments[i] / hcfforpl2[i]) )/ ((float) (oddmenttotal2 / hcfforpl2[i]));
if(oddmenttotal2 / hcfforpl2[i] == 0 || p2oddments[i] / hcfforpl2[i] == 0)
{
disply2[i] = "0";
}
else
{
disply2[i] = p2oddments[i] / hcfforpl2[i] + "/" + oddmenttotal2 / hcfforpl2[i];
}
if(disply1[i] == null)
{
disply2[i] = "0";
}
}
if( flag != 1.0)
throw new ArithmeticException();
}
}