best_clusters.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include <classif.h>
00055
00057 int
00058 best_clusters(double *best_clusters, double *pc_eof_days, char *type, int npart, int nclassif, int neof, int ncluster, int ndays) {
00072 double min_meandistval;
00073 double meandistval;
00074 double maxdistval;
00075 double minval;
00076 double dist_bary;
00077 double val;
00078
00079 double *tmpcluster = NULL;
00080 double *testclusters = NULL;
00081
00082 int min_cluster = -1;
00083 int min_partition = -1;
00084
00085 int part;
00086 int part1;
00087 int part2;
00088 int clust;
00089 int clust1;
00090 int clust2;
00091 int eof;
00092
00093 int niter;
00094 int niter_min;
00095
00096 (void) fprintf(stdout, "%s:: BEGIN: Find the best partition of clusters.\n", __FILE__);
00097
00098 niter_min = 99999;
00099
00100
00101 tmpcluster = (double *) calloc(neof*ncluster, sizeof(double));
00102 if (tmpcluster == NULL) alloc_error(__FILE__, __LINE__);
00103 testclusters = (double *) calloc(neof*ncluster*npart, sizeof(double));
00104 if (testclusters == NULL) alloc_error(__FILE__, __LINE__);
00105
00106
00107 (void) fprintf(stdout, "%s:: Generating %d partitions of clusters.\n", __FILE__, npart);
00108 for (part=0; part<npart; part++) {
00109 #if DEBUG >= 1
00110 (void) fprintf(stdout, "%s:: Generating %d/%d partition of clusters.\n", __FILE__, part+1, npart);
00111 #endif
00112 niter = generate_clusters(tmpcluster, pc_eof_days, type, nclassif, neof, ncluster, ndays);
00113 if (niter < niter_min) niter_min = niter;
00114 for (clust=0; clust<ncluster; clust++)
00115 for (eof=0; eof<neof; eof++)
00116 testclusters[part+eof*npart+clust*npart*neof] = tmpcluster[eof+clust*neof];
00117 }
00118
00121 min_meandistval = 9999999999.9;
00122 min_partition = -1;
00123
00124 (void) fprintf(stdout, "%s:: Computing distance between each partitions of clusters.\n", __FILE__);
00125 for (part1=0; part1<npart; part1++) {
00126 #if DEBUG >= 1
00127 (void) fprintf(stdout, "%s:: Partition %d/%d.\n", __FILE__, part1+1, npart);
00128 #endif
00129 meandistval = 0.0;
00130 for (part2=0; part2<npart; part2++) {
00131
00132
00133 if (part1 != part2) {
00134
00135 maxdistval = -9999999999.9;
00136
00137 for (clust1=0; clust1<ncluster; clust1++) {
00138
00139
00140 minval = 9999999999.9;
00141 min_cluster = -1;
00142 for (clust2=0; clust2<ncluster; clust2++) {
00143
00144 if ( !strcmp(type, "euclidian") ) {
00145
00146 dist_bary = 0.0;
00147 for (eof=0; eof<neof; eof++) {
00148 val = testclusters[part2+eof*npart+clust1*npart*neof] - testclusters[part1+eof*npart+clust2*npart*neof];
00149 dist_bary += (val * val);
00150 }
00151
00152 dist_bary = sqrt(dist_bary);
00153 }
00154 else {
00155 (void) fprintf(stderr, "best_clusters: ABORT: Unknown distance type=%s!!\n", type);
00156 (void) abort();
00157 }
00158
00159
00160 if (dist_bary < minval) {
00161 minval = dist_bary;
00162 min_cluster = clust2;
00163 }
00164 }
00165
00166 if (min_cluster == -1) {
00167 (void) fprintf(stderr, "best_clusters: ABORT: Error in algorithm. Cannot find best cluster!\n");
00168 (void) abort();
00169 }
00170
00171
00172 if (minval > maxdistval)
00173 maxdistval = minval;
00174 }
00175
00176
00177 meandistval += maxdistval;
00178 }
00179 }
00180
00181 meandistval = meandistval / (double) (npart-1);
00182
00183 if (meandistval < min_meandistval) {
00184 min_meandistval = meandistval;
00185 min_partition = part1;
00186 }
00187 }
00188
00189 if (min_partition == -1) {
00190
00191 (void) fprintf(stderr, "best_clusters: ABORT: Error in algorithm. Cannot find best partition!\n");
00192 (void) abort();
00193 }
00194
00195
00196 (void) fprintf(stdout, "%s:: Save best partition of clusters.\n", __FILE__);
00197 for (clust=0; clust<ncluster; clust++)
00198 for (eof=0; eof<neof; eof++)
00199 best_clusters[eof+clust*neof] = testclusters[min_partition+eof*npart+clust*npart*neof];
00200
00201
00202 (void) free(tmpcluster);
00203 (void) free(testclusters);
00204
00205 (void) fprintf(stdout, "%s:: END: Find the best partition of clusters. Partition %d selected.\n", __FILE__, min_partition);
00206
00207 return niter_min;
00208 }