Exemplo n.º 1
0
def convert(filename):
    start = time.time()
    manager,alpha = orig.parse_bdd(filename+".zdd")
    end = time.time()
    print "      sdd node count: %d" % sdd.sdd_count(alpha)
    print "            sdd size: %d" % sdd.sdd_size(alpha)
    print "     sdd model count: %d" % sdd.sdd_model_count(alpha,manager)
    print "  global model count: %d" % orig.global_model_count(alpha,manager)
    print "       read bdd time: %.3fs" % (end-start)

    sdd.sdd_save(filename + ".sdd",alpha)
    #sdd.sdd_save_as_dot(filename +".sdd.dot",alpha)
    vtree = sdd.sdd_manager_vtree(manager)
    sdd.sdd_vtree_save(filename + ".vtree",vtree)
    #sdd.sdd_vtree_save_as_dot(filename +".vtree.dot",vtree)

    print "===================="
    print "before garbage collecting..." 
    print "live size:", sdd.sdd_manager_live_count(manager)
    print "dead size:", sdd.sdd_manager_dead_count(manager)
    print "garbage collecting..."
    sdd.sdd_manager_garbage_collect(manager)
    print "live size:", sdd.sdd_manager_live_count(manager)
    print "dead size:", sdd.sdd_manager_dead_count(manager)
Exemplo n.º 2
0
def run():
    with timer.Timer("reading dataset"):
        dataset = util.read_binary_dataset(test_filename)
        domain = util.read_header(test_filename)
        '''
        if OPTIONS.majority_circuit_opt:
            l = len(domain)
            for k in xrange(num_trees):
                domain["Tree_%d" % k] = l+k
        '''

    with timer.Timer("initializing manager"):
        # start sdd manager
        var_count = len(domain) - 1
        vtree = sdd.sdd_vtree_new(var_count, "balanced")
        manager = sdd.sdd_manager_new(vtree)
        #sdd.sdd_manager_auto_gc_and_minimize_on(manager)
        #sdd.sdd_manager_auto_gc_and_minimize_off(manager)
        sdd_state = SddState(vtree, manager)

    with timer.Timer("reading constraints"):
        constraint_sdd, constraint_info = encode_logical_constraints(
            constraint_filename, manager, domain)
        sdd.sdd_ref(constraint_sdd, manager)

    with timer.Timer("reading trees"):
        tree_states = []
        for filename in sorted(glob.glob(tree_basename.replace('%d', '*'))):
            tree = pygv.AGraph(filename)
            tree_state = TreeState(tree, domain, constraint_info)
            tree_states.append(tree_state)
            #tree.layout(prog='dot')
            #tree.draw(filename+".png")
        #num_trees = len(tree_states)

    with timer.Timer("compiling trees"):
        forest_sdds, _ = izip(*forest_sdds_iter(tree_states, sdd_state))
        #forest_sdds = list(forest_sdds_iter(tree_states,sdd_state))

        forest_sdds = [
            (tree_state, tree_sdd)
            for tree_state, tree_sdd in zip(tree_states, forest_sdds)
        ]
        cmpf = lambda x, y: cmp(sdd.sdd_size(x[1]), sdd.sdd_size(y[1]))
        forest_sdds.sort(cmp=cmpf)
        tree_states = [tree_state for tree_state, tree_sdd in forest_sdds]

        #ACACAC
        sdd.sdd_manager_auto_gc_and_minimize_off(manager)
        sdd.sdd_manager_minimize_limited(manager)
        stats = SddSizeStats()
        for tree_state, tree_sdd in forest_sdds:
            stats.update(tree_sdd)
            sdd.sdd_deref(tree_sdd, manager)
        sdd.sdd_manager_garbage_collect(manager)
        forest_sdds, used_vars_list = izip(
            *forest_sdds_iter(tree_states, sdd_state))
    print stats

    with timer.Timer("compiling all", prefix="| "):
        alpha = compile_all(forest_sdds, used_vars_list, num_trees, domain,
                            manager, constraint_sdd)

    with timer.Timer("evaluating"):
        msg = util.evaluate_dataset_all_sdd(dataset, alpha, manager)
    print "|     trees : %d" % num_trees
    print "--- evaluating majority vote on random forest (compiled):"
    print msg
    print "|  all size :", sdd.sdd_size(alpha)
    print "|  all count:", sdd.sdd_count(alpha)
    print " model count:", sdd.sdd_global_model_count(alpha, manager)

    with timer.Timer("checking monotonicity"):
        result = is_monotone(alpha, manager)
    print "Is monotone?", result

    #for tree_sdd in forest_sdds: sdd.sdd_deref(tree_sdd,manager)
    print "===================="
    print "before garbage collecting..."
    print "live size:", sdd.sdd_manager_live_count(manager)
    print "dead size:", sdd.sdd_manager_dead_count(manager)
    print "garbage collecting..."
    sdd.sdd_manager_garbage_collect(manager)
    print "live size:", sdd.sdd_manager_live_count(manager)
    print "dead size:", sdd.sdd_manager_dead_count(manager)

    vtree = sdd.sdd_manager_vtree(manager)
    print "Writing sdd file %s and vtree file %s" % (sdd_filename,
                                                     vtree_filename)
    sdd.sdd_save(sdd_filename, alpha)
    sdd.sdd_vtree_save(vtree_filename, vtree)

    print "Writing constraint sdd file %s and constraint vtree file %s" % (
        constraint_sdd_filename, constraint_vtree_filename)
    sdd.sdd_save(constraint_sdd_filename, constraint_sdd)
    sdd.sdd_vtree_save(constraint_vtree_filename, vtree)
