-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolution.cpp
66 lines (54 loc) · 2.33 KB
/
evolution.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
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include "src/ChessGame.hpp"
#include "src/GenePool.hpp"
#include "src/Utilities.hpp"
int main() {
GenePool evo_tourney;
// Add 20 organisms to the tournament
// Empty curly braces denotes no preferences (defaults to random move)
for (int i = 0; i < 20; ++i) { evo_tourney.add_organism({}); }
std::size_t round_count = 0;
while (true) {
std::cout << "Round " << round_count++ << '\n';
// Let each engine play every other engine as black and white 2 times
evo_tourney.evaluate_fitness(2);
evo_tourney.sort_by_fitness();
// Output round results
const std::vector<Organism> &organisms = evo_tourney.get_organisms();
std::vector<std::string> names;
std::size_t max_name_w = 10;
for (std::size_t i = 0; i < organisms.size(); ++i) {
names.push_back(
Engine::PreferenceChain(organisms[i].genome).get_name()
);
if (names[i].size() > max_name_w) { max_name_w = names[i].size(); }
}
std::cout << " " << std::right << std::setw(max_name_w) << "Organism"
<< ' ' << std::left << std::setw(6) << "Wins" << ' '
<< std::setw(6) << "Draws" << ' ' << std::setw(6) << "Losses"
<< ' ' << std::setw(6) << "W/L" << '\n';
for (std::size_t i = 0; i < organisms.size(); ++i) {
const Organism &org = organisms[i];
std::cout << std::right << std::setw(2) << i << ". "
<< std::setw(max_name_w) << names[i] << ' ' << std::left
<< std::setw(6) << org.num_wins << ' ' << std::setw(6)
<< org.num_draws << ' ' << std::setw(6) << org.num_losses
<< ' ' << std::setw(6) << std::fixed
<< std::setprecision(2)
<< static_cast<double>(org.num_wins) /
static_cast<double>(org.num_losses)
<< '\n';
}
std::cout << std::endl;
// Kill off the bottom 10
evo_tourney.cull(10);
// Let each survivor create 1 offspring
// Offspring can insert, delete, swap, or replace preference tokens
evo_tourney.breed(1);
}
return EXIT_SUCCESS;
}