示例#1
0
 def __init__(self, problem):
     """creates a searcher from a problem
     """
     self.problem = problem
     self.initialize_frontier()
     self.num_expanded = 0
     self.add_to_frontier(Path(problem.start_node()))
     super().__init__()
示例#2
0
 def result(self, path1, path2):
 	current = path2
 	result = path1
 	while current.arc != None:
 		result = Path(result, Arc(current.arc.to_node, current.arc.from_node, 1, current.arc.action))
 		current = current.initial
 	#print(result)
 	return result
示例#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
示例#4
0
 def __init__(self, problem):
     """creates a searcher from a problem
     """
     self.problem = problem
     self.initialize_frontier()
     self.initialize_f_frontier()
     self.initialize_b_frontier(
     )  # frontier for the bidirectional (back starting)
     self.num_f_expanded = 0
     self.num_b_expanded = 0
     self.add_to_f_frontier(Path(problem.start_node()))
     self.add_to_b_frontier(Path(
         problem.goal_node()))  # start state is the goal node
     super().__init__()
     self.max_display_level
     self.f_closed = set()
     self.b_closed = set()
     self.new_path = []
 def iterativeDeepeningSearch(self):
     while (self.hit_depth_bound):
         self.hit_depth_bound = False
         retVal = self.search(self.depth_bound)
         if retVal != None:
             return retVal
         self.depth_bound += 1
         self.initialize_frontier
         self.add_to_frontier(Path(self.problem.start_node()))
 def __init__(self, problem):
     """creates a searcher from a problem
     """
     self.problem = problem
     self.initialize_frontier()
     self.num_expanded = 0
     self.add_to_frontier(Path(problem.start_node()))
     super().__init__()
     self.max_display_level
     self.visited = set()  # pruning
示例#7
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.")
 def search(self):
     """returns an optimal solution to a problem with cost less than bound.
     returns None if there is no solution with cost less than bound."""
     self.frontier = [Path(self.problem.start_node())]
     self.num_expanded = 0
     while self.frontier:
         path = self.frontier.pop()
         if path.cost+self.problem.heuristic(path.end()) < self.bound:
             self.display(3,"Expanding:",path,"cost:",path.cost)
             self.num_expanded += 1
             if self.problem.is_goal(path.end()):
                 self.best_path = path
                 self.bound = path.cost
                 self.display(2,"New best path:",path," cost:",path.cost)
             else:
                 neighs = self.problem.neighbors(path.end())
                 self.display(3,"Neighbors are", neighs)
                 for arc in reversed(list(neighs)):
                     self.add_to_frontier(Path(path, arc))
     self.display(1,"Number of paths expanded:",self.num_expanded)
     self.solution = self.best_path
     return self.best_path
示例#9
0
 def __init__(self, problem):
     """creates a searcher from a problem
     """
     self.problem = problem
     self.initialize_frontier()
     self.num_expanded = 0
     self.add_to_frontier(Path(problem.start_node()))
     super().__init__()
     #self.max_display_level
     #self.visitedNode=[] #for the linear time
     #maybe needs to initialize a visited paths list--- to avoid the duplicates
     #have a set instead
     self.closed = []
示例#10
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)
示例#11
0
    def search(self, time_limit=20):
        """returns (next) path from the problem's start node
        to a goal node. 
        Returns None if no path exists.
        """
        if time_limit:
            start = time.time()
            skip = 0
            skip_limit = 100000  # only check the time this often

        while not self.empty_frontier():

            if time_limit:
                if skip < skip_limit:
                    skip += 1
                else:
                    skip = 0
                    run_time = time.time() - start
                    if run_time > time_limit:
                        self.display(1, "Time limit ", time_limit,
                                     "exceeded after", run_time, "seconds")
                        return None

            path = self.frontier.pop()
            self.display(2, "Expanding:", path, "(cost:", path.cost, ")")
            self.num_expanded += 1
            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
            else:
                neighs = self.problem.neighbors(path.end())
                self.display(3, "Neighbors are", neighs)
                for arc in reversed(neighs):
                    self.add_to_frontier(Path(path, arc))
                self.display(3, "Frontier:", self.frontier)
        self.display(1, "No (more) solutions. Total of", self.num_expanded,
                     "paths expanded.")
示例#12
0
 def search(self):
     """returns (next) path from the problem's start node
     to a goal node.
     Returns None if no path exists.
     """
     while not self.empty_frontier():
         path = self.frontier.pop(0)  # dir([])    pop
         self.display(2, "Expanding:", path, "(cost:", path.cost, ")")
         self.num_expanded += 1
         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
         else:
             neighs = self.problem.neighbors(path.end())
             self.display(3, "Neighbors are", neighs)
             for arc in reversed(neighs):
                 self.add_to_frontier(Path(path, arc))
             self.display(3, "Frontier:", self.frontier)
     self.display(1, "No (more) solutions. Total of",
                  self.num_expanded, "paths expanded.")
示例#13
0
    def search(self):
        """returns (next) path from the problem's start node
        to a goal node. 
        Returns None if no path exists.
        """
        count = 0
        indexClosed = 0

        while not self.empty_frontier():
            path = self.frontier.pop(0)

            self.display(2, "Expanding:", path, "(cost:", path.cost, ")")
            # self.visitedNode.append(nodeList)  # add path to the visitedNode list
            self.num_expanded += 1
            #print(path)
            if self.problem.is_goal(path.end()):
                # solution found
                print("Maximum closed list:  ", indexClosed)
                #print(path)
                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
            else:
                neighs = self.problem.neighbors(path.end())
                self.display(3, "Neighbors are", neighs)
                for arc in reversed(neighs):
                    if arc.to_node not in self.closed:
                        self.add_to_frontier(Path(path, arc))
                        self.closed.append(arc.to_node)
                        if (indexClosed < len(self.closed)):
                            indexClosed = len(self.closed)

                self.display(3, "Frontier:", self.frontier)
        self.display(1, "No (more) solutions. Total of", self.num_expanded,
                     "paths expanded.")
