📄 rsx.c
字号:
/*---------------------------------------------------------------------- File : rsx.c Contents: rule set execution Authors : Christian Borgelt History : 06.07.2002 file created from file dtx.c 23.04.2003 missing AS_MARKED added for second reading 16.08.2003 slight changes in error message output 09.11.2004 execution time output added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <math.h>#include <string.h>#include <time.h>#include <assert.h>#ifndef SC_SCAN#define SC_SCAN#endif#include "scan.h"#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#ifndef RS_DESC#define RS_DESC#endif#ifndef RS_PARSE#define RS_PARSE#endif#include "io.h"#include "rules.h"#ifdef STORAGE#include "storage.h"#endif/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "rsx"#define DESCRIPTION "rule set execution"#define VERSION "version 1.5 (2004.11.09) " \ "(c) 2002-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) /* cannot open file */#define E_FREAD (-3) /* read error on file */#define E_FWRITE (-4) /* write error on file */#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_EMPTY (-10) /* empty rule set */#define E_HEAD (-11) /* more than one head attribute */#define E_CLASS (-10) /* missing class (head attribute) */#define E_UNKNOWN (-12) /* unknown error */#define SEC_SINCE(t) ((clock()-(t)) /(double)CLOCKS_PER_SEC)/*---------------------------------------------------------------------- Type Definitions----------------------------------------------------------------------*/typedef struct { /* --- prediction result --- */ ATT *att; /* target attribute */ int type; /* type of the target attribute */ char *n_pred; /* name of prediction column */ int w_pred; /* width of prediction column */ INST pred; /* predicted value */ char *n_supp; /* name of support column */ int w_supp; /* width of support column */ float supp; /* support of prediction */ char *n_conf; /* name of confidence column */ int w_conf; /* width of confidence column */ float conf; /* confidence of prediction */} RESULT; /* (prediction result) *//*---------------------------------------------------------------------- Constants----------------------------------------------------------------------*/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_EMPTY -10 */ "rule set is empty\n", /* E_HEAD -11 */ "more than one head attribute in rule set\n", /* E_CLASS -12 */ "missing attribute \"%s\" in file %s\n", /* E_UNKNOWN -13 */ "unknown error\n"};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/const char *prgname = NULL; /* program name for error messages */static SCAN *scan = NULL; /* scanner */static RULESET *ruleset = NULL; /* rule set to execute */static ATTSET *attset = NULL; /* attribute set */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file */static RESULT res = { /* prediction result */ NULL, AT_SYM, /* target attribute and its type */ "rs", 0, { 0 }, /* data for prediction column */ NULL, 0, 0.0F, /* data for support column */ NULL, 0, 0.0F }; /* data for confidence column *//*---------------------------------------------------------------------- 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 error message */ va_end(args); /* end argument evaluation */ } #ifndef NDEBUG if (ruleset) rs_delete(ruleset, 0); if (attset) as_delete(attset); /* clean up memory */ if (scan) sc_delete(scan); /* and close files */ 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 infout (ATTSET *set, FILE *file, int mode, CCHAR *seps){ /* --- write additional information */ static char *cfmt = "%.2f"; /* format for confidence output */ int i, k; /* loop variables, buffers */ CCHAR *pred; /* name of prediction column/value */ char *supp, *conf; /* support and confidence */ char b0[32], b1[32], b2[32]; /* buffers for output */ if (mode & AS_ATT) { /* if to write header */ cfmt = (res.type == AT_SYM) ? "%.2f" : "%g"; pred = res.n_pred; /* get names of prediction column, */ supp = res.n_supp; /* support column, and */ conf = res.n_conf; /* confidence column */ if (mode & AS_ALIGN) { /* if to align fields */ if ((mode & AS_WEIGHT) || supp || conf) { i = att_valwd(res.att, 0); k = (int)strlen(pred); res.w_pred = (i > k) ? i : k; } /* compute width of class column */ if (supp && ((mode & AS_WEIGHT) || conf)) { k = (int)strlen(supp); res.w_supp = (k > 4) ? k : 4; } /* compute width of support column */ if (conf && (mode & AS_WEIGHT)) { k = (int)strlen(conf); res.w_conf = (k > 4) ? k : 4; } } } /* compute width of errors column */ else { /* if to write normal record */ if (res.type == AT_SYM) { /* if the target att. is symbolic */ if (res.pred.i <= UV_SYM) pred = "?"; else pred = att_valname(res.att, res.pred.i); } else { /* if the target att. is numeric */ if (res.pred.f <= UV_FLT) pred = "?"; else { sprintf(b0, "%g", res.pred.f); pred = b0; } } /* print the predicted value */ if (res.n_supp) sprintf(b1, "%.1f", res.supp); if (res.n_conf) sprintf(b2, cfmt, res.conf); supp = b1; conf = b2; /* get and format field contents */ } /* and get pointers to buffers */ k = fprintf(file, pred); /* write the prediction result */ for (i = res.w_pred -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ if (res.n_supp) { /* if to write the support */ fputc(seps[1], file); /* write a field separator and */ k = fprintf(file, supp); /* the support of the prediction */ for (i = res.w_supp -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ } if (res.n_conf) { /* if to write the confidence */ fputc(seps[1], file); /* write a field separator and the */ k = fprintf(file, conf); /* confidence of the prediction */ for (i = res.w_conf -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ }} /* infout() *//*--------------------------------------------------------------------*/int main (int argc, char* argv[]){ /* --- main function */ int i, k = 0; /* loop variables, counter */ char *s; /* to traverse options */ char **optarg = NULL; /* option argument */ char *fn_hdr = NULL; /* name of table header file */ char *fn_tab = NULL; /* name of table file */ char *fn_rs = NULL; /* name of rule set file */ char *fn_out = NULL; /* name of output file */ char *blanks = NULL; /* blanks */ char *fldseps = NULL; /* field separators */ char *recseps = NULL; /* record separators */ char *uvchars = NULL; /* unknown value characters */ int inflags = 0, f; /* table file read flags */ int outflags = AS_ATT; /* table file write flags */ int adapt = 0; /* flag for rule set adaptation */ int tplcnt = 0; /* number of tuples */ double tplwgt = 0.0; /* weight of tuples */ double errcnt = 0.0; /* number of misclassifications */ int maxlen = 0; /* maximal output line length */ int attid; /* loop variable for attributes */ float wgt; /* tuple/instantiation weight */ RULE *rule; /* to traverse the rules */ INST *inst; /* instance of target attribute */ double diff, d; /* difference to true value */ TFSERR *err; /* error information */ clock_t t; /* timer for measurements */ 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 */ printf("usage: %s [options] rsfile " "[-d|-h hdrfile] tabfile [outfile]\n", argv[0]); printf("%s\n", DESCRIPTION);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -