-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
170 lines (140 loc) · 5.35 KB
/
Copy pathtest.cpp
File metadata and controls
170 lines (140 loc) · 5.35 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
#include "evol-puzzle.h"
#include <assert.h>
#include <sstream>
/**
* @brief Compares two arrays for equality.
*
* This function checks if two arrays of integers are equal by comparing
* each element. The arrays are assumed to have a size of TILE_SIZE.
*
* @param arr1 The first array to compare.
* @param arr2 The second array to compare.
* @return true if all elements in both arrays are equal, false otherwise.
*/
bool assertArrayEqual(const int* arr1, const int* arr2){
for (int i = 0; i < TILE_SIZE; i++){
if (arr1[i] != arr2[i]){
return false;
}
}
return true;
}
int main(){
// --- Test rotateToLeftByOneIndex
int arr[] = {1,2,3,4};
int expected[] = {2,3,4,1};
rotateToLeftByOneIndex(arr);
assert(assertArrayEqual(arr, expected));
rotateToLeftByOneIndex(arr);
int expected2[] = {3,4,1,2};
assert(assertArrayEqual(arr, expected2));
// -------------------------------
// --- Test swapTile
int** puzzle = allocatePuzzle();
readInput("Ass1Input.txt", puzzle);
// second copy for comparison
int copy_puzzle[TILES_IN_PUZZLE_COUNT][TILE_SIZE];
for (int i = 0; i < TILES_IN_PUZZLE_COUNT; i++){
for (int j = 0; j < TILE_SIZE; j++){
copy_puzzle[i][j] = puzzle[i][j];
}
}
swapTile(puzzle);
// checking if swaps happened properly
int swap_count = 0;
for (int i = 0; i < TILES_IN_PUZZLE_COUNT; i++){
if (!assertArrayEqual(puzzle[i], copy_puzzle[i])){
swap_count++;
}
}
assert(swap_count == 2);
// -------------------------------
// --- Test generatePopulation
readInput("Ass1Input.txt", puzzle);
int POPULATION_SIZE = 1000;
//int population_arr[POPULATION_SIZE][TILES_IN_PUZZLE_COUNT][TILE_SIZE];
int*** population_arr = allocatePopulation(POPULATION_SIZE);
generatePopulation(population_arr, puzzle, POPULATION_SIZE);
swap_count = 0;
for (int i = 1; i < POPULATION_SIZE; i++){
for (int j = 0; j < TILES_IN_PUZZLE_COUNT; j++){
if (!assertArrayEqual(population_arr[0][j], population_arr[i][j])){
swap_count++;
}
}
assert(swap_count >= 20);
swap_count = 0;
}
// ----
// --- Test countEdgeMismatch
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < POPULATION_SIZE; i++){
countEdgeMismatch(population_arr[i]);
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;
cout << "\nTime taken to count edge mismatches in " << POPULATION_SIZE << "puzzles: " << elapsed.count() << \
" seconds \n---> " << elapsed.count()/POPULATION_SIZE << " s/puzzle" << endl;
// --- Test OnePointCrossover
int** parent1 = population_arr[0];
int** parent2 = population_arr[1];
int initialParent1[TILES_IN_PUZZLE_COUNT][TILE_SIZE];
int initialParent2[TILES_IN_PUZZLE_COUNT][TILE_SIZE];
for (int i = 0; i < TILES_IN_PUZZLE_COUNT; i++) {
for (int j = 0; j < TILE_SIZE; j++) {
initialParent1[i][j] = parent1[i][j];
initialParent2[i][j] = parent2[i][j];
}
}
int crossoverPoint = onePointCrossover(parent1, parent2);
// Verify the crossover operation
for (int i = 0; i < crossoverPoint; i++) {
for (int j = 0; j < TILE_SIZE; j++) {
assert(assertArrayEqual(parent1[i], initialParent1[i]));
assert(assertArrayEqual(parent2[i], initialParent2[i]));
}
}
for (int i = crossoverPoint; i < TILES_IN_PUZZLE_COUNT; i++) {
for (int j = 0; j < TILE_SIZE; j++) {
assert(assertArrayEqual(parent1[i], initialParent2[i]));
assert(assertArrayEqual(parent2[i], initialParent1[i]));
}
}
// --- Test TwoPointCrossover
int** parent3 = population_arr[2];
int** parent4 = population_arr[3];
// Capture the initial state of parent3 and parent4
int initialParent3[TILES_IN_PUZZLE_COUNT][TILE_SIZE];
int initialParent4[TILES_IN_PUZZLE_COUNT][TILE_SIZE];
for (int i = 0; i < TILES_IN_PUZZLE_COUNT; i++) {
for (int j = 0; j < TILE_SIZE; j++) {
initialParent3[i][j] = parent3[i][j];
initialParent4[i][j] = parent4[i][j];
}
}
pair<int, int> crossoverPoints = twoPointCrossover(parent3, parent4);
int point1 = crossoverPoints.first;
int point2 = crossoverPoints.second;
// Verify the crossover operation
for (int i = 0; i < point1; i++) {
assert(assertArrayEqual(parent3[i], initialParent3[i]));
assert(assertArrayEqual(parent4[i], initialParent4[i]));
}
for (int i = point1; i <= point2; i++) {
assert(assertArrayEqual(parent3[i], initialParent4[i]));
assert(assertArrayEqual(parent4[i], initialParent3[i]));
}
for (int i = point2 + 1; i < TILES_IN_PUZZLE_COUNT; i++) {
assert(assertArrayEqual(parent3[i], initialParent3[i]));
assert(assertArrayEqual(parent4[i], initialParent4[i]));
}
// ---
// --- Test evaluateFitness
vector<pair<int, int>> fitness_results = evaluateFitness(population_arr, POPULATION_SIZE);
// checking if sorted properly
for (int i = 1; i < fitness_results.size(); i++) {
assert(fitness_results[i-1].second >= fitness_results[i].second);
}
cout << "\n\n" << "All tests passed!" << "\n\n";
return 0;
}