From 99d592a71ef05abca277edc5a8d43b441e528531 Mon Sep 17 00:00:00 2001 From: emeric Date: Sat, 9 Mar 2019 14:19:24 +0100 Subject: [PATCH] Add a SOM test --- test/som-test/SomTest.cpp | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 test/som-test/SomTest.cpp diff --git a/test/som-test/SomTest.cpp b/test/som-test/SomTest.cpp new file mode 100644 index 00000000..2c96f54e --- /dev/null +++ b/test/som-test/SomTest.cpp @@ -0,0 +1,99 @@ +#include +#include +#include + +#include "DataNormalizer.hpp" +#include "Network.hpp" + +#define CHECK(pred) assert((pred)) + +using namespace SOM; + +int main(int argc, char* argv[]) +{ + static const InputVector::value_type EPSILON = 0.01; + { + Matrix testMatrix {2, 2, 123}; + assert((testMatrix[{0,0}] == 123)); + assert((testMatrix[{0,1}] == 123)); + assert((testMatrix[{1,0}] == 123)); + assert((testMatrix[{1,1}] == 123)); + } + + { + InputVector test1 {2}; + test1[0] = 0; + test1[1] = 1; + + InputVector test2 {2}; + test2[0] = 1; + test2[1] = 0; + + InputVector test3 {test1}; + test3 += test2; + assert(std::abs(test3[0] - 1) < EPSILON); + assert(std::abs(test3[1] - 1) < EPSILON); + } + + { + Network network {2, 2, 1}; + + const InputVector weights {1, 1}; + std::vector trainData { + { 1, 50 }, + { 1, 100 }, + { 1, 150 }, + { 1, 200 }, + }; + + DataNormalizer normalizer {1}; + normalizer.computeNormalizationFactors(trainData); + for (auto& data: trainData) + normalizer.normalizeData(data); + + network.dump(std::cout); + network.train(trainData, 20); + network.dump(std::cout); + + std::cout << "MEAN dist = " << network.computeRefVectorsDistanceMean() << std::endl; + std::cout << "MEDIAN dist = " << network.computeRefVectorsDistanceMedian() << std::endl; + + auto distFunc {network.getDistanceFunc()}; + + assert((std::abs(distFunc({1, 0}, {1, 1}, weights) - 1) < EPSILON)); + assert((std::abs(distFunc({1, 0}, {1, 2}, weights) - 4) < EPSILON)); + assert((std::abs(distFunc({1, 0}, {1, 0.33}, weights) - distFunc({1, 0.66}, {1, 1.}, weights)) < EPSILON)); + + { + std::set positions; + for (const InputVector& data : trainData) + positions.insert(network.getClosestRefVectorPosition(data)); + assert(positions.size() == 4); + } + + { + Position pos {network.getClosestRefVectorPosition(InputVector{1, 0.66})}; + for (std::size_t i {}; i < 40; ++i) + { + InputVector input {1, 130 + static_cast(i) }; + normalizer.normalizeData(input); + + assert( network.getClosestRefVectorPosition(input) == pos); + } + } + + { + Position pos {network.getClosestRefVectorPosition(InputVector{1, 1})}; + for (std::size_t i {}; i < 40; ++i) + { + InputVector input {1, 180 + static_cast(i) }; + normalizer.normalizeData(input); + + assert( network.getClosestRefVectorPosition(input) == pos); + } + } + + } + + return 0; +}