def dfs(problem): """ Depth first graph search algorithm - implemented for you 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 or None if there is no solution """ closed = set() # keep track of our explored states fringe = data_structures.Stack() # for dfs, the fringe is a stack state = problem.start_state() root = data_structures.Node(state) fringe.push(root) 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) fringe.push(child_node)
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 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 bfs(problem): """ Breadth 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 or None if there is no solution """ closed = set() fringe = data_structures.Queue() state = problem.start_state() root = data_structures.Node(state) fringe.push(root) 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) fringe.push(child_node)
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 bfs(problem): """ Breadth 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 or None if there is no solution """ # Enter your code here and remove the pass statement below #pass closed = set() # keep track of our explored states fringe = data_structures.Queue() state = problem.start_state() root = data_structures.Node(state, None, None) fringe.push(root) 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) fringe.push(child_node) 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() # 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 create_nodes(node_file_name, disp_file_name, force_file_name): nodes = [] node_file = open(node_file_name, 'r') # read the total num nodes node_file.readline() for line in node_file: # input files have tabs vals = line.strip().split(' ')[1:] # append node to list of nodes nodes.append(ds.Node(float(vals[0]), float(vals[1]))) node_file.close() # read displacements and add the knowns in disp_file = open(disp_file_name, 'r') disp_file.readline() for line in disp_file: vals = line.strip().split(' ') if len(vals) < 3: continue # input the displacement into the corresponding dof nodes[int(vals[0]) - 1].dofs[int(vals[1]) - 1].disp = float(vals[2]) disp_file.close() # finally read forces and input into nodes force_file = open(force_file_name, 'r') force_file.readline() for line in force_file: vals = line.strip().split(' ') if len(vals) < 3: continue # known forces are input into the corresponding dof nodes[int(vals[0]) - 1].dofs[int(vals[1]) - 1].force = float(vals[2]) force_file.close() return nodes
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)