def uniformCostSearch(problem): """Search the node of least total cost first.""" "*** YOUR CODE HERE ***" tried = [] fringe = util.PriorityQueue() fringe.push(util.Node(problem.getStartState()), 0) # While there are still options while not fringe.isEmpty(): tryNode = fringe.pop() # Debugging: # print("Trying:", tryNode.state) # Are we there yet? if problem.isGoalState(tryNode.state): break # If n wasn't tried before if tryNode.state not in tried: tried.append(tryNode.state) # Add child nodes to search successors = problem.getSuccessors(tryNode.state) for child in successors: # state, parent, action, cost node = util.Node(child[0], tryNode, child[1], tryNode.path_cost + child[2]) # Haven't tried this node yet if node.state not in tried: fringe.push(node, node.path_cost) # Debugging: # print(node.path_cost) actions = [node.action for node in tryNode.path()] actions.pop(0) return actions
def breadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" root = util.Node() root.state = problem.getStartState() fringe = util.Queue() fringe.push(root) # keep track of already explored nodes closed_set = set() while not fringe.isEmpty(): node = fringe.pop() if problem.isGoalState(node.state): return node.path if node.state not in closed_set: closed_set.add(node.state) successors = problem.getSuccessors(node.state) for successor in successors: if successor[0] not in closed_set: element = util.Node() element.state = successor[0] element.dir = successor[1] element.path = node.path + [successor[1]] fringe.push(element) return []
def aStarSearch(problem, heuristic=nullHeuristic): """Search the node that has the lowest combined cost and heuristic first.""" "*** YOUR CODE HERE ***" closed = set() root_node = util.Node(problem.getStartState()) fringe = util.PriorityQueue() fringe.push(root_node, 0) while not fringe.isEmpty(): node = fringe.pop() end_state = node.state if problem.isGoalState(end_state): return node.solution() if end_state not in closed: closed.add(end_state) for next_state, action, cost in problem.getSuccessors(end_state): h = heuristic(next_state, problem) g = node.path_cost + cost priority = h + g child_node = util.Node(next_state, node, action, g) fringe.push(child_node, priority) print("H", h, "G", g, "PRIORITY", priority, "FRINGE", fringe) return None
def breadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" closed = set() root_node = util.Node(problem.getStartState()) fringe = util.Queue() fringe.push(root_node) while not fringe.isEmpty(): node = fringe.pop() end_state = node.state # print ("node = ", node) # print ("end_state = ", end_state) if problem.isGoalState(end_state): # print("node.solution = ", node.solution) return node.solution() if end_state not in closed: closed.add(end_state) # print("closed = ", closed) for next_state, action, cost in problem.getSuccessors(end_state): child_node = util.Node(next_state, node, action) # print ("next_state = ", next_state, "action = ", action, "cost = ", cost) fringe.push(child_node) return None
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first [2nd Edition: p 75, 3rd Edition: p 87] Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm [2nd Edition: Fig. 3.18, 3rd Edition: Fig 3.7]. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() print "Is the start a goal?", problem.isGoalState(problem.getStartState()) print "Start's successors:", problem.getSuccessors(problem.getStartState()) """ root_node = util.Node(problem.getStartState()) frontier_set = util.Stack() frontier_set.push(root_node) explored_set = util.Queue() while True: if frontier_set.isEmpty(): # no more frontier!! return None frontier = frontier_set.pop() if problem.isGoalState(frontier.state): return frontier.actions explored_set.push(frontier) for s in problem.getSuccessors(frontier.state): new_frontier = util.Node(s[0], s[1], s[2]) if (new_frontier not in frontier_set) and (new_frontier not in explored_set): new_frontier.update_parent(frontier) frontier_set.push(new_frontier)
def aStarSearch(problem, heuristic=nullHeuristic): """Search the node that has the lowest combined cost and heuristic first.""" root = util.Node() root.state = problem.getStartState() root.cost = 0 fringe = util.PriorityQueue() fringe.push(root, 0) closed_set = set() while not fringe.isEmpty(): node = fringe.pop() if problem.isGoalState(node.state): return node.path if node.state not in closed_set: closed_set.add(node.state) successors = problem.getSuccessors(node.state) for successor in successors: if successor[0] not in closed_set: heurval = heuristic(successor[0], problem) element = util.Node() element.state = successor[0] element.dir = successor[1] element.path = node.path + [successor[1]] element.priority = node.priority + successor[2] fringe.push(element, element.priority + heurval)
def uniformCostSearch(problem): """Search the node of least total cost first.""" root = util.Node() root.state = problem.getStartState() root.priority = 1 fringe = util.PriorityQueue() fringe.push(root, 1) closed_set = set() while not fringe.isEmpty(): node = fringe.pop() if node.state not in closed_set: closed_set.add(node.state) if problem.isGoalState(node.state): return node.path successors = problem.getSuccessors(node.state) for successor in successors: if successor[0] not in closed_set: element = util.Node() element.state = successor[0] element.dir = successor[1] element.path = node.path + [successor[1]] element.priority = node.priority + successor[2] fringe.push(element, element.priority) print(closed_set) return []
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first. Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: """ root = util.Node() root.state = problem.getStartState() fringe = util.Stack() fringe.push(root) # keep track of already explored nodes closed_set = set() while not fringe.isEmpty(): node = fringe.pop() if problem.isGoalState(node.state): return node.path if node.state not in closed_set: closed_set.add(node.state) successors = problem.getSuccessors(node.state) for successor in successors: if successor[0] not in closed_set: element = util.Node() element.state = successor[0] element.dir = successor[1] element.path = node.path + [successor[1]] fringe.push(element) return []
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first [2nd Edition: p 75, 3rd Edition: p 87] Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm [2nd Edition: Fig. 3.18, 3rd Edition: Fig 3.7]. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() print "Is the start a goal?", problem.isGoalState(problem.getStartState()) print "Start's successors:", problem.getSuccessors(problem.getStartState()) """ explored = set() frontier = util.Stack() frontier.push(util.Node((problem.getStartState(), None, None))) while not frontier.isEmpty(): node = frontier.pop() state = node.state if problem.isGoalState(state): return node.getPath() if state in explored: continue explored.add(state) for successor in problem.getSuccessors(state): if successor[0] not in explored: frontier.push(util.Node(successor, node)) return []
def aStarSearch(problem, heuristic=nullHeuristic): """Search the node that has the lowest combined cost and heuristic first.""" print("Doing an A* search") tried = set() fringe = util.PriorityQueue() fringe.push(util.Node(problem.getStartState()), 0) previous = () # While there are still options while not fringe.isEmpty(): tryNode = fringe.pop() # Debugging: # print("Trying:", tryNode.state) # Are we there yet? if problem.isGoalState(tryNode.state): break # If n wasn't tried before if tryNode.state not in tried: tried.add(tryNode.state) # Add child nodes to search successors = problem.getSuccessors(tryNode.state) for state, action, cost in successors: # state, parent, action, cost node = util.Node(state, tryNode, action, tryNode.path_cost + cost) # Haven't tried this node yet if node.state not in tried: currentHeuristic = node.path_cost + heuristic(node.state, problem) fringe.push(node, currentHeuristic) # Debugging: # print(node.path_cost) actions = [node.action for node in tryNode.path()] actions.pop(0) return actions
def breadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" tried = set() fringe = util.Queue() fringe.push(util.Node(problem.getStartState())) # While there are still options while not fringe.isEmpty(): tryNode = fringe.pop() # Are we there yet? if problem.isGoalState(tryNode.state): break # If n wasn't tried before if tryNode.state not in tried: tried.add(tryNode.state) # Add child nodes to search successors = problem.getSuccessors(tryNode.state) for child in successors: # state, parent, action, cost node = util.Node(child[0], tryNode, child[1], child[2]) # Haven't tried this node yet if node.state not in tried: fringe.push(node) # print(node.path_cost) actions = [node.action for node in tryNode.path()] actions.pop(0) return actions
def p_expression(p): ''' expression : expression PLUS expression | expression MINUS expression | expression DIVIDE expression | expression ASTERISK expression | expression PERCENTAGE expression ''' node = util.Node() if p[2] == '*': node.value = "MUL" node.addChild(p[1]) node.addChild(p[3]) if p[2] == '+': node.value = "PLUS" node.addChild(p[1]) node.addChild(p[3]) if p[2] == '/': node.value = "DIV" node.addChild(p[1]) node.addChild(p[3]) if p[2] == '-': node.value = "MINUS" node.addChild(p[1]) node.addChild(p[3]) p[0] = node
def runIntegrationTests(): # Load database createTestDatabase() exit() logFile = "samples/sample.log" watcherInstance = logwatch_manager.LogWatch(0, "Test Watcher", None) watcherInstance.setMatch( ("WHOLE", "EQ", "NOTE: Not using GLX TFP!", False, True), ()) watcherInstance.combineMatch(("WHOLE", "RE", ".*Anacron.*", False, False), "OR", ()) watcherInstance.combineMatch(("WHOLE", "RE", ".*anacron.*", False, False), "OR", (1, )) watcherInstance.combineMatch(("WHOLE", "RE", ".*ERROR.*", False, True), "OR", (0, )) watcherInstance.combineMatch(("WHOLE", "RE", ".*STREAM.*", False, True), "AND", (0, 1)) watcherInstance.combineMatch(("WHOLE", "RE", ".*dhcp.*", False, True), "OR", (0, 0)) watcherInstance.combineMatch(("WHOLE", "RE", ".*address.*", False, True), "AND", (0, 0, 1)) watcherInstance.combineMatch(("WHOLE", "RE", ".*rfkill.*", False, True), "OR", (0, 0, 0)) # Asserting if rules are loaded successfully with open("samples/sample_conf.json", "rb") as f: sampleRules = util.Node().loadJSON(json.load(f)["rules"]) assert sampleRules == watcherInstance.rules print("Rules are created successfully.", file=sys.stderr)
def p_num(p): ''' number : NUMBER ''' node = util.Node() node.value = "CONST(" + str(p[1]) + ")" p[0] = node
def p_name(p): ''' name : NAME ''' node = util.Node() node.value = "VAR(" + p[1] + ")" p[0] = node
def shortest_path(source, target): """ Returns the shortest list of (movie_id, person_id) pairs that connect the source to the target. If no possible path, returns None. """ # Build initial frontier with source as first node start = util.Node(source, parent=None, action=None) # frontier = util.StackFrontier() # Uncomment for Depth First Search frontier = util.QueueFrontier() # Uncomment for Breadth First Search frontier.add(start) # Explored set explored = [] # Main loop to find solution while True: # Check if frontier is empty if frontier.empty(): return None # Remove node from frontier working_node = frontier.remove() # Check if node is target if working_node.id == target: # If node is target return solution steps = [] while working_node.parent is not None: steps.append((working_node.movie, working_node.id)) working_node = working_node.parent steps.reverse() return steps # If node is not target add to explored set explored.append(working_node.movie) # If node is not target add new nodes to frontier neighbors = neighbors_for_person(working_node.id) for movie, name in neighbors: if movie not in explored: node = util.Node(name, working_node, movie) frontier.add(node)
def p_starname(p): ''' starname : ASTERISK name ''' node = util.Node() node.value = "DEREF" node.addChild(p[2]) p[0] = node
def p_andname(p): ''' andname : AMPERSAND name ''' node = util.Node() node.value = "ADDR" node.addChild(p[2]) p[0] = node
def p_uminus(p): ''' expression : MINUS expression %prec UMINUS ''' node = util.Node() node.value = "UMINUS" node.addChild(p[2]) p[0] = node
def uniformCostSearch(problem): "Search the node of least total cost first. " explored = set() frontier = util.PriorityQueueWithFunction(lambda node: node.pathCost) frontier.push(util.Node((problem.getStartState(), None, None))) while not frontier.isEmpty(): node = frontier.pop() state = node.state if problem.isGoalState(state): return node.getPath() if state in explored: continue explored.add(state) for successor in problem.getSuccessors(state): if successor[0] not in explored: frontier.push(util.Node(successor, node)) return []
def aStarSearch(problem, heuristic=nullHeuristic): "Search the node that has the lowest combined cost and heuristic first." explored = set() frontier = util.PriorityQueueWithFunction( lambda node: heuristic(node.state, problem)) frontier.push(util.Node((problem.getStartState(), None, None))) while not frontier.isEmpty(): node = frontier.pop() state = node.state if problem.isGoalState(state): return node.getPath() if state in explored: continue explored.add(state) for successor in problem.getSuccessors(state): if successor[0] not in explored: frontier.push(util.Node(successor, node)) return []
def uniformCostSearch(problem): "Search the node of least total cost first. " root_node = util.Node(problem.getStartState()) frontier_set = util.PriorityQueue() frontier_set.push(root_node, root_node.cost) explored_set = util.Queue() while True: if frontier_set.isEmpty(): # no more frontier!! return None frontier = frontier_set.pop() if problem.isGoalState(frontier.state): return frontier.actions explored_set.push(frontier) for s in problem.getSuccessors(frontier.state): new_frontier = util.Node(s[0], s[1], s[2]) if (new_frontier not in frontier_set) and (new_frontier not in explored_set): new_frontier.update_parent(frontier) frontier_set.push(new_frontier, new_frontier.cost)
def breadthFirstSearch(problem): """ Search the shallowest nodes in the search tree first. [2nd Edition: p 73, 3rd Edition: p 82] """ explored = set() frontier = util.Queue() frontier.push(util.Node((problem.getStartState(), None, None))) while not frontier.isEmpty(): node = frontier.pop() state = node.state if problem.isGoalState(state): return node.getPath() if state in explored: continue explored.add(state) for successor in problem.getSuccessors(state): if successor[0] not in explored: frontier.push(util.Node(successor, node)) return []
def aStarSearch(problem, heuristic=nullHeuristic): "Search the node that has the lowest combined cost and heuristic first." root_node = util.Node(problem.getStartState()) frontier_set = util.PriorityQueue() astar_cost = root_node.cost + heuristic(root_node.state, problem) frontier_set.push(root_node, astar_cost) explored_set = util.Queue() while True: if frontier_set.isEmpty(): # no more frontier!! return None frontier = frontier_set.pop() if problem.isGoalState(frontier.state): return frontier.actions explored_set.push(frontier) for s in problem.getSuccessors(frontier.state): new_frontier = util.Node(s[0], s[1], s[2]) if (new_frontier not in frontier_set) and (new_frontier not in explored_set): new_frontier.update_parent(frontier) astar_cost = new_frontier.cost + heuristic(new_frontier.state, problem) frontier_set.push(new_frontier, astar_cost)
def solve(self): self.num_explored = 0 start = util.Node(state=self.start, parent=None, action=None, goal=self.goal) frontier = util.AstarFrontier() frontier.add(start) self.explored = set() # find solution while True: if frontier.empty(): raise Exception("no solution") node = frontier.remove(self.goal) self.num_explored += 1 if node.state == self.goal: actions = [] cells = [] while node.parent is not None: actions.append(node.action) cells.append(node.parent) node = node.parent actions.reverse() cells.reverse() self.solution = (actions, cells) return self.explored.add(node.state) for action, state in self.neighbors(node.state): if (not frontier.contains_state(state) and state not in self.explored): child = util.Node(state=state, parent=node, action=action, goal=self.goal) frontier.add(child)
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first. Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() print "Is the start a goal?", problem.isGoalState(problem.getStartState()) print "Start's successors:", problem.getSuccessors(problem.getStartState()) """ "*** YOUR CODE HERE ***" tried = [] fringe = util.Stack() fringe.push(util.Node(problem.getStartState())) # While there are still options while not fringe.isEmpty(): tryNode = fringe.pop() # Debugging: # print("Trying:", tryNode.state) # Are we there yet? if problem.isGoalState(tryNode.state): break # If n wasn't tried before if tryNode.state not in tried: tried.append(tryNode.state) # Add child nodes to search successors = problem.getSuccessors(tryNode.state) for child in successors: # state, parent, action, cost node = util.Node(child[0], tryNode, child[1], child[2]) # Haven't tried this node yet if node.state not in tried: fringe.push(node) actions = [node.action for node in tryNode.path()] actions.pop(0) return actions
def breadthFirstSearch(problem): """ Search the shallowest nodes in the search tree first. [2nd Edition: p 73, 3rd Edition: p 82] """ root_node = util.Node(problem.getStartState()) frontier_set = util.Queue() frontier_set.push(root_node) explored_set = util.Queue() while True: if frontier_set.isEmpty(): # no more frontier!! return None frontier = frontier_set.pop() if problem.isGoalState(frontier.state): return frontier.actions explored_set.push(frontier) for s in problem.getSuccessors(frontier.state): new_frontier = util.Node(s[0], s[1], s[2]) if (new_frontier not in frontier_set) and (new_frontier not in explored_set): new_frontier.update_parent(frontier) frontier_set.push(new_frontier)
def p_assignstmt(p): ''' assignstmt : starname EQUALS expression | name EQUALS expression ''' node = util.Node() node.value = "ASGN" node.addChild(p[1]) node.addChild(p[3]) global count_assign count_assign += 1 global node_list node_list.append(node) p[0] = node
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first. Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: """ # print "Start:", problem.getStartState() # print "Is the start a goal?", problem.isGoalState(problem.getStartState()) # print "Start's successors:", problem.getSuccessors(problem.getStartState()) "*** YOUR CODE HERE ***" closed = set() root_node = util.Node(problem.getStartState()) fringe = util.Stack() fringe.push(root_node) while not fringe.isEmpty(): node = fringe.pop() end_state = node.state # print ("node = ", node) # print ("end_state = ", end_state) if problem.isGoalState(end_state): # print("node.solution = ", node.solution) return node.solution() if end_state not in closed: closed.add(end_state) # print("closed = ", closed) for next_state, action, cost in problem.getSuccessors(end_state): child_node = util.Node(next_state, node, action) # print ("next_state = ", next_state, "action = ", action, "cost = ", cost) fringe.push(child_node) return None
def uniformCostSearch(problem): """Search the node of least total cost first.""" "*** YOUR CODE HERE ***" closed = set() root_node = util.Node(problem.getStartState()) #next_state, action, cost = problem.getSuccessors(root_node) #priority = 0 fringe = util.PriorityQueue() fringe.push(root_node, 0) while not fringe.isEmpty(): node = fringe.pop() end_state = node.state if problem.isGoalState(end_state): return node.solution() if end_state not in closed: closed.add(end_state) for next_state, action, cost in problem.getSuccessors(end_state): priority = node.path_cost + cost child_node = util.Node(next_state, node, action, priority) print("CHILD NODE", child_node) fringe.push(child_node, priority) return None