Test best classification algorithm. More...
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <libgen.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <classif.h>
Go to the source code of this file.
Defines | |
#define | _GNU_SOURCE |
GNU extensions. | |
Functions | |
void | show_usage (char *pgm) |
C prototypes. | |
int | main (int argc, char **argv) |
Main program. |
Test best classification algorithm.
Definition in file testbestclassif.c.
#define _GNU_SOURCE |
GNU extensions.
Definition at line 59 of file testbestclassif.c.
int main | ( | int | argc, | |
char ** | argv | |||
) |
Main program.
[in] | argc | Number of command-line arguments. |
[in] | argv | Vector of command-line argument strings. |
Definition at line 102 of file testbestclassif.c.
References alloc_error(), banner(), best_clusters(), and show_usage().
00103 { 00111 int i; 00112 int j; 00113 int neof; 00114 int ndays; 00115 int nclusters; 00116 int nclassif; 00117 int npart; 00118 00119 double *pc_eof_days = NULL; 00120 double *clusters = NULL; 00121 00122 char *fileoutclust = NULL; 00123 FILE *fileoutclust_ptr = NULL; 00124 char *fileoutpc = NULL; 00125 FILE *fileoutpc_ptr = NULL; 00126 00127 const gsl_rng_type *T; 00128 gsl_rng *rng; 00129 00130 /* Print BEGIN banner */ 00131 (void) banner(basename(argv[0]), "1.0", "BEGIN"); 00132 00133 /* Get command-line arguments and set appropriate variables */ 00134 for (i=1; i<argc; i++) { 00135 if ( !strcmp(argv[i], "-h") ) { 00136 (void) show_usage(basename(argv[0])); 00137 (void) banner(basename(argv[0]), "OK", "END"); 00138 return 0; 00139 } 00140 else if ( !strcmp(argv[i], "-o_clust") ) { 00141 fileoutclust = (char *) malloc((strlen(argv[++i])+1) * sizeof(char)); 00142 if (fileoutclust == NULL) alloc_error(__FILE__, __LINE__); 00143 (void) strcpy(fileoutclust, argv[i]); 00144 fileoutclust_ptr = fopen(fileoutclust, "w"); 00145 if (fileoutclust_ptr == NULL) { 00146 (void) fprintf(stderr, "%s: Cannot open file %s for output!\n", __FILE__, fileoutclust); 00147 (void) banner(basename(argv[0]), "ABORT", "END"); 00148 (void) abort(); 00149 } 00150 } 00151 else if ( !strcmp(argv[i], "-o_pc") ) { 00152 fileoutpc = (char *) malloc((strlen(argv[++i])+1) * sizeof(char)); 00153 if (fileoutpc == NULL) alloc_error(__FILE__, __LINE__); 00154 (void) strcpy(fileoutpc, argv[i]); 00155 fileoutpc_ptr = fopen(fileoutpc, "w"); 00156 if (fileoutpc_ptr == NULL) { 00157 (void) fprintf(stderr, "%s: Cannot open file %s for output!\n", __FILE__, fileoutpc); 00158 (void) banner(basename(argv[0]), "ABORT", "END"); 00159 (void) abort(); 00160 } 00161 } 00162 else { 00163 (void) fprintf(stderr, "%s:: Wrong arg %s.\n\n", basename(argv[0]), argv[i]); 00164 (void) show_usage(basename(argv[0])); 00165 (void) banner(basename(argv[0]), "ABORT", "END"); 00166 (void) abort(); 00167 } 00168 } 00169 00170 /* Use simulated 2 EOFs */ 00171 neof = 2; 00172 /* Use simulated 18000 days */ 00173 ndays = 18000; 00174 /* Try 1000 classifications */ 00175 nclassif = 1000; 00176 /* Use 10 clusters */ 00177 nclusters = 10; 00178 /* Try 100 partitions for best classification */ 00179 npart = 30; 00180 00181 /* Initialize random number generator */ 00182 T = gsl_rng_default; 00183 rng = gsl_rng_alloc(T); 00184 (void) gsl_rng_set(rng, time(NULL)); 00185 00186 /* Allocate memory */ 00187 pc_eof_days = (double *) calloc(neof*ndays, sizeof(double)); 00188 if (pc_eof_days == NULL) alloc_error(__FILE__, __LINE__); 00189 clusters = (double *) calloc(neof*nclusters, sizeof(double)); 00190 if (clusters == NULL) alloc_error(__FILE__, __LINE__); 00191 00192 /* Generate a double between 0.0 and 1.0 */ 00193 for (i=0; i<neof-1; i++) { 00194 for (j=0; j<ndays/2; j++) { 00195 pc_eof_days[j+i*ndays] = gsl_ran_gaussian(rng, 1.0) + 3.0; 00196 #if DEBUG >= 9 00197 (void) fprintf(stderr, "eof %d day %d pc_eof_days %lf\n", i, j, pc_eof_days[j+i*ndays]); 00198 #endif 00199 } 00200 for (j=ndays/2; j<ndays; j++) { 00201 pc_eof_days[j+i*ndays] = gsl_ran_gaussian(rng, 1.0) + 6.0; 00202 #if DEBUG >= 9 00203 (void) fprintf(stderr, "eof %d day %d pc_eof_days %lf\n", i, j, pc_eof_days[j+i*ndays]); 00204 #endif 00205 } 00206 } 00207 00208 i = neof-1; 00209 for (j=0; j<ndays; j++) { 00210 pc_eof_days[j+i*ndays] = gsl_ran_gaussian(rng, 1.0) + 4.5; 00211 #if DEBUG >= 9 00212 (void) fprintf(stderr, "eof %d day %d pc_eof_days %lf\n", i, j, pc_eof_days[j+i*ndays]); 00213 #endif 00214 } 00215 00216 (void) gsl_rng_free(rng); 00217 00218 /* Find clusters: test best classification algorithm */ 00219 (void) best_clusters(clusters, pc_eof_days, "euclidian", npart, nclassif, neof, nclusters, ndays); 00220 00221 /* Output data */ 00222 for (i=0; i<neof; i++) 00223 for (j=0; j<nclusters; j++) 00224 (void) fprintf(fileoutclust_ptr, "%d %d %lf\n", i, j, clusters[i+j*neof]); 00225 00226 for (i=0; i<neof; i++) 00227 for (j=0; j<ndays; j++) 00228 (void) fprintf(fileoutpc_ptr, "%d %d %lf\n", i, j, pc_eof_days[j+i*ndays]); 00229 00230 (void) fclose(fileoutclust_ptr); 00231 (void) fclose(fileoutpc_ptr); 00232 00233 /* Free memory */ 00234 (void) free(pc_eof_days); 00235 (void) free(clusters); 00236 (void) free(fileoutclust); 00237 (void) free(fileoutpc); 00238 00239 /* Print END banner */ 00240 (void) banner(basename(argv[0]), "OK", "END"); 00241 00242 return 0; 00243 }
void show_usage | ( | char * | pgm | ) |
C prototypes.
Local Subroutines.
Show usage for program command-line arguments.
[in] | pgm | Program name. |
Definition at line 249 of file testbestclassif.c.