def compute_ip_node_values(self, example, test=False): '''computes value of input nodes using proof tree''' predicate = example.split(" ")[0] facts = self.train_facts if test: facts = self.test_facts literal_name = predicate.split("(")[0] predicate_time_label = predicate[-2] clause_head = "" nodes = self.get_nodes() for node in nodes: rule = node.get_clause() rule_body = rule.split(";") for item in rule_body: item_name = item.split("(")[0] item_time_label = item[-2] if item_name == literal_name and item_time_label == predicate_time_label: clause_head = item sets = self.get_sets_ip_nodes() for s in sets: clause_body = "" for node in s: if node.is_input(): clause_body += node.get_clause().replace(";", ",") + "," clause = clause_head + ":-" + clause_body[:-1] if clause_body[:-1] != "true": truth = Prover.prove(facts, predicate, clause) for node in s: if node.is_input(): node.set_value(int(truth))
def computeAdviceGradient(example): '''computes the advice gradients as nt-nf''' nt, nf = 0, 0 target = Utils.data.target.split('(')[0] for clause in Utils.data.adviceClauses: if Prover.prove(Utils.data, example, clause): if target in Utils.data.adviceClauses[clause]['preferred']: nt += 1 if target in Utils.data.adviceClauses[clause]['nonPreferred']: nf += 1 return (nt - nf)
def inferTreeValue(clauses,query,data): '''returns probability of query given data and clauses learned ''' for clause in clauses: #for every clause in the tree clauseCopy = deepcopy(clause) clauseValue = float(clauseCopy.split(" ")[1]) clauseRule = clauseCopy.split(" ")[0].replace(";",",") if not clauseRule.split(":-")[1]: return clauseValue if Prover.prove(data,query,clauseRule): #check if query satisfies clause return clauseValue
def compute_tree_distance_value(facts, action): '''computes shaping reward based on shaping function''' f_lines, f_value = [], 0 with open("tree_distance.txt") as f: f_lines = f.read().splitlines() for f_line in f_lines: potential = float(f_line.split(" ")[0]) clause = f_line.split(" ")[1].replace(";", ",") action_literal = clause.split(":-")[0].split("(")[0] if action_literal == action.split("(")[0]: if Prover.prove(facts, action, clause): f_value = potential return f_value
def getTrueExamples(self,clause,test,data): '''returns all examples that satisfy clause with conjoined test literal ''' tExamples = [] #intialize list of true examples clauseCopy = deepcopy(clause) if clauseCopy[-1] == "-": #construct clause for prover clauseCopy += test elif clauseCopy[-1] == ';': clauseCopy = clauseCopy.replace(';',',')+test print ("testing clause: ",clauseCopy) # --> to keep track of output, following for loop can be parallelized for example in self.examples: if Prover.prove(data,example,clauseCopy): #prove if example satisfies clause tExamples.append(example) return tExamples