def greedysearch(bn, scr, iters, cstr=None): gr_better = operator.__gt__ gr_accept = lambda n,sss : n>sss["curr_score"] gr_step = bnsearch.stepper_f(bnsearch.acts, gr_better, gr_accept, cstr) gr_stop = bnsearch.stopper_f(iters) gr_isss = bnsearch.initial_search_status(bn,scr) return bnsearch.localsearch(gr_isss, gr_step, gr_stop)
def main(bdtfile, scoretype='BDeu', ess=1.0, time=None, iters=None, outfile=None, constraint_file="", startbn=None, cachefile=None): global searchtime cstrs = Constraints(constraint_file) if startbn != None: bn = bnmodule.load(startbn, do_pic=False) sc = scorefactory.getscorer(bdtfile, scoretype, ess, cachefile=cachefile) else: bn, sc = bnsearch.empty_net(bdtfile, scoretype, ess, cachefile=cachefile) if constraint_file: # should check if compatible with start for a in cstrs.must: bn.addarc(a) sc.score_new(bn) bn.picall() searchtime = sigpool.str2time(time) endtime = tim.time() + searchtime t0 = find_initial_temperature(bn, sc) isss = { "searchtime": searchtime, "endtime": endtime, "t0": t0, "t": t0 } isss.update(bnsearch.initial_search_status(bn, sc)) bnsearch.localsearch(isss, sa_step, sa_stop) if outfile: isss["best_bn"].save(outfile) print isss["best_score"]
def find_initial_temperature(bn, sc): def it_accept(n, sss): d = n - sss["curr_score"] if d < 0: sss["dsum"] += d sss["k"] += 1 return True it_step = bnsearch.stepper_f(bnsearch.acts, operator.__gt__, it_accept) it_stop = bnsearch.stopper_f(5000) isss = {"k": 0, "dsum": 0.0} isss.update(bnsearch.initial_search_status(bn, sc)) bnsearch.localsearch(isss, it_step, it_stop) return isss["dsum"] / isss["k"] / math.log(0.8)
if __name__ == '__main__': import sys def sa_accept(n, sss): c = sss["curr_score"] wp = math.exp((n - c) / sss["t"]) apt = n > c or random.uniform(0, 1) < wp return apt sa_step = bnsearch.stepper_f(bnsearch.acts, operator.__gt__, sa_accept) def sa_stop(sss): endtime = sss["endtime"] currtime = time.time() lefttime = endtime - currtime sss["t"] = sss["t0"] * lefttime / searchtime # update temperature return currtime >= endtime bn, sc = bnsearch.sys2bnscr() searchtime = int(sys.argv[4]) endtime = time.time() + searchtime t0 = find_initial_temperature(bn, sc) isss = {"searchtime": searchtime, "endtime": endtime, "t0": t0, "t": t0} isss.update(bnsearch.initial_search_status(bn, sc)) bnsearch.localsearch(isss, sa_step, sa_stop) print isss["best_score"], isss["best_bn"].arcs()