⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clx.c

📁 聚类算法全集以及内附数据集
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------  File    : clx.c  Contents: probabilistic and fuzzy cluster induction  Author  : Christian Borgelt  History : 30.01.2003 file created            31.01.2003 cluster index output added            11.03.2003 hard cluster assigment added            17.05.2003 normalization parameters moved to cluster file            16.08.2003 slight changes in error message output            25.02.2004 source files clx.c und mclx.c combined            22.04.2004 bug concerning additional field reading fixed            28.01.2006 conditional compilation simplified----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <math.h>#include <time.h>#include <assert.h>#ifdef MATVERSION#ifndef MAT_READ#define MAT_READ#endif#else  /* #ifdef MATVERSION */#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#include "io.h"#endif /* #ifdef MATVERSION */#ifndef CLS_PARSE#define CLS_PARSE#endif#ifndef CLS_EXTFN#define CLS_EXTFN#endif#include "cluster.h"/*----------------------------------------------------------------------  Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME     "clx"#define DESCRIPTION "probabilistic and fuzzy cluster execution"#define VERSION     "version 2.7 (2006.01.28)         " \                    "(c) 2003-2006   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 errors on input file */#define E_PATCNT   (-10)        /* no pattern found */#define E_UNKNOWN  (-18)        /* unknown error *//*----------------------------------------------------------------------  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_PATCNT  -10 */  "no pattern in file %s\n",  /*    -11 to -15 */  NULL, NULL, NULL, NULL, NULL,  /* E_VALUE   -16 */  "file %s, record %d: "                         "illegal value %s in field %d\n",  /* E_FLDCNT  -17 */  "file %s, record %d: "                         "%s%d field(s) instead of %d\n",  /* E_UNKNOWN -18 */  "unknown error\n",};/*----------------------------------------------------------------------  Global Variables----------------------------------------------------------------------*/const  char   *prgname = NULL;  /* program name for error messages */#ifdef MATVERSIONstatic TFSCAN *tfscan  = NULL;  /* table file scanner */static char   *patfmt  = "%g";  /* format for pattern values */#elsestatic ATTSET *attset  = NULL;  /* attribute set */static char   *clsname = NULL;  /* name of cluster index field */static char   *msdname = NULL;  /* name of membership degree field */#endifstatic SCAN   *scan    = NULL;  /* scanner for cluster set desc. */static CLSET  *clset   = NULL;  /* cluster set */static FILE   *in      = NULL;  /* input  file */static FILE   *out     = NULL;  /* output file */static char   *msdfmt  = "%.2f";/* format for membership degrees *//*----------------------------------------------------------------------  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_delete(clset);   /* and close files */  #ifdef MATVERSION  if (tfscan) tfs_delete(tfscan);  #else  if (attset) as_delete(attset);  #endif  if (scan)   sc_delete(scan);  if (in  && (in  != stdin))  fclose(in);  if (out && (out != stdout)) fclose(out);  #endif  exit(code);                   /* abort the program */}  /* error() *//*--------------------------------------------------------------------*/#ifndef MATVERSIONstatic void infout (ATTSET *set, FILE *file, int mode, const char *seps){                               /* --- write additional information */  int i, n;                     /* loop variable, number of clusters */  if (mode & AS_ATT) {          /* if to write the header */    if (clsname) {              /* if only to write a cluster index */      fputs(clsname, file);     /* write the cluster index field name */      if (msdname) {            /* if to add the membership degree */        fputc(seps[1], file);   /* print a separator */        fputs(msdname, file);   /* and the membership degree */      } }    else {                      /* if to write all membership degrees */      for (n = cls_clscnt(clset), i = 0; i < n; i++) {        fputc(seps[1], file);   /* traverse the clusters */        fprintf(file, "%d", i); /* and print a separator */      }                         /* and the cluster index */    } }  else {                        /* if to write a data tuple */    i = cls_exec(clset, NULL, NULL);  /* execute the cluster set */    if (clsname) {              /* if only to write a cluster index */      fprintf(file, "%d", i);   /* write the cluster index */      if (msdname) {            /* if to add the membership degree */        fputc(seps[1], file);   /* print a separator */        fprintf(file, msdfmt, cls_msdeg(clset, i));      } }                       /* print the membership degree */    else {                      /* if to write all membership degrees */      for (n = cls_clscnt(clset), i = 0; i < n; i++) {        fputc(seps[1], file);   /* traverse the clusters */        fprintf(file, msdfmt, cls_msdeg(clset, i));      }                         /* print a separator */    }                           /* and the membership degree */  }}  /* infout() */#endif/*--------------------------------------------------------------------*/int main (int argc, char *argv[]){                               /* --- main function */  int    i, k = 0;              /* loop variables, counters */  char   *s;                    /* to traverse options */  char   **optarg = NULL;       /* option argument */  char   *fn_cls  = NULL;       /* name of cluster set file */  #ifndef MATVERSION  char   *fn_hdr  = NULL;       /* name of table header file */  #endif  char   *fn_in   = NULL;       /* name of input  table file */  char   *fn_out  = NULL;       /* name of output table file */  char   *blanks  = NULL;       /* blanks */  char   *fldseps = NULL;       /* field  separators */  char   *recseps = NULL;       /* record separators */  #ifdef MATVERSION  char   seps[4]  = "  \n";     /* separator characters */  int    clsidx   = 0;          /* flag for cluster index output */  int    msdeg    = 0;          /* flag for membership degree */  int    patcnt   = 0;          /* number of test patterns */  double *pat;                  /* to traverse the patterns */  #else  int    inflags  = 0;          /* table file read  flags */  int    outflags = AS_ATT, f;  /* table file write flags */  int    tplcnt   = 0;          /* number of tuples */  double tplwgt   = 0.0;        /* weight of tuples */  #endif  int    attcnt;                /* number of attributes */  int    clscnt;                /* number of clusters */  TFSERR *err;                  /* error information */  prgname = argv[0];            /* get program name for error msgs. */  /* --- print startup/usage message --- */  if (argc > 1) {               /* if arguments are given */    fprintf(stderr, "%s - %s\n", argv[0], DESCRIPTION);    fprintf(stderr, VERSION); } /* print a startup message */  else {                        /* if no argument is given */    #ifdef MATVERSION    printf("usage: %s [options] clsfile patfile outfile\n", argv[0]);    #else    printf("usage: %s [options] clsfile "                    "[-d|-h hdrfile] tabfile outfile\n", argv[0]);    #endif    printf("%s\n", DESCRIPTION);    printf("%s\n", VERSION);    #ifdef MATVERSION    printf("-c       print only the cluster index "                    "(default: membership degrees)\n");    printf("-m       print membership degree with cluster index "                    "(only together with -c)\n");    printf("-p#      output format for pattern values     "                    "(default: \"%s\")\n", patfmt);    #else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -