示例#1
0
def search(grid,init,goal,cost):
    # ----------------------------------------
    # insert code here
    # ----------------------------------------
    Nr=len(grid)
    Nc=len(grid[0])

    def childNode(grid, loc, delta, delta_name):
        child=[]
        for i in range(len(delta)):
            move=delta[i]
            newloc=[loc[0]+move[0], loc[1]+move[1]]
            if loc[0]+move[0]>=0 and loc[0]+move[0]<Nr \
            and loc[1]+move[1]>=0 and loc[1]+move[1]<Nc \
            and grid[newloc[0]][newloc[1]] ==0:
                child.append([newloc,delta_name[i]])
        return child

    visited=[]
    frontier=Queue()
    frontier.push([init,0])

    while not frontier.isEmpty():
        loc, rlen=frontier.pop()
        if not loc in visited:
            visited.append(loc)
            if loc==goal:
                return [rlen, loc[0], loc[1]]

            for nextloc, move_name in childNode(grid, loc, delta, delta_name):
                if not nextloc in visited:
                    frontier.push([nextloc, rlen+cost])
    return 'fail'
def search(grid, init, goal, cost):
    # ----------------------------------------
    # insert code here
    # ----------------------------------------
    Nr = len(grid)
    Nc = len(grid[0])
    expand = [[-1 for j in range(Nc)] for i in range(Nr)]
    policy = [[' ' for j in range(Nc)] for i in range(Nr)]
    path = [[' ' for j in range(Nc)] for i in range(Nr)]
    count = 0

    def childNode(grid, loc, delta, delta_name):
        child = []
        for i in range(len(delta)):
            move = delta[i]
            newloc = [loc[0] + move[0], loc[1] + move[1]]
            if loc[0]+move[0]>=0 and loc[0]+move[0]<Nr \
            and loc[1]+move[1]>=0 and loc[1]+move[1]<Nc \
            and grid[newloc[0]][newloc[1]] ==0:
                child.append([newloc, delta_name[i]])
        return child

    visited = []
    frontier = Queue()
    frontier.push([init, ' ', 0])

    while not frontier.isEmpty():
        loc, mov, rlen = frontier.pop()
        if not loc in visited:
            visited.append(loc)
            policy[loc[0]][loc[1]] = mov

            if loc == goal:
                path[goal[0]][goal[1]] = '*'
                while loc != init:
                    x, y = loc
                    if policy[x][y] == 'v':
                        loc = [x - 1, y]
                        path[loc[0]][loc[1]] = 'v'
                    elif policy[x][y] == '^':
                        loc = [x + 1, y]
                        path[loc[0]][loc[1]] = '^'
                    elif policy[x][y] == '>':
                        loc = [x, y - 1]
                        path[loc[0]][loc[1]] = '>'
                    elif policy[x][y] == '<':
                        loc = [x, y + 1]
                        path[loc[0]][loc[1]] = '<'

                return path

            for nextloc, move_name in childNode(grid, loc, delta, delta_name):
                if not nextloc in visited:
                    frontier.push([nextloc, move_name, rlen + cost])
    return 'fail'
def search(grid, init, goal, cost):
    # ----------------------------------------
    # modify code below
    # ----------------------------------------
    Nr = len(grid)
    Nc = len(grid[0])
    expand = [[-1 for j in range(Nc)] for i in range(Nr)]
    count = 0

    def childNode(grid, loc, delta, delta_name):
        child = []
        for i in range(len(delta)):
            move = delta[i]
            newloc = [loc[0] + move[0], loc[1] + move[1]]
            if loc[0]+move[0]>=0 and loc[0]+move[0]<Nr \
            and loc[1]+move[1]>=0 and loc[1]+move[1]<Nc \
            and grid[newloc[0]][newloc[1]] ==0:
                child.append([newloc, delta_name[i]])
        return child

    visited = []
    frontier = Queue()
    frontier.push([init, 0])

    while not frontier.isEmpty():
        loc, rlen = frontier.pop()
        if not loc in visited:
            visited.append(loc)
            expand[loc[0]][loc[1]] = count
            count += 1

            if loc == goal:
                return expand

            for nextloc, move_name in childNode(grid, loc, delta, delta_name):
                if not nextloc in visited:
                    frontier.push([nextloc, rlen + cost])
    return 'fail'
示例#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
def general_search(problem):
    """Problem: {doamin, method, graph, coords, start, goal}
    method: B, D, I, U, A

    route: graph search (maintain a closed set) except Astar
    tsp: tree search
    """
    method = problem['method']

    if method not in ['B', 'D', 'I', 'U', 'A']:
        print("Enter Correct method!")
        return None
    if problem['start'] not in problem['graph'].get_nodes() or (
            problem['domain'] == 'route' and problem['goal'] not in problem['graph'].get_nodes()):
        print("Enter Correct Start/Goal!")
        return None

    # BFS, DFS
    if method in ['B', 'D']:
        if method == 'B':
            frontier = Queue()
        else:
            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

            # Expansion
            closed_set.add(node.value)
            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

    """Uniform Cost Search / Astar Search"""
    if method in ['U', 'A']:
        frontier = PriorityQueue()
        start_node = Node(problem['start'])
        priority = 0 if method == 'U' else astar_heuristic(problem, start_node)
        frontier.push(priority, 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
            # Expansion
            adj = problem['graph'].adj(node.value)
            for child_value in quicksort(adj):
                if push_or_not(problem, node, child_value, closed_set):
                    child_cost = node.cost_so_far + \
                        problem['graph'].get_cost(node.value, child_value)  # g_n
                    child_node = Node(child_value, node, child_cost)
                    priority = child_cost if method == 'U' else child_cost + \
                        astar_heuristic(problem, child_node)
                    frontier.push(priority, child_node)
        return None

    """Iterative Deepening Search"""
    if method == 'I':
        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
        max_depth = 20
        for i in range(max_depth):
            result = depth_limited(problem, i)
            if result:
                return result
示例#6
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