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

📄 lvqt.c

📁 LVQ - Learning Vector Quantization Demonstration Download xlvq Linux executable (128 kb) wlvq.ex
💻 C
📖 第 1 页 / 共 3 页
字号:
/*----------------------------------------------------------------------  File    : lvqt.c  Contents: learning vector quantization training  Author  : Christian Borgelt  History : 11.03.2003 file created from file cli.c            15.05.2003 normalization modes added (option -j)            05.06.2003 multiple parameter retrieval simplified            07.06.2003 prototype size scaling factor added            15.08.2003 adapted to new module radfn            16.08.2003 slight changes in error message output            26.02.2004 source files mlvqt.c and lvqt.c combined----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <float.h>#include <time.h>#include <assert.h>#include "params.h"#ifdef MATVERSION#ifndef MAT_READ#define MAT_READ#endif#include "matrix.h"#else  /* #ifdef MATVERSION */#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#ifndef TAB_RDWR#define TAB_RDWR#endif#include "io.h"#endif /* #ifdef MATVERSION */#ifndef SC_SCAN#define SC_SCAN#endif#include "scan.h"#ifndef LVQ_PARSE#define LVQ_PARSE#endif#ifndef MATVERSION#ifndef LVQ_EXTFN#define LVQ_EXTFN#endif#endif#include "lvq.h"#ifdef STORAGE#include "storage.h"#endif/*----------------------------------------------------------------------  Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME     "lvqt"#define DESCRIPTION "learning vector quantization training"#define VERSION     "version 1.13 (2004.09.29)        " \                    "(c) 2003-2004   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)        /* illegal init. or norm. mode */#define E_PARAM    (-11)        /* illegal parameter value */#define E_LRATE    (-12)        /* illegal learning rate */#define E_METHOD   (-13)        /* illegal variance update method */#define E_PATCNT   (-14)        /* pattern file is empty */#define E_UNKNOWN  (-18)        /* unknown error *//*----------------------------------------------------------------------  Type Definitions----------------------------------------------------------------------*/typedef struct {                /* --- initialization mode info. --- */  int  code;                    /* code of initialization mode */  char *name;                   /* name of initialization mode */  char *desc;                   /* description */} MODEINFO;                     /* (initialization 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 init. or norm. mode %s\n",  /* E_PARAM   -11 */  "illegal parameter value %g\n",  /* E_LRATE   -12 */  "illegal learning rate %g\n",  /* E_METHOD  -13 */  "illegal variance update method %s\n",  /* E_PATCNT  -14 */  "pattern file is empty\n",  /*           -15 */  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"};static const MODEINFO initab[] = { /* table of initialization modes */  { LVQ_CENTER,  "center",  "center of the data space"                },  { LVQ_UNIFORM, "uniform", "sample from a uniform distribution"      },  { LVQ_DIAG,    "diag",    "points on the diagonal of the data space"},  { LVQ_LATIN,   "latin",   "latin hypercube sampling"                },  { LVQ_POINTS,  "points",  "randomly chosen points of the dataset"   },  { -1,          NULL,      NULL   /* sentinel */                     },};static const MODEINFO nrmtab[] = { /* table of normalization modes */  { LVQ_NONE,    "none",    "no normalization"       },  { LVQ_SUM1,    "sum1",    "normalize to sum 1"     },  { LVQ_MAX1,    "max1",    "normalize to maximum 1" },  { LVQ_HARD,    "hard",    "hard assignment"        },  { -1,          NULL,      NULL   /* sentinel */    },};static const MODEINFO updtab[] = { /* table of update modes */  { LVQ_DIST,     "dist",   "average distance to center"   },  { LVQ_SQUARE,   "square", "average squared distance"     },  { LVQ_VAR,      "var",    "isotropic variance"           },  { LVQ_WEIGHT,   "weight", "weight of assigned patterns", },  { -1,            NULL,    NULL   /* sentinel */                   },};/*----------------------------------------------------------------------  Global Variables----------------------------------------------------------------------*/const  char   *prgname = NULL;  /* program name for error messages */static SCAN   *scan    = NULL;  /* scanner for initial neural network */#ifdef MATVERSIONstatic TFSCAN *tfscan  = NULL;  /* table file scanner */static MATRIX *matrix  = NULL;  /* matrix of training patterns */static FILE   *in      = NULL;  /* input  file */#elsestatic ATTSET *attset  = NULL;  /* attribute set */static TABLE  *table   = NULL;  /* table of training patterns */#endifstatic LVQNET *lvq     = NULL;  /* learning vector quantization net */static FILE   *out     = NULL;  /* output file *//*----------------------------------------------------------------------  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 (lvq)    lvq_delete(lvq);       /* and close files */  #ifdef MATVERSION  if (matrix) mat_delete(matrix);  if (tfscan) tfs_delete(tfscan);  #else  if (table)  tab_delete(table, 0);  if (attset) as_delete(attset);  #endif  if (scan)   sc_delete(scan);  #ifdef MATVERSION  if (in  && (in  != stdin))  fclose(in);  #endif  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 */

⌨️ 快捷键说明

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