def testNpuzzle ( self, size = 8 ): side = int(sqrt(size+1)) size = side*side-1 row = lambda n: n[0] col = lambda n: n[1] def repr ( node ): puzzle = node.item() side = int(sqrt(len(puzzle))) lines = [[None]*side]*side for i, p in enumerate(puzzle): lines[p[0]][p[1]] = str(i) return "%s (%d)" % ('\n'.join([''.join(r) for r in lines]), node.cost()) def successor ( n ): hole = n[0] adj = (range(row(hole)-1, row(hole)+2), range(col(hole)-1, col(hole)+2)) result = [] for i in range(1, size+1): for c in (0, 1): if n[i][c] == hole[c] and n[i][1-c] in adj[1-c]: nxt = list(n) nxt[0], nxt[i] = nxt[i], nxt[0] result.append(tuple(nxt)) return result def successorInPlace ( n ): hole = n[0] adj = (range(row(hole)-1, row(hole)+2), range(col(hole)-1, col(hole)+2)) for i in range(1, size+1): for c in (0, 1): if n[i][c] == hole[c] and n[i][1-c] in adj[1-c]: nl = list(n) nl[0], nl[i] = nl[i], nl[0] yield tuple(nl) goal = tuple([(r,c) for r in range(side) for c in range(side)]) initial = tuple(goal) for n in range(6): initial = choice(successor(initial)) # more than 6 implies IDS would take more than 1K recursions cost = lambda s,n: 1 #sys.setrecursionlimit(2000) problem = Problem(initial, goal, successor, cost) solver = ProblemSolver(repr) solver.solve(problem, ProblemSolver.iterativeDeepeningGenerator(successor), True) self.assertTrue(solver.solved(problem)) print ("N-puzzle, solution found using IDDF search: " + str(solver.solution)) print("Algorithm performance: ", solver.performance, ", memory used: ", solver.resources) solver = CostBasedProblemSolver(repr) #solver.solve(problem, CostBasedProblemSolver.iterativeLengtheningGenerator(successor, cost), True) #self.assertTrue(solver.solved(problem)) #print ("N-puzzle, solution found using iterative lengthening search: " + str(solver.solution)) #print("Algorithm performance: ", solver.performance, ", memory used: ", solver.resources) manhattanDistance = lambda s: abs(row(s[1])-row(s[0])) + abs(col(s[1])-col(s[0])) h = lambda n: reduce(lambda x,y: x+y, map(manhattanDistance, zip(n,goal))) solver = CostBasedProblemSolver(repr) solver.solve(problem, CostBasedProblemSolver.aStarGenerator(successor, cost, h), True) self.assertTrue(solver.solved(problem)) print ("N-puzzle, solution found using A* search: " + str(solver.solution)) print("Algorithm performance: ", solver.performance, ", memory used: ", solver.resources)
def testNavigation ( self ): distance = { ("Oradea","Zerind"): 71, ("Oradea","Sibiu"): 151, ("Neamt","Iasi"): 87, ("Zerind","Arad"): 75, ("Iasi","Vaslui"): 92, ("Arad","Sibiu"): 140, ("Arad","Timisoara"): 118, ("Sibiu","Fagaras"): 99, ("Sibiu","Rimnicu Vilcea"): 80, ("Fagaras","Bucharest"): 1211, #211, ("Vaslui","Urziceni"): 142, ("Rimnicu Vilcea","Pitesti"): 97, ("Rimnicu Vilcea","Craiova"): 146, ("Timisoara","Lugoj"): 111, ("Lugoj","Mehadia"): 70, ("Pitesti","Bucharest"): 101, ("Pitesti","Craiova"): 138, ("Hirsova","Urziceni"): 98, ("Hirsova","Eforie"): 86, ("Urcizeni","Bucharest"): 85, ("Mehadia","Drobeta"): 75, ("Bucharest","Giurgiu"): 90, ("Drobeta","Craiova"): 120, } sld_bucharest = { "Arad": 366, "Bucharest": 0, "Craiova": 160, "Drobeta": 242, "Eforie": 161, "Fagaras": 1300, #176, "Giurgiu": 77, "Hirsova": 151, "Iasi": 226, "Lugoj": 244, "Mehadia": 241, "Neamt": 234, "Oradea": 380, "Pitesti": 100, "Rimnicu Vilcea": 193, "Sibiu": 253, "Timisoara": 329, "Urcizeni": 80, "Vaslui": 199, "Zerind": 374, } def repr ( node ): parent = '' if not node.isInitial(): parent = node.parent().item() + ' ->' return "%s %s (%d)" % (parent, node.item(), node.cost()) initial = "Arad" goal = "Bucharest" cost = lambda s, n: distance.get((s,n), distance.get((n,s))) or (s==n and 0) successor = lambda n: [p[1-i] for p in distance for i in (0,1) if p[i] == n] h = lambda n: sld_bucharest[n] problem = Problem(initial, goal, successor, cost) solver = ProblemSolver(repr) solver.solve(problem, ProblemSolver.iterativeDeepeningGenerator(successor), True) self.assertTrue(solver.solved(problem)) print ("Navigation: solution found using ID search: " + str(solver.solution)) print("Algorithm performance: ", solver.performance, ", memory used: ", solver.resources) solver = ProblemSolver(repr) solver.solve(problem, CostBasedProblemSolver.greedyBestFirstGenerator(successor, h), True) self.assertTrue(solver.solved(problem)) print ("Navigation: solution found using greedy-best-first search: " + str(solver.solution)) print("Algorithm performance: ", solver.performance, ", memory used: ", solver.resources) solver = CostBasedProblemSolver(repr) solver.solve(problem, CostBasedProblemSolver.uniformCostGenerator(successor, cost)) self.assertTrue(solver.solved(problem)) print ("Navigation: solution found using uniform-cost search: " + str(solver.solution)) print("Algorithm performance: ", solver.performance, ", memory used: ", solver.resources) solver = CostBasedProblemSolver(repr) solver.solve(problem, CostBasedProblemSolver.aStarGenerator(successor, cost, h)) self.assertTrue(solver.solved(problem)) print ("Navigation: solution found using A*: " + str(solver.solution)) print("Algorithm performance: ", solver.performance, ", memory used: ", solver.resources)