def dfs_solver(csp): """depth-first search solver""" path = Searcher(Search_from_CSP(csp)).search() if path is not None: return path.end() else: return None
def run(problem,name): print("\n\n*******",name) print("\nA*:") asearcher = AStarSearcher(problem) print("Path found:",asearcher.search()," cost=",asearcher.solution.cost) print("there are",asearcher.frontier.count(asearcher.solution.cost), "elements remaining on the queue with f-value=",asearcher.solution.cost) print("\nA* with MPP:"), msearcher = SearcherMPP(problem) print("Path found:",msearcher.search()," cost=",msearcher.solution.cost) print("there are",msearcher.frontier.count(msearcher.solution.cost), "elements remaining on the queue with f-value=",msearcher.solution.cost) bound = asearcher.solution.cost+0.01 print("\nBranch and bound (with too-good initial bound of", bound,")") tbb = DF_branch_and_bound(problem,bound) # cheating!!!! print("Path found:",tbb.search()," cost=",tbb.solution.cost) print("Rerunning B&B") print("Path found:",tbb.search()) bbound = asearcher.solution.cost*2+10 print("\nBranch and bound (with not-very-good initial bound of", bbound, ")") tbb2 = DF_branch_and_bound(problem,bbound) # cheating!!!! print("Path found:",tbb2.search()," cost=",tbb2.solution.cost) print("Rerunning B&B") print("Path found:",tbb2.search()) print("\nDepth-first search: (Use ^C if it goes on forever)") tsearcher = Searcher(problem) print("Path found:",tsearcher.search()," cost=",tsearcher.solution.cost)
def __init__(self, problem, pruning=None, method='astar', max_expanded=10000): """max_expanded is a bound on the number of paths expanded (to prevent infinite computation)""" self.method = method self.max_expanded = max_expanded self.pruning = pruning Searcher.__init__(self, problem, method) if self.pruning == 'mpp': self.explored = set()
def run(problem, name): print("\n\n*******", name) print("\nA*:") tsearcher = Searcher(problem) print("Path found:", tsearcher.search(), " cost=", tsearcher.solution.cost) print("there are", tsearcher.frontier.count(tsearcher.solution.cost), "elements remaining on the queue with f-value=", tsearcher.solution.cost) print("\nA* with MPP:"), msearcher = SearcherMPP(problem) print("Path found:", msearcher.search(), " cost=", msearcher.solution.cost) print("there are", msearcher.frontier.count(msearcher.solution.cost), "elements remaining on the queue with f-value=", msearcher.solution.cost) print("\nBranch and bound (with too-good initial bound):") tbb = DF_branch_and_bound(problem, tsearcher.solution.cost + 0.1) # cheating!!!! print("Path found:", tbb.search(), " cost=", tbb.solution.cost)
if self.csp.consistent(new_env): res.append(Arc(node, new_env)) return res from cspExamples import csp1, csp2, test, crossword1, crossword1d from searchGeneric import Searcher def dfs_solver(csp): """depth-first search solver""" path = Searcher(Search_from_CSP(csp)).search() if path is not None: return path.end() else: return None if __name__ == "__main__": test(dfs_solver) ## Test Solving CSPs with Search: searcher1 = Searcher(Search_from_CSP(csp1)) #print(searcher1.search()) # get next solution searcher2 = Searcher(Search_from_CSP(csp2)) #print(searcher2.search()) # get next solution searcher3 = Searcher(Search_from_CSP(crossword1)) #print(searcher3.search()) # get next solution searcher4 = Searcher(Search_from_CSP(crossword1d)) #print(searcher4.search()) # get next solution (warning: slow)
def ac_search_solver(csp): """arc consistency (search interface)""" sol = Searcher(Search_with_AC_from_CSP(csp)).search() if sol: return {v:select(d) for (v,d) in sol.end().items()}
res.append(Arc(node, new_env)) return res from cspExamples import csp1, csp2, test, crossword1, crossword1d from searchGeneric import Searcher def dfs_solver(csp): """depth-first search solver""" path = Searcher(Search_from_CSP(csp)).search() if path is not None: return path.end() else: return None if __name__ == "__main__": test(dfs_solver) ## Test Solving CSPs with Search: searcher1 = Searcher(Search_from_CSP(csp1)) #print(searcher1.search()) # get next solution #searcher2 = Searcher(Search_from_CSP(csp2)) #print(searcher2.search()) # get next solution #searcher3 = Searcher(Search_from_CSP(crossword1)) #print(searcher3.search()) # get next solution #searcher4 = Searcher(Search_from_CSP(crossword1d)) #print(searcher4.search()) # get next solution (warning: slow)