Skip to content

Commit

Permalink
save on exit and output to standard error
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
mrdcvlsc committed Nov 1, 2023
1 parent dea1a85 commit 30b8d25
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
28 changes: 14 additions & 14 deletions src/ffnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "ffnn.hpp"

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

FFNN::FFNN() : input_layer(), w1(), hidden_layer(), w2(), output_layer()
{
Expand All @@ -16,13 +16,13 @@ void FFNN::randomize_network()
{
for (size_t j = 0; j < INPUTS; ++j) {
for (size_t i = 0; i < HIDDEN; ++i) {
w1(i, j) = new_random_weight(rand_engine);
w1(i, j) = random_weight(rand_engine);
}
}

for (size_t j = 0; j < HIDDEN; ++j) {
for (size_t i = 0; i < OUTPUT; ++i) {
w2(i, j) = new_random_weight(rand_engine);
w2(i, j) = random_weight(rand_engine);
}
}
}
Expand Down Expand Up @@ -60,18 +60,18 @@ void FFNN::mutate()
// mutate weight 1
for (size_t j = 0; j < INPUTS; ++j) {
for (size_t i = 0; i < HIDDEN; ++i) {
size_t chance = random_chance(rand_engine);
float chance = random_chance(rand_engine);
if (chance <= MUTATION_CHANCE_THRESHOLD) {
w1(i, j) = new_random_weight(rand_engine);
w1(i, j) = random_weight(rand_engine);
}
}
}

for (size_t j = 0; j < HIDDEN; ++j) {
for (size_t i = 0; i < OUTPUT; ++i) {
size_t chance = random_chance(rand_engine);
float chance = random_chance(rand_engine);
if (chance <= MUTATION_CHANCE_THRESHOLD) {
w2(i, j) = new_random_weight(rand_engine);
w2(i, j) = random_weight(rand_engine);
}
}
}
Expand All @@ -82,7 +82,7 @@ void FFNN::combine(FFNN const &nn1, FFNN const &nn2)
// mutate weight 1
for (size_t j = 0; j < INPUTS; ++j) {
for (size_t i = 0; i < HIDDEN; ++i) {
size_t chance = random_chance(rand_engine);
float chance = random_chance(rand_engine);
if (chance <= WEIGHT_SELECTION_CHANCE_THRESHOLD) {
w1(i, j) = nn1.w1(i, j);
} else {
Expand All @@ -93,7 +93,7 @@ void FFNN::combine(FFNN const &nn1, FFNN const &nn2)

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

std::cout << "------------ saved ------------\n";
std::cout << "------------ saved ------------\n";
std::cout << "w1 = \n\n" << w1 << "\n\nw2 = \n\n" << w2 << "\n\n";

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

std::cout << "------------ loaded ------------\n";
std::cout << "w1 = \n\n" << w1 << "\n\nw2 = \n\n" << w2 << "\n\n";

nn_file.read(reinterpret_cast<char *>(w1.data()), w1.size() * sizeof(float));
nn_file.read(reinterpret_cast<char *>(w2.data()), w2.size() * sizeof(float));
nn_file.close();

std::cout << "------------ loaded ------------\n";
std::cout << "w1 = \n\n" << w1 << "\n\nw2 = \n\n" << w2 << "\n\n";

return nn_file.good();
}
10 changes: 5 additions & 5 deletions src/ffnn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ struct FFNN
static constexpr size_t OUTPUT = 1;

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

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

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

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

Eigen::Matrix<float, INPUTS, 1> input_layer;
Eigen::Matrix<float, HIDDEN, INPUTS> w1;
Expand Down
18 changes: 13 additions & 5 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int main(int argc, char *args[])
bool capture_frames = false;
if (argc == 2) {
if (std::string(args[1]) == "capture") {
std::cout << "frame capture flag enabled\n";
std::cerr << "frame capture flag enabled\n";
capture_frames = true;
capture_texture.create(WINDOW_WIDTH, WINDOW_HEIGHT);
}
Expand All @@ -78,9 +78,9 @@ int main(int argc, char *args[])
GeneticAlgorithm genetic_algorithm;

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

while (window.isOpen()) {
Expand All @@ -90,6 +90,14 @@ int main(int argc, char *args[])
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
genetic_algorithm.rank_fitness(birds);

if (birds.collection[0].neural_net.save_network("fittest.nn")) {
std::cerr << "fittest network saved\n";
} else {
std::cerr << "failed to save the fittest network\n";
}

window.close();
}

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

if (birds.collection[0].neural_net.save_network("fittest.nn")) {
std::cout << "fittest network saved\n";
std::cerr << "fittest network saved\n";
} else {
std::cout << "failed to save the fittest network\n";
std::cerr << "failed to save the fittest network\n";
}

genetic_algorithm.apply_mutations(birds);
Expand Down

0 comments on commit 30b8d25

Please sign in to comment.