KMedian.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @file KMedian.h
  3. * @brief KMedian (aka K-medoid)
  4. * @author Alexander Freytag
  5. * @date 23-04-2013 (dd-mm-yyyy)
  6. */
  7. #ifndef KMEDIANINCLUDE
  8. #define KMEDIANINCLUDE
  9. #include <core/basics/Config.h>
  10. #include <core/vector/Distance.h>
  11. #include <core/vector/MatrixT.h>
  12. #include <core/vector/VectorT.h>
  13. #include "ClusterAlgorithm.h"
  14. namespace OBJREC {
  15. /**
  16. * @class KMedian
  17. * @brief KMedian (aka K-medoid)
  18. * @author Alexander Freytag
  19. * @date 23-04-2013 (dd-mm-yyyy)
  20. */
  21. class KMedian : public ClusterAlgorithm
  22. {
  23. protected:
  24. /************************
  25. *
  26. * protected variables
  27. *
  28. **************************/
  29. //! desired number of clusters
  30. int noClusters;
  31. //! specify which distance to use for calculating assignments
  32. std::string distanceType;
  33. //! the actual distance metric
  34. NICE::VectorDistance<double> *distancefunction;
  35. //! maximum difference between prototype-solutions of two iterations for convergence
  36. double d_minDelta;
  37. //! maximum number of iterations until convergence
  38. int i_maxIterations;
  39. /************************
  40. *
  41. * protected methods
  42. *
  43. **************************/
  44. //! compute the distance between two features using the specified distance metric
  45. double vectorDistance(const NICE::Vector &vector1, const NICE::Vector &vector2, uint distancetype);
  46. //! compute assignments of all given features wrt to the currently known prototypes (cluster medoids) == ~ E-step
  47. double compute_assignments ( const NICE::VVector & features,
  48. const NICE::VVector & prototypes,
  49. std::vector<int> & assignment );
  50. //! compute number of assignments for every currently found cluster
  51. double compute_weights ( const NICE::VVector & features,
  52. std::vector<double> & weights,
  53. std::vector<int> & assignment );
  54. //! compute the difference between prototypes of previous iteration and those currently found
  55. double compute_delta ( const NICE::VVector & oldprototypes,
  56. const NICE::VVector & prototypes );
  57. //! compute (update) prototypes given the current assignments == ~ M-step
  58. int compute_prototypes ( const NICE::VVector & features,
  59. NICE::VVector & prototypes,
  60. std::vector<double> & weights,
  61. const std::vector<int> & assignment );
  62. //! have an initial guess, i.e., randomly pick some features as initial cluster centroids
  63. void initial_guess ( const NICE::VVector & features,
  64. NICE::VVector & prototypes );
  65. //! give additional information for the current iteration
  66. void print_iteration ( int iterations,
  67. NICE::VVector & prototypes,
  68. double delta );
  69. public:
  70. ///////////////////// ///////////////////// /////////////////////
  71. // CONSTRUCTORS / DESTRUCTORS
  72. ///////////////////// ///////////////////// /////////////////////
  73. /**
  74. * @brief default constructor
  75. * @date 12-02-2014 (dd-mm-yyyy )
  76. * @author Alexander Freytag
  77. */
  78. KMedian ( );
  79. /**
  80. * @brief simple constructor
  81. * @param _noClusters the number of clusters to be computed
  82. * @param _distanceMode a string specifying the distance function to be used (default: euclidean)
  83. */
  84. KMedian( const int & _noClusters , const std::string & _distanceMode="euclidean");
  85. /**
  86. * @brief standard constructor
  87. * @param conf config file specifying all relevant variable settings
  88. * @param _section name of the section within the configfile where the settings can be found (default: KMedian)
  89. */
  90. KMedian( const NICE::Config * _conf, const std::string & _confSection = "KMedian");
  91. /** simple destructor */
  92. virtual ~KMedian();
  93. /**
  94. * @brief Jobs previously performed in the config-version of the constructor, read settings etc.
  95. * @author Alexander Freytag
  96. * @date 12-02-2014 ( dd-mm-yyyy )
  97. */
  98. void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "KMedian");
  99. ///////////////////// ///////////////////// /////////////////////
  100. // CLUSTERING STUFF
  101. ///////////////////// ///////////////////// //////////////////
  102. /**
  103. *@brief this is the actual method that performs the clustering for a given set of features
  104. *@author Alexander Freytag
  105. *@date 25-04-2013 (dd-mm-yyyy)
  106. *@param features input features to be clustered
  107. *@param prototypes computed prototypes (cluster medoids) for the given samples
  108. *@param weights number of assignments for every cluster
  109. *@param assignment explicite assignments of features to computed cluster medoids
  110. */
  111. void cluster ( const NICE::VVector & features,
  112. NICE::VVector & prototypes,
  113. std::vector<double> & weights,
  114. std::vector<int> & assignment );
  115. ///////////////////// INTERFACE PERSISTENT /////////////////////
  116. // interface specific methods for store and restore
  117. ///////////////////// INTERFACE PERSISTENT /////////////////////
  118. /**
  119. * @brief Load object from external file (stream)
  120. * @author Alexander Freytag
  121. * @date 12-02-2014 ( dd-mm-yyyy )
  122. */
  123. void restore ( std::istream & is, int format = 0 );
  124. /**
  125. * @brief Save object to external file (stream)
  126. * @author Alexander Freytag
  127. * @date 12-02-2014 ( dd-mm-yyyy )
  128. */
  129. void store ( std::ostream & os, int format = 0 ) const;
  130. /**
  131. * @brief Clear object
  132. * @author Alexander Freytag
  133. * @date 12-02-2014 ( dd-mm-yyyy )
  134. */
  135. void clear ();
  136. };
  137. } // namespace
  138. #endif