TestKMedian.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. void TestKMedian::testKMedianPersistent()
  65. {
  66. if (verboseStartEnd)
  67. std::cerr << "================== TestKMedian::testKMedianPersistent ===================== " << std::endl;
  68. Config * conf = new Config;
  69. std::string section ( "KMedian" );
  70. conf->sS( section, "distanceType", "euclidean" );
  71. conf->sI( section, "maxIterations", 200 );
  72. conf->sI( section, "noClusters", 2 );
  73. OBJREC::KMedian kMedian;
  74. kMedian.initFromConfig( conf, section );
  75. // TEST STORING ABILITIES
  76. if ( verbose )
  77. std::cerr << " TEST STORING ABILITIES FOR KMEDIAN" << std::endl;
  78. std::string s_destination_save ( "myKMedian.txt" );
  79. std::filebuf fbOut;
  80. fbOut.open ( s_destination_save.c_str(), ios::out );
  81. std::ostream os (&fbOut);
  82. //
  83. kMedian.store( os );
  84. //
  85. fbOut.close();
  86. // TEST RESTORING ABILITIES
  87. if ( verbose )
  88. std::cerr << " TEST RESTORING ABILITIES FOR KMEDIAN" << std::endl;
  89. OBJREC::KMedian kMedianRestore;
  90. std::string s_destination_load ( "myKMedian.txt" );
  91. std::filebuf fbIn;
  92. fbIn.open ( s_destination_load.c_str(), ios::in );
  93. std::istream is (&fbIn);
  94. //
  95. kMedianRestore.restore( is );
  96. //
  97. fbIn.close();
  98. // currently, we have no possibility to actually verify that the restore-operation was successfull, i.e.,
  99. // it returned an object identical to the stored one.
  100. // However, if we reached this point, at least something went right, so we should be happy...
  101. //don't waste memory
  102. delete conf;
  103. if (verboseStartEnd)
  104. std::cerr << "================== TestKMedian::testKMedianPersistent done ===================== " << std::endl;
  105. }
  106. #endif