📄 neval.c
字号:
/*---------------------------------------------------------------------- File : neval.c contents: network evaluation Author : Christian Borgelt History : 02.03.1997 file created 08.09.1997 minor improvements 02.02.1998 option -u (unknown values characters) added 23.06.1998 adapted to modified attset functions 18.04.1999 simplified using the new module 'io' 30.04.1999 one point coverage computation added 15.12.1999 option -c (compare to reference network) added 25.11.2001 adapted to modified scanner functions 22.01.2002 adapted to modified gramod and ptree modules 24.02.2002 check for multiple assignment of stdin improved 04.04.2002 adapted to new functions gm_consum and gm_parsum 16.01.2003 optional approximation for poss. maximum removed 16.08.2003 slight changes in error message output----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <assert.h>#include <math.h>#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#ifndef GM_PARSE#define GM_PARSE#endif#include "table.h"#include "gramod.h"#include "io.h"#ifdef STORAGE#include "storage.h"#endif/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "neval"#define DESCRIPTION "probabilistic/possibilistic network evaluation"#define VERSION "version 1.10 (2004.08.12) " \ "(c) 1997-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_LOOP (-10) /* condition loop */#define E_UNKNOWN (-11) /* unknown error *//*---------------------------------------------------------------------- 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_LOOP -10 */ "condition loop detected\n", /* E_UNKNOWN -11 */ "unknown error\n"};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/const char *prgname = NULL; /* program name for error messages */static SCAN *scan = NULL; /* scanner */static ATTSET *attset = NULL; /* attribute set */static GRAMOD *gramod = NULL; /* graphical model */static ATTSET *refas = NULL; /* attribute set of reference network */static GRAMOD *refgm = NULL; /* reference graphical model */static TABLE *table = NULL; /* table to evaluate model on */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file *//*---------------------------------------------------------------------- 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 if (table) tab_delete(table, 0); if (refgm) gm_delete(refgm, 0); if (refas) as_delete(refas); if (gramod) gm_delete(gramod, 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() *//*--------------------------------------------------------------------*/int main (int argc, char *argv[]){ /* --- main function */ int i, k = 0; /* loop variables, counter */ int n, l, p, f = 0; /* loop variables, buffers */ 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_net = NULL; /* name of network file */ char *fn_out = NULL; /* name of output file */ char *fn_ref = NULL; /* name of reference network file */ char *blanks = NULL; /* blanks */ char *fldseps = NULL; /* field separators */ char *recseps = NULL; /* record separators */ char *uvchars = NULL; /* unknown value characters */ int type = GM_AUTO; /* type of graphical model */ int diffsum = 0; /* compute sum of poss. degree diffs. */ int flags = AS_NOXATT|AS_NOXVAL; /* table file read flags */ int tplcnt = 0; /* number of tuples */ double tplwgt = 0.0; /* weight of tuples */ double imposs = 0; /* number of impossible tuples */ float lcorr = 0; /* Laplace correction */ double res[3]; /* aggregation of evaluation results */ double rgm[3], rtab[3]; /* tuple specific evaluation results */ TUPLE *tpl; /* to traverse the tuples */ float wgt; /* instantiation weight */ int addcnt, miscnt; /* additional and missing conditions */ 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 */ printf("usage: %s [options] ", argv[0]); printf( "netfile [-d|-h hdrfile] tabfile [outfile]\n"); printf("%s\n", DESCRIPTION); printf("%s\n", VERSION); printf("-c <fn> compare to reference network in file <fn>\n"); printf("-s compute sum of possibility differences\n"); printf("-L# Laplace correction (default: %g)\n", lcorr); printf("-b/f/r# blanks, field and record separators " "(default: \" \\t\", \" \\t\", \"\\n\")\n"); printf("-u# unknown value characters (default: \"?\")\n"); printf("-n number of tuple occurrences in last field\n"); printf("netfile file containing network structure to evaluate\n"); printf("-d use default table header " "(field names = field numbers)\n"); printf("-h read table header (field names) from hdrfile\n"); printf("hdrfile file containing table header (field names)\n"); printf("tabfile table file to read " "(first record containing field names)\n"); printf("outfile file to write evaluation results to\n"); return 0; /* print a usage message */ } /* and abort the program */ /* --- evaluate arguments --- */ for (i = 1; i < argc; i++) { /* traverse arguments */ s = argv[i]; /* get option argument */ if (optarg) { *optarg = s; optarg = NULL; continue; } if ((*s == '-') && *++s) { /* -- if argument is an option */ while (1) { /* traverse characters */ switch (*s++) { /* evaluate option */ case 'c': optarg = &fn_ref; break; case 's': diffsum = 1; break; case 'L': lcorr = (float)strtod(s, &s); break; case 'b': optarg = &blanks; break; case 'f': optarg = &fldseps; break; case 'r': optarg = &recseps; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -