diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | include/brain/neural_link.h | 4 | ||||
-rw-r--r-- | src/brain/neural_link.cpp | 4 | ||||
-rw-r--r-- | src/game_state.cpp | 45 | ||||
-rw-r--r-- | test/neural_network.cpp | 23 |
5 files changed, 57 insertions, 21 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9cbfa61..fd6a32d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.2.2) project(Spacebot) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -Wall -Wpedantic -Wextra -fprofile-use") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wpedantic -Wextra") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/include/brain/neural_link.h b/include/brain/neural_link.h index 5a63ba4..f3d12a6 100644 --- a/include/brain/neural_link.h +++ b/include/brain/neural_link.h @@ -7,10 +7,10 @@ class NeuralLink { public: - NeuralLink(std::shared_ptr<NeuralNode> input, double weight); + NeuralLink(std::weak_ptr<NeuralNode> input, double weight); double weightedActivation() const; private: - std::shared_ptr<NeuralNode> _input; + std::weak_ptr<NeuralNode> _input; double _weight; }; diff --git a/src/brain/neural_link.cpp b/src/brain/neural_link.cpp index f8d2b29..c61feb4 100644 --- a/src/brain/neural_link.cpp +++ b/src/brain/neural_link.cpp @@ -1,11 +1,11 @@ #include "brain/neural_link.h" -NeuralLink::NeuralLink(std::shared_ptr<NeuralNode> input, double weight) +NeuralLink::NeuralLink(std::weak_ptr<NeuralNode> input, double weight) :_input(input), _weight(weight) { } double NeuralLink::weightedActivation() const { - return _input->activation()*_weight; + return _input.lock()->activation()*_weight; } diff --git a/src/game_state.cpp b/src/game_state.cpp index eb02005..c6e5d70 100644 --- a/src/game_state.cpp +++ b/src/game_state.cpp @@ -104,7 +104,7 @@ void GameState::logState() const } } -int getPyramidOffset(int bottomWidth, int x, int y, int resultIfLeft, int resultIfRight) +int getPyramidOffset(int bottomWidth, int maxWidth, int x, int y, int resultIfLeft, int resultIfRight) { int currentRowWidth = bottomWidth; int currentRowOffset = 0; @@ -112,6 +112,10 @@ int getPyramidOffset(int bottomWidth, int x, int y, int resultIfLeft, int result { currentRowOffset += currentRowWidth; currentRowWidth += 2; + if (currentRowWidth > maxWidth) + { + currentRowWidth = maxWidth; + } } if (x > currentRowWidth/2) @@ -136,15 +140,15 @@ int getRectangularOffset(int width, int x, int y) std::vector<bool> GameState::toBitArray() const { - std::vector<bool> result(172); + std::vector<bool> result(102); - int alienOffset = 0; //Aliens are 0 to 100 - int bulletOffset = 101; //Bullets are 101 to 135 - int shieldOffset = 136; //Shields are 136 to 150 - int missileOffset = 151; //Missile info is 151 to 152 - int positionOffset = 153; //Position is 153 to 168 - int existingBuildingsOffset = 169; //Existing buildings is 169 to 170 - int canBuildOffset = 171; //Can build here is 171 + int alienOffset = 0; //Aliens are 0 to 70 + int bulletOffset = 71; //Bullets are 71 to 90 + int shieldOffset = 91; //Shields are 91 to 93 + int missileOffset = 94; //Missile info is 94 to 95 + int positionOffset = 96; //Position is 96 to 98 + int existingBuildingsOffset = 99; //Existing buildings is 99 to 100 + int canBuildOffset = 101; //Can build here is 101 int playerX = GAME_WIDTH/2; int playerY = GAME_AREA_LINES-3; @@ -161,27 +165,27 @@ std::vector<bool> GameState::toBitArray() const continue; } - result.at(alienOffset + getPyramidOffset(3, alien.x()-playerX, playerY-1-alien.y(), 99, 100)) = true; + result.at(alienOffset + getPyramidOffset(3, 9, alien.x()-playerX, playerY-1-alien.y(), 69, 70)) = true; } for (auto const& bullet : _bullets) { - if (bullet.y() >= playerY || bullet.y() < playerY-5 || bullet.x() > playerX+3 || bullet.x() < playerX-3) + if (bullet.y() >= playerY || bullet.y() < playerY-4 || bullet.x() > playerX+2 || bullet.x() < playerX-2) { continue; } - result.at(bulletOffset + getRectangularOffset(7, bullet.x()-playerX, playerY-1-bullet.y())) = true; + result.at(bulletOffset + getRectangularOffset(5, bullet.x()-playerX, playerY-1-bullet.y())) = true; } for (auto const& shield : _shields) { - if (shield.y() < playerY-3 || shield.x() < playerX-2 || shield.x() > playerX+2) + if (shield.y() < playerY-3 || shield.x() < playerX-1 || shield.x() > playerX+1) { continue; } - result.at(shieldOffset + getRectangularOffset(5, shield.x()-playerX, playerY-1-shield.y())) = true; + result.at(shieldOffset + shield.x()-playerX+1) = true; } if (_missiles.size() > 0) @@ -195,7 +199,18 @@ std::vector<bool> GameState::toBitArray() const if (_playerSpaceship) { - result.at(positionOffset + playerX-2) = true; + if (playerX <= GAME_WIDTH/3) + { + result.at(positionOffset) = true; + } + else if (playerX >= GAME_WIDTH*2/3) + { + result.at(positionOffset+2) = true; + } + else + { + result.at(positionOffset+1) = true; + } } result.at(canBuildOffset) = true; diff --git a/test/neural_network.cpp b/test/neural_network.cpp index dd029d6..801f0ec 100644 --- a/test/neural_network.cpp +++ b/test/neural_network.cpp @@ -28,7 +28,7 @@ SCENARIO("network is read from istream") file << "s0 n3 0.5" << std::endl; file << "n3 n1 1" << std::endl; file << "b0 n0 0.4" << std::endl; - file << "b0 n3 0.5" << std:: endl; + file << "b0 n3 0.5" << std::endl; WHEN("the network is initialized") { @@ -41,4 +41,25 @@ SCENARIO("network is read from istream") } } } + + GIVEN("a valid recurrant config file") + { + std::stringstream file; + file << "s0 n3 0.5" << std::endl; + file << "n3 n1 1" << std::endl; + file << "b0 n0 0.4" << std::endl; + file << "b0 n3 0.5" << std::endl; + file << "n1 n3 0.5" << std::endl; + + WHEN("the network converges") + { + NeuralNetwork network(std::move(file), 1, 3); + + THEN("the network is constructed correctly") + { + network.setInput(0, 1); + REQUIRE(network.findMaxOutputIndex() == 1); + } + } + } } |