void PrintHeader(UInt8 level, char * text, ...) /* Purpose: Print header to output. */ { char buffer[256]; UInt16 len, half_len; UInt8 color; va_list argp; char * hchr; if (text == NULL) text = ""; va_start(argp, text); vsprintf(buffer, text, argp); va_end(argp); len = StrLen(buffer); if (len > 70) { len = 2; } else { len = 70 - len; } hchr = "="; if (level > 1) hchr = "-"; if (level > 2) hchr = "."; if (G_PRINT_LOG != NULL) { fprintf(G_PRINT_LOG, "<h%d>", level); } color = PrintColor(RED+GREEN); half_len = len / 2; PrintRepeat(hchr, half_len); Print(" "); Print(buffer); Print(" "); PrintRepeat(hchr, len - half_len); PrintColor(color); if (G_PRINT_LOG != NULL) { fprintf(G_PRINT_LOG, "</h%d>", level); } Print("\n"); }
int PerformKMeans(TRAININGSET *pTS, CODEBOOK *pCB, PARTITIONING *pP, int clus, int repeats, int InitMethod, int quietLevel, int useInitial) { PARTITIONING Pnew, Pinit; CODEBOOK CBnew, CBinit; llong distance[BookSize(pTS)]; llong distanceInit[BookSize(pTS)]; double totalTime, error, currError; int i, better, iter, totalIter; SetClock(&totalTime); totalIter = 0; currError = error = 0; if ((clus < 1) || (BookSize(pTS) < clus) || (repeats < 1)) { return 1; /* clustering failed */ } InitializeSolutions(pTS, pCB, pP, &CBnew, &Pnew, &CBinit, &Pinit, distanceInit, clus, useInitial); PrintHeader(quietLevel); /* perform repeats time full K-means */ for (i = 0; i < repeats; i++) { better = iter = 0; GenerateSolution(pTS, &CBnew, &Pnew, &CBinit, &Pinit, distance, distanceInit, InitMethod, useInitial); KMeansIterate(pTS, &CBnew, &Pnew, distance, quietLevel, i, &iter, &totalTime, &error, useInitial); totalIter += iter; /* got better result */ if ((i == 0) || (error < currError)) { CopyCodebook(&CBnew, pCB); CopyPartitioning(&Pnew, pP); currError = error; better = 1; } PrintRepeat(quietLevel, repeats, i, iter, error, GetClock(totalTime), better); } PrintFooterKM(quietLevel, currError, repeats, GetClock(totalTime), totalIter); FreeCodebook(&CBnew); FreePartitioning(&Pnew); FreeCodebook(&CBinit); FreePartitioning(&Pinit); return 0; } /* PerformKmeans() */