summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2015-08-02 20:45:24 +0200
committerJustin Worthe <justin.worthe@gmail.com>2015-08-02 20:45:24 +0200
commit1049b6acb37c244ef7470b2e7fe4145a616d3df3 (patch)
treedbb5d8d0eb670cbd10d645dc4b8fa24ac70e192e /src
parent87ef7e90829053b7bc336f7316c3facb6c51e781 (diff)
Changed network innards to use maps
Diffstat (limited to 'src')
-rw-r--r--src/brain/neural_network.cpp73
1 files changed, 20 insertions, 53 deletions
diff --git a/src/brain/neural_network.cpp b/src/brain/neural_network.cpp
index 30da46c..d6a5e15 100644
--- a/src/brain/neural_network.cpp
+++ b/src/brain/neural_network.cpp
@@ -7,11 +7,11 @@ NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, int numberOfSenso
for (int i=0; i<numberOfSensors; ++i)
{
- _sensors.push_back(std::make_shared<Sensor>(i));
+ _sensors[i] = std::make_shared<Sensor>(i);
}
for (int i=0; i<numberOfOutputs; ++i)
{
- _outputs.push_back(std::make_shared<Neuron>(i));
+ _outputs[i] = std::make_shared<Neuron>(i);
}
parseFile(std::move(networkConfigFile));
@@ -25,11 +25,11 @@ NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, std::vector<bool>
{
auto sensor = std::make_shared<Sensor>(i);
sensor->setActivation(sensorInitialValues.at(i) ? 1 : 0);
- _sensors.push_back(sensor);
+ _sensors[i] = sensor;
}
for (int i=0; i<numberOfOutputs; ++i)
{
- _outputs.push_back(std::make_shared<Neuron>(i));
+ _outputs[i] = std::make_shared<Neuron>(i);
}
parseFile(std::move(networkConfigFile));
@@ -84,79 +84,46 @@ void NeuralNetwork::addLink(std::shared_ptr<NeuralNode> source, std::shared_ptr<
std::shared_ptr<Sensor> NeuralNetwork::findOrAddSensor(int id)
{
- std::shared_ptr<Sensor> result;
- for (auto node : _sensors)
+ bool sensorExists = _sensors.count(id) > 0;
+ if (!sensorExists)
{
- if (node->id() == id)
- {
- result = node;
- break;
- }
+ _sensors[id] = std::make_shared<Sensor>(id);
}
- if (!result)
- {
- result = std::make_shared<Sensor>(id);
- _sensors.push_back(result);
- }
- return result;
+ return _sensors.at(id);
}
std::shared_ptr<Neuron> NeuralNetwork::findOrAddNeuron(int id)
{
- std::shared_ptr<Neuron> result;
- for (auto node : _hiddenNodes)
- {
- if (node->id() == id)
- {
- result = node;
- break;
- }
- }
- if (result)
- {
- return result;
- }
- for (auto node : _outputs)
+ bool isOutput = _outputs.count(id) > 0;
+ if (isOutput)
{
- if (node->id() == id)
- {
- result = node;
- break;
- }
+ return _outputs.at(id);
}
- if (result)
+
+ bool hiddenNeuronExists = _hiddenNodes.count(id) > 0;
+ if (!hiddenNeuronExists)
{
- return result;
+ _hiddenNodes[id] = std::make_shared<Neuron>(id);
}
-
- result = std::make_shared<Neuron>(id);
- _hiddenNodes.push_back(result);
- return result;
+ return _hiddenNodes.at(id);
}
void NeuralNetwork::setInput(int inputIndex, double activation)
{
- for (auto sensor : _sensors)
- {
- if (sensor->id() == inputIndex)
- {
- sensor->setActivation(activation);
- break;
- }
- }
+ _sensors.at(inputIndex)->setActivation(activation);
}
int NeuralNetwork::findMaxOutputIndex() const
{
double currentMaxActivation = 0;
int currentMaxIndex = 0;
- for (auto output : _outputs)
+ for (std::pair<const int, std::shared_ptr<Neuron>> outputPair : _outputs)
{
- double activation = output->activation();
+ double activation = outputPair.second->activation();
if (activation >= currentMaxActivation)
{
currentMaxActivation = activation;
- currentMaxIndex = output->id();
+ currentMaxIndex = outputPair.first;
}
}
return currentMaxIndex;