25 #define LOG(verbose, text) { \ 26 vector<ostream*> &outputs = (verbose? gVerboseLogOutputs : gLogOutputs); \ 27 if (outputs.size() > 0) { \ 28 ostringstream string_stream; \ 29 string_stream << text; \ 30 for (int i = 0; i < (int)outputs.size(); i++) \ 31 *(outputs[i]) << string_stream.str(); \ 46 return double(clock()) / CLOCKS_PER_SEC;
55 double start_time,
double *min_time,
double *max_time,
56 double *total_time,
Scalar *best_centers,
int *best_assignment) {
63 for (
int iteration = 0; !is_done; iteration++) {
65 is_done = (iteration > 0 && new_cost >= (1 - kEpsilon) * old_cost);
67 LOG(
true,
"Completed iteration #" << (iteration+1) <<
", cost=" << new_cost <<
"..." << endl);
72 LOG(
false,
"Completed run: cost=" << old_cost <<
" (" << this_time <<
" seconds)" << endl);
75 if (*min_cost < 0 || old_cost < *min_cost) {
77 if (best_assignment != 0)
79 if (best_centers != 0)
84 if (*max_cost < old_cost) *max_cost = old_cost;
85 *total_cost += old_cost;
86 if (*min_time < 0 || *min_time > this_time)
87 *min_time = this_time;
88 if (*max_time < this_time) *max_time = this_time;
89 *total_time += this_time;
94 double min_time,
double max_time,
double total_time,
int num_attempts) {
95 LOG(
false,
"Aggregate info over " << num_attempts <<
" runs:" << endl);
96 LOG(
false,
" Cost: min=" << min_cost <<
" average=" << (total_cost / num_attempts)
97 <<
" max=" << max_cost << endl);
98 LOG(
false,
" Time: min=" << min_time <<
" average=" << (total_time / num_attempts)
99 <<
" max=" << max_time << endl << endl);
104 Scalar *ret_centers,
int *ret_assignment) {
108 LOG(
false,
"Running k-means..." << endl);
110 LOG(
false,
"Done preprocessing..." << endl);
114 int *unused_centers = (
int*)malloc(
sizeof(
int)*
n);
115 KM_ASSERT(centers != 0 && unused_centers != 0);
116 Scalar min_cost = -1, max_cost = -1, total_cost = 0;
117 double min_time = -1, max_time = -1, total_time = 0;
121 memset(centers +
n*d, -1, (k-d)*
sizeof(
Scalar));
126 for (
int attempt = 0; attempt < attempts; attempt++) {
130 for (
int i = 0; i <
n; i++)
131 unused_centers[i] = i;
132 int num_unused_centers =
n;
133 for (
int i = 0; i < k; i++) {
136 unused_centers[j] = unused_centers[num_unused_centers];
140 RunKMeansOnce(tree,
n, k, d,
points, centers, &min_cost, &max_cost, &total_cost, start_time,
141 &min_time, &max_time, &total_time, ret_centers, ret_assignment);
143 LogMetaStats(min_cost, max_cost, total_cost, min_time, max_time, total_time, attempts);
146 free(unused_centers);
153 Scalar *ret_centers,
int *ret_assignment) {
157 LOG(
false,
"Running k-means++..." << endl);
159 LOG(
false,
"Done preprocessing..." << endl);
164 Scalar min_cost = -1, max_cost = -1, total_cost = 0;
165 double min_time = -1, max_time = -1, total_time = 0;
168 for (
int attempt = 0; attempt < attempts; attempt++) {
175 RunKMeansOnce(tree,
n, k, d,
points, centers, &min_cost, &max_cost, &total_cost, start_time,
176 &min_time, &max_time, &total_time, ret_centers, ret_assignment);
178 LogMetaStats(min_cost, max_cost, total_cost, min_time, max_time, total_time, attempts);
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
Scalar RunKMeans(int n, int k, int d, Scalar *points, int attempts, Scalar *ret_centers, int *ret_assignment)
GLsizei const GLfloat * points
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define LOG(verbose, text)
Scalar RunKMeansPlusPlus(int n, int k, int d, Scalar *points, int attempts, Scalar *ret_centers, int *ret_assignment)
Scalar DoKMeansStep(int k, Scalar *centers, int *assignment) const
void ClearKMeansLogging()
static vector< ostream * > gVerboseLogOutputs
#define KM_ASSERT(expression)
Scalar SeedKMeansPlusPlus(int k, Scalar *centers) const
void AddKMeansLogging(std::ostream *out, bool verbose)
static void RunKMeansOnce(const KmTree &tree, int n, int k, int d, Scalar *points, Scalar *centers, Scalar *min_cost, Scalar *max_cost, Scalar *total_cost, double start_time, double *min_time, double *max_time, double *total_time, Scalar *best_centers, int *best_assignment)
static double GetSeconds()
static vector< ostream * > gLogOutputs
void LogMetaStats(Scalar min_cost, Scalar max_cost, Scalar total_cost, double min_time, double max_time, double total_time, int num_attempts)