📄 lvqx.c
字号:
/*---------------------------------------------------------------------- File : lvqx.c Contents: learning vector quantization execution Author : Christian Borgelt History : 11.03.2003 file created from file lvqx.c 15.05.2003 normalization parameters moved to lvq file 16.08.2003 slight changes in error message output 26.02.2004 source files mlvqx.c and lvqx.c combined----------------------------------------------------------------------*/#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#include "matrix.h"#else /* #ifdef MATVERSION */#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#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"/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "lvqx"#define DESCRIPTION "learning vector quantization execution"#define VERSION "version 1.8 (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 errors on input file */#define E_PATCNT (-10) /* no pattern found */#define E_PATSIZE (-11) /* illegal pattern size */#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", /* E_PATSIZE -11 */ "illegal pattern size %d\n", /* -12 to -15 */ 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 */static SCAN *scan = NULL; /* scanner for network description */#ifdef MATVERSIONstatic TFSCAN *tfscan = NULL; /* table file scanner */static char *patfmt = "%g"; /* format for pattern values */#elsestatic ATTSET *attset = NULL; /* attribute set */static char *vecname = NULL; /* name of vector index field */static char *actname = NULL; /* name of neuron activation field */#endifstatic LVQNET *lvq = NULL; /* learning vector quantization net */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file */static char *outfmt = "%.2f";/* format for neuron activations *//*---------------------------------------------------------------------- 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 (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, k, n; /* loop variable, number of clusters */ if (mode & AS_ATT) { /* if to write the header */ if (vecname) { /* if only to write a vector index */ fputs(vecname, file); /* write the vector index field name */ if (actname) { /* if to add the neuron activation */ fputc(seps[1], file); /* print a separator and */ fputs(actname, file); /* the neuron activation field name */ } } else { /* if to write all membership degrees */ for (n = lvq_cnt(lvq), i = 0; i < n; i++) { fputc(seps[1], file); /* traverse the neurons */ fprintf(file, "%d", i); /* and print a separator */ } /* and the neuron index */ } } else { /* if to write a data tuple */ i = lvq_exec(lvq,NULL,NULL);/* execute the neural network */ if (vecname) { /* if only to write a vector index */ fprintf(file, "%d", i); /* write the vector index */ if (actname) { /* if to add the neuron activation */ fputc(seps[1], file); /* print a separator */ fprintf(file, outfmt, lvq_activ(lvq, i)); } } /* print the neuron activation */ else { /* if to write all neuron activations */ for (n = lvq_cnt(lvq), k = 0; k < n; k++) { fputc(seps[1], file); /* traverse the clusters */ fprintf(file, outfmt, lvq_activ(lvq, k)); } /* print a separator and */ } /* the neuron activation */ }} /* 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_lvq = NULL; /* name of neural network file */ #ifndef MATVERSION char *fn_hdr = NULL; /* name of table header file */ #endif char *fn_pat = NULL; /* name of 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 vecidx = 0; /* flag for vector index output */ int nactiv = 0; /* flag for neuron activation */ int patcnt = 0; /* number of test patterns */ int valcnt = 0; /* number of values in a pattern */ int dimcnt; /* number of dimensions */ int veccnt; /* number of reference vectors */ 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 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] lvqfile patfile outfile\n", argv[0]); #else printf("usage: %s [options] lvqfile " "[-d|-h hdrfile] tabfile outfile\n", argv[0]); #endif printf("%s\n", DESCRIPTION); printf("%s\n", VERSION); #ifdef MATVERSION printf("-c print only the vector index " "(default: neuron activations)\n"); printf("-m print neuron activation with vector index\n"); printf("-p# output format for pattern values " "(default: \"%s\")\n", patfmt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -