def solve(self, problem): # TODO: implement backtracking search. # TODO: add any supporting function you need domains = init_domains() restrict_domain(domains, problem) stack = [] while True: if not self.propagate(domains): is_consistent = True for spot in sd_spots: if len(domains[spot]) != 1: is_consistent = False break if is_consistent: return domains reset_domain = copy.deepcopy(domains) spot, val = self.search(domains) stack.append((spot, val, reset_domain)) else: if len(stack) == 0: return None domains = self.backtrack(stack, domains)
def solve(self, problem): domains = init_domains() restrict_domain(domains, problem) back_track_stack = [] # TODO: implement backtracking search. while True: result = self.propagate(problem, domains) if result: # You have found your solution if self.all_values_assigned(domains): #print("Problem is completed") clean_domain = self.clean_domain(domains) return clean_domain # Make a decision and then propagate everything else: domain_copy = copy.deepcopy(domains) var, val = self.make_decision(problem, domains) back_track_stack.append((domain_copy, var, val)) domains[var] = [val] # Here, we apply the decision # This corresponds to an error else: # There is no solution if len(back_track_stack) == 0: return None # Restore the old domain with this decision removed aka backtracking else: old_domain, var, val = back_track_stack.pop() old_domain[var].remove(val) domains = old_domain return None
def _test(return_dict, problem, disp=False, ec=False): ai = AI() orig_domains = init_domains() restrict_domain(orig_domains, problem) if disp: print("====Problem====") display(orig_domains) print() start = time.time() if not ec: result = ai.solve(problem) else: with open(CNF_FILE, 'w') as file: file.write(ai.sat_encode(problem)) stream = os.popen("./picosat {}".format(CNF_FILE)) output = stream.read() if len(output) == 0: print("ERROR: picosat not installed/in PATH.") result = None else: os.remove(CNF_FILE) sat_assignments = parse_picosat(output) result = ai.sat_decode(sat_assignments) end = time.time() t = end - start # we assume all test cases are solveable passed = False if result == None else verify(result, orig_domains) if disp: if result != None: print("====Solution===") display(result) print("====sd_domain===") display(orig_domains) # this should never happen with our test cases else: print("==No solution==") print() print("Time: {} seconds.".format(t)) print() if passed: print("Solution: PASSED.") else: print("Solution: FAILED.") print() return_dict["result"] = passed
def solve(self, problem): domains = init_domains() restrict_domain(domains, problem) # TODO: implement backtracking search. # TODO: delete this block -> # Note that the display and test functions in the main file take domains as inputs. # So when returning the final solution, make sure to take your assignment function # and turn the value into a single element list and return them as a domain map. for spot in sd_spots: domains[spot] = [1] return domains
def solve(self, problem): domains = init_domains() restrict_domain(domains, problem) stack = [] while True: if self.propagate(domains) == False: if self.dom_size(domains) == True: return domains orig_domains = copy.deepcopy(domains) spot, num = self.search(domains) stack.append((spot, num, orig_domains)) else: if len(stack) == 0: return None domains = self.backtrack(stack, domains) self.conflict = False
def solve(self, problem): domains = init_domains() restrict_domain(domains, problem) # initialize empty stack spots = [] while True: # check confilict if self.propagate(domains) is not False: # check consistent assignment for all spot if self.isconsistent(domains) is not False: return domains else: # copy domains in order to back track orig_domains = copy.deepcopy(domains) # make decision spot, num = self.search(domains) spots.append((spot, num, orig_domains)) else: # if stack is empty then no solution if len(spots) == 0: return None else: # back track domains domains = self.backtrack(spots, domains)