RandomClustering.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @file RandomClustering.h
  3. * @brief Clustering by randomly picking some samples from the set of features as representatives
  4. * @author Alexander Freytag
  5. * @date 03-06-2013 (dd-mm-yyyy)
  6. */
  7. #ifndef RANDOMCLUSTERERNINCLUDE
  8. #define RANDOMCLUSTERERNINCLUDE
  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 RandomClustering
  17. * @brief Clustering by randomly picking some samples from the set of features as representatives
  18. * @author Alexander Freytag
  19. * @date 03-06-2013 (dd-mm-yyyy)
  20. */
  21. class RandomClustering : 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. /************************
  36. *
  37. * protected methods
  38. *
  39. **************************/
  40. //! compute assignments of all given features wrt to the currently known prototypes (cluster medoids) == ~ E-step
  41. double compute_assignments ( const NICE::VVector & features,
  42. const NICE::VVector & prototypes,
  43. std::vector<int> & assignment );
  44. //! compute number of assignments for every currently found cluster
  45. double compute_weights ( const NICE::VVector & features,
  46. std::vector<double> & weights,
  47. std::vector<int> & assignment );
  48. //! compute (update) prototypes given the current assignments == ~ M-step
  49. int compute_prototypes ( const NICE::VVector & features,
  50. NICE::VVector & prototypes,
  51. std::vector<double> & weights,
  52. const std::vector<int> & assignment );
  53. public:
  54. ///////////////////// ///////////////////// /////////////////////
  55. // CONSTRUCTORS / DESTRUCTORS
  56. ///////////////////// ///////////////////// /////////////////////
  57. /**
  58. * @brief default constructor
  59. * @date 13-02-2014 (dd-mm-yyyy )
  60. * @author Alexander Freytag
  61. */
  62. RandomClustering ( );
  63. /**
  64. * @brief simple constructor
  65. * @param[in] _noClasses the number of clusters to be computed
  66. * @param[in] _distanceMode a string specifying the distance function to be used (default: euclidean)*
  67. * @date 03-06-2013 (dd-mm-yyyy)
  68. */
  69. RandomClustering( const int & _noClasses , const std::string & _distanceMode="euclidean" );
  70. /**
  71. * @brief standard constructor
  72. * @param[in] conf config file specifying all relevant variable settings
  73. * @param[in] _section name of the section within the configfile where the settings can be found (default: RandomClustering)
  74. * @date 03-06-2013 (dd-mm-yyyy)
  75. */
  76. RandomClustering( const NICE::Config * _conf, const std::string & _confSection = "RandomClustering");
  77. /** simple destructor */
  78. virtual ~RandomClustering();
  79. /**
  80. * @brief Jobs previously performed in the config-version of the constructor, read settings etc.
  81. * @author Alexander Freytag
  82. * @date 13-02-2014 ( dd-mm-yyyy )
  83. */
  84. void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "RandomClustering");
  85. ///////////////////// ///////////////////// /////////////////////
  86. // CLUSTERING STUFF
  87. ///////////////////// ///////////////////// //////////////////
  88. /**
  89. * @brief this is the actual method that performs the clustering for a given set of features
  90. * @author Alexander Freytag
  91. * @date 03-06-2013 (dd-mm-yyyy)
  92. * @param features input features to be clustered
  93. * @param prototypes computed prototypes (randomly chosen) for the given samples
  94. * @param weights number of assignments for every cluster
  95. * @param assignment explicite assignments of features to computed cluster medoids
  96. */
  97. void cluster ( const NICE::VVector & features,
  98. NICE::VVector & prototypes,
  99. std::vector<double> & weights,
  100. std::vector<int> & assignment );
  101. ///////////////////// INTERFACE PERSISTENT /////////////////////
  102. // interface specific methods for store and restore
  103. ///////////////////// INTERFACE PERSISTENT /////////////////////
  104. /**
  105. * @brief Load object from external file (stream)
  106. * @author Alexander Freytag
  107. * @date 13-02-2014 ( dd-mm-yyyy )
  108. */
  109. void restore ( std::istream & is, int format = 0 );
  110. /**
  111. * @brief Save object to external file (stream)
  112. * @author Alexander Freytag
  113. * @date 13-02-2014 ( dd-mm-yyyy )
  114. */
  115. void store ( std::ostream & os, int format = 0 ) const;
  116. /**
  117. * @brief Clear object
  118. * @author Alexander Freytag
  119. * @date 13-02-2014 ( dd-mm-yyyy )
  120. */
  121. void clear ();
  122. };
  123. } // namespace
  124. #endif