def uniform_cost_graph_search(problem): initial_node = Node(problem.initial_state(), None, 0, 0) frontier = [] frontier.append(initial_node) explored = set() counter = 0 while True: if not frontier: return (None, counter) else: counter += 1 n = heappop(frontier) explored.add(n.state) if problem.is_goal(n.state): return (n, counter) else: for s, c in problem.successors(n.state): if s not in explored: i = state_index(frontier, s) new_node = Node(s, n, n.depth + 1, n.cost + c) if i >= 0: if frontier[i].cost > new_node.cost: frontier[i] = new_node heapify(frontier) else: heappush(frontier, new_node)
def breadth_first_graph_search(problem): initial_node = Node(problem.initial_state(), None, 0, 0) frontier = deque() frontier.append(initial_node) # Initialize "explored" as a set to keep the states checked explored = set() counter = 0 while True: if not frontier: return (None, counter) else: counter += 1 n = frontier.popleft() # Add the state to "explored" explored.add(n.state) if problem.is_goal(n.state): return (n, counter) else: for s, c in problem.successors(n.state): # Add "s" to the frontier list only when it is # not in the explored set nor in the frontier list if s not in explored and state_index(frontier, s) < 0: new_node = Node(s, n, n.depth + 1, n.cost + c) frontier.append(new_node)
def enqueue(self, value): if self.has_space(): item_to_add = Node(value) print("Adding " + str(item_to_add.get_value()) + " to the queue!") if self.is_empty(): self.head = item_to_add self.tail = item_to_add else: self.tail.set_next_node(item_to_add) self.tail = item_to_add self.size += 1 else: print("Sorry, no more room!")
def depth_first_tree_search(problem): initial_node = Node(problem.initial_state(), None, 0, 0) frontier = deque() frontier.append(initial_node) counter = 0 while True: if not frontier: return (None, counter) else: counter += 1 n = frontier.pop() if problem.is_goal(n.state): return (n, counter) else: for s, c in problem.successors(n.state): new_node = Node(s, n, n.depth + 1, n.cost + c) frontier.append(new_node)
def depth_limited_graph_search(problem, limit): initial_node = Node(problem.initial_state(), None, 0, 0) frontier = deque() frontier.append(initial_node) explored = set() counter = 0 while True: if not frontier: return (None, counter) else: counter += 1 n = frontier.pop() explored.add(n.state) if problem.is_goal(n.state): return (n, counter) elif n.depth < limit: for s, c in problem.successors(n.state): if s not in explored and state_index(frontier, s) < 0: new_node = Node(s, n, n.depth + 1, n.cost + c) frontier.append(new_node)
def breadth_first_tree_search(problem): # Create the initial node containing the initial state initial_node = Node(problem.initial_state(), None, 0, 0) # Initialize a deque storing nodes to be visited frontier = deque() frontier.append(initial_node) counter = 0 while True: # Is frontier empty? if not frontier: return (None, counter) else: counter += 1 # Remove a node from the frontier n = frontier.popleft() # Return the node if it contains a goal state if problem.is_goal(n.state): return (n, counter) else: # Generate successors and add to the frontier for s, c in problem.successors(n.state): new_node = Node(s, n, n.depth + 1, n.cost + c) frontier.append(new_node)
def generateNodes(self): nodes = [] for row in range(self.gridsize): nodes.append([]) for column in range(self.gridsize): chance = self.DENSITY * .1 if random.random() <= chance and self.map[row][column] == 0: """node = Node(self.screen, row, column) node.drawx = 1+(40*row) node.drawy = 1+(40*column) self.nodeGroup.add(node) self.total_nodes += 1""" nodes[row].append(2) self.map[row][column] = 2 location = (1+20*column+column, 20*row+row) node = Node(2, location) self.nodeGroup.add(node) print(location)