def depth_first(self, xy1, xy2):
        """Execute a depth first search."""
        tile_col1, tile_row1 = self.the_map.xy_to_cr(xy1[0], xy1[1])
        tile_col2, tile_row2 = self.the_map.xy_to_cr(xy2[0], xy2[1])

        successor_to_parent_map = {}
        start_state = (tile_col1, tile_row1)
        successor_to_parent_map[(
            start_state,
            None)] = None  # (Successor, Action) -> (Parent, Action)

        open_list = Stack()
        open_list.push((start_state, None))
        closed = []

        while not open_list.isEmpty():
            current_state, action_to_current_state = open_list.pop()

            if current_state == (tile_col2, tile_row2):
                return self.__get_action_path(
                    (current_state, action_to_current_state),
                    successor_to_parent_map)

            if current_state not in closed:
                for successor_state, action, step_cost in self.__get_successors(
                        current_state):
                    open_list.push((successor_state, action))

                    if successor_state not in closed:
                        successor_to_parent_map[(successor_state, action)] = (
                            current_state, action_to_current_state)

            closed.append(current_state)
        return []
 def depth_limited(problem, limit):
     """Depth Limited Search"""
     frontier = Stack()
     start_node = Node(problem['start'])
     frontier.push(start_node)
     closed_set = set()
     while not frontier.isEmpty():
         node = frontier.pop()
         closed_set.add(node.value)
         if goal_test(problem, node):
             path = node.get_path()
             cost = cal_path_cost(problem['graph'], path)
             return path, cost, frontier.count
         if node.depth == limit:
             pass
         else:
             # Expansion
             adj = problem['graph'].adj(node.value)
             for child_value in quicksort(adj):
                 if push_or_not(problem, node, child_value, closed_set):
                     child_node = Node(child_value, node)
                     frontier.push(child_node)
     return None
예제 #3
0
    def convert(self, verbose):
        self.verbose = verbose
        operators = "^*+-\/"
        # operands = string.ascii_lowercase + string.digits
        output = Queue()
        stack = Stack()

        for token in self.infix:
            operands = re.match("^[a-z]$|^\d+\.\d+$|^\d+$", token)
            operators = re.match("^[+-\/*^]$", token)
            # if token is a number or variable add it to putput
            if operands:
                output.push(token)
                if self.verbose == True:
                    print "1 token = %s, output = %s" % (token, output.list[::-1])
                # if the token is variable add it mapVariable dictionary
                if token in string.ascii_lowercase:
                    self.mapVariables[token] = ""
            # if token is an operator
            elif operators:
                # while there is another operator on the stack
                while not stack.isEmpty():
                    # if operator is lef-associative and its precedence is less than or equal to that on stack, or operator has precedence less than that on stack (is not left-associative)
                    if stack.lastOnStack() != "(" and (
                        (
                            self.precedence[token][0] <= self.precedence[stack.lastOnStack()][0]
                            and self.precedence[token][1] == "L"
                        )
                        or self.precedence[token][0] < self.precedence[stack.lastOnStack()][0]
                    ):
                        # push operator to output from stack
                        output.push(stack.pop())
                        if self.verbose == True:
                            print "2 token = %s, output = %s" % (token, output.list[::-1])
                    else:
                        break
                # push operator to stack
                stack.push(token)
                if self.verbose == True:
                    print "3 token = %s, stack = %s" % (token, stack.list[::-1])
            # if token is left parenthesis push it to stack
            elif token == "(":
                stack.push(token)
                if self.verbose == True:
                    print "4 token = %s, stack = %s" % (token, stack.list[::-1])
            # if token is right parenthesis
            elif token == ")":
                # until token at the top of stack is not left parethesis
                while stack.lastOnStack() != "(":
                    # push from stack to output
                    output.push(stack.pop())
                    if self.verbose == True:
                        print "5 token = %s, output = %s" % (token, output.list[::-1])
                # and pop left parethesis from stack but not to output
                stack.pop()
        if self.verbose == True:
            print "Left on stack " + str(stack.list[::-1])
            print "Output " + str(output.list)
        self._rpn = output.list[::-1]
        while len(stack.list) > 0:
            self._rpn.append(stack.pop())
        if self.verbose == True:
            print "RPN value = " + str(self._rpn)
        return self._rpn
예제 #4
0
    def convert(self, verbose):
        self.verbose = verbose
        operators = '^*+-\/'
        #operands = string.ascii_lowercase + string.digits
        output = Queue()
        stack = Stack()

        for token in self.infix:
            operands = re.match('^[a-z]$|^\d+\.\d+$|^\d+$', token)
            operators = re.match('^[+-\/*^]$', token)
            # if token is a number or variable add it to putput
            if operands:
                output.push(token)
                if self.verbose == True:
                    print "1 token = %s, output = %s" % (token,
                                                         output.list[::-1])
                # if the token is variable add it mapVariable dictionary
                if token in string.ascii_lowercase:
                    self.mapVariables[token] = ''
            #if token is an operator
            elif operators:
                # while there is another operator on the stack
                while not stack.isEmpty():
                    # if operator is lef-associative and its precedence is less than or equal to that on stack, or operator has precedence less than that on stack (is not left-associative)
                    if stack.lastOnStack() != '(' and (
                        (self.precedence[token][0] <=
                         self.precedence[stack.lastOnStack()][0]
                         and self.precedence[token][1] == 'L')
                            or self.precedence[token][0] <
                            self.precedence[stack.lastOnStack()][0]):
                        # push operator to output from stack
                        output.push(stack.pop())
                        if self.verbose == True:
                            print "2 token = %s, output = %s" % (
                                token, output.list[::-1])
                    else:
                        break
                # push operator to stack
                stack.push(token)
                if self.verbose == True:
                    print "3 token = %s, stack = %s" % (token,
                                                        stack.list[::-1])
            # if token is left parenthesis push it to stack
            elif token == '(':
                stack.push(token)
                if self.verbose == True:
                    print "4 token = %s, stack = %s" % (token,
                                                        stack.list[::-1])
            # if token is right parenthesis
            elif token == ')':
                # until token at the top of stack is not left parethesis
                while stack.lastOnStack() != '(':
                    # push from stack to output
                    output.push(stack.pop())
                    if self.verbose == True:
                        print "5 token = %s, output = %s" % (token,
                                                             output.list[::-1])
                # and pop left parethesis from stack but not to output
                stack.pop()
        if self.verbose == True:
            print "Left on stack " + str(stack.list[::-1])
            print "Output " + str(output.list)
        self._rpn = output.list[::-1]
        while len(stack.list) > 0:
            self._rpn.append(stack.pop())
        if self.verbose == True:
            print "RPN value = " + str(self._rpn)
        return self._rpn