summaryrefslogtreecommitdiff
path: root/2015-spacebot/test/neural_network.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2015-spacebot/test/neural_network.cpp')
-rw-r--r--2015-spacebot/test/neural_network.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/2015-spacebot/test/neural_network.cpp b/2015-spacebot/test/neural_network.cpp
new file mode 100644
index 0000000..418f5c4
--- /dev/null
+++ b/2015-spacebot/test/neural_network.cpp
@@ -0,0 +1,122 @@
+#include "catch.hpp"
+#include <sstream>
+
+#include "brain/neural_network.h"
+
+SCENARIO("network is read from istream")
+{
+ GIVEN("an empty config file")
+ {
+ std::stringstream file;
+ file << "" << std::endl;
+
+ WHEN ("the network is initialized")
+ {
+ NeuralNetwork network(std::move(file), 1, 2);
+
+ THEN("the specified number of inputs and outputs are created")
+ {
+ REQUIRE(network.numberOfSensors() == 1);
+ REQUIRE(network.numberOfOutputs() == 2);
+ }
+ }
+ }
+
+ GIVEN("a valid 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;
+
+ WHEN("the network is initialized")
+ {
+ NeuralNetwork network(std::move(file), 1, 3);
+
+ THEN("the network is constructed correctly")
+ {
+ REQUIRE(network.linkExists("s0", "n3", 0.5));
+ REQUIRE(network.linkExists("n3", "n1", 1));
+ REQUIRE(network.linkExists("b0", "n0", 0.4));
+ REQUIRE(network.linkExists("b0", "n3", 0.5));
+ }
+ THEN("The network evaluates correctly")
+ {
+ network.setInput(0, 1);
+ REQUIRE(network.findMaxOutputIndex() == 1);
+ }
+ }
+ }
+
+ 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);
+ }
+ }
+ }
+
+ GIVEN("my handcoded config file")
+ {
+ std::stringstream file;
+ file << "b0 n0 20" << std::endl;
+ file << "s55 n3 10" << std::endl;
+ file << "b0 n4 -10" << std::endl;
+ file << "s59 n4 -50" << std::endl;
+ file << "s60 n4 20" << std::endl;
+ file << "b0 n6 10" << std::endl;
+ file << "s51 n6 -10" << std::endl;
+ file << "s53 n6 -10" << std::endl;
+ file << "n3 n0 -20" << std::endl;
+ file << "n4 n0 -20" << std::endl;
+ file << "n6 n0 -20" << std::endl;
+ file << "n3 n4 -20" << std::endl;
+ file << "n6 n3 -20" << std::endl;
+ file << "n6 n4 -20" << std::endl;
+
+ WHEN("the netwok is constructed")
+ {
+ std::vector<bool> sensors(61);
+
+ NeuralNetwork network(std::move(file), sensors, 7);
+ THEN("it is constructred correctly")
+ {
+ REQUIRE(network.linkExists("b0", "n0", 20));
+ REQUIRE(network.linkExists("s55", "n3", 10));
+ REQUIRE(network.linkExists("b0", "n4", -10));
+ REQUIRE(network.linkExists("s59", "n4", -50));
+ REQUIRE(network.linkExists("s60", "n4", 20));
+ REQUIRE(network.linkExists("b0", "n6", 10));
+ REQUIRE(network.linkExists("s51", "n6", -10));
+ REQUIRE(network.linkExists("s53", "n6", -10));
+ REQUIRE(network.linkExists("n3", "n0", -20));
+ REQUIRE(network.linkExists("n4", "n0", -20));
+ REQUIRE(network.linkExists("n6", "n0", -20));
+ REQUIRE(network.linkExists("n3", "n4", -20));
+ REQUIRE(network.linkExists("n6", "n3", -20));
+ REQUIRE(network.linkExists("n6", "n4", -20));
+ }
+
+ THEN("it has the right number of nodes and sensors")
+ {
+ REQUIRE(network.numberOfSensors() == 61);
+ REQUIRE(network.numberOfOutputs() == 7);
+ REQUIRE(network.numberOfNeurons() == 7);
+ }
+ }
+ }
+}