Exemple #1
0
    def search(self):
        """returns (next) path from the problem's start node
        to a goal node.
        Returns None if no path exists.
        """
        paths = Path(None)
        while not self.empty_frontier():
            paths = self.frontier.pop(0)
            self.add_to_expanded_state(paths.end())
            self.frontier_state.remove(paths.end())
            self.display(2, "expanding:", paths, "(cost:", paths.cost, ")")
            self.num_expanded += 1

            neighs = self.problem.neighbors(paths.end())
            self.display(3, "Neighbors are", neighs)
            for arc in neighs:
                if (arc.to_node not in self.expanded_state) or (
                        arc.to_node in self.frontier_state):
                    self.add_to_frontier(Path(paths, arc))
                    self.frontier_state.append(Path(paths, arc).end())
                    self.display(3, "Frontier:", self.frontier)
                    if self.problem.is_goal(arc.to_node):
                        self.display(1, self.num_expanded,
                                     "paths have been expanded and",
                                     len(self.frontier),
                                     "paths remain in the frontier")
                        self.solution = Path(paths, arc)
                        return Path(paths, arc)
        self.display(1, "No (more) solutions. Total of", self.num_expanded,
                     "paths expanded.")
Exemple #2
0
 def search(self):
     path=Path(None)
     while not self.empty_frontier():
         print()
         print("explored: ",self.explored)
         print("front: ",self.frontier)
         path=self.frontier.pop(0)
         #self.explored.add(path)
         print("path:",path,"  pathend",path.end())
         neighs = self.problem.neighbors(path.end())
         print("neighs: ",neighs)
         self.display(3, "Neighbors are", neighs)
         self.num_expanded+=1
         for arc in neighs:
             print("arc: ",arc," arc_to:",arc.to_node)
             if arc.to_node not in self.explored:
                 print("not have visited: ",arc.to_node)
                 self.add_to_frontier(Path(path,arc))
                 #self.explored.add(Path(path,arc))
                 self.explored.add(arc.to_node)
                 if self.problem.is_goal(path.end()):  # solution found
                     self.display(1, self.num_expanded, "paths have been expanded and",
                                  len(self.frontier), "paths remain in the frontier")
                     self.solution = path  # store the solution found
                     return path
     self.display(1, "No (more) solutions. Total of",
                  self.num_expanded, "paths expanded.")
Exemple #3
0
 def search(self):
     """returns (next) path from the problem's start node
     to a goal node.
     Returns None if no path exists.
     """
     path = Path(None)
     while not self.empty_frontier():
         # pop the first one
         path = self.frontier.pop(0)
         self.add_to_expended_state(path.end())
         self.frontier_state.remove(path.end())
         self.display(2, "Expanding:", path, "(cost:", path.cost, ")")
         self.num_expanded += 1
         neighs = self.problem.neighbors(path.end())
         self.display(3, "Neighbors are", neighs)
         for arc in neighs:
             if (arc.to_node not in self.expended_state) or (
                     arc.to_node in self.frontier_state):
                 self.add_to_frontier(Path(path, arc))
                 self.frontier_state.remove(path.end())
                 self.display(3, "Frontier:", self.frontier)
                 if self.problem.is_goal(path.end()):
                     self.display(1, self.num_expanded,
                                  "paths have been expanded and",
                                  len(self.frontier),
                                  "paths remain in the frontier")
                     self.solution = path  # store the solution found
                     return path  # then stop the searcher
Exemple #4
0
    def search(self):
        path = Path(None)
        while not self.empty_frontier():
            path = self.frontier.pop(0)
            self.add_to_expanded_city(path.end())

            self.frontier_city.remove(path.end())
            self.display(2, "Expanding:", path, "(cost:", path.cost, ")")
            self.num_expanded += 1

            neighbors = self.problem.neighbors(path.end())
            self.display(3, "Neighbors are", neighbors)

            for arc in neighbors:
                if (arc.to_node not in self.expanded_city) or (arc.to_node in self.frontier_city):
                    self.add_to_frontier(Path(path, arc))
                    self.frontier_city.append(Path(path, arc).end())
                    self.display(3, "Frontier:", self.frontier)
                    if self.problem.is_goal(arc.to_node):  # solution found
                        self.display(1, self.num_expanded, "paths have been expanded and",
                                     len(self.frontier), "paths remain in the frontier")
                        self.solution = Path(path, arc)
                        return Path(path, arc)