def astar(problem, heuristic): """ A* graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py heuristic (a function) the heuristic function to be used :return: list of actions representing the solution to the quest or None if there is no solution """ # Enter your code here and remove the pass statement below closed = set() # keep track of our explored states fringe = data_structures.PriorityQueue() state = problem.start_state() root = data_structures.Node(state, None, None) fringe.push(root, root.cumulative_cost + heuristic(state, problem)) while not fringe.is_empty(): node = fringe.pop() if problem.is_goal(node.state): return node.solution() # we found a solution if node.state not in closed: # we are implementing graph search closed.add(node.state) for child_state, action, action_cost in problem.expand(node.state): child_node = data_structures.Node(child_state, node, action) child_node.cumulative_cost = node.cumulative_cost + action_cost fringe.push(child_node, child_node.cumulative_cost + heuristic(child_state, problem)) return None # Failure - no solution was found
def ucs(problem): """ Uniform cost first graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py) :return: list of actions representing the solution to the quest """ closed = set() frontier = data_structures.PriorityQueue() state = problem.start_state() root = data_structures.Node(state) frontier.push(root, 0) while True: if frontier.is_empty(): return None node = frontier.pop() # remove s with smallest priority from frontier if node.state not in closed: closed.add(node.state) # add s to explored if problem.is_goal(node.state): return node.solution() for child_state, action, action_cost in problem.successors( node.state): child_node = data_structures.Node(child_state, node, action) child_node.cumulative_cost = node.cumulative_cost + action_cost # update frontier with node and priority + cost frontier.push(child_node, child_node.cumulative_cost)
def ucs(problem): """ Uniform cost first graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py) :return: list of actions representing the solution to the quest """ # Enter your code here and remove the pass statement below closed = set() # keep track of our explored states fringe = data_structures.PriorityQueue() # for UCS, the fringe is a ???? state = problem.start_state() root = data_structures.Node(state) fringe.push(root, root.cumulative_cost) while True: if fringe.is_empty(): return None # Failure - fringe is empty and no solution was found node = fringe.pop() if problem.is_goal(node.state): return node.solution() if node.state not in closed: # we are implementing graph search closed.add(node.state) for child_state, action, action_cost in problem.successors( node.state): child_node = data_structures.Node( child_state, node, action, (node.cumulative_cost + action_cost)) fringe.push(child_node, child_node.cumulative_cost)
def ucs(problem): """ Uniform cost first graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py) :return: list of actions representing the solution to the quest """ # Enter your code here and remove the pass statement below closed = set() fringe = data_structures.PriorityQueue() state = problem.start_state() root = data_structures.Node(state) fringe.push(root, 0) while True: if fringe.is_empty(): return None node = fringe.pop() if problem.is_goal(node.state): return node.solution() if node.state not in closed: closed.add(node.state) for child_state, action, action_cost in problem.successors( node.state): child_node = data_structures.Node(child_state, node, action) child_node.cumulative_cost = node.cumulative_cost + action_cost fringe.push(child_node, child_node.cumulative_cost)
def ucs(problem): """ Uniform cost first graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py) :return: list of actions representing the solution to the quest """ closed = set() # keep track of our explored fringe = data_structures.PriorityQueue( ) # for ucs, the fringe is a Priority queue state = problem.start_state() root = data_structures.Node(state, None, None) fringe.push(root, root.cumulative_cost) while not fringe.is_empty(): node = fringe.pop() if problem.is_goal(node.state): return node.solution() # we found a solution if node.state not in closed: # we are implementing graph search closed.add(node.state) for child_state, action, action_cost in problem.expand(node.state): child_node = data_structures.Node( child_state, node, action, node.cumulative_cost + action_cost) fringe.push(child_node, child_node.cumulative_cost) return None
def astar(problem, heuristic): """ A* graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py heuristic (a function) the heuristic function to be used :return: list of actions representing the solution to the quest or None if there is no solution """ # Enter your code here and remove the pass statement below closed = set() fringe = data_structures.PriorityQueue() state = problem.start_state() root = data_structures.Node(state) fringe.push(root, heuristic(state, problem)) while True: if fringe.is_empty(): return None node = fringe.pop() if problem.is_goal(node.state): return node.solution() if node.state not in closed: closed.add(node.state) for child_state, action, action_cost in problem.successors( node.state): # Tie Breaking h = heuristic(child_state, problem) # goal = problem.medals.copy().pop() # start = problem.start_state()[0] # dx1 = state[0][0] - goal[0] # dy1 = state[0][1] - goal[1] # dx2 = start[0] - goal[0] # dy2 = start[1] - goal[1] # cross = abs(dx1*dy2 - dx2*dy1) # h += cross*0.001 h *= (1.0 + (1 / 100)) # if child_state not in closed: child_node = data_structures.Node( child_state, node, action, action_cost + node.cumulative_cost) f = child_node.cumulative_cost + h fringe.push(child_node, f)
assert dequewithwrapper.size() == 3 # test object reference is returned dequewithobj = ds.Deque([[0, 1], [2, 3], [4, 5]]) # deque of lists (dequewithobj.peek_first())[0] = 6 assert (dequewithobj.peek_first())[0] == 6 assert (dequewithobj.poll())[0] == 6 (dequewithobj.peek_last())[0] = 10 assert (dequewithobj.peek_last())[0] == 10 assert (dequewithobj.pop())[0] == 10 #================================== # HEAP TESTS #================================== heap = ds.PriorityQueue() assert heap.size() == 0 assert heap.isEmpty() heap.offer('d') heap.offer('b') heap.offer('c') heap.offer('g') heap.offer('a') heap.offer('z') heap.offer('m') # check correct order largerpoll = chr(ord('z') + 1) # guaranteed to be larger than 'z' while (not heap.isEmpty()): smallerpoll = heap.poll()