-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetworkConnector.cpp
136 lines (113 loc) · 4.75 KB
/
NeuralNetworkConnector.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "NeuralNetworkConnector.h"
#include "Random.h"
#include "Algorithm.h"
#include <assert.h>
using namespace nlohmann;
NeuralNetworkConnector::NeuralNetworkConnector(unsigned inputs, unsigned outputs)
: weights_(inputs, std::vector<double>(outputs, 0.0))
{
std::vector<size_t> inputIndexes = util::CreateSeries<size_t>(0, inputs);
std::vector<size_t> outputIndexes = util::CreateSeries<size_t>(0, outputs);
// so we don't just connect input 1 to output 1, and input 2 to output 2 etc
Random::Shuffle(inputIndexes);
Random::Shuffle(outputIndexes);
/*
* Creates a bunch of 1:1 connections between inputs and outputs. The larger
* of inputs or outputs will therefore have some unconnected nodes.
*/
util::IterateBoth(inputIndexes, outputIndexes, [&](const size_t& in, const size_t& out)
{
// set the weight of an input to an output to 1 so it is a "direct passthrough" connection
weights_.at(in).at(out) = 1.0;
});
}
NeuralNetworkConnector::NeuralNetworkConnector(std::vector<std::vector<double>>&& weights)
: weights_(std::move(weights))
{
}
void NeuralNetworkConnector::PassForward(const std::vector<double>& inputValues, std::vector<double>& outputValues)
{
assert(inputValues.size() == weights_.size() && outputValues.size() == weights_.at(0).size());
util::IterateBoth(inputValues, weights_, [&outputValues](const double& input, const std::vector<double>& inputWeights) -> void
{
util::IterateBoth(inputWeights, outputValues, [&input](const double& inputWeight, double& output) -> void
{
output += input * inputWeight;
});
});
}
std::shared_ptr<NeuralNetworkConnector> NeuralNetworkConnector::WithMutatedConnections() const
{
std::vector<std::vector<double>> newWeights = weights_;
switch (Random::WeightedIndex({ 90, 8, 2 })) {
case 0:
// 90% Chance mutate a single connection
if (newWeights.size() > 0) {
size_t input = Random::Number<size_t>(size_t{ 0 }, newWeights.size() - 1);
if (newWeights.at(input).size() > 0) {
size_t output = Random::Number<size_t>(size_t{ 0 }, newWeights.at(input).size() - 1);
newWeights.at(input).at(output) += Random::Gaussian(0.0, 0.2);
}
}
break;
case 1:
// 8% Chance mutate all connections
for (auto& input : newWeights) {
for (auto& output : input) {
output += Random::Gaussian(0.0, 0.1);
}
}
break;
case 2:
// 2% Chance shuffle connections
for (auto& input : newWeights) {
Random::Shuffle(input);
}
break;
}
return std::make_shared<NeuralNetworkConnector>(std::move(newWeights));
}
std::shared_ptr<NeuralNetworkConnector> NeuralNetworkConnector::WithInputAdded(size_t index) const
{
std::vector<std::vector<double>> newWeights = weights_;
size_t newInputIndex = std::min(index, newWeights.size());
auto newInputIter = newWeights.begin();
std::advance(newInputIter, newInputIndex);
newWeights.insert(newInputIter, std::vector<double>(GetOutputCount(), 0.0));
return std::make_shared<NeuralNetworkConnector>(std::move(newWeights));
}
std::shared_ptr<NeuralNetworkConnector> NeuralNetworkConnector::WithInputRemoved(size_t index) const
{
std::vector<std::vector<double>> newWeights = weights_;
if (!newWeights.empty()) {
size_t newInputIndex = std::min(index, newWeights.size() - 1);
auto newInputIter = newWeights.begin();
std::advance(newInputIter, newInputIndex);
newWeights.erase(newInputIter);
}
return std::make_shared<NeuralNetworkConnector>(std::move(newWeights));
}
std::shared_ptr<NeuralNetworkConnector> NeuralNetworkConnector::WithOutputAdded(size_t index) const
{
std::vector<std::vector<double>> newWeights = weights_;
for (auto& connections : newWeights) {
size_t newOutputIndex = std::min(index, connections.size());
auto newOutputIter = connections.begin();
std::advance(newOutputIter, newOutputIndex);
connections.insert(newOutputIter, 0.0);
}
return std::make_shared<NeuralNetworkConnector>(std::move(newWeights));
}
std::shared_ptr<NeuralNetworkConnector> NeuralNetworkConnector::WithOutputRemoved(size_t index) const
{
std::vector<std::vector<double>> newWeights = weights_;
for (auto& connections : newWeights) {
if (!connections.empty()) {
size_t newOutputIndex = std::min(index, connections.size() - 1);
auto newOutputIter = connections.begin();
std::advance(newOutputIter, newOutputIndex);
connections.erase(newOutputIter);
}
}
return std::make_shared<NeuralNetworkConnector>(std::move(newWeights));
}