📄 neval.c
字号:
case 'u': optarg = &uvchars; break; case 'n': flags |= AS_WEIGHT; break; case 'd': flags |= AS_DFLT; break; case 'h': optarg = &fn_hdr; break; default : error(E_OPTION, *(--s)); break; } /* set option variables */ if (!*s) break; /* if at end of string, abort loop */ if (optarg) { *optarg = s; optarg = NULL; break; } } } /* get option argument */ else { /* -- if argument is no option */ switch (k++) { /* evaluate non-option */ case 0: fn_net = argv[i]; break; case 1: fn_tab = argv[i]; break; case 2: fn_out = argv[i]; break; default: error(E_ARGCNT); break; } /* note filenames */ } } if (optarg) error(E_OPTARG); /* check option argument */ if ((k < 2) || (k > 3)) error(E_ARGCNT); if (fn_hdr && (strcmp(fn_hdr, "-") == 0)) fn_hdr = ""; /* convert "-" to "" */ if (fn_ref && (strcmp(fn_ref, "-") == 0)) fn_ref = ""; /* convert "-" to "" */ i = (!fn_net || !*fn_net) ? 1 : 0; if (!fn_tab || !*fn_tab) i++; if ( fn_hdr && !*fn_hdr) i++; if ( fn_ref && !*fn_ref) i++;/* check assignments of stdin: */ if (i > 1) error(E_STDIN); /* stdin must not be used twice */ if (fn_hdr) /* set the header file flag */ flags = AS_ATT | (flags & ~AS_DFLT); /* --- read the network --- */ scan = sc_create(fn_net); /* create a scanner */ if (!scan) error((!fn_net || !*fn_net) ? E_NOMEM : E_FOPEN, fn_net); attset = as_create("domains", att_delete); if (!attset) error(E_NOMEM); /* create an attribute set */ fprintf(stderr, "\nreading %s ... ", sc_fname(scan)); if ((sc_nexter(scan) < 0) /* start scanning (get first token) */ || (as_parse(attset, scan, AT_ALL) != 0) || (as_attcnt(attset) <= 0)) /* parse attribute set */ error(E_PARSE, sc_fname(scan)); gramod = gm_parse(attset, scan, GM_AUTO|GM_ALL); if (!gramod || !sc_eof(scan)) /* parse the distributions */ error(E_PARSE, sc_fname(scan)); fprintf(stderr, "[%d attribute(s)] done.\n", as_attcnt(attset)); sc_delete(scan); scan = NULL; /* delete the scanner */ if (gm_check(gramod, -1, 0) < 0) error(E_LOOP); /* check for a condition loop */ gm_setlc(gramod, lcorr); /* set the Laplace correction */ type = gm_type(gramod); /* and get the aggregation mode */ if ((type == GM_POSS) && diffsum) { table = tab_create("opc", attset, (TPL_DELFN*)tpl_delete); if (!table) error(E_NOMEM); /* create a table */ } /* for the one point coverages */ /* --- read and evaluate the table --- */ as_chars(attset, blanks, fldseps, recseps, uvchars); in = io_hdr(attset, fn_hdr, fn_tab, flags, 1); if (!in) error(1); /* read the table header */ f = AS_INST | (flags & ~(AS_ATT|AS_DFLT)); i = ((flags & AS_DFLT) && !(flags & AS_ATT)) ? 0 : as_read(attset, in, f); res[0] = res[1] = res[2] = 0; /* init. the evaluation results */ rgm[2] = 1; /* and set default maximum */ while (i == 0) { /* traverse the tuples */ wgt = as_getwgt(attset); /* get the tuple weight */ if (type == GM_POSS) { /* possibilistic graphical model */ if (diffsum) { /* if to compute diffs, store tuple */ if (tab_tpladd(table, NULL) != 0) error(E_NOMEM); } else { /* if only to sum values */ gm_execx(gramod, rgm); /* execute the graphical model */ res[0] += wgt *rgm[0]; /* and sum the weighted */ res[1] += wgt *rgm[1]; /* evaluation results */ res[2] += wgt *rgm[2]; } } else { /* probabilistic graphical model */ gm_execx(gramod, rgm); /* execute the graphical model */ if (rgm[0] > 0) res[0] += wgt *log(rgm[0]); if (rgm[1] > 0) res[1] += wgt *log(rgm[1]); if (rgm[2] > 0) res[2] += wgt *log(rgm[2]); } if (rgm[2] <= 0) /* if the maximum prob./poss. is 0, */ imposs += wgt; /* count the tuple as impossible */ tplcnt++; tplwgt += wgt; /* increment the tuple counter */ i = as_read(attset, in, f); /* and sum the tuple weight, */ } /* then try to read the next record */ if (i < 0) { /* if an error occurred, */ err = as_err(attset); /* get the error information */ tplcnt += (flags & (AS_ATT|AS_DFLT)) ? 1 : 2; io_error(i, fn_tab, tplcnt, err->s, err->fld, err->exp); error(1); /* print an error message */ } /* and abort the program */ if (in != stdin) fclose(in); /* close the input file */ in = NULL; /* and clear the variable */ fprintf(stderr, "[%d/%g tuple(s)] done.\n", tplcnt, tplwgt); if ((type == GM_POSS) && diffsum) { fprintf(stderr, "computing one point coverages ... "); tab_reduce(table); /* reduce the table and */ tplcnt = tab_tplcnt(table); /* get the reduced number of tuples */ for (i = tplcnt; --i >= 0; ) { tpl = tab_tpl(table, i); /* traverse the table */ tpl_info(tpl)->f = tpl_getwgt(tpl); } /* copy the tuple weights */ if (tab_opc(table, TAB_COND|TAB_NORM) != 0) error(E_NOMEM); /* compute one point coverages */ fprintf(stderr, "done.\n"); /* and print a success message */ fprintf(stderr, "summing possibility differences ... "); for (i = tplcnt; --i >= 0; ) { tpl = tab_tpl(table, i); /* traverse the table again and */ tpl_toas(tpl); /* copy tuple to the attribute set */ gm_execx(gramod, rgm); /* execute the graphical model and */ tab_possx(table, tpl, rtab); /* check the o.p.c. table */ wgt = tpl_info(tpl)->f; /* get the (old) tuple weight and */ res[0] += wgt *fabs(rgm[0] -rtab[0]); res[1] += wgt *fabs(rgm[1] -rtab[1]); res[2] += wgt *fabs(rgm[2] -rtab[2]); if (rgm[2] <= 0) /* if the maximum possibility is 0, */ imposs += wgt; /* count the tuple as impossible */ } fprintf(stderr, "done.\n"); /* of the evaluation results */ } /* and print a success message */ /* --- print the results --- */ if (fn_out && *fn_out) /* if an output file name is given, */ out = fopen(fn_out, "w"); /* open the output file */ else { /* if no output file name is given, */ out = stdout; fn_out = "<stdout>"; } /* write to std. output */ fprintf(stderr, "writing %s ... ", fn_out); if (!out) error(E_FOPEN, fn_out); fprintf(out, "evaluation of %s on %s:\n", fn_net, fn_tab); fprintf(out, "number of attributes : %d\n", gm_attcnt(gramod)); fprintf(out, "number of conditions : %d\n", gm_consum(gramod)); fprintf(out, "number of parameters : %d\n", gm_parsum(gramod)); fprintf(out, "number of tuples : %g\n", tplwgt); fprintf(out, "impossible tuples : %g (%4.1f%%)\n", imposs, (tplwgt > 0) ? (imposs/tplwgt) *100 : 0); if (type == GM_PROB) { /* print aggregated probabilities */ fprintf(out, "log(average prob.) : %g\n", res[0]); fprintf(out, "log(minimum prob.) : %g\n", res[1]); fprintf(out, "log(maximum prob.) : %g\n", res[2]); } else { /* print aggregated possibilities */ fprintf(out, "average poss. sum : %g\n", res[0]); fprintf(out, "minimum poss. sum : %g\n", res[1]); fprintf(out, "maximum poss. sum : %g\n", res[2]); } /* --- compare to reference network --- */ if (fn_ref) { /* if name of reference network given */ scan = sc_create(fn_ref); /* create a scanner */ if (!scan) error((!fn_ref || !*fn_ref) ? E_NOMEM : E_FOPEN, fn_ref); refas = as_create("domains", att_delete); if (!refas) error(E_NOMEM); /* create an attribute set */ fprintf(stderr, "reading %s ... ", sc_fname(scan)); if ((sc_nexter(scan) < 0) /* start scanning (get first token) */ || (as_parse(refas, scan, AT_ALL) != 0) || (as_attcnt(refas) <= 0))/* parse attribute set */ error(E_PARSE, sc_fname(scan)); for (i = as_attcnt(attset); --i >= 0; ) /* clear all */ att_setmark(as_att(attset, i), 0); /* read markers */ refgm = gm_parse(attset, scan, GM_PROB|GM_ALL); if (!refgm || !sc_eof(scan))/* parse the distributions */ error(E_PARSE, sc_fname(scan)); fprintf(stderr, "[%d attribute(s)] done.\n", as_attcnt(refas)); sc_delete(scan); scan = NULL; /* delete the scanner and */ addcnt = miscnt = 0; /* traverse the attributes */ for (i = as_attcnt(attset); --i >= 0; ) { n = 0; /* traverse the conditions */ for (k = gm_concnt(refgm, i); --k >= 0; ) { p = gm_conid(refgm, i, k); for (l = gm_concnt(gramod, i); --l >= 0; ) if (gm_conid(gramod, i, l) == p) break; /* search for the current condition */ if (l < 0) n++; /* if the condition could not be */ } /* found, count it as missing */ miscnt += n; /* sum missing conditions */ addcnt += gm_concnt(gramod, i) -(gm_concnt(refgm, i) -n); } /* sum additional conditions */ fprintf(out, "comparison to %s:\n", fn_ref); fprintf(out, "additional conditions: %d\n", addcnt); fprintf(out, "missing conditions: %d\n", miscnt); } if (out != stdout) { /* if not written to standard output, */ i = fclose(out); out = NULL;/* close the output file */ if (i != 0) error(E_FWRITE, fn_out); } /* check for success and */ fprintf(stderr, "done.\n"); /* print a success message */ /* --- clean up --- */ #ifndef NDEBUG if (table) tab_delete(table, 0); if (refgm) gm_delete(refgm, 0); if (refas) as_delete(refas); /* delete reference model and */ gm_delete(gramod, 1); /* evaluated graphical model */ #endif #ifdef STORAGE showmem("at end of program"); /* check memory usage */ #endif return 0; /* return 'ok' */} /* main() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -