def smartBacktracking(self, assignment): if self.complete(assignment): #if the assignment is complete, return and print maze print("Var. Assignments:", self.variableAssignments, ", Time: ", time.time() - self.t_start) mazes.printMaze(assignment) return assignment if debug: time.sleep(0.1) mazes.printMaze(assignment) node = self.getNode_i(assignment) #get a node that has not been visited self.variableAssignments += 1 if node is None: #when all nodes have been visited but the assignment is not complete, instant fail return False if len(self.island) > 0: for icolor in self.island: if self.hasIsland(icolor, self.colorVisited[icolor]): return False else: self.island.remove(icolor) for color in self.getColors(node): if self.consistant(color, node, assignment): #if the color we have chosen is legal, use it self.visited.append(node) self.colorVisited[color].append(node) result = self.smartBacktracking(assignment) #move on to next node if result: return result self.visited.remove(node) #that branch failed, backtrack self.colorVisited[color].remove(node) if color in self.completeColors: self.completeColors.remove(color) node.value = '_' return False
def dumbBacktracking(self, assignment): if self.complete( assignment ): #if the assignment is complete, return and print maze mazes.printMaze(assignment) return assignment node = self.getNode(assignment) #get a node that has not been visited if node is None: #when all nodes have been visited but the assignment is not complete, instant fail return False if len(self.island) > 0: for icolor in self.island: if self.hasIsland(icolor, self.colorVisited[icolor]): return False else: self.island.remove(icolor) for color in self.getColors(node): if self.consistant( color, node, assignment): #if the color we have chosen is legal, use it self.visited.append(node) self.colorVisited[color].append(node) result = self.dumbBacktracking( assignment) #move on to next node if result: return result self.visited.remove(node) #that branch failed, backtrack self.colorVisited[color].remove(node) if color in self.completeColors: self.completeColors.remove(color) node.value = '_' return False
def __init__(self, maze): self.domain = [] self.start = {} self.finish = {} self.visited = [] self.colorVisited = defaultdict(list) self.island = [] self.completeColors = [] self.findStartandFinish(maze) mazes.printMaze(maze)
def solveMaze(filePath, dumb, smart): maze = mazes.readMaze(filePath) maze_i = mazes.readMaze(filePath) mazeCSP = CSP(maze) mazeCSP_i = CSP(maze_i) if dumb or not smart: print("################## Dumb ##################") print("Solving", filePath) mazes.printMaze(maze) mazeCSP.dumbBacktracking(maze) if smart or not dumb: print("############### Intelligent ###############") print("Solving", filePath) mazes.printMaze(maze_i) mazeCSP_i.smartBacktracking(maze_i)
def printResults(self, search, currentNode, cost): #print path node = currentNode steps = 0 while node.previous is not None: if node.value is not 'P' and node.value is not '*': steps += 1 node.value = '.' node = node.previous print("{} \nCost: {}, Steps {}".format(search, cost, steps)) print("Solved Maze:") mazes.printMaze(self.maze) #clean maze node = currentNode while node.previous is not None: if node.value is not 'P' and node.value is not '*': node.value = ' ' node = node.previous