def do_individual_experiment(args): lookahead, seed = args heur = npuzzle.ManhattanDistanceHeuristic(False) goal = npuzzle.NPuzzleBoard.done rta = search.Rta(heur=heur, goal=goal, lookahead=lookahead, learn=False) env = npuzzle.NPuzzle(n1sqrt=n1sqrt, difficulty=0.5, seed=seed) done, elapsed = rta(env, stats=True) return env.steps(), elapsed
def manual_play(): env = npuzzle.NPuzzle(n1sqrt=n1sqrt, difficulty=0.5) env.render() while True: print("---") print_usage() cmd = int(input("Cmd: ")) if cmd == -1: break done = env.step(cmd) env.render() if done: print("done in {} step(s)!".format(env.steps())) break
import npuzzle p = npuzzle.NPuzzle(3) p.visualise() tile = p.read_tile(1, 2) print(tile) p.reset() p.visualise() try: p.read_tile(3, 3) except IndexError: print('index eror test passed')
return True if odd_even_inv == 1 and odd_even_space_idx == 0: return True else: return False except: for i in range(3): for j in range(3): if env.read_tile(i, j) is None: env_line.append(9) else: env_line.append(env.read_tile(i, j)) for i in range(len(env_line)): for j in range(i + 1, len(env_line)): if env_line[i] > env_line[j]: count += 1 if count % 2 == 0: odd_even_inv = 0 # inversion is even return True else: odd_even_inv = 1 # inversion is odd return False if __name__ == "__main__": env = npuzzle.NPuzzle(4) # env = npuzzle.NPuzzle(4) env.reset() env.visualise() # just check print(is_solvable(env))
def __blank_row_number_from_bottom(tiles, size): index = tiles.index(None) row = index // size row_decs = size - row return row_decs def __is_even(N): return N % 2 == 0 def __inversion_count(tiles, size): inversion_count = 0 for i in range(size * size - 1): n = tiles[i] for j in range(i, size * size - 1): n_next = tiles[j + 1] if n_next is not None and n is not None and n > n_next: inversion_count += 1 return inversion_count if __name__ == "__main__": # testing suite env = npuzzle.NPuzzle(3) # instance of NPuzzle class env.reset() # random shuffle env.visualise() # just to show the tiles # just check print(is_solvable(env)) # should output True or False<Paste>
def astar(seed): env = npuzzle.NPuzzle(n1sqrt=n1sqrt, difficulty=0.5, seed=seed) heur = npuzzle.ManhattanDistanceHeuristic(False) goal = npuzzle.NPuzzleBoard.done plan, _, elapsed = search.astar(env.state(), heur, goal, True) return len(plan), elapsed