/* walk -- walk through a tree, evaluating nodes */ extern List *walk(Tree *tree0, Binding *binding0, int flags) { Tree *volatile tree = tree0; Binding *volatile binding = binding0; SIGCHK(); top: if (tree == NULL) return true; switch (tree->kind) { case nConcat: case nList: case nQword: case nVar: case nVarsub: case nWord: case nThunk: case nLambda: case nCall: case nPrim: { List *list; Ref(Binding *, bp, binding); list = glom(tree, binding, TRUE); binding = bp; RefEnd(bp); return eval(list, binding, flags); } case nAssign: return assign(tree->u[0].p, tree->u[1].p, binding); case nLet: case nClosure: Ref(Tree *, body, tree->u[1].p); binding = letbindings(tree->u[0].p, binding, binding, flags); tree = body; RefEnd(body); goto top; case nLocal: return local(tree->u[0].p, tree->u[1].p, binding, flags); case nFor: return forloop(tree->u[0].p, tree->u[1].p, binding, flags); case nMatch: return matchpattern(tree->u[0].p, tree->u[1].p, binding); case nExtract: return extractpattern(tree->u[0].p, tree->u[1].p, binding); default: panic("walk: bad node kind %d", tree->kind); } NOTREACHED; }
int main(int argc, char **arg) { clock_t start, endc, endt; double elapsedc, elapsedt; extmanager MyManager; DdNode *bdd; bddfileheader fileheader; int i, ivarcnt, code; gradientpair tvalue; double probability = -1.0; char *varpattern; varpattern = NULL; code = -1; params = loadparam(argc, arg); if (params.errorcnt > 0) { printhelp(argc, arg); for (i = 0; i < params.errorcnt; i++) { fprintf(stderr, "Error: not known or error at parameter %s.\n", arg[params.error[i]]); } return -1; } if (params.online == 0 && params.loadfile == -1) { printhelp(argc, arg); fprintf(stderr, "Error: you must specify a loading file.\n"); return -1; } if (params.method != 0 && arg[params.method][0] != 'g' && arg[params.method][0] != 'p' && arg[params.method][0] != 'o' && arg[params.method][0] != 'l') { printhelp(argc, arg); fprintf(stderr, "Error: you must choose a calculation method beetween " "[p]robability, [g]radient, [l]ine search, [o]nline.\n"); return -1; } if (params.debug) DEBUGON; RAPIDLOADON; SETMAXBUFSIZE(params.maxbufsize); #ifndef _WIN32 signal(SIGINT, termhandler); if (params.ppid != NULL) { signal(SIGALRM, pidhandler); alarm(5); } else { signal(SIGALRM, handler); alarm(params.timeout); } #endif if (params.online) { MyManager.manager = simpleBDDinit(0); MyManager.t = HIGH(MyManager.manager); MyManager.f = LOW(MyManager.manager); MyManager.varmap = InitNamedVars(1, 0); bdd = OnlineGenerateBDD(MyManager.manager, &MyManager.varmap); ivarcnt = GetVarCount(MyManager.manager); } else { fileheader = ReadFileHeader(arg[params.loadfile]); switch (fileheader.filetype) { case BDDFILE_SCRIPT: if (params.inputfile == -1) { printhelp(argc, arg); fprintf(stderr, "Error: an input file is necessary for this type of " "loading file.\n"); return -1; } MyManager.manager = simpleBDDinit(fileheader.varcnt); MyManager.t = HIGH(MyManager.manager); MyManager.f = LOW(MyManager.manager); MyManager.varmap = InitNamedMultiVars( fileheader.varcnt, fileheader.varstart, fileheader.bvarcnt); if (LoadMultiVariableData(MyManager.manager, MyManager.varmap, arg[params.inputfile]) == -1) return -1; start = clock(); bdd = FileGenerateBDD(MyManager.manager, MyManager.varmap, fileheader); endc = clock(); elapsedc = ((double)(endc - start)) / CLOCKS_PER_SEC; printf("elapsed_construction(%lf).\n", elapsedc); ivarcnt = fileheader.varcnt; break; case BDDFILE_NODEDUMP: if (params.inputfile == -1) { printhelp(argc, arg); fprintf(stderr, "Error: an input file is necessary for this type of " "loading file.\n"); return -1; } MyManager.manager = simpleBDDinit(fileheader.varcnt); MyManager.t = HIGH(MyManager.manager); MyManager.f = LOW(MyManager.manager); MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); bdd = LoadNodeDump(MyManager.manager, MyManager.varmap, fileheader.inputfile); ivarcnt = fileheader.varcnt; break; default: fprintf(stderr, "Error: not a valid file format to load.\n"); return -1; break; } } #ifndef _WIN32 alarm(0); #endif // problem specifics if (bdd != NULL) { ivarcnt = RepairVarcnt(&MyManager.varmap); code = 0; /* if (params.inputfile != -1) { if (LoadVariableData(MyManager.varmap, arg[params.inputfile]) == -1) return -1; if (!all_loaded(MyManager.varmap, 1)) return -1; }*/ MyManager.his = InitHistory(ivarcnt); if (params.method != 0) { switch (arg[params.method][0]) { case 'g': for (i = 0; i < MyManager.varmap.varcnt; i++) { if (MyManager.varmap.vars[i] != NULL) { varpattern = extractpattern(MyManager.varmap.vars[i]); if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern); probability = tvalue.probability; double factor = sigmoid(MyManager.varmap.dvalue[i], params.sigmoid_slope) * (1 - sigmoid(MyManager.varmap.dvalue[i], params.sigmoid_slope)) * params.sigmoid_slope; if (varpattern == NULL) { printf("query_gradient(%s,%s,%1.12f).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient * factor); } else { varpattern[strlen(varpattern) - 2] = '\0'; printf("query_gradient(%s,%s,%1.12f).\n", arg[params.queryid], varpattern, tvalue.gradient * factor); } ReInitHistory(MyManager.his, MyManager.varmap.varcnt); } if (varpattern != NULL) free(varpattern); } else { fprintf(stderr, "Error: no variable name given for parameter.\n"); } } if (probability < 0.0) { // no nodes, so we have to calculate probability ourself tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL); probability = tvalue.probability; } printf("query_probability(%s,%1.12f).\n", arg[params.queryid], probability); break; case 'l': tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL); probability = tvalue.probability; printf("query_probability(%s,%1.12f).\n", arg[params.queryid], probability); break; case 'p': printf("probability(%1.12f).\n", CalcProbability(MyManager, bdd)); break; case 'o': onlinetraverse(MyManager.manager, MyManager.varmap, MyManager.his, bdd); break; default: myexpand(MyManager, bdd); break; } } else { start = clock(); // simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, //"bdd.dot"); printf("probability(%1.12f).\n", ret_prob(MyManager, bdd)); endt = clock(); elapsedt = ((double)(endt - start)) / CLOCKS_PER_SEC; printf("elapsed_traversing(%lf).\n", elapsedt); // myexpand(MyManager, bdd); } if (params.savedfile > -1) SaveNodeDump(MyManager.manager, MyManager.varmap, bdd, arg[params.savedfile]); if (params.exportfile > -1) simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, arg[params.exportfile]); ReInitHistory(MyManager.his, MyManager.varmap.varcnt); free(MyManager.his); } if (MyManager.manager != NULL) { KillBDD(MyManager.manager); exit(code); free(MyManager.varmap.dvalue); free(MyManager.varmap.ivalue); free(MyManager.varmap.dynvalue); for (i = 0; i < MyManager.varmap.varcnt; i++) { free(MyManager.varmap.vars[i]); free(MyManager.varmap.mvars[i].probabilities); free(MyManager.varmap.mvars[i].booleanVars); } free(MyManager.varmap.vars); free(MyManager.varmap.mvars); free(MyManager.varmap.bVar2mVar); } if (params.error != NULL) free(params.error); return code; }