Exemplo n.º 3
0
def compile_all(forest_sdds,
                used_vars_list,
                num_trees,
                domain,
                manager,
                constraint_sdd=None):
    half = int(math.ceil(num_trees / 2.0))
    true_sdd = sdd.sdd_manager_true(manager)
    false_sdd = sdd.sdd_manager_false(manager)
    last_size = 2**16

    if not constraint_sdd:
        constraint_sdd = sdd.sdd_manager_true(manager)
    true_sdd = constraint_sdd

    sdd.sdd_ref(true_sdd, manager)

    to_compile_sdds = [tree_sdd for tree_sdd in forest_sdds]
    used_vars_list = [used_vars for used_vars in used_vars_list]
    '''
    if OPTIONS.majority_circuit_opt:
        majority_sdds = [sdd.sdd_manager_literal(domain["Tree_%d" % i], manager) for i in xrange(num_trees)]    
        for single_sdd in majority_sdds:
            sdd.sdd_ref(single_sdd, manager)
        to_compile_sdds = majority_sdds
        used_vars_list = [set() for _ in forest_sdds]
    '''

    cur = [true_sdd, false_sdd]
    used_vars = set()

    for k in xrange(num_trees):
        last, cur = cur, []

        tree_index = pick_next_tree(used_vars_list, used_vars)
        tree_sdd = to_compile_sdds[tree_index]
        used_vars |= used_vars_list[tree_index]
        to_compile_sdds = to_compile_sdds[:tree_index] + to_compile_sdds[
            tree_index + 1:]
        used_vars_list = used_vars_list[:tree_index] + used_vars_list[
            tree_index + 1:]

        for i in xrange(min(half, k + 1) + 1):
            cur_sdd = last[i]
            #cur_sdd = sdd.sdd_conjoin(sdd.sdd_negate(tree_sdd,manager),cur_sdd,manager)
            """
            elif i+(num_trees-k) < half: # don't bother
                cur_sdd = sdd.sdd_manager_false(manager)
            """
            if i == 0:
                pass
            elif i > 0:
                alpha = sdd.sdd_conjoin(tree_sdd, last[i - 1], manager)
                sdd.sdd_deref(last[i - 1], manager)
                cur_sdd = sdd.sdd_disjoin(cur_sdd, alpha, manager)
            sdd.sdd_ref(cur_sdd, manager)
            cur.append(cur_sdd)

            if sdd.sdd_manager_dead_count(manager) >= 2 * last_size:
                sdd.sdd_manager_garbage_collect(manager)
            if sdd.sdd_manager_live_count(manager) >= 2 * last_size:
                print "*",
                sdd.sdd_manager_minimize_limited(manager)
                last_size = 2 * last_size

        if k >= half: sdd.sdd_deref(last[-2], manager)
        sdd.sdd_deref(tree_sdd, manager)
        cur.append(false_sdd)

        print "%d" % (num_trees - k),
        sys.stdout.flush()
        #print "%d/%d" % (k,num_trees)
        print "live size:", sdd.sdd_manager_live_count(manager)
        #print "dead size:", sdd.sdd_manager_dead_count(manager)
        sdd.sdd_manager_garbage_collect(manager)
        #sdd.sdd_manager_minimize_limited(manager)

    #for alpha in cur: sdd.sdd_deref(alpha,manager)
    ret = cur[-2]
    '''
    if OPTIONS.majority_circuit_opt:
        # save ret (the majority circuit)
        # save each individual tree_sdd
        vtree = sdd.sdd_manager_vtree(manager)
        majority_sdd_filename = "%s_majority.sdd" % sdd_basename
        majority_vtree_filename = "%s_majority.vtree" % sdd_basename
        print "Writing majority sdd file %s and majority vtree file %s" % (majority_sdd_filename, majority_vtree_filename)
        sdd.sdd_save(majority_sdd_filename,ret)
        sdd.sdd_vtree_save(majority_vtree_filename,vtree)

        print "Writing individual tree sdds..."
        for k,tree_sdd in enumerate(forest_sdds):
            tree_name = "tree_%d" % k
            tree_sdd_filename = "%s_majority_%s.sdd" % (sdd_basename, tree_name)
            sdd.sdd_save(tree_sdd_filename, tree_sdd)

        gamma = sdd.sdd_manager_true(manager)
        for k,tree_sdd in enumerate(forest_sdds):
            new_gamma = sdd.sdd_conjoin(gamma, tree_sdd, manager)
            sdd.sdd_ref(new_gamma, manager)
            sdd.sdd_deref(gamma, manager)
            gamma = new_gamma

            if sdd.sdd_manager_dead_count(manager) >= 2*last_size:
                sdd.sdd_manager_garbage_collect(manager)
            if sdd.sdd_manager_live_count(manager) >= 2*last_size:
                print "*",
                sdd.sdd_manager_minimize_limited(manager)
                last_size = 2*last_size

            print "%d" % k,
            sys.stdout.flush()
            print "live size:", sdd.sdd_manager_live_count(manager)
        ret = sdd.sdd_conjoin(ret, gamma, manager)
        
        #existential quantification
        print "Existential quantification..."
        exists_map = sdd.new_intArray(len(domain))
        for i in xrange(len(domain)):
            sdd.intArray_setitem(exists_map,i,0)
        for i in xrange(num_trees):
            lit = domain["Tree_%d" % i]
            sdd.intArray_setitem(exists_map,lit,1)
        ret = sdd.sdd_exists_multiple(exists_map, ret, manager)
    '''

    return ret
