aboutsummaryrefslogtreecommitdiff
path: root/NNUtils.cpp
blob: 611768ac3bc662c5fc1c7712477d0b52173875d8 (plain)
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
#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);
        }
    }
}