-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
87 lines (81 loc) · 2.66 KB
/
utils.cpp
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
#include "utils.hpp"
#include "chromosome.hpp"
#include "ga.hpp"
#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>
#include "gui.hpp"
int howManyAlive(int board[BOARD_SIZE][BOARD_SIZE]) {
// count how many cells are alive in a given board
int alive = 0;
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
alive += board[row][col];
}
}
return alive;
}
void copyBoard(int board[BOARD_SIZE][BOARD_SIZE],
int newBoard[BOARD_SIZE][BOARD_SIZE]) {
// copy a board to a new board
for (int row = 0; row < BOARD_SIZE; row++) {
memcpy(newBoard[row], board[row], BOARD_SIZE * sizeof(int));
}
}
// test chromosome crossover
void testCrossover() {
std::cout << "Testing crossover" << std::endl;
std::shared_ptr<Chromosome> parent1 = std::make_shared<Chromosome>();
std::shared_ptr<Chromosome> parent2 = std::make_shared<Chromosome>();
std::cout << "Parent 1:" << std::endl;
parent1->calculateScore();
parent1->calculateFitness(0);
parent1->printBoard();
std::cout << std::endl;
std::cout << "Parent 1 score: " << parent1->score << std::endl;
std::cout << "Parent 2:" << std::endl;
parent2->calculateScore();
parent2->calculateFitness(0);
parent2->printBoard();
std::cout << std::endl;
std::cout << "Parent 2 score: " << parent2->score << std::endl;
std::shared_ptr<Chromosome> offspring =
std::make_shared<Chromosome>(parent1, parent2);
std::cout << "Offspring:" << std::endl;
offspring->calculateScore();
offspring->calculateFitness(0);
offspring->printBoard();
std::cout << std::endl;
std::cout << "Offspring score: " << offspring->score << std::endl;
}
void testSelection() {
std::cout << "Testing selection" << std::endl;
std::vector<std::shared_ptr<Chromosome>> chromosomes;
for (int i = 0; i < 100; i++) {
std::shared_ptr<Chromosome> chromosome = std::make_shared<Chromosome>();
chromosome->score = i;
chromosomes.push_back(chromosome);
}
std::cout << "Before selection:" << std::endl;
for (int i = 0; i < 100; i++) {
std::cout << chromosomes[i]->score << std::endl;
}
GeneticAlgorithm::rouletteWheelSelection(chromosomes, 5);
std::cout << "After selection:" << std::endl;
for (int i = 0; i < 5; i++) {
std::cout << chromosomes[i]->score << std::endl;
}
}
void testCrossoverOnGUI() {
std::shared_ptr<Chromosome> parent1 = std::make_shared<Chromosome>();
std::shared_ptr<Chromosome> parent2 = std::make_shared<Chromosome>();
std::shared_ptr<Chromosome> offspring =
std::make_shared<Chromosome>(parent1, parent2);
GUI::setBoard(parent1->board);
GUI::next();
GUI::setBoard(parent2->board);
GUI::next();
GUI::setBoard(offspring->board);
GUI::next();
}