Exemplo n.º 1
0
def test_goal_dist_heuristic():
    min_len = 4
    max_len = 10
    indicators = dict((d, ['I%d' % d]) for d in range(1, max_len))
    indicator_set = set(reduce(lambda x, y: x + y, indicators.values()))
    tree_gen = TreeGen()
    tree_gen.min_branch_len = min_len
    tree_gen.max_branch_len = max_len

    src_root = tree_gen.generate_indicators(indicators)
    src_root.make_graphviz(open('src_tree.dot', 'w'))
    tgt_root = src_root.deep_copy()
    # change all predicates in the target to be lowercase version of source
    tgt_root.map(state_preds_lower)
    tgt_root.make_graphviz(open('tgt_tree.dot', 'w'))

    src_preds = list(src_root.get_all_predicates())

    num_preds = len(src_preds)

    src_preds.sort()
    src_weights = fmincon.run_fmincon(src_root)

    # the heuristic for the source
    hs = heuristic.Heuristic(dict(zip(src_preds, src_weights)))
    #hs = heuristic.Heuristic(indicators)

    src_search = AStar(src_root, lambda x: hs(x.preds))
    iter = src_search.gen
    iterations = 0
    try:
        while not iter.next():
            iterations += 1
    except StopIteration:
        src_iters = iterations

    non_ind_preds = [p for p in src_preds if p not in indicator_set]

    data = []

    for n_ind, n_rest, mapping in ind_mapping_gen(indicator_set, non_ind_preds,
                                                  num_preds, 100):
        #for n_matches, mapping in reg_mapping_gen(src_preds, 100):
        #print "The mapping is ..."
        #for s,t in mapping.items():
        #	print s, t

        tgt_preds2weights = {}
        for p, w in zip(src_preds, src_weights):
            if p in mapping:
                # we assign the weight of the source predicate to the target
                tgt_preds2weights[mapping[p]] = w

        ht = heuristic.Heuristic(tgt_preds2weights)
        tgt_search = AStar(tgt_root, lambda x: ht(x.preds))
        iter = tgt_search.gen
        iterations = 0
        try:
            while not iter.next():
                iterations += 1
        except StopIteration:
            tgt_iters = iterations

        #print 'The target problem takes %d iterations' % tgt_iters
        #print 'Solution is', [''.join(s.preds) for s in tgt_search.solution]

        data.append((n_ind, n_rest, tgt_iters))
        #data.append((n_matches, tgt_iters))

    n_preds = len(src_preds)
    return (n_preds, src_iters, data)
Exemplo n.º 2
0
	objfun_file.write('f=%s;' % make_matlab_min_func(root))
	objfun_file.close()

	num_vars = len(root.get_all_predicates())
	cmd_file = open('do_opt.m', 'w')
	cmd_file.write('x0=[%s];\n' % ','.join(['0'] * num_vars))
	cmd_file.write('lb=[%s];\n' % ','.join(['0'] * num_vars))
	cmd_file.write('[x,fval] = fmincon(@objfun,x0,[],[],[],[],lb);\n')
	cmd_file.write('csvwrite(\'result.csv\', x);\n')
	cmd_file.close()

def run_fmincon(root):
	make_files(root)
	#server = 'scs.itd.umich.edu'
	output_file = 'result.csv'
	matlab_cmd = 'matlab -nojvm -nodisplay -r \'do_opt;quit\' &> /dev/null'
	#os.system('scp objfun.m %s:%s/' % (server, d))
	#os.system('scp do_opt.m %s:%s/' % (server, d))
	#os.system('ssh %s "cd %s; %s"' % (server, d, matlab_cmd))
	#os.system('scp %s:%s/%s .' % (server, d, output_file))
	assert os.system(matlab_cmd) == 0, "Matlab run failed"
	result = [float(s) for s in open(output_file).read().split(',')]
	return result

if __name__ == '__main__':
	from treegen import TreeGen
	tgen = TreeGen()
	root = tgen.generate_random()
	result = run_fmincon(root)
	print result
Exemplo n.º 3
0
 def __init__(self, gen_method='generate_random()'):
     self.tree_gen = TreeGen()
     self.gen_method = gen_method
     self.preserve_preds = set()