TestKMedian.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifdef NICE_USELIB_CPPUNIT
  2. #include <string>
  3. #include <exception>
  4. #include "TestKMedian.h"
  5. #include <core/basics/Config.h>
  6. #include "vislearning/math/distances/genericDistance.h"
  7. const bool verboseStartEnd = true;
  8. const bool verbose = false;
  9. const std::string distanceType = "euclidean";
  10. using namespace OBJREC;
  11. using namespace NICE;
  12. using namespace std;
  13. CPPUNIT_TEST_SUITE_REGISTRATION( TestKMedian );
  14. void TestKMedian::setUp() {
  15. }
  16. void TestKMedian::tearDown() {
  17. }
  18. void TestKMedian::testKMedianClustering()
  19. {
  20. if (verboseStartEnd)
  21. std::cerr << "================== TestKMedian::testKMedianClustering ===================== " << std::endl;
  22. Config * conf = new Config;
  23. std::string section ( "KMedian" );
  24. conf->sS( section, "distanceType", "euclidean" );
  25. conf->sI( section, "maxIterations", 200 );
  26. conf->sI( section, "noClusters", 2 );
  27. OBJREC::KMedian kMedian ( conf, section );
  28. //create some artificial data
  29. NICE::VVector features;
  30. NICE::Vector x1 (2); x1[0] = 1; x1[1] = 1; features.push_back(x1);
  31. NICE::Vector x2 (2); x2[0] = 4; x2[1] = 1; features.push_back(x2);
  32. NICE::Vector x3 (2); x3[0] = 2; x3[1] = 4; features.push_back(x3);
  33. NICE::Vector x4 (2); x4[0] = 10; x4[1] = 3; features.push_back(x4);
  34. NICE::Vector x5 (2); x5[0] = 8; x5[1] = 3; features.push_back(x5);
  35. NICE::Vector x6 (2); x6[0] = 4; x6[1] = 3; features.push_back(x6);
  36. NICE::Vector x7 (2); x7[0] = 3; x7[1] = 2; features.push_back(x7);
  37. NICE::Vector x8 (2); x8[0] = 1; x8[1] = 3; features.push_back(x8);
  38. NICE::Vector x9 (2); x9[0] = 9; x9[1] = 2; features.push_back(x9);
  39. //cluster data
  40. NICE::VVector prototypes;
  41. std::vector<double> weights;
  42. std::vector<int> assignment;
  43. kMedian.cluster ( features, prototypes, weights, assignment );
  44. //check whether the results fits the ground truth
  45. //NOTE
  46. // If no random initialization is activated, we initially grab x2 and x8.
  47. // After 3 iterations, we should have converged and obtain x5 and x7.
  48. NICE::VectorDistance<double> * distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  49. if ( verbose )
  50. {
  51. std::cerr << " x9: " << x9 << " cl1: " << prototypes[0] << std::endl;
  52. std::cerr << " x7: " << x7 << " cl2: " << prototypes[1] << std::endl;
  53. }
  54. double distX9Cl1 ( distancefunction->calculate( x9, prototypes[0] ) );
  55. double distX7Cl2 ( distancefunction->calculate( x7, prototypes[1] ) );
  56. CPPUNIT_ASSERT_DOUBLES_EQUAL( distX9Cl1, 0.0, 1e-8);
  57. CPPUNIT_ASSERT_DOUBLES_EQUAL( distX7Cl2, 0.0, 1e-8);
  58. std::cerr << " successfull " << std::endl;
  59. //don't waste memory
  60. delete conf;
  61. if (verboseStartEnd)
  62. std::cerr << "================== TestKMedian::testKMedianClustering done ===================== " << std::endl;
  63. }
  64. #endif