-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.c
89 lines (73 loc) · 2.54 KB
/
serial.c
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
#include "assistFunctions.h"
#include "conflict.h"
#include "mutate.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void slave(int N, int *board1, int *board2, int** newBoards)
{
crossoverRandomSplit(N, board1, board2, 0.15, newBoards);
newBoards[2] = board1;
newBoards[3] = board2;
mutateMaxConflict(N, newBoards[2]);
mutateMaxConflict(N, newBoards[3]);
}
int main(int argc, char **argv)
{
srand(time(NULL));
int numSets = 4;
int N = 64;
double genDivFactor = 0.3;
time_t startTime = time(NULL);
int** boards = (int **)malloc(numSets * N * sizeof(int));
int** newBoards = (int **)malloc(4 * N * sizeof(int));
int* conflictScores = (int *)malloc(numSets * sizeof(int));
int* newConflictScores = (int *)malloc(4 * sizeof(int));
for (int i = 0; i < numSets; i++) {
boards[i] = (int *)malloc(N * sizeof(int));
fillRandom(N, boards[i]);
conflictScores[i] = computeConflictScore(N, boards[i]);
printf("Board %d | Conflict Score: %d\n", i, conflictScores[i]);
// printBoard(N, boards[i]);
}
for (int i = 0; i < 4; i++) {
newBoards[i] = (int *)malloc(N * sizeof(int));
}
int iter = 0;
while (sum(numSets, conflictScores) != 0) {
int i1 = rand() % numSets;
int i2;
do {
i2 = rand() % numSets;
} while(i2 == i1);
slave(N, boards[i1], boards[i2], newBoards);
for (int i = 0; i < 4; i++) {
newConflictScores[i] = computeConflictScore(N, newBoards[i]);
}
for (int i = 0; i < 4; i++) {
int maxExistingConflictScore = max(numSets, conflictScores);
if (newConflictScores[i] < maxExistingConflictScore) {
int j = getIndex(numSets, conflictScores, maxExistingConflictScore);
boards[j] = newBoards[i];
conflictScores[j] = newConflictScores[i];
}
}
if (randDouble() < genDivFactor) {
mutateTrulyRandom(N, boards[rand() % numSets]);
}
if (iter % 1000 == 0) {
printf("Conflict Scores: ");
for (int i = 0; i < numSets; i++) {
printf("%d ", conflictScores[i]);
}
printf("\n");
}
iter++;
}
time_t endTime = time(NULL);
for (int i = 0; i < numSets; i++) {
printf("Board %d | Conflict Score: %d\n", i, conflictScores[i]);
// printBoard(N, boards[i]);
}
printf("Solved in %d iterations in %li seconds\n", iter, endTime - startTime);
}