summaryrefslogtreecommitdiff
path: root/src/brain/neural_network.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/brain/neural_network.cpp')
-rw-r--r--src/brain/neural_network.cpp81
1 files changed, 38 insertions, 43 deletions
diff --git a/src/brain/neural_network.cpp b/src/brain/neural_network.cpp
index d6a5e15..9061194 100644
--- a/src/brain/neural_network.cpp
+++ b/src/brain/neural_network.cpp
@@ -1,5 +1,6 @@
#include "brain/neural_network.h"
#include "brain/neuron.h"
+#include <limits>
NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, int numberOfSensors, int numberOfOutputs)
{
@@ -11,7 +12,9 @@ NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, int numberOfSenso
}
for (int i=0; i<numberOfOutputs; ++i)
{
- _outputs[i] = std::make_shared<Neuron>(i);
+ auto output = std::make_shared<Neuron>(i);
+ _outputs.push_back(output);
+ _neurons[i] = output;
}
parseFile(std::move(networkConfigFile));
@@ -21,15 +24,17 @@ NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, std::vector<bool>
{
_biasNode = std::make_shared<BiasNode>();
- for (int i=0; i<sensorInitialValues.size(); ++i)
+ for (unsigned int i=0; i<sensorInitialValues.size(); ++i)
{
auto sensor = std::make_shared<Sensor>(i);
- sensor->setActivation(sensorInitialValues.at(i) ? 1 : 0);
+ sensor->setActivation(sensorInitialValues[i] ? 1 : 0);
_sensors[i] = sensor;
}
for (int i=0; i<numberOfOutputs; ++i)
{
- _outputs[i] = std::make_shared<Neuron>(i);
+ auto output = std::make_shared<Neuron>(i);
+ _outputs.push_back(output);
+ _neurons[i] = output;
}
parseFile(std::move(networkConfigFile));
@@ -38,14 +43,18 @@ NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, std::vector<bool>
void NeuralNetwork::parseFile(std::istream &&file)
{
double weight;
- for (std::string src, dest;
- file >> src && file >> dest && file >> weight; )
- {
- char srcType = src.at(0);
- int srcId = std::stoi(src.substr(1));
- char destType = dest.at(0);
- int destId = std::stoi(dest.substr(1));
+ char srcType;
+ int srcId;
+ int destId;
+ while (file.get(srcType) &&
+ file >> srcId &&
+ file.ignore(std::numeric_limits<std::streamsize>::max(), 'n') &&
+ file >> destId &&
+ file >> weight &&
+ file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'))
+ {
+
std::shared_ptr<NeuralNode> source;
std::shared_ptr<Neuron> destination;
switch (srcType)
@@ -56,21 +65,11 @@ void NeuralNetwork::parseFile(std::istream &&file)
case 'b':
source = _biasNode;
break;
- case 'n':
- source = findOrAddNeuron(srcId);
- break;
default:
- throw 1;
- }
- switch (destType)
- {
- case 'n':
- destination = findOrAddNeuron(destId);
- break;
- default:
- throw 1;
+ source = findOrAddNeuron(srcId);
}
-
+ destination = findOrAddNeuron(destId);
+
addLink(source, destination, weight);
}
@@ -78,34 +77,30 @@ void NeuralNetwork::parseFile(std::istream &&file)
void NeuralNetwork::addLink(std::shared_ptr<NeuralNode> source, std::shared_ptr<Neuron> destination, double weight)
{
- auto link = std::make_shared<NeuralLink>(source, weight);
- destination->addInput(link);
+ std::unique_ptr<NeuralLink> link(new NeuralLink(source, weight));
+ destination->addInput(std::move(link));
}
std::shared_ptr<Sensor> NeuralNetwork::findOrAddSensor(int id)
{
- bool sensorExists = _sensors.count(id) > 0;
- if (!sensorExists)
+ auto sensor = _sensors[id];
+ if (!sensor)
{
- _sensors[id] = std::make_shared<Sensor>(id);
+ sensor = std::make_shared<Sensor>(id);
+ _sensors[id] = sensor;
}
- return _sensors.at(id);
+ return sensor;
}
std::shared_ptr<Neuron> NeuralNetwork::findOrAddNeuron(int id)
{
- bool isOutput = _outputs.count(id) > 0;
- if (isOutput)
- {
- return _outputs.at(id);
- }
-
- bool hiddenNeuronExists = _hiddenNodes.count(id) > 0;
- if (!hiddenNeuronExists)
+ auto neuron = _neurons[id];
+ if (!neuron)
{
- _hiddenNodes[id] = std::make_shared<Neuron>(id);
+ neuron = std::make_shared<Neuron>(id);
+ _neurons[id] = neuron;
}
- return _hiddenNodes.at(id);
+ return neuron;
}
void NeuralNetwork::setInput(int inputIndex, double activation)
@@ -117,13 +112,13 @@ int NeuralNetwork::findMaxOutputIndex() const
{
double currentMaxActivation = 0;
int currentMaxIndex = 0;
- for (std::pair<const int, std::shared_ptr<Neuron>> outputPair : _outputs)
+ for (unsigned int i=0; i<_outputs.size(); ++i)
{
- double activation = outputPair.second->activation();
+ double activation = _outputs[i]->activation();
if (activation >= currentMaxActivation)
{
currentMaxActivation = activation;
- currentMaxIndex = outputPair.first;
+ currentMaxIndex = i;
}
}
return currentMaxIndex;