def _learn_one_clause(self, examples: Task, hypothesis_space: TopDownHypothesisSpace) -> Clause: """ Learns a single clause Returns a clause """ # reset the search space hypothesis_space.reset_pointer() # empty the pool just in case self.initialise_pool() # put initial candidates into the pool self.put_into_pool(hypothesis_space.get_current_candidate()) current_cand = None score = -100 while current_cand is None or ( len(self._candidate_pool) > 0 and not self.stop_inner_search(score, examples, current_cand)): # get first candidate from the pool current_cand = self.get_from_pool() # expand the candidate _ = hypothesis_space.expand(current_cand) # this is important: .expand() method returns candidates only the first time it is called; # if the same node is expanded the second time, it returns the empty list # it is safer than to use the .get_successors_of method exps = hypothesis_space.get_successors_of(current_cand) exps = self.process_expansions(examples, exps, hypothesis_space) # add into pool self.put_into_pool(exps) score = self.evaluate(examples, current_cand, hypothesis_space) if self._print: print(f"- New clause: {current_cand}") print( f"- Candidates has value {round(score,2)} for metric '{self._eval_fn.name()}'" ) return current_cand
def _learn_one_clause(self, examples: Task, hypothesis_space: TopDownHypothesisSpace) -> Clause: """ Learns a single clause Returns a clause """ # reset the search space hypothesis_space.reset_pointer() # empty the pool just in case self.initialise_pool() # put initial candidates into the pool self.put_into_pool([Triplet(hypothesis_space.get_current_candidate()[0]).get_tuple()]) current_cand = None first = True score = -100 while current_cand is None or ( self._candidate_pool.qsize() > 0 and not self.stop_inner_search(score, examples, current_cand)): # get first candidate from the pool current_cand = self.get_from_pool() if first: self.example_weights[current_cand] = self.get_initial_weights(examples) first = False print("-------------------------------------------------------") print("CURRENT CANDIDATE: ") print("\t ", current_cand) print("STATS: ") score = self.evaluate(examples, current_cand) print("\t length: ", len(current_cand)) print("\t Pool size: ", self._candidate_pool.qsize(), "\n") self.exp_count += 1 self.max_queue_len = max(self.max_queue_len, self._candidate_pool.qsize()) if self.exp_count == 101: self.stopped_early = True break if not current_cand.is_recursive(): # expand the candidate and get possible expansions _ = hypothesis_space.expand(current_cand) exps = hypothesis_space.get_successors_of(current_cand) # Get scores for primitives using current candidate and each example primitives = self.get_best_primitives(examples, current_cand) exps = self.process_expansions(current_cand, examples, exps, primitives, hypothesis_space) # add into pool self.put_into_pool(exps) if self._candidate_pool.qsize() == 0: self.stopped_early = True break if self.stopped_early: return None self._solver.asserta(current_cand) return current_cand
def _learn_one_clause(self, examples: Task, hypothesis_space: TopDownHypothesisSpace) -> Clause: """ Learns a single clause Returns a clause """ # reset the search space hypothesis_space.reset_pointer() # empty the pool just in case self.initialise_pool() # put initial candidates into the pool self.put_into_pool(hypothesis_space.get_current_candidate()) current_cand = None score = -100 self._expansionOneClause = 0 best = None while current_cand is None or (len(self._candidate_pool) > 0) and self._expansionOneClause < 40000: # get first candidate from the pool current_cand = self.get_from_pool() print(self._expansion) # expand the candidate _ = hypothesis_space.expand(current_cand) # this is important: .expand() method returns candidates only the first time it is called; # if the same node is expanded the second time, it returns the empty list # it is safer than to use the .get_successors_of method exps = hypothesis_space.get_successors_of(current_cand) exps = [cl for cl in exps if len(cl) <= self._max_body_literals] new_exps = [] # check if every clause has solutions for cl in exps: y = self.evaluate(task, cl) if y[0] > 0: new_exps.append(cl) if best == None: best = (y[0], cl) if (y[1] == 0): self._expansion += 1 self._expansionOneClause += 1 self._result.append((self._expansion, y[1])) return cl self._expansion += 1 self._expansionOneClause += 1 self._result.append((self._expansion, y[0])) if y[0] > best[0]: best = (y[0], cl) else: if (y[0] == best[0]) & (len(cl.get_literals()) < len( best[1].get_literals())): best = (y[0], cl) else: hypothesis_space.remove(cl) # add into pull self.put_into_pool(new_exps) print(best) return best[1]