def rec_DFS(self, visited, current): # mark current board as visited visited.append(current) if Problem.isSolved(current): return current else: for x in Problem.expand(current): if x in visited: return None else: ret = self.rec_DFS(visited, x) if ret is not None: return ret return None
def greedy(self, start): found = False visited = [] toVisit = [] # Priority queue heapq.heappush(toVisit, start) while len(toVisit) != 0 and found is False: if len(toVisit) == 0: return None board = heapq.heappop(toVisit) visited.append(board) if Problem.isSolved(board) is True: return board else: for x in Problem.expand(board): if x not in visited: heapq.heappush(toVisit, x)
def DFS(self, start): found = False visited = [] toVisit = [start] # Priority queue while len(toVisit) != 0 and found is False: if len(toVisit) == 0: return None board = toVisit.pop(0) visited.append(board) if Problem.isSolved(board) is True: return board else: aux = [] for x in Problem.expand(board): if x not in visited: aux.append(x) toVisit += aux
def tests(): c1 = Board([0, 0, 0, 0]) c2 = Board([0, 0, 0, 0]) p = Problem(c1) contr = Controller(p) assert (c1 == c2) # Configuration assert (c1.getSize() == 4) assert (c1.getBoard() == [0, 0, 0, 0]) print(c1.nextConfiguration(0)) assert (c1.nextConfiguration(0).getBoard() == [1, 0, 0, 0]) assert (c1.nextConfiguration(1).getBoard() == [0, 1, 0, 0]) # Problem aux = p.expand(c1) assert (len(aux) == 4) assert (aux[0].getBoard() == [1, 0, 0, 0]) # ... print('tests passed')