示例#14
0
 def __init__(self, problem):
     super().__init__(problem)
     self.frontier_state = []
     self.expanded_state = []
     self.add_to_frontier_state(Path(problem.start_node()).end())
示例#15
0
    def search(self):
        """returns next path from the problem's start node
        to a goal node.
        Returns None if no path exists.
        """
        if self.pruning == 'mpp':
            while not self.frontier.empty():
                path = self.frontier.pop()
                if path.end() not in self.explored:
                    self.display(2, "Expanding: ", path, "(cost:", path.cost,
                                 ")")
                    self.explored.add(path.end())
                    self.num_expanded += 1
                    if self.problem.is_goal(path.end()):
                        self.display(1, self.num_expanded,
                                     "paths have been expanded and",
                                     len(self.frontier.frontierpq),
                                     "paths remain in the frontier")
                        return path
                    else:
                        neighs = self.problem.neighbors(path.end())
                        for arc in neighs:
                            self.add_to_frontier(Path(path, arc))
                        self.display(3, "Frontier:", self.frontier)
        elif self.pruning == 'cycle':
            while not self.frontier.empty():
                path = self.frontier.pop()
                if path.end() not in path.initial_nodes(
                ):  # new part for cycle pruning
                    self.display(2, "Expanding: ", path, "(cost:", path.cost,
                                 ")")
                    self.num_expanded += 1
                    if self.problem.is_goal(path.end()):
                        self.display(1, self.num_expanded,
                                     "paths have been expanded and",
                                     len(self.frontier.frontierpq),
                                     "paths remain in the frontier")
                        return path
                    else:
                        neighs = self.problem.neighbors(path.end())
                        for arc in neighs:
                            self.add_to_frontier(Path(path, arc))
                        self.display(3, "Frontier:", elf.frontier)

        else:  # no pruning
            while not self.frontier.empty(
            ) and self.num_expanded < self.max_expanded:
                path = self.frontier.pop()
                self.display(2, "Expanding: ", path, "(cost:", path.cost, ")")
                self.num_expanded += 1
                if self.problem.is_goal(path.end()):
                    self.display(1, self.num_expanded,
                                 "paths have been expanded and",
                                 len(self.frontier.frontierpq),
                                 "paths remain in the frontier")
                    return path
                else:
                    neighs = self.problem.neighbors(path.end())
                    for arc in neighs:
                        self.add_to_frontier(Path(path, arc))
                    self.display(3, "Frontier:", self.frontier)

        self.display(1, "Total of", self.frontier.frontier_index,
                     "paths expanded.")
示例#16
0
    def search(self):
        """returns (next) path from the problem's start node
        to a goal node.
        Returns None if no path exists.
        """
        while not self.empty_f_frontier() and not self.empty_b_frontier():
            path = self.f_frontier.pop(0)
            if path.end() in self.b_closed:
                # print("intersect beforehand")
                path.list = self.merge_path(path, b_path)
                self.solution = path.list
                return path.list

            b_path = self.b_frontier.pop(0)
            if path.end() == b_path.end():
                # print("intersection on")
                path.list = self.merge_path(path, b_path)
                self.solution = path.list
                return path.list

            if path.end() not in self.f_closed:
                self.display(2, "Expanding:", path, "(cost:", path.cost, ")")

                self.f_closed.add(path.end())  # add path to the visited list
                self.num_f_expanded += 1

                if self.problem.is_goal(path.end()):  # solution found
                    self.display(1, self.num_f_expanded,
                                 "paths have been expanded and",
                                 len(self.f_frontier + self.b_frontier),
                                 "paths remain in the frontier")
                    self.solution = path  # store the solution found
                    return path
                else:
                    neighs = self.problem.neighbors(path.end())
                    self.display(3, "Neighbors are", neighs)
                    for arc in reversed(neighs):
                        self.add_to_f_frontier(Path(path, arc))
                    self.display(3, "Frontier:", self.f_frontier)

            if b_path.end() not in self.b_closed:
                self.display(2, "Expanding:", b_path, "(cost:", b_path.cost,
                             ")")
                self.b_closed.add(b_path.end())  # add path to the visited list
                self.num_b_expanded += 1

                if self.problem.is_start(b_path.end()):  # solution found
                    self.display(1, self.num_b_expanded,
                                 "paths have been expanded and",
                                 len(self.b_frontier + self.f_frontier),
                                 "paths remain in the frontier")
                    self.solution = b_path
                    return b_path
                else:
                    neighs = self.problem.neighbors(b_path.end(), True)
                    self.display(3, "Neighbors are", neighs)
                    for arc in reversed(neighs):
                        self.add_to_b_frontier(Path(b_path, arc))
                    self.display(3, "Frontier:", self.f_frontier)

        self.display(1, "No (more) solutions. Total of", self.num_f_expanded,
                     "paths expanded.")
示例#17
0
 def __init__(self,problem):
     super().__init__(problem)
     self.frontier_state=[]
     self.explored=set()
     self.explored.add(Path(problem.start_node()))