diff options
author | Paul Oliver <contact@pauloliver.dev> | 2024-02-29 03:15:03 +0100 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2024-02-29 03:15:30 +0100 |
commit | 6fd23da97fa9700f59c61a966b4bf7d25fa46b34 (patch) | |
tree | d5fe3a8305a5f57e5b4cedc8300e951c74696cc5 /NNUtils.cpp |
Diffstat (limited to 'NNUtils.cpp')
-rw-r--r-- | NNUtils.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/NNUtils.cpp b/NNUtils.cpp new file mode 100644 index 0000000..611768a --- /dev/null +++ b/NNUtils.cpp @@ -0,0 +1,54 @@ +#include "NNUtils.hpp" + +std::default_random_engine rGenerator; + +void seedRand() +{ + rGenerator.seed(std::chrono::system_clock::now().time_since_epoch().count()); +} + +float realRand(float low, float hi) +{ + std::uniform_real_distribution<float> fDistributor(low, hi); + return fDistributor(rGenerator); +} + +int intRand(int low, int hi) +{ + std::uniform_int_distribution<int> iDistributor(low, hi - 1); + return iDistributor(rGenerator); +} + +float sigmoid(float input, float sensitivity) +{ + return 1.f / (1.f + expf(-input / sensitivity)); +} + +void crossover(const Chromosome &parent, const Chromosome &mother, Chromosome &child1, Chromosome &child2) +{ + if (parent.size() != mother.size()) + { + return; + } + + child1.clear(); + child2.clear(); + + int cop = intRand(0, parent.size()); + + child1.insert(child1.end(), parent.begin(), parent.begin() + cop); + child1.insert(child1.end(), mother.begin() + cop, mother.end()); + child2.insert(child2.end(), mother.begin(), mother.begin() + cop); + child2.insert(child2.end(), parent.begin() + cop, parent.end()); +} + +void mutate(Chromosome &chromosome, float mutationRate) +{ + for (auto &i : chromosome) + { + if (realRand(0.f, 1.f) < mutationRate) + { + i = realRand(-1.f, 1.f); + } + } +} |