From 2e6ecf423c8228ac8de4badf4fc2d037a876b7ff Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 1 Aug 2015 22:50:00 +0200 Subject: Reading network from file --- src/brain/neural_link.cpp | 4 +- src/brain/neural_network.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++ src/brain/neuron.cpp | 5 +++ src/game_state.cpp | 5 +++ src/spacebot.cpp | 28 +++++++----- 5 files changed, 133 insertions(+), 12 deletions(-) (limited to 'src') 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 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(); + for (int i=0; i(i)); @@ -11,6 +13,107 @@ NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, int numberOfSenso { _outputs.push_back(std::make_shared(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 source; + std::shared_ptr 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 source, std::shared_ptr destination, double weight) +{ + auto link = std::make_shared(source, weight); + destination->addInput(link); +} + +std::shared_ptr NeuralNetwork::findOrAddSensor(int id) +{ + std::shared_ptr result; + for (auto node : _sensors) + { + if (node->id() == id) + { + result = node; + break; + } + } + if (!result) + { + result = std::make_shared(id); + _sensors.push_back(result); + } + return result; +} + +std::shared_ptr NeuralNetwork::findOrAddNeuron(int id) +{ + std::shared_ptr 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(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 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 GameState::toBitArray() const +{ + return std::vector(); +} 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 +#include "brain/neural_network.h" #include 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(Move::NOTHING); - int max = static_cast(Move::BUILD_SHIELD); - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution dis(min, max); - return static_cast(dis(gen)); + auto sensorInputs = _gameState.toBitArray(); + + NeuralNetwork network(std::ifstream(_networkConfigFilename), + sensorInputs.size(), + static_cast(Move::BUILD_SHIELD)); + + for (int i=0; i(moveInt); } void Spacebot::writeMove(const Move& move) { - std::ofstream resultStream(outputFilename); + std::ofstream resultStream(_outputFilename); resultStream << MoveStringMapper().toString(move) << std::endl; return; } -- cgit v1.2.3