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 sls_solver(csp, prob_best=0.7): """stochastic local searcher (with prob_best=0.7)""" se0 = SLSearcher(csp) se0.search(1000, prob_best) return se0.current_assignment def any_conflict_solver(csp): """stochastic local searcher (any-conflict)""" return sls_solver(csp, 0) if __name__ == "__main__": test(sls_solver) test(any_conflict_solver) from cspExamples import csp1, csp2, crossword1 ## Test Solving CSPs with Search: #se1 = SLSearcher(csp1); print(se1.search(100)) #se2 = SLSearcher(csp2); print(se2.search(1000,1.0)) # greedy #se2 = SLSearcher(csp2); print(se2.search(1000,0)) # any_conflict #se2 = SLSearcher(csp2); print(se2.search(1000,0.7)) # 70% greedy; 30% any_conflict #SLSearcher.max_display_level=2 #more detailed display #se3 = SLSearcher(crossword1); print(se3.search(100),0.7) #p = Runtime_distribution(csp2) #p.plot_run(1000,1000,0) # any_conflict #p.plot_run(1000,1000,1.0) # greedy #p.plot_run(1000,1000,0.7) # 70% greedy; 30% any_conflict
def select(iterable): """select an element of iterable. Returns None if there is no such element. This implementation just picks the first element. For many of the uses, which element is selected does not affect correctness, but may affect efficiency. """ for e in iterable: return e # returns first element found from cspExamples import test def ac_solver(csp): "arc consistency (solve_one)" return Con_solver(csp).solve_one() if __name__ == "__main__": test(ac_solver) from searchProblem import Arc, Search_problem class Search_with_AC_from_CSP(Search_problem,Displayable): """A search problem with arc consistency and domain splitting A node is a CSP """ def __init__(self, csp): self.cons = Con_solver(csp) #copy of the CSP self.domains = self.cons.make_arc_consistent() def is_goal(self, node): """node is a goal if all domains have 1 element""" return all(len(node[var])==1 for var in node)
newDomain = [] elif newDomain[1] == "midday": print("midday") elif newDomain[1] == "late-afternoon,": print("lateAfternoon") constraintList = [] from constraint import sameDay, oneDayBetween, oneHourBetween, before for constraint in constraints: if constraint[1] == "before": C = Constraint((constraint[0], constraint[2]), before) elif constraint[1] == "same-day": C = Constraint((constraint[0], constraint[2]), sameDay) elif constraint[1] == "one-day-between": C = Constraint((constraint[0], constraint[2]), oneDayBetween) elif constraint[1] == "one-hour-between": C = Constraint((constraint[0], constraint[2]), oneHourBetween) constraintList.append(C) csp1 = CSP(newmeetingsDict, constraintList) from operator import lt, ne, eq, gt from cspExamples import test, ne_ from searchGeneric import Searcher, AStarSearcher #Applying AStar search on it? from display import Displayable, visualize from cspConsistency import Search_with_AC_from_CSP, ac_search_solver test(ac_search_solver, csp1)