📄 cli.c
字号:
/*---------------------------------------------------------------------- File : cli.c Contents: probabilistic and fuzzy cluster induction Author : Christian Borgelt History : 2001.09.15 file created from file mlpt.c 2002.09.09 neural network update methods added 2003.01.30 data normalization moved to cluster.c 2003.03.20 bug in function msfnpar fixed 2003.05.15 options -n, -g, -z changed, options -q, -j added 2003.05.16 noise clustering added (option -y) 2003.06.07 cluster size scaling factor added 2003.08.16 slight changes in error message output 2004.02.25 source files cli.c amd mcli.c combined 2004.03.18 normalization parameters added 2004.03.19 cluster weight regularization added 2004.04.06 parameters for competitive learning added 2004.07.13 normalization of center vectors added 2004.07.27 option -C added (epochs to update centers only) 2007.01.10 adapted to changed cluster interface 2007.02.14 adapted to changed module tabscan 2007.03.16 table and matrix version combined (option -M) 2007.07.07 bug with excluded target fixed (AM_MARKED) 2007.10.10 evaluation of attribute directions added 2008.03.01 redesign for feature weighting completed 2008.04.14 option -K added (neural network update) 2008.04.17 option -P added (for faster experiments) 2008.04.22 sorting the clusters by their centers added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <float.h>#include <time.h>#include <assert.h>#include "params.h"#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#ifndef TAB_RDWR#define TAB_RDWR#endif#include "io.h"#ifndef MAT_READ#define MAT_READ#endif#ifndef CLS_EXTFN#define CLS_EXTFN#endif#ifndef CLS_PARSE#define CLS_PARSE#endif#include "cluster.h"#ifdef STORAGE#include "storage.h"#endif/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "cli"#define DESCRIPTION "probabilistic and fuzzy cluster induction"#define VERSION "version 3.1 (2008.04.25) " \ "(c) 2001-2008 Christian Borgelt"/* --- error codes --- */#define OK 0 /* no error */#define E_NONE 0 /* no error */#define E_NOMEM (-1) /* not enough memory */#define E_FOPEN (-2) /* file open failed */#define E_FREAD (-3) /* file read failed */#define E_FWRITE (-4) /* file write failed */#define E_OPTION (-5) /* unknown option */#define E_OPTARG (-6) /* missing option argument */#define E_ARGCNT (-7) /* wrong number of arguments */#define E_STDIN (-8) /* double assignment of stdin */#define E_PARSE (-9) /* parse error */#define E_MODE (-10) /* invalid init. or norm. mode */#define E_RFNPAR (-11) /* invalid radial function parameter */#define E_NRMPAR (-12) /* invalid normalization parameter */#define E_MFEXP (-13) /* invalid membership exponent */#define E_METHOD (-14) /* invalid update method */#define E_MODIFY (-15) /* invalid update modifier */#define E_TPLCNT (-18) /* data file is empty */#define E_MOMENT (-19) /* invalid momentum coefficient */#define E_RADIUS (-20) /* invalid initial radius */#define E_REGPAR (-21) /* invalid regularization parameter */#define E_LRATE (-22) /* invalid learning rate */#define E_UPDPAR (-23) /* invalid update parameter */#define E_EPOCHS (-24) /* invalid number of epochs */#define E_TARGET (-25) /* unknown target attribute */#define E_UNKNOWN (-26) /* unknown error */#define SEC_SINCE(t) ((clock()-(t)) /(double)CLOCKS_PER_SEC)/*---------------------------------------------------------------------- Type Definitions----------------------------------------------------------------------*/typedef struct { /* --- mode information --- */ int code; /* code of mode */ char *name; /* name of mode */ char *desc; /* description */} MODEINFO; /* (mode information) *//*---------------------------------------------------------------------- Constants----------------------------------------------------------------------*/static const char *errmsgs[] = { /* error messages */ /* E_NONE 0 */ "no error\n", /* E_NOMEM -1 */ "not enough memory\n", /* E_FOPEN -2 */ "cannot open file %s\n", /* E_FREAD -3 */ "read error on file %s\n", /* E_FWRITE -4 */ "write error on file %s\n", /* E_OPTION -5 */ "unknown option -%c\n", /* E_OPTARG -6 */ "missing option argument\n", /* E_ARGCNT -7 */ "wrong number of arguments\n", /* E_STDIN -8 */ "double assignment of standard input\n", /* E_PARSE -9 */ "parse error(s) on file %s\n", /* E_MODE -10 */ "unknown mode/method %s\n", /* E_RFNPAR -11 */ "invalid radial function parameter %g\n", /* E_NRMPAR -12 */ "invalid normalization parameter %g\n", /* E_MFEXP -13 */ "invalid membership/weight exponent %g\n", /* E_METHOD -14 */ "unknown parameter update method %s\n", /* E_MODIFY -15 */ "unknown parameter update modifier %s\n", /* E_VALUE -16 */ "file %s, record %d: " "invalid value %s in field %d\n", /* E_FLDCNT -17 */ "file %s, record %d: " "%s%d field(s) instead of %d\n", /* E_TPLCNT -18 */ "data file is empty\n", /* E_MOMENT -19 */ "invalid momentum coefficient %g\n", /* E_RADIUS -20 */ "invalid initial radius %g\n", /* E_REGPAR -21 */ "invalid regularization parameter %g\n", /* E_LRATE -22 */ "invalid learning rate %g\n", /* E_UPDPAR -23 */ "invalid update parameter %g\n", /* E_EPOCHS -24 */ "invalid number of epochs: %d\n", /* E_TARGET -25 */ "unknown target attribute %s\n", /* E_UNKNOWN -26 */ "unknown error\n"};static const MODEINFO initab[] = { /* table of initialization modes */ { CLS_CENTER, "center", "center of the data space" }, { CLS_UNIFORM, "uniform", "samples from a uniform distribution" }, { CLS_DIAG, "diag", "points on the diagonal of the data space"}, { CLS_LATIN, "latin", "latin hypercube sampling" }, { CLS_POINTS, "points", "randomly chosen points of the data set" }, { -1, NULL, NULL /* sentinel */ },};static const MODEINFO nrmtab[] = { /* table of normalization modes */ { CLS_NONE, "none", "no normalization" }, { CLS_SUM1, "sum1", "normalize to sum 1" }, { CLS_MAX1, "max1", "normalize to maximum 1" }, { CLS_HARD, "hard", "hard assignment" }, { -1, NULL, NULL /* sentinel */ },};static const MODEINFO updtab[] = { /* table of update methods *//*{ CLS_GRADIENT, "gradient", "gradient based method" }, */ { CLS_ALTOPT, "altopt", "alternating optimization" }, { CLS_COMPLRN, "complrn", "competitive learning" }, { -1, NULL, NULL /* sentinel */ },};static const MODEINFO modtab[] = { /* table of update modifiers */ { CLS_NONE, "none", "standard update" }, { CLS_EXPAND, "expand", "expand change by a factor" }, { CLS_MOMENTUM, "momentum", "update with momentum term" }, { CLS_ADAPTIVE, "adaptive", "self-adaptive change factor" }, { CLS_RESILIENT, "resilient", "resilient update" }, { CLS_QUICK, "quick", "quickprop analog" }, { -1, NULL, NULL /* sentinel */ },};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/const char *prgname = NULL; /* program name for error messages */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file */static ATTSET *attset = NULL; /* attribute set */static ATTMAP *attmap = NULL; /* attribute map */static TABLE *table = NULL; /* table of training tuples */static MATRIX *matrix = NULL; /* matrix of training tuples */static TABSCAN *tscan = NULL; /* table scanner */static SCAN *scan = NULL; /* scanner for initial cluster set */static CLSET *clset = NULL; /* cluster set */static char *buf = NULL; /* buffer for file names *//*---------------------------------------------------------------------- Random Number Functions----------------------------------------------------------------------*/#ifdef DRAND48 /* if library for drand48() available */extern void srand48 (long seed);extern double drand48 (void); /* use drand48 functions */#define dseed(s) srand48((long)(s))#define drand drand48#else /* if only standard rand() available */#define dseed(s) srand((unsigned)(s))static double drand (void){ return rand()/(RAND_MAX +1.0); }#endif/*---------------------------------------------------------------------- Main Functions----------------------------------------------------------------------*/static void error (int code, ...){ /* --- print error message */ va_list args; /* list of variable arguments */ const char *msg; /* error message */ assert(prgname); /* check the program name */ if (code < E_UNKNOWN) code = E_UNKNOWN; if (code < 0) { /* if to report an error, */ msg = errmsgs[-code]; /* get the error message */ if (!msg) msg = errmsgs[-E_UNKNOWN]; fprintf(stderr, "\n%s: ", prgname); va_start(args, code); /* get variable arguments */ vfprintf(stderr, msg, args);/* print the error message */ va_end(args); /* end argument evaluation */ } #ifndef NDEBUG /* clean up memory */ if (clset) cls_deletex(clset, 0); /* and close files */ if (matrix) mat_delete(matrix); if (table) tab_delete(table, 0); if (attmap) am_delete(attmap); if (attset) as_delete(attset); if (scan) sc_delete(scan); if (tscan) ts_delete(tscan); if (buf) free(buf); if (in && (in != stdin)) fclose(in); if (out && (out != stdout)) fclose(out); #endif #ifdef STORAGE showmem("at end of program"); /* check memory usage */ #endif exit(code); /* abort the program */} /* error() *//*--------------------------------------------------------------------*/static void help (void){ /* --- print help on init. modes */ int i; /* loop variable */ fprintf(stderr, "\n"); /* terminate startup message */ printf("list of initialization modes (option -i#):\n"); printf("(choice of initial positions of the cluster centers;\n"); printf("a random offset can be added with the option -o#)\n"); printf(" name initialization mode\n"); for (i = 0; initab[i].name; i++) /* list of initialization modes */ printf(" %-10s %s\n", initab[i].name, initab[i].desc); printf("list of membership normalization modes (option -E#)\n"); printf(" name membership normalization mode\n"); for (i = 0; nrmtab[i].name; i++) /* list of normalization modes */ printf(" %-10s %s\n", nrmtab[i].name, nrmtab[i].desc); printf("list of parameter update methods (option -a#)\n"); printf(" name parameter update method\n"); for (i = 0; updtab[i].name; i++) /* list of update methods */ printf(" %-10s %s\n", updtab[i].name, updtab[i].desc); printf("list of parameter update modifiers (option -A#)\n"); printf(" name parameter update method\n"); for (i = 0; modtab[i].name; i++) /* list of update modifiers */ printf(" %-10s %s\n", modtab[i].name, modtab[i].desc); exit(0); /* abort the program */} /* help() *//*--------------------------------------------------------------------*/static int code (const MODEINFO *tab, const char *name){ /* --- get code of processing mode */ for ( ; tab->name; tab++) /* look up name in table */ if (strcmp(tab->name, name) == 0) return tab->code; /* return the code of the init. mode */ return -1; /* or an error indicator */} /* code() *//*--------------------------------------------------------------------*/int main (int argc, char *argv[]){ /* --- main function */ int i, k = 0, n; /* loop variables, buffers */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -