-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulation.cpp
More file actions
67 lines (63 loc) · 1.53 KB
/
Copy pathpopulation.cpp
File metadata and controls
67 lines (63 loc) · 1.53 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
/*
* population.cpp
*
* Created on: Apr 25, 2021
* Author: albus
*/
#include "population.hpp"
population::population(uint_fast32_t size) {
this->size = size;
this->elites = 5;
this->score = 0;
while (this->individuals.size() != this->size) {
board board = *(new class board());
if (board.validate())
this->individuals.push_back(board);
}
}
void population::score_calc() {
this->reset_scores();
for (auto it = this->individuals.begin(); it != this->individuals.end();
++it) {
it->calc_score();
}
}
void population::sort() {
std::sort(this->individuals.begin(), this->individuals.end(),
[](board &a, board &b) {
return a.get_score() > b.get_score();
});
}
void population::reset_scores() {
for (auto it = this->individuals.begin(); it != this->individuals.end();
++it) {
it->set_score(0);
}
}
void population::mutate() {
for (int i = 0; i < this->elites; i++) {
this->individuals.push_back(this->individuals[i]);
}
for (auto it = this->individuals.begin() + this->elites;
it != this->individuals.end(); ++it) {
it->mutate();
}
}
void population::print_best_ind(int i) {
// if (this->individuals[0].get_score()>512)
{
std::cout << this->individuals[0].get_score() << " generation:" << i
<< std::endl;
for (uint8_t i = 0; i < 8; i++) {
std::cout << unsigned((this->individuals[0].get_queens()[i]))
<< ",";
}
std::cout << std::endl;
}
this->score = this->individuals[0].get_score();
}
int_fast32_t population::get_score() {
return this->score;
}
population::~population() {
}