diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2015-08-01 22:50:00 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2015-08-01 22:50:00 +0200 |
commit | 2e6ecf423c8228ac8de4badf4fc2d037a876b7ff (patch) | |
tree | 82624e97a3d1ee47db05e320bf0014726f68c09d /src/spacebot.cpp | |
parent | 1f40ce6f3c710d6339d2001a56f401b6a9dcd525 (diff) |
Reading network from file
Diffstat (limited to 'src/spacebot.cpp')
-rw-r--r-- | src/spacebot.cpp | 28 |
1 files changed, 18 insertions, 10 deletions
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; } |