def test_possibles_moves(self) -> None: print_header(self._testMethodName) print('') rh = RushHour([ True, False, True, False, False, True, False, True, False, True, False, True ], [2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 3], [2, 2, 0, 0, 3, 1, 1, 3, 0, 4, 5, 5]) s = State([1, 0, 3, 1, 1, 4, 3, 4, 4, 2, 4, 1]) s2 = State([1, 0, 3, 1, 1, 4, 3, 4, 4, 2, 4, 2]) val1 = len(rh.possible_moves(s)) self.assertEqual(val1, 5) print(val1) val2 = len(rh.possible_moves(s2)) self.assertEqual(val1, 5) print(val2)
def solve_Astar(self, state: State) -> (State or None, int): visited = set() priority_queue = [] state.h = state.estimee1() heapq.heappush(priority_queue, state) while len(priority_queue) > 0: state_to_evaluate: State = heapq.heappop(priority_queue) if state_to_evaluate in visited: continue visited.add(state_to_evaluate) if state_to_evaluate.success(): return state_to_evaluate, len(visited) else: children: List[State] = self.possible_moves(state_to_evaluate) for child in children: if child not in visited: child.h = child.estimee1() heapq.heappush(priority_queue, child) return None, len(visited)
def test1(self): print_header(self._testMethodName) print('') s0 = State(self.positioning) b = not s0.success() print(b) s = s0.move(1, 1) print(s.prev == s0) b = b and s.prev == s0 print(s0.pos[1], " ", s.pos[1]) s = s.move(6, 1) s = s.move(1, -1) s = s.move(6, -1) print(s == s0) b = b and s == s0 s = s.move(1, 1) s = s.move(2, -1) s = s.move(3, -1) s = s.move(4, 1) s = s.move(4, -1) s = s.move(5, -1) s = s.move(5, 1) s = s.move(5, -1) s = s.move(6, 1) s = s.move(6, 1) s = s.move(6, 1) s = s.move(7, 1) s = s.move(7, 1) s = s.move(0, 1) s = s.move(0, 1) s = s.move(0, 1) print(s.success()) b = b and s.success() print("\n", "résultat correct" if b else "mauvais résultat") self.assertTrue(b)
def test2(self): """ move_on describe the position of the car, according to its orientation """ print_header(self._testMethodName) print('') rh = RushHour([True, True, False, False, True, True, False, False], [2, 2, 3, 2, 3, 2, 3, 3], [2, 0, 0, 0, 5, 4, 5, 3]) s = State([1, 0, 1, 4, 2, 4, 0, 1]) rh.free_pos = RushHour.get_free_pos(s, rh.cars) b = True print(rh.free_pos) ans = [[False, False, True, True, True, False], [False, True, True, False, True, False], [False, False, False, False, True, False], [False, True, True, False, True, True], [False, True, True, True, False, False], [False, True, False, False, False, True]] b = b and (rh.free_pos[i, j] == ans[i][j] for i in range(6) for j in range(6)) print("\n", "résultat correct" if b else "mauvais résultat") self.assertTrue(b)
def test_solve81_Astar(self): self.rh = RushHour(*game2) self.s = State([3, 0, 1, 0, 1, 1, 1, 0, 3, 4, 4, 0, 3]) self.algo_name = 'solve_Astar' self.expected_nb_step = 81 self.do_test()
def test_solve16_Astar(self): self.rh = RushHour(*game1) self.s = State([1, 0, 1, 4, 2, 4, 0, 1]) self.algo_name = 'solve_Astar' self.expected_nb_step = 16 self.do_test()
def test_solve46_Astar(self): self.rh = RushHour(*game3) self.s = State([1, 0, 3, 1, 1, 4, 3, 4, 4, 2, 4, 1]) self.algo_name = 'solve_Astar' self.expected_nb_step = 46 self.do_test()