def main(): """ Do whatever you want in here; this is for you. The examples below shows how your functions might be used. """ # initialize a random transition array of length 8 import time t = time.time() ta = TArray(9) # compute path using bfs #bfs_path = bfs(ta) #print(len(bfs_path)) #print(time.time() - t) #t = time.time() #ta = TileGame(3) #pathAStar = astar(ta, tilegame_heuristic) #print("Path of A star") #TArray.print_path(pathAStar) #print(len(pathAStar)) # display path pathBfs = bfs(ta) print("Path of BFS") TArray.print_path(pathBfs) #pathDfs = dfs(ta) #print("Path of dFS") #TArray.print_path(pathDfs) #print(time.time() - t) #pathIds = ids(ta) #print("Path of IDS") #TArray.print_path(pathIds) #print(time.time() - t) goal_state = (0, 1, 2, 3, 4, 5, 6, 7, 8) pathBds = bds(ta, goal_state) print("Path of BDS") TArray.print_path(pathBds) print(time.time() - t) # initialize a random 3x3 TileGame problem #simple_problem = ((1,2,3),(4,5,6),(7,8,9)) goal_state = ((1, 2, 3), (4, 5, 6), (7, 8, 9)) tg = TileGame(3) path = astar(tg, tilegame_heuristic) TileGame.print_pretty_path(path)
def _check_algorithm(self, algorithm): simple_state = ((1, 2, 3), (4, 5, 6), (7, 8, 9)) # Construct a TileGame where the start and goal states are the same tg = TileGame(3, simple_state, simple_state) path = algorithm(tg) self.assertEqual( path[0], simple_state, "Path should start with the start state" ) self.assertEqual(path[-1], simple_state, "Path should end with the end state") self.assertEqual(len(path), 1, "Path length should be one")
def _simple_problem(self, algorithm, shortest): simple_problem = ((1, 2), (4, 3)) goal_state = ((1, 2), (3, 4)) # Construct a small TileGame one state away from the goal tg = TileGame(2, simple_problem, goal_state) path = algorithm(tg) self.assertEqual( path[0], simple_problem, "Path should start with the start state" ) self.assertEqual(path[-1], goal_state, "Path should end with the goal state") if shortest: self.assertEqual(len(path), 2, "Path length should be two")
def main(): """ Do whatever you want in here; this is for you. An example below shows how your functions might be used. """ # sys.setrecursionlimit(100000) # initialize a random 3x3 TileGame problem tg = TileGame(3) # print(TileGame.board_to_pretty_string(tg.get_start_state())) # compute path # path = bfs(tg) # path = dfs(tg) # path = ids(tg) path = bds(tg, ((1, 2, 3), (4, 5, 6), (7, 8, 9))) # path = bds(tg, ((1,2),(3,4))) # path = astar(tg, tilegame_heuristic) # display path TileGame.print_pretty_path(path) print(len(path)) # an example with DGraphs: small_dgraph = DGraph([[None, 1], [1, None]], {1}) print(bfs(small_dgraph))
def tilegame_heuristic(state): """ Produces a real number for the given tile game state representing an estimate of the cost to get to the goal state. """ state_list = TileGame.tuple_to_list(state) count = 0 d = len(state_list) for i in range(d): for j in range(len(state_list[i])): row = (state_list[i][j] - 1) / d col = (state_list[i][j] - 1) % d count += abs(row - i) + abs(col - j) return count / 2
def tilegame_heuristic(state): """ Produces a number for the given tile game state representing an estimate of the cost to get to the goal state. Input: state - the tilegame state to evaluate. Consult handout for how the tilegame state is represented Output: a number (int, float, etc.) """ list = TileGame.tuple_to_list(state) dim = len(list) step = 0 for i in range(dim): for j in range(dim): correct_i = int((list[i][j] - 1) / dim) correct_j = (list[i][j] - 1) % dim step += (abs(i - correct_i) + abs(j - correct_j)) return int(step / 2)
def main(): """ Do whatever you want in here; this is for you. An example below shows how your functions might be used. """ # initialize a random 3x3 TileGame problem tg = TileGame(3) print TileGame.board_to_pretty_string(tg.get_start_state()) # compute path # path1 = bfs(tg) # print len(path1) # path2 = ids(tg) # print len(path2) # path3 = bds(tg, tg.goal_state) # print len(path3) path4 = astar(tg, tilegame_heuristic) # display path TileGame.print_pretty_path(path4) print len(path4) # an example with DGraphs: small_dgraph = DGraph([[None, None, 1], [1, None, 1], [1, 1, None]], {1}) print ids(small_dgraph)