Skip to content

Commit 30b8d25

Browse files
committed
save on exit and output to standard error
- closing the program will now calculate first the fittest bird then it will save the neural network of that bird. - converted all standard output 'cout' to output on standard error 'cerr' instead.
1 parent dea1a85 commit 30b8d25

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

src/ffnn.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#include "ffnn.hpp"
66

7-
std::uniform_int_distribution<size_t> FFNN::random_chance(0U, 100U);
8-
std::uniform_real_distribution<float> FFNN::new_random_weight(-1.f, 1.f);
7+
std::uniform_real_distribution<float> FFNN::random_chance(0.f, 1.f);
8+
std::uniform_real_distribution<float> FFNN::random_weight(-1.f, 1.f);
99

1010
FFNN::FFNN() : input_layer(), w1(), hidden_layer(), w2(), output_layer()
1111
{
@@ -16,13 +16,13 @@ void FFNN::randomize_network()
1616
{
1717
for (size_t j = 0; j < INPUTS; ++j) {
1818
for (size_t i = 0; i < HIDDEN; ++i) {
19-
w1(i, j) = new_random_weight(rand_engine);
19+
w1(i, j) = random_weight(rand_engine);
2020
}
2121
}
2222

2323
for (size_t j = 0; j < HIDDEN; ++j) {
2424
for (size_t i = 0; i < OUTPUT; ++i) {
25-
w2(i, j) = new_random_weight(rand_engine);
25+
w2(i, j) = random_weight(rand_engine);
2626
}
2727
}
2828
}
@@ -60,18 +60,18 @@ void FFNN::mutate()
6060
// mutate weight 1
6161
for (size_t j = 0; j < INPUTS; ++j) {
6262
for (size_t i = 0; i < HIDDEN; ++i) {
63-
size_t chance = random_chance(rand_engine);
63+
float chance = random_chance(rand_engine);
6464
if (chance <= MUTATION_CHANCE_THRESHOLD) {
65-
w1(i, j) = new_random_weight(rand_engine);
65+
w1(i, j) = random_weight(rand_engine);
6666
}
6767
}
6868
}
6969

7070
for (size_t j = 0; j < HIDDEN; ++j) {
7171
for (size_t i = 0; i < OUTPUT; ++i) {
72-
size_t chance = random_chance(rand_engine);
72+
float chance = random_chance(rand_engine);
7373
if (chance <= MUTATION_CHANCE_THRESHOLD) {
74-
w2(i, j) = new_random_weight(rand_engine);
74+
w2(i, j) = random_weight(rand_engine);
7575
}
7676
}
7777
}
@@ -82,7 +82,7 @@ void FFNN::combine(FFNN const &nn1, FFNN const &nn2)
8282
// mutate weight 1
8383
for (size_t j = 0; j < INPUTS; ++j) {
8484
for (size_t i = 0; i < HIDDEN; ++i) {
85-
size_t chance = random_chance(rand_engine);
85+
float chance = random_chance(rand_engine);
8686
if (chance <= WEIGHT_SELECTION_CHANCE_THRESHOLD) {
8787
w1(i, j) = nn1.w1(i, j);
8888
} else {
@@ -93,7 +93,7 @@ void FFNN::combine(FFNN const &nn1, FFNN const &nn2)
9393

9494
for (size_t j = 0; j < HIDDEN; ++j) {
9595
for (size_t i = 0; i < OUTPUT; ++i) {
96-
size_t chance = random_chance(rand_engine);
96+
float chance = random_chance(rand_engine);
9797
if (chance <= WEIGHT_SELECTION_CHANCE_THRESHOLD) {
9898
w2(i, j) = nn1.w2(i, j);
9999
} else {
@@ -111,7 +111,7 @@ bool FFNN::save_network(std::string const &output_filename)
111111
nn_file.write(reinterpret_cast<char *>(w2.data()), w2.size() * sizeof(float));
112112
nn_file.close();
113113

114-
std::cout << "------------ saved ------------\n";
114+
std::cout << "------------ saved ------------\n";
115115
std::cout << "w1 = \n\n" << w1 << "\n\nw2 = \n\n" << w2 << "\n\n";
116116

117117
return nn_file.good();
@@ -125,12 +125,12 @@ bool FFNN::load_network(std::string const &filename)
125125
return false;
126126
}
127127

128+
std::cout << "------------ loaded ------------\n";
129+
std::cout << "w1 = \n\n" << w1 << "\n\nw2 = \n\n" << w2 << "\n\n";
130+
128131
nn_file.read(reinterpret_cast<char *>(w1.data()), w1.size() * sizeof(float));
129132
nn_file.read(reinterpret_cast<char *>(w2.data()), w2.size() * sizeof(float));
130133
nn_file.close();
131134

132-
std::cout << "------------ loaded ------------\n";
133-
std::cout << "w1 = \n\n" << w1 << "\n\nw2 = \n\n" << w2 << "\n\n";
134-
135135
return nn_file.good();
136136
}

src/ffnn.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ struct FFNN
2323
static constexpr size_t OUTPUT = 1;
2424

2525
/// \brief 30% chance to mutate a weight.
26-
static constexpr size_t MUTATION_CHANCE_THRESHOLD = 30;
26+
static constexpr float MUTATION_CHANCE_THRESHOLD = 0.3f;
2727

28-
/// \brief 50% chance to choose from parentA's weight.
29-
static constexpr size_t WEIGHT_SELECTION_CHANCE_THRESHOLD = 50;
28+
/// \brief 50%/50% chance to choose from parent A's or parent B's weight.
29+
static constexpr float WEIGHT_SELECTION_CHANCE_THRESHOLD = 0.5f;
3030

3131
/// \brief generates number from 1 to 100.
32-
static std::uniform_int_distribution<size_t> random_chance;
32+
static std::uniform_real_distribution<float> random_chance;
3333

3434
/// \brief generates weight from -0.5 to 0.5.
35-
static std::uniform_real_distribution<float> new_random_weight;
35+
static std::uniform_real_distribution<float> random_weight;
3636

3737
Eigen::Matrix<float, INPUTS, 1> input_layer;
3838
Eigen::Matrix<float, HIDDEN, INPUTS> w1;

src/main.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int main(int argc, char *args[])
6161
bool capture_frames = false;
6262
if (argc == 2) {
6363
if (std::string(args[1]) == "capture") {
64-
std::cout << "frame capture flag enabled\n";
64+
std::cerr << "frame capture flag enabled\n";
6565
capture_frames = true;
6666
capture_texture.create(WINDOW_WIDTH, WINDOW_HEIGHT);
6767
}
@@ -78,9 +78,9 @@ int main(int argc, char *args[])
7878
GeneticAlgorithm genetic_algorithm;
7979

8080
if (!birds.collection[0].neural_net.load_network("fittest.nn")) {
81-
std::cout << "error loading the fittest network\n";
81+
std::cerr << "error loading the fittest network\n";
8282
} else {
83-
std::cout << "the previous fittests network is loaded\n";
83+
std::cerr << "the previous fittests network is loaded\n";
8484
}
8585

8686
while (window.isOpen()) {
@@ -90,6 +90,14 @@ int main(int argc, char *args[])
9090
sf::Event event;
9191
while (window.pollEvent(event)) {
9292
if (event.type == sf::Event::Closed) {
93+
genetic_algorithm.rank_fitness(birds);
94+
95+
if (birds.collection[0].neural_net.save_network("fittest.nn")) {
96+
std::cerr << "fittest network saved\n";
97+
} else {
98+
std::cerr << "failed to save the fittest network\n";
99+
}
100+
93101
window.close();
94102
}
95103

@@ -175,9 +183,9 @@ int main(int argc, char *args[])
175183
genetic_algorithm.rank_fitness(birds);
176184

177185
if (birds.collection[0].neural_net.save_network("fittest.nn")) {
178-
std::cout << "fittest network saved\n";
186+
std::cerr << "fittest network saved\n";
179187
} else {
180-
std::cout << "failed to save the fittest network\n";
188+
std::cerr << "failed to save the fittest network\n";
181189
}
182190

183191
genetic_algorithm.apply_mutations(birds);

0 commit comments

Comments
 (0)