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 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)
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))