KMedian.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * @brief simple constructor
  72. * @param _noClusters the number of clusters to be computed
  73. * @param _distanceMode a string specifying the distance function to be used (default: euclidean)
  74. */
  75. KMedian( const int & _noClusters , const std::string & _distanceMode="euclidean");
  76. /**
  77. * @brief standard constructor
  78. * @param conf config file specifying all relevant variable settings
  79. * @param _section name of the section within the configfile where the settings can be found (default: KMedian)
  80. */
  81. KMedian( const NICE::Config *conf, const std::string & _section = "KMedian");
  82. /** simple destructor */
  83. virtual ~KMedian();
  84. /**
  85. *@brief this is the actual method that performs the clustering for a given set of features
  86. *@author Alexander Freytag
  87. *@date 25-04-2013 (dd-mm-yyyy)
  88. *@param features input features to be clustered
  89. *@param prototypes computed prototypes (cluster medoids) for the given samples
  90. *@param weights number of assignments for every cluster
  91. *@param assignment explicite assignments of features to computed cluster medoids
  92. */
  93. void cluster ( const NICE::VVector & features,
  94. NICE::VVector & prototypes,
  95. std::vector<double> & weights,
  96. std::vector<int> & assignment );
  97. };
  98. } // namespace
  99. #endif