def test_heuristic_gen(self): root = self.gen_tree() pvar = pred_variance(root) max_var = 1.5 * max(pvar.values()) pvar1 = dict((p, max_var - v) for p, v in pvar.items()) pvar1s = sum(pvar1.values()) weights = dict((p, root.num_occurrences(p) * v / pvar1s) for p, v in pvar1.items()) for p, w in weights.items(): print '%s: %f' % (p, w) preds = list(root.get_all_predicates()) preds.sort() weights = fmincon.run_fmincon(root) h = heuristic.Heuristic(dict(zip(preds, weights))) root.make_graphviz(open('temp.dot', 'w')) search = AStar(root, lambda x: h(x.preds)) iter = search.gen iterations = 0 try: while not iter.next(): iterations += 1 except StopIteration: print iterations print[''.join(s.preds) for s in search.solution]
def test_heuristic_gen(self): root = self.gen_tree() pvar = pred_variance(root) max_var = 1.5 * max(pvar.values()) pvar1 = dict((p, max_var - v) for p, v in pvar.items()) pvar1s = sum(pvar1.values()) weights = dict((p, root.num_occurrences(p) * v / pvar1s) for p, v in pvar1.items()) for p, w in weights.items(): print '%s: %f' % (p, w) preds = list(root.get_all_predicates()) preds.sort() weights = fmincon.run_fmincon(root) h = heuristic.Heuristic(dict(zip(preds,weights))) root.make_graphviz(open('temp.dot', 'w')) search = AStar(root, lambda x: h(x.preds)) iter = search.gen iterations = 0 try: while not iter.next(): iterations += 1 except StopIteration: print iterations print [''.join(s.preds) for s in search.solution]
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)
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)
def test_all(self, nbins, split_prob): #src_root, src_rs, tgt_root, tgt_rs = self.gen_src_tgt_split(split_prob) src_root, src_rs, tgt_root, tgt_rs = self.gen_contradict() src_file = 'source.kif' tgt_file = 'target.kif' src_rs.make_kif(src_root.preds, open(src_file, 'w')) tgt_rs.make_kif(tgt_root.preds, open(tgt_file, 'w')) src_preds = list(src_root.get_all_predicates()) src_preds.sort() src_weights = fmincon.run_fmincon(src_root) # the heuristic for the source hs = heuristic.Heuristic(dict(zip(src_preds, src_weights))) 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 print 'The source problem takes %d iterations' % src_iters print 'Solution is', [''.join(s.preds) for s in src_search.solution] src_bins = dict((Predicate(p, Sentence.STATE, []), b) for p, b in make_bins(src_preds, nbins).items()) tgt_bins = dict( (Predicate(p.get_name().lower(), Sentence.STATE, []), b) for p, b in src_bins.items()) mapping = rule_mapper2.map_kifs(src_file, tgt_file, src_bins, tgt_bins) str_mapping = dict( (s.get_name(), t.get_name()) for s, t in mapping.items()) print "The mapping is ..." for s, t in str_mapping.items(): print s, t num_correct = 0 num_correct_inds = 0 for s, t in str_mapping.items(): if s.lower() == t: num_correct += 1 if s[0] == 'I' and s[-1] in '0123456789': num_correct_inds += 1 print "Number of correct predicates:", num_correct tgt_preds = [] tgt_weights = [] for p, w in zip(src_preds, src_weights): if p in str_mapping: # we assign the weight of the source predicate to the target tgt_preds.append(str_mapping[p]) tgt_weights.append(w) ht = heuristic.Heuristic(dict(zip(tgt_preds, tgt_weights))) tgt_search = AStar(tgt_root, lambda x: ht(x.preds)) iter = tgt_search.gen 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] os.remove(src_file) os.remove(tgt_file) ret_labels = [ 'len(src_preds)', 'num_correct', 'num_correct_inds', 'src_iters', 'tgt_iters' ] ret_vals = eval('[%s]' % ','.join(ret_labels)) return (ret_labels, ret_vals)
def test_all(self, nbins, split_prob): #src_root, src_rs, tgt_root, tgt_rs = self.gen_src_tgt_split(split_prob) src_root, src_rs, tgt_root, tgt_rs = self.gen_contradict() src_file = 'source.kif' tgt_file = 'target.kif' src_rs.make_kif(src_root.preds, open(src_file,'w')) tgt_rs.make_kif(tgt_root.preds, open(tgt_file,'w')) src_preds = list(src_root.get_all_predicates()) src_preds.sort() src_weights = fmincon.run_fmincon(src_root) # the heuristic for the source hs = heuristic.Heuristic(dict(zip(src_preds, src_weights))) 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 print 'The source problem takes %d iterations' % src_iters print 'Solution is', [''.join(s.preds) for s in src_search.solution] src_bins = dict((Predicate(p, Sentence.STATE, []), b) for p,b in make_bins(src_preds, nbins).items()) tgt_bins = dict((Predicate(p.get_name().lower(), Sentence.STATE, []), b) for p, b in src_bins.items()) mapping = rule_mapper2.map_kifs(src_file, tgt_file, src_bins, tgt_bins) str_mapping = dict((s.get_name(), t.get_name()) for s,t in mapping.items()) print "The mapping is ..." for s,t in str_mapping.items(): print s, t num_correct = 0 num_correct_inds = 0 for s, t in str_mapping.items(): if s.lower() == t: num_correct += 1 if s[0] == 'I' and s[-1] in '0123456789': num_correct_inds += 1 print "Number of correct predicates:", num_correct tgt_preds = [] tgt_weights = [] for p,w in zip(src_preds, src_weights): if p in str_mapping: # we assign the weight of the source predicate to the target tgt_preds.append(str_mapping[p]) tgt_weights.append(w) ht = heuristic.Heuristic(dict(zip(tgt_preds, tgt_weights))) tgt_search = AStar(tgt_root, lambda x: ht(x.preds)) iter = tgt_search.gen 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] os.remove(src_file) os.remove(tgt_file) ret_labels = ['len(src_preds)', 'num_correct', 'num_correct_inds', 'src_iters', 'tgt_iters' ] ret_vals = eval('[%s]' % ','.join(ret_labels)) return (ret_labels, ret_vals)