summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/brain/neural_link.cpp4
-rw-r--r--src/brain/neural_network.cpp103
-rw-r--r--src/brain/neuron.cpp5
-rw-r--r--src/game_state.cpp5
-rw-r--r--src/spacebot.cpp28
5 files changed, 133 insertions, 12 deletions
diff --git a/src/brain/neural_link.cpp b/src/brain/neural_link.cpp
index d217236..f8d2b29 100644
--- a/src/brain/neural_link.cpp
+++ b/src/brain/neural_link.cpp
@@ -1,7 +1,7 @@
#include "brain/neural_link.h"
-NeuralLink::NeuralLink(double weight)
- :_weight(weight)
+NeuralLink::NeuralLink(std::shared_ptr<NeuralNode> input, double weight)
+ :_input(input), _weight(weight)
{
}
diff --git a/src/brain/neural_network.cpp b/src/brain/neural_network.cpp
index 2d3b902..15eedca 100644
--- a/src/brain/neural_network.cpp
+++ b/src/brain/neural_network.cpp
@@ -3,6 +3,8 @@
NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, int numberOfSensors, int numberOfOutputs)
{
+ _biasNode = std::make_shared<BiasNode>();
+
for (int i=0; i<numberOfSensors; ++i)
{
_sensors.push_back(std::make_shared<Sensor>(i));
@@ -11,6 +13,107 @@ NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, int numberOfSenso
{
_outputs.push_back(std::make_shared<Neuron>(i));
}
+
+ parseFile(std::move(networkConfigFile));
+}
+
+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));
+
+ std::shared_ptr<NeuralNode> source;
+ std::shared_ptr<Neuron> destination;
+ switch (srcType)
+ {
+ case 's':
+ source = findOrAddSensor(srcId);
+ break;
+ 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;
+ }
+
+ addLink(source, destination, weight);
+ }
+
+}
+
+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::shared_ptr<Sensor> NeuralNetwork::findOrAddSensor(int id)
+{
+ std::shared_ptr<Sensor> result;
+ for (auto node : _sensors)
+ {
+ if (node->id() == id)
+ {
+ result = node;
+ break;
+ }
+ }
+ if (!result)
+ {
+ result = std::make_shared<Sensor>(id);
+ _sensors.push_back(result);
+ }
+ return result;
+}
+
+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)
+ {
+ if (node->id() == id)
+ {
+ result = node;
+ break;
+ }
+ }
+ if (result)
+ {
+ return result;
+ }
+
+ result = std::make_shared<Neuron>(id);
+ _hiddenNodes.push_back(result);
+ return result;
}
void NeuralNetwork::setInput(int inputIndex, double activation)
diff --git a/src/brain/neuron.cpp b/src/brain/neuron.cpp
index 8c2e47c..7ea02c6 100644
--- a/src/brain/neuron.cpp
+++ b/src/brain/neuron.cpp
@@ -22,3 +22,8 @@ double Neuron::activation() const
}
return sigmoid(activationSum);
}
+
+void Neuron::addInput(std::shared_ptr<NeuralLink> link)
+{
+ _inputLinks.push_back(link);
+}
diff --git a/src/game_state.cpp b/src/game_state.cpp
index d99ca12..5fed683 100644
--- a/src/game_state.cpp
+++ b/src/game_state.cpp
@@ -88,3 +88,8 @@ void GameState::logState()
std::cout << "Spaceship" << spaceship.coords() << std::endl;
}
}
+
+std::vector<bool> GameState::toBitArray() const
+{
+ return std::vector<bool>();
+}
diff --git a/src/spacebot.cpp b/src/spacebot.cpp
index 5f87df9..1f8f2b8 100644
--- a/src/spacebot.cpp
+++ b/src/spacebot.cpp
@@ -1,11 +1,12 @@
#include "spacebot.h"
#include "move_string_mapper.h"
-#include <random>
+#include "brain/neural_network.h"
#include <fstream>
Spacebot::Spacebot(std::string outputPath)
- : outputFilename(outputPath+"/move.txt"),
- gameState(std::ifstream(outputPath+"/map.txt"))
+ : _outputFilename(outputPath+"/move.txt"),
+ _networkConfigFilename("brain.nn"),
+ _gameState(std::ifstream(outputPath+"/map.txt"))
{
}
@@ -17,17 +18,24 @@ void Spacebot::writeNextMove()
Move Spacebot::chooseMove()
{
- int min = static_cast<int>(Move::NOTHING);
- int max = static_cast<int>(Move::BUILD_SHIELD);
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_int_distribution<int> dis(min, max);
- return static_cast<Move>(dis(gen));
+ auto sensorInputs = _gameState.toBitArray();
+
+ NeuralNetwork network(std::ifstream(_networkConfigFilename),
+ sensorInputs.size(),
+ static_cast<int>(Move::BUILD_SHIELD));
+
+ for (int i=0; i<sensorInputs.size(); ++i)
+ {
+ network.setInput(i, sensorInputs[i] ? 1 : 0);
+ }
+
+ int moveInt = network.findMaxOutputIndex();
+ return static_cast<Move>(moveInt);
}
void Spacebot::writeMove(const Move& move)
{
- std::ofstream resultStream(outputFilename);
+ std::ofstream resultStream(_outputFilename);
resultStream << MoveStringMapper().toString(move) << std::endl;
return;
}