Exemplo n.º 4
0
# 41044208702632496804
# 1568758030464750013214100
# 182413291514248049241470885236 
if __name__ == '__main__':
    if len(sys.argv) != 3:
        print "usage: %s [GRID-M] [GRID-N]" % sys.argv[0]
        exit(1)
    m,n = (int(sys.argv[1]),int(sys.argv[2]))
    g = Graph.grid_graph(m,n)

    manager = start_manager(g)
    alpha = encode_graph(g,manager)
    #sdd.sdd_deref(alpha,manager)
    print_grids(alpha,m,n,g,manager)

    """ UNCOMMENT TO SAVE SDD/VTREE
    sdd_filename = "paths-%d-%d.sdd" % (m,n)
    sdd_vtree_filename = "paths-%d-%d.vtree" % (m,n)
    sdd.sdd_save(sdd_filename,alpha)
    sdd.sdd_vtree_save(sdd_vtree_filename,sdd.sdd_manager_vtree(manager))
    """

    print "===================="
    print "before garbage collecting..." 
    print "live size:", sdd.sdd_manager_live_count(manager)
    print "dead size:", sdd.sdd_manager_dead_count(manager)
    print "garbage collecting..."
    sdd.sdd_manager_garbage_collect(manager)
    print "live size:", sdd.sdd_manager_live_count(manager)
    print "dead size:", sdd.sdd_manager_dead_count(manager)