Пример #1
0
    def search(self):
        stack = [self.root]

        output = []
        while stack:
            current_node = stack.pop()

            if is_in_closed_list(
                    self.closed_list[str(current_node.state).__hash__() %
                                     1000],
                    str(current_node.state).__hash__()
            ):  # recursive ba parent ha
                continue
            self.visited_nodes += 1
            if current_node.is_goal(goal_state=self.goal):
                output.append(current_node.state)
                for parent in current_node.parents():
                    output.append(parent.state)
                output.reverse()
                break
            else:
                for child in current_node.expand():
                    stack.append(child)
                    self.expanded_nodes += 1
                self.closed_list.append(current_node)
        return output
Пример #2
0
    def search(self):
        queue = [self.root]
        output = []

        while queue:
            current_node = queue[select_min_cost(queue)]
            if is_in_closed_list(
                    self.closed_list[str(current_node.state).__hash__() %
                                     1000],
                    str(current_node.state).__hash__()):
                queue.pop(queue.index(current_node))
                continue
            self.visited_nodes += 1

            if current_node.is_goal(goal_state=self.goal):
                output.append(current_node.state)
                for parent in current_node.parents():
                    output.append(parent.state)
                output.reverse()
                break
            else:
                for child in current_node.expand():
                    queue.append(child)
                    self.expanded_nodes += 1
                self.closed_list[str(current_node.state).__hash__() %
                                 1000].append(
                                     str(current_node.state).__hash__())
                queue.pop(queue.index(current_node))

        return output
Пример #3
0
    def search(self, hn):
        self.root.set_fn(fn(self.root, self.goal, hn))
        fringe = [self.root]
        output = []
        current_node = heapq.heappop(fringe)
        while True:
            if is_in_closed_list(self.closed_list[str(current_node.state).__hash__() % 1000],
                                 str(current_node.state).__hash__()):
                current_node = heapq.heappop(fringe)
                continue
            self.visited_nodes += 1
            if current_node.is_goal(self.goal):
                break
            else:
                for child in current_node.expand():
                    child.set_fn(fn(child, self.goal, hn))
                    heapq.heappush(fringe, child)
                    self.expanded_nodes += 1

            self.closed_list[str(current_node.state).__hash__() % 1000].append(str(current_node.state).__hash__())
            current_node = heapq.heappop(fringe)
        print("found in depth {}".format(current_node.depth))
        output.append(current_node.state)
        for parent in current_node.parents():
            output.append(parent.state)

        output.reverse()
        return output
Пример #4
0
    def search(self, hn):
        self.root.set_fn(fn(self.root, self.goal, hn))
        min_cutoff = self.root.fn
        current_node = None
        i = 0
        self.visited_nodes = 0
        self.expanded_nodes = 0

        while True:

            cutoff = min_cutoff
            min_cutoff = sys.maxsize
            self.closed_list = [[] for _ in range(1000)]
            fringe = [self.root]

            while fringe:
                current_node = heapq.heappop(fringe)
                if current_node.fn > cutoff:
                    continue
                if is_in_closed_list(
                        self.closed_list[str(current_node.state).__hash__() %
                                         1000],
                        str(current_node.state).__hash__()):
                    continue
                self.visited_nodes += 1
                if current_node.is_goal(self.goal):
                    break
                else:
                    for child in current_node.expand():
                        child.set_fn(fn(child, self.goal, hn))
                        self.expanded_nodes += 1
                        if child.fn > cutoff:
                            if child.fn < min_cutoff:
                                min_cutoff = child.fn
                        heapq.heappush(fringe, child)
                    self.closed_list[str(current_node.state).__hash__() %
                                     1000].append(
                                         str(current_node.state).__hash__())
            if current_node.is_goal(self.goal):
                break
        print('found in {}'.format(current_node.depth))
        output = [current_node.state]
        for parent in current_node.parents():
            output.append(parent.state)

        output.reverse()
        return output
Пример #5
0
    def search(self):
        root_queue = [self.root]
        goal_queue = [self.goal]
        matched_root = None
        matched_goal = None
        result = []

        while root_queue and goal_queue:
            # searching for match
            for root_child in root_queue:
                for goal_child in goal_queue:
                    if goal_child.compare(root_child):
                        matched_root = root_child
                        matched_goal = goal_child.parent
                        break
                if matched_root: break
            if matched_root: break  # match founded

            for i in range(len(root_queue)):
                current_node = root_queue.pop(0)
                self.root_closed_list[str(current_node.state).__hash__() %
                                      1000].append(
                                          str(current_node.state).__hash__())

                for child in current_node.expand():
                    if not is_in_closed_list(
                            self.root_closed_list[str(child.state).__hash__() %
                                                  1000],
                            str(child.state).__hash__()):
                        root_queue.append(child)
                        self.expanded_nodes += 1

            for root_child in root_queue:
                for goal_child in goal_queue:
                    if goal_child.compare(root_child):
                        matched_root = root_child
                        matched_goal = goal_child.parent
                        break
                if matched_root: break
            if matched_root: break  # match founded

            for i in range(len(goal_queue)):
                current_node = goal_queue.pop(0)
                self.goal_closed_list[str(current_node.state).__hash__() %
                                      1000].append(
                                          str(current_node.state).__hash__())

                for child in current_node.expand():
                    if not is_in_closed_list(
                            self.goal_closed_list[str(child.state).__hash__() %
                                                  1000],
                            str(child).__hash__()):
                        goal_queue.append(child)
                        self.expanded_nodes += 1
        result.append(matched_root.state)
        for item in matched_root.parents():
            result.append(item.state)
        result.reverse()
        result.append(matched_goal.state)
        for item in matched_goal.parents():
            result.append(item.state)

        return result