From 8eebf1079fbed2848ee47cf990f5def5926a0c1f Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 21:24:15 +0200 Subject: Refile for merging repos --- 2015-spacebot/test/neural_network.cpp | 122 ++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 2015-spacebot/test/neural_network.cpp (limited to '2015-spacebot/test/neural_network.cpp') 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 + +#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 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); + } + } + } +} -- cgit v1.2.3