def generate_samples(depth: int, iterations: int) -> Iterable[Sample]: def get_next_move(last_move): while True: next_move = np.random.choice(list(Move)) if next_move != last_move: return next_move for _ in range(iterations): c, last_move = Cube(), None for d in range(depth): last_move = move = get_next_move(last_move) yield Sample(Cube(c.change_by(move)), d + 1)
def test_scatter_and_return(self): c = Cube() c.change_by(Move.BACK) c.change_by(Move.LEFT_CTR) c.change_by(Move.DOWN) self.assertFalse(c.is_solved()) c.change_by(Move.DOWN_CTR) c.change_by(Move.LEFT) c.change_by(Move.BACK_CTR) self.assertTrue(c.is_solved())
def test_mutability(self): mut = Cube() changed = mut.change_by(Move.DOWN) self.assertFalse(mut.is_solved()) self.assertListEqual(mut.front, changed.front)
def test_back_move_and_return(self): c = Cube() c.change_by(Move.BACK) self.assertFalse(c.is_solved()) c.change_by(Move.BACK_CTR) self.assertTrue(c.is_solved())
def test_left_move_and_return(self): c = Cube() c.change_by(Move.LEFT) self.assertFalse(c.is_solved()) c.change_by(Move.LEFT_CTR) self.assertTrue(c.is_solved())
def test_down_move_and_return(self): c = Cube() c.change_by(Move.DOWN) c.change_by(Move.DOWN_CTR) self.assertTrue(c.is_solved())
def test_not_solved_after_move(self): c = Cube() c.change_by(Move.DOWN) self.assertFalse(c.is_solved())
def generate_random_cube(iterations: int = 100) -> Cube: c = Cube() for _ in range(iterations): c.change_by(np.random.choice(list(Move))) return c