Example #1
0
def test_best_first_search_without_heuristic():
    """
    Best first search without a heuristic is essentially breadth first.
    """
    for goal in range(1, 10):
        p = AnnotatedProblem(EasyProblem(0, goal))
        sol = next(best_first_search(p, graph=False))
        assert sol.state_node.state == goal
        assert p.nodes_expanded == pow(2, goal + 1) - 2
        assert p.goal_tests == pow(2, goal)

    try:
        p = EasyProblem(0, 10)
        next(best_first_search(p, graph=False, cost_limit=5))
        assert False
    except StopIteration:
        pass
Example #2
0
def test_best_first_search_with_heuristic():
    """
    Best heuristic is essentially depth first.
    """
    for goal in range(1, 10):
        p = AnnotatedProblem(HeuristicEasyProblem(0, extra=goal))
        sol = next(best_first_search(p, search=tree_search))
        assert sol.state == goal
        assert p.nodes_expanded == goal*2 
        assert p.goal_tests == goal+1
Example #3
0
def test_best_first_search_without_heuristic():
    """
    Best first search without a heuristic is essentially breadth first.
    """
    for goal in range(1, 10):
        p = AnnotatedProblem(EasyProblem(0, extra=goal))
        sol = next(best_first_search(p, search=tree_search))
        assert sol.state == goal
        assert p.nodes_expanded == pow(2,goal+1)-2
        assert p.goal_tests == pow(2,goal)
Example #4
0
def test_best_first_search_with_heuristic():
    """
    Best heuristic is essentially depth first.
    """
    for goal in range(1, 10):
        p = AnnotatedProblem(HeuristicEasyProblem(0, goal))
        sol = next(best_first_search(p, graph=False))
        assert sol.state_node.state == goal
        assert p.nodes_expanded == goal * 2
        assert p.goal_tests == goal + 1
Example #5
0
def exhaustive_generalization_search(h, x):
    """
    This computes all possible partial antiunifications. I'm not 100% sure, but
    I'm pretty sure that It is guranteed to return all possible minimal
    specializations of the hypothesis. However, since it has to build up a
    match from nothing and since the solutions are at the leaves of the tree
    and since there are many possible paths to the same solution, this can be
    an expensive approach. 
    """

    au_table = build_antiunify_table(h, x)
    problem = AntiUnifyProblem(frozenset(),
                               extra=(h, frozenset(x), au_table, frozenset()))
    for sol in best_first_search(problem):
        return set([variablize_hypothesis(sol.state)])
        print(sol.state)
        break
    return set(
        variablize_hypothesis(sol.state) for sol in best_first_search(problem))
Example #6
0
    def explain_value_iter(self, state, value):
        """
        Returns an iterator for explainations of the provided value in the
        provided state.
        """
        extra = {}
        extra["actions"] = self.action_set.get_function_dict()
        extra["epsilon"] = self.act_params['epsilon']
        extra['tested'] = set()
        depth_limit = self.act_params['depth_limit']
        state = {k: state[k] for k in state if k[0] != '_'}
        problem = ActionPlannerProblem((tuple(state.items()),
                                        None, value), extra=extra)
        try:
            for solution in best_first_search(problem, cost_limit=depth_limit):
                state, chosen, goal = solution.state
                yield chosen[0]
        except StopIteration:
            pass

        yield str(value)
Example #7
0
    def explain_value(self, state, value, num_expl=1, time_limit=float('inf')):
        """
        This function uses a planner compute the given value from the current
        state. The function returns a plan.
        """
        extra = {}
        extra["actions"] = self.action_set.get_function_dict()
        extra["epsilon"] = self.act_params['epsilon']
        extra['tested'] = set()
        depth_limit = self.act_params['depth_limit']

        state = {k: state[k] for k in state if k[0] != '_'}

        problem = ActionPlannerProblem((tuple(state.items()), None, value),
                                       extra=extra)

        explanations = []
        
        #print ("EXPLAINING ", value)
        s_time = time()
        try:
            for solution in best_first_search(problem, cost_limit=depth_limit):
                #print(solution)
                if len(solution.path()) > 0:
                    state, chosen, goal = solution.state
                    #print(chosen, solution.cost())
                    explanations.append(chosen[0])
                if len(explanations) == num_expl:
                    break
                if time() - s_time > time_limit: 
                    break
        except StopIteration:
            #print("EXPLAIN FAILED")
            pass

        if len(explanations) == 0:
            return [str(value)]
        else:
            return explanations
Example #8
0
 def cost_limited(problem):
     return best_first_search(problem, cost_limit=4)