Beispiel #1
0
    def BFS_GraphSearch(self):
        node = Node(self.initial_state, (), None)
        m = self.m
        if goal_test(node.info, m):
            return node
        self.frontier.append(node)

        while len(self.frontier) != 0:

            node = self.frontier.pop(0)
            par = node
            node = node.info
            self.explored.append(node)
            all_actions = actions(node)
            for action in all_actions:
                child = Node(do_action(node, action[0], action[1]), action, par)
                all_front_node = [x.info for x in self.frontier]
                if child.info not in (self.explored or all_front_node):
                    if goal_test(child.info, m):
                        return child

                    if child.info not in all_front_node:
                        self.frontier.append(child)

        return []
Beispiel #2
0
    def AStar_GraphSearch(self):
        node = Node(self.initial_state, (), None)
        distance = self.calculate_heuristic(node.info) + 0
        m = self.m
        if goal_test(node.info, m):
            return node
        self.frontier.append([node, distance, 0])

        while len(self.frontier) != 0:
            node = self.find_min_node()
            self.frontier.remove(node)
            par = node[0]
            dist = node[2]
            self.explored.append(node)
            node = node[0].info
            all_actions = actions(node)
            for action in all_actions:
                child = Node(do_action(node, action[0], action[1]), action, par)
                all_front_node = [x[0].info for x in self.frontier]
                if child.info not in (self.explored or all_front_node):
                    if goal_test(child.info, m):
                        return child

                    if child.info not in all_front_node:
                        distance = self.calculate_heuristic(child.info) + dist
                        self.frontier.append([child, distance, dist + 1])

        return []
Beispiel #3
0
 def recursive_DLS(self, node, limit):
     m = self.m
     if goal_test(node.info, m):
         return ['goal', node]
     elif limit == 0:
         return ['cutoff', node]
     else:
         par = node
         cutoff_occurred = False
         all_actions = actions(node.info)
         self.explored += 1
         for action in all_actions:
             child = Node(do_action(node.info, action[0], action[1]),
                          action, par)
             self.generate += 1
             result = self.recursive_DLS(child, limit - 1)
             res_state = result[0]
             if res_state == 'cutoff':
                 cutoff_occurred = True
             elif res_state != 'fail':
                 return result
         if cutoff_occurred:
             return ['cutoff', node]
         else:
             return ['fail']
Beispiel #4
0
    def assign(self, num: int = None, val: Generic[T] = None, container: list = None):
        """
        Populates self with nodes using the given parameters
        :param num: represents the number of occurrences of val to assign to list
        :param val: value to have n occurrences
        :param container: used to generate nodes with its contents
        """
        self.clear()
        node = self.node

        if container:
            for item in container:
                node.next = Node(item, prev=node)
                node = node.next
        elif num:
            for _ in range(num):
                node.next = Node(val, prev=node)
                node = node.next

        node.next, self.node.prev = self.node, node
Beispiel #5
0
    def __init__(self, num: int = None, val: Generic[T] = None, container: list = None) -> None:
        """
        Creates root node and sets its prev and next member variable to itself
        Assigns list with param values given
        :param num: count of val occurrences
        :param val: value to be stored in Node
        :param container: contains elements used in assign
        """
        self.node = Node(None)
        self.node.prev = self.node.next = self.node

        if num or container:
            self.assign(num, val, container)
Beispiel #6
0
    def insert(self, position: Node, val: Generic[T], num: int = 1) -> Node:
        """
        RECURSIVE
        Places node before given position with a value of val
        When num is given, insert num occurrences of node
        :param position: Node index to insert new node before
        :param val: value to insert
        :param num: number of insertions of val at position index
        :return: node that points to the first of the newly inserted nodes
        """
        new_node = Node(val, position, position.prev)

        if num < 1:
            return new_node
        position.prev.next = new_node
        position.prev = new_node
        return self.insert(position, val, num-1)
Beispiel #7
0
 def depth_limited_search(self, limit):
     par = Node(self.initial_state, (), None)
     return self.recursive_DLS(par, limit)