-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerationAndStatistics
More file actions
781 lines (691 loc) · 34.4 KB
/
generationAndStatistics
File metadata and controls
781 lines (691 loc) · 34.4 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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
package org.example;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import javax.swing.*;
import java.sql.SQLOutput;
import java.util.*;
// packages for the graph
public class Main {
public static void main(String[] args) {
// PARAMETERS that can be changed
int populationSize = 500;
int badLimit = 5;
int fitnessToChangeTheNumberOfMutations = 5;
int numberOfMutationsWithGoodFitness = 1;
Integer maxValue = 400;
Scanner scanner = new Scanner(System.in);
// generating tests and running them
int numberOfValues = 21;
int[] averageFitness = new int[21];
int[] bestFitness = new int[21];
int[] iterationGlobal = new int[21];
// NUMBER OF TESTS FOR EACH COMPLEXITY
int tests = 10;
int globalCounter = 0;
for (int e = 20; e <= 40; e += 1) {
Integer averageFit = 0;
Integer globalBest = -1000000;
Integer numOfIterations = 0;
for (int q = 0; q < tests; q += 1) {
boolean test = true;
while (test) {
// generate the test
// basic solution that will be randomly swapped
String[][] basicSudoku = {
{"1", "8", "5", "4", "3", "6", "2", "9", "7"},
{"4", "3", "7", "5", "9", "2", "8", "1", "6"},
{"6", "9", "2", "8", "1", "7", "3", "4", "5"},
{"3", "7", "6", "2", "8", "9", "4", "5", "1"},
{"2", "1", "4", "3", "7", "5", "9", "6", "8"},
{"9", "5", "8", "6", "4", "1", "7", "3", "2"},
{"8", "6", "3", "1", "2", "4", "5", "7", "9"},
{"7", "2", "1", "9", "5", "3", "6", "8", "4"},
{"5", "4", "9", "7", "6", "8", "1", "2", "3"}
};
Integer globalIterations = 0;
Random rand = new Random();
String[][] sudoku = new String[9][9];
for (int l = 0; l < 9; l ++) {
for (int d = 0; d < 9; d++) {
sudoku[l][d] = basicSudoku[l][d];
}
}
int[][] dummyFixed = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
dummyFixed[i][j] = 0;
}
}
for (int i = 0; i < 30; i++) {
boolean flag = true;
int whatChanges = rand.nextInt(5);
// if we just transpose the matrix
if (whatChanges == 0) {
String[][] testing = new String[9][9];
for (int l = 0; l < 9; l++) {
for (int d = 0; d < 9; d++) {
testing[l][d] = sudoku[d][l];
}
}
}
// if we are exchanging rows
if (whatChanges == 1) {
int whatPart = rand.nextInt(2);
int row1 = 0;
int row2 = 0;
if (whatPart == 0) {
row1 = rand.nextInt(3);
row2 = rand.nextInt(3);
while (row2 == row1) {
row2 = rand.nextInt(3);
}
}
if (whatPart == 1) {
row1 = rand.nextInt(3) + 3;
row2 = rand.nextInt(3) + 3;
while (row2 == row1) {
row2 = rand.nextInt(3) + 3;
}
}
if (whatPart == 2) {
row1 = rand.nextInt(3) + 6;
row2 = rand.nextInt(3) + 6;
while (row2 == row1) {
row2 = rand.nextInt(3) + 6;
}
}
String[] change = new String[9];
for (int k = 0; k < 9; k++) {
change[k] = sudoku[row1][k];
}
for (int k = 0; k < 9; k++) {
sudoku[row1][k] = sudoku[row2][k];
}
for (int k = 0; k < 9; k++) {
sudoku[row2][k] = change[k];
}
}
// if we are exchanging columns
if (whatChanges == 2) {
int whatPart = rand.nextInt(2);
int column1 = 0;
int column2 = 0;
if (whatPart == 0) {
column1 = rand.nextInt(3);
column2 = rand.nextInt(3);
while (column2 == column1) {
column2 = rand.nextInt(3);
}
}
if (whatPart == 1) {
column1 = rand.nextInt(3) + 3;
column2 = rand.nextInt(3) + 3;
while (column2 == column1) {
column2 = rand.nextInt(3) + 3;
}
}
if (whatPart == 2) {
column1 = rand.nextInt(3) + 6;
column2 = rand.nextInt(3) + 6;
while (column2 == column1) {
column2 = rand.nextInt(3) + 6;
}
}
String[] change = new String[9];
for (int k = 0; k < 9; k++) {
change[k] = sudoku[k][column1];
}
for (int k = 0; k < 9; k++) {
sudoku[k][column1] = sudoku[k][column2];
}
for (int k = 0; k < 9; k++) {
sudoku[k][column2] = change[k];
}
}
// if we are exchanging the row segments
if (whatChanges == 3) {
int firstSegment = rand.nextInt(2);
int secondSegment = rand.nextInt(2);
while (secondSegment == firstSegment) {
secondSegment = rand.nextInt(2);
}
String[][] change = new String[3][9];
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 9; l++) {
change[k][l] = sudoku[(firstSegment*3+k)][l];
}
}
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 9; l++) {
sudoku[(firstSegment*3+k)][l] = sudoku[(secondSegment*3+k)][l];
}
}
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 9; l++) {
sudoku[(secondSegment*3+k)][l] = change[k][l];
}
}
}
// if we are exchanging the column segments
if (whatChanges == 4) {
int firstSegment = rand.nextInt(2);
int secondSegment = rand.nextInt(2);
while (secondSegment == firstSegment) {
secondSegment = rand.nextInt(2);
}
String[][] change = new String[9][3];
for (int k = 0; k < 9; k++) {
for (int l = 0; l < 3; l++) {
change[k][l] = sudoku[k][(firstSegment*3+l)];
}
}
for (int k = 0; k < 9; k++) {
for (int l = 0; l < 3; l++) {
sudoku[k][(firstSegment*3+l)] = sudoku[k][(secondSegment*3+l)];
}
}
for (int k = 0; k < 9; k++) {
for (int l = 0; l < 3; l++) {
sudoku[k][(secondSegment*3+l)] = change[k][l];
}
}
}
}
for (int i = 0; i < (81 - e); i++) {
int index1 = rand.nextInt(9);
int index2 = rand.nextInt(9);
sudoku[index1][index2] = "-";
}
int[][] fixed = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (!sudoku[i][j].equals("-")) {
fixed[i][j] = 1;
}
else {
fixed[i][j] = 0;
}
}
}
// build the initial population
// it is build with the right subgrids and random rows/columns
String[][][] population = new String[populationSize][9][9];
for (int i = 0; i < populationSize; i++) {
String[][] matrix = new String[9][9];
for (int j = 0; j < 3; j++) {
for (int t = 0; t < 3; t++) {
String nums = "";
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
if (fixed[k][m] == 1) {
// if the number is given it cannot be changed
matrix[k][m] = sudoku[k][m];
nums = nums + sudoku[k][m];
}
}
}
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
if (!(fixed[k][m] == 1)) {
// the empty cells are filled with generated values
matrix[k][m] = generateNum(nums);
nums = nums + matrix[k][m];
}
}
}
}
}
population[i] = matrix;
}
// variables that will be used inside the generation loop
int epsilon = 1;
int badIterations = 0;
int bestResult = 10000;
int difference = 10000000;
int iterations = 0;
// a parameter that states how many successful iterations can there be
// after this limit the algorithm should restart with a new population
int maxNumOfIterations = 1000000000;
boolean generation = true;
boolean testInternal = true;
// the main loop for the algorithm
while (generation && testInternal) {
numOfIterations += 1;
globalIterations += 1;
// System.out.println(globalIterations);
if (globalIterations >= maxValue) {
testInternal = false;
}
iterations += 1;
int oldResult = bestResult;
// array that will contain indexes for each fitness
// it will help with computations later
ArrayList<ArrayList<Integer>> forEachFitness = new ArrayList<>();
// calculating fitness function for current population
// and filling in the results
int[] results = new int[populationSize];
int indexOfAnswer = -1;
int currentMinResult = 1000000000;
int currentWorstResult = 0;
for (int i = 0; i < populationSize; i++) {
results[i] = fitnessFunction(population[i], fixed);
while (forEachFitness.size() - 1 < results[i]) {
ArrayList<Integer> indexes = new ArrayList<>();
forEachFitness.add(indexes);
}
forEachFitness.get(results[i]).add(i);
// currentWorstResult shows the worst result in the current population
// it will help to choose the fittest parents later
if (results[i] > currentWorstResult) {
currentWorstResult = results[i];
}
// calculating the best result in the population
if (results[i] < currentMinResult) {
currentMinResult = results[i];
}
// updating the global best result
if (results[i] < bestResult) {
bestResult = results[i];
indexOfAnswer = i;
}
}
// calculating the difference between the last best result and a new one
difference = Math.abs(oldResult - bestResult);
// case when the sudoku has been solved
if (bestResult == 0) {
for (int i = 0; i < populationSize; i++) {
averageFit += results[i];
}
if (bestResult > globalBest) {
globalBest = bestResult;
}
generation = false;
test = false;
System.out.println();
System.out.println("Test " + (q + 1) + " for " + e + " given values");
System.out.println("Sudoku:");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(sudoku[i][j] + " ");
}
System.out.print("\n");
}
System.out.println();
System.out.println("Answer:");
for (int k = 0; k < 9; k++) {
for (int j = 0; j < 9; j++) {
if (j == 8) {
System.out.print(population[indexOfAnswer][k][j]);
} else {
System.out.print(population[indexOfAnswer][k][j] + " ");
}
}
if (k != 8) {
System.out.print("\n");
}
}
System.out.println();
}
boolean newPopulation = false;
// if the algorithm did not finish
if (bestResult != 0) {
// start over if the algorithm has reached the local optimum or made too many iterations
if (difference < epsilon || oldResult < bestResult) {
badIterations += 1;
if (badIterations >= badLimit || iterations >= maxNumOfIterations) {
newPopulation = true;
iterations = 1;
badIterations = 0;
// start again with new population
bestResult = 10000;
difference = 10000000;
// build the initial population again
population = new String[populationSize][9][9];
for (int i = 0; i < populationSize; i++) {
String[][] matrix = new String[9][9];
for (int j = 0; j < 3; j++) {
for (int t = 0; t < 3; t++) {
String nums = "";
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
if (fixed[k][m] == 1) {
matrix[k][m] = sudoku[k][m];
nums = nums + sudoku[k][m];
}
}
}
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
if (!(fixed[k][m] == 1)) {
matrix[k][m] = generateNum(nums);
nums = nums + matrix[k][m];
}
}
}
}
}
population[i] = matrix;
}
}
} else {
badIterations -= 1;
}
// we continue with the next step if the population has not been restarted
if (!newPopulation) {
// choose 60% of the population as parents based on their fitness
double numberOfParents = ((double) populationSize) / 100 * 60;
int numPar = (int) numberOfParents;
int[] parents = new int[numPar];
boolean f = true;
int count = 0;
// the needed number of parents will be chosen for crossover
for (int i = bestResult; i <= currentWorstResult; i++) {
if (f) {
if (!forEachFitness.get(i).isEmpty()) {
for (Integer num : forEachFitness.get(i)) {
parents[count] = num;
count += 1;
if (count == numPar) {
f = false;
i = currentWorstResult + 1;
break;
}
}
}
}
}
// array for the new population of children
String[][][] childrenPopulationBefMut = new String[populationSize][9][9];
// apply crossover until the number of children reaches the size of the population
int counter = 0;
boolean flag = true;
for (int i = 0; i < numPar; i++) {
if (flag) {
for (int j = i + 1; j < numPar; j++) {
if (counter < populationSize) {
int index1 = parents[i];
int index2 = parents[j];
childrenPopulationBefMut[counter] = crossover(population[index1], population[index2], fixed);
} else {
flag = false;
break;
}
counter += 1;
}
} else {
break;
}
}
String[][][] childrenPopulation = new String[populationSize][9][9];
// now we can mutate the children to keep the population diverse
for (int i = 0; i < populationSize; i++) {
childrenPopulation[i] = mutate(childrenPopulationBefMut[i], fixed, bestResult, numberOfMutationsWithGoodFitness, fitnessToChangeTheNumberOfMutations);
}
// work with the new population in the next iteration
population = childrenPopulation;
}
}
}
}
}
averageFitness[globalCounter] = averageFit/(tests*populationSize);
iterationGlobal[globalCounter] = numOfIterations/10;
bestFitness[globalCounter] = globalBest;
globalCounter += 1;
}
System.out.println();
System.out.println("Average Fitness Function result and the Average Number of Iterations");
System.out.println();
for (int i = 0; i < 21; i++) {
System.out.println("With given " + (20 + i) + " values");
System.out.println("Fitness: " + averageFitness[i] + ", iterations: " + iterationGlobal[i]);
System.out.println();
}
System.out.println("--------------------------");
System.out.println("Best Fitness (fitness 0 means that the sudoku has no conflicts)");
System.out.println();
for (int i = 0; i < 21; i++) {
System.out.println("With given " + (20 + i) + " values");
System.out.println(bestFitness[i] + " ");
System.out.println();
}
System.out.println();
XYChart chart1 = new XYChartBuilder()
.title("AI assignment2 statistics. Veronika Levasheva")
.xAxisTitle("# of givens")
.yAxisTitle("Average fitness function results")
.build();
chart1.addSeries("Fitness function result average", new int[]{20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, averageFitness);
JPanel chartPanel1 = new XChartPanel<>(chart1);
JFrame frame1 = new JFrame("Statistic. Average fitness function results");
frame1.add(chartPanel1);
frame1.setSize(800, 600);
frame1.show();
XYChart chart2 = new XYChartBuilder()
.title("AI assignment2 statistics. Veronika Levasheva")
.xAxisTitle("# of givens")
.yAxisTitle("Average number of iterations")
.build();
chart2.addSeries("Iterations", new int[]{20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, iterationGlobal);
JPanel chartPanel2 = new XChartPanel<>(chart2);
JFrame frame2 = new JFrame("Statistic. Average number of iterations");
frame2.add(chartPanel2);
frame2.setSize(800, 600);
frame2.show();
XYChart chart3 = new XYChartBuilder()
.title("AI assignment2 statistics. Veronika Levasheva")
.xAxisTitle("# of givens")
.yAxisTitle("Best fitness function result")
.build();
chart3.addSeries("Best result", new int[]{20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, bestFitness);
JPanel chartPanel3 = new XChartPanel<>(chart3);
JFrame frame3 = new JFrame("Statistic. Best fitness function result");
frame3.add(chartPanel3);
frame3.setSize(800, 600);
frame3.show();
}
// function to randomly fill in the cell
public static String generateNum(String usedNumbs) {
String nums = "123456789";
Random random = new Random();
boolean flag = true;
while (flag) {
String newNum = String.valueOf(nums.charAt((int) (random.nextFloat() * nums.length())));
// check that the number is not in the subgrid yet
if (!usedNumbs.contains(newNum)) {
return newNum;
}
}
return null;
}
// function to access the fitness of the individual solution
public static int fitnessFunction(String[][] matrix, int[][] fixed) {
// an array with all fixed numbers for rows
String[] fixedForRows = new String[9];
for (int i = 0; i < 9; i++) {
String fix = "";
for (int j = 0; j < 9; j++) {
if (fixed[i][j] == 1) {
fix = fix + matrix[i][j];
}
}
fixedForRows[i] = fix;
}
// an array with all fixed numbers for columns
String[] fixedForColumns = new String[9];
for (int j = 0; j < 9; j++) {
String fix = "";
for (int i = 0; i < 9; i++) {
if (fixed[i][j] == 1) {
fix = fix + matrix[i][j];
}
}
fixedForColumns[j] = fix;
}
// count the number of conflicts in the rows
int rowConflicts = 0;
for (int i = 0; i < 9; i++) {
Set<String> usedNums = new HashSet<>();
for (int k = 0; k < fixedForRows[i].length(); k++) {
usedNums.add(String.valueOf(fixedForRows[i].charAt(k)));
}
for (int j = 0; j < 9; j++) {
if (!usedNums.isEmpty() && usedNums.contains(matrix[i][j]) && fixed[i][j] != 1) {
// add two if there is a conflict with the fixed cell
// add one if the cell repeats an ordinary cell
if (fixedForRows[i].contains(matrix[i][j])) {
rowConflicts += 2;
} else {
rowConflicts += 1;
}
}
if (fixed[i][j] != 1) {
usedNums.add(matrix[i][j]);
}
}
}
// count the number of conflicts in the columns
int columnConflicts = 0;
for (int j = 0; j < 9; j++) {
Set<String> usedNums = new HashSet<>();
for (int k = 0; k < fixedForColumns[j].length(); k++) {
usedNums.add(String.valueOf(fixedForColumns[j].charAt(k)));
}
for (int i = 0; i < 9; i++) {
if (!usedNums.isEmpty() && usedNums.contains(matrix[i][j]) && fixed[i][j] != 1) {
// add two if there is a conflict with the fixed cell
// add one if the cell repeats an ordinary cell
if (fixedForColumns[j].contains(matrix[i][j])) {
columnConflicts += 2;
} else {
columnConflicts += 1;
}
}
if (fixed[i][j] != 1) {
usedNums.add(matrix[i][j]);
}
}
}
// the value of the fitness function is the sum of the squares of conflicts
return rowConflicts * rowConflicts + columnConflicts * columnConflicts;
}
// function for the crossover between the two parents
public static String[][] crossover(String[][] firstParent, String[][] secondParent, int[][] fixed) {
Random random = new Random();
String[][] child = new String[9][9];
int variance = 0;
int onlyParent = 0;
// randomly choose a parent for each subgrid of a child
for (int j = 0; j < 3; j++) {
for (int t = 0; t < 3; t++) {
int chosenParent = random.nextInt(2);
if (chosenParent == 0) {
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
child[k][m] = firstParent[k][m];
variance += 1;
onlyParent = 1;
}
}
} else {
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
child[k][m] = secondParent[k][m];
variance += 1;
onlyParent = 2;
}
}
}
}
}
// case when the child took after only the first parent in all subgrids
if (variance == 1 && onlyParent == 1) {
boolean notFixed = true;
while (notFixed) {
int j = random.nextInt(3);
int t = random.nextInt(3);
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
child[k][m] = secondParent[k][m];
}
}
}
}
// case when the child took after only the second parent in all subgrids
if (variance == 1 && onlyParent == 2) {
boolean notFixed = true;
while (notFixed) {
int j = random.nextInt(3);
int t = random.nextInt(3);
for (int k = j * 3; k < (j + 1) * 3; k++) {
for (int m = t * 3; m < (t + 1) * 3; m++) {
child[k][m] = firstParent[k][m];
}
}
}
}
return child;
}
// function for mutation
public static String[][] mutate(String[][] child, int[][] fixed, int bestResult, int basicNumMut, int limit) {
int numberOfMutations = 2;
Random random = new Random();
int mut = random.nextInt(5);
numberOfMutations = mut;
// change the number of mutations is the fitness is high
// (but it cannot be zero)
if (bestResult < limit) {
numberOfMutations = basicNumMut;
}
for (int i = 0; i < numberOfMutations; i++) {
boolean fixedCells = true;
while (fixedCells) {
// randomly choose a subgrid for mutation
int j = random.nextInt(3);
int t = random.nextInt(3);
// randomly choose the first cell for swapping
int index1 = random.nextInt((j + 1) * 3 - j * 3) + j * 3;
int index2 = random.nextInt((t + 1) * 3 - t * 3) + t * 3;
int d = 0;
int w = 0;
boolean flag = true;
while (flag) {
// choose the second cell for swapping
d = random.nextInt((j + 1) * 3 - j * 3) + j * 3;
w = random.nextInt((t + 1) * 3 - t * 3) + t * 3;
if (d != index1 || w != index2) {
flag = false;
}
}
// swap the cells within the subgrid if the cells are not fixed
if (fixed[index1][index2] != 1 && fixed[d][w] != 1) {
String num = child[index1][index2];
child[index1][index2] = child[d][w];
child[d][w] = num;
fixedCells = false;
}
}
}
return child;
}
}
class Triple{
ArrayList<Integer> firstElement;
ArrayList<Integer> secondElement;
ArrayList<Integer> thirdElement;
public Triple(ArrayList<Integer> first, ArrayList<Integer> second, ArrayList<Integer> third) {
this.firstElement = first;
this.secondElement = second;
this.thirdElement = third;
}
public ArrayList<Integer> getFirstElement() {
return firstElement;
}
public ArrayList<Integer> getSecondElement() {
return secondElement;
}
public ArrayList<Integer> getThirdElement() {
return thirdElement;
}
}