def main(): # initialize the problems problem_1 = Problem1('L1.txt') problem_2 = Problem1('L2.txt') problem_3 = Problem1('L3.txt') # create the graphs based on the text file problem_1.create_graph() problem_2.create_graph() problem_3.create_graph() # create the actual problem to search problem_1 = InstrumentedProblem( GraphProblem(problem_1.start_coordinates, problem_1.end_coordinates, problem_1.graph)) problem_2 = InstrumentedProblem( GraphProblem(problem_2.start_coordinates, problem_2.end_coordinates, problem_2.graph)) problem_3 = InstrumentedProblem( GraphProblem(problem_3.start_coordinates, problem_3.end_coordinates, problem_3.graph)) #compare the search algorithms on all problems compare_searchers(problems=[problem_1, problem_2, problem_3], header=[ 'Algorithm', 'L1 ((1,1), (14,19))', 'L2 ((1,1), (12,11))', 'L3 ((1,1), (13,4))' ], searchers=[ uniform_cost_search, greedy_best_first_graph_search, astar_search ])
def main(): ''' #Explicitly create an instrumented problem ab = InstrumentedProblem(GraphProblem('A','B',romania)) goal = uniform_cost_search(ab) print("Path = ",goal.path()) print("Cost = ",goal.path_cost) print("Expanded/Goal Tests/Generated/Cost/Goal Found") ab.final_cost = goal.path_cost print(ab) ''' #To change h dynamically, provide a selection parameter in the constructor of your #derived class, then use that parameter to choose the version of h in your #overriden version of h in the derived class #You might need to create multiple versions of the problem, one for each value of the parameter simpleGraphCreation() compare_searchers(problems=[ GraphProblem('A', 'B', romania), GraphProblem('A', 'N', romania) ], header=['Algorithm', 'Romania(A, B)', 'Romania(A, N)'], searchers=[ uniform_cost_search, greedy_best_first_graph_search, astar_search ]) print()
def informed_puzzle(seed, scrambles): compare_searchers(problems=[Puzzle(3,seed,scrambles), PuzzleMisplaced(3,seed,scrambles), PuzzleManhattan(3,seed,scrambles)], header=['Searcher','8-puzzle h=0', '8-puzzle misplaced', '8-puzzle Manhattan'], searchers=[astar_search])
def main(): L1 = graphHelper() L2 = graphHelper() L3 = graphHelper() print("Manhattan") compare_searchers(problems=[ InstrumentedProblem(ManhattanProblem(L1[0], L1[1], L1[2])), InstrumentedProblem(ManhattanProblem(L2[0], L2[1], L2[2])), InstrumentedProblem(ManhattanProblem(L3[0], L3[1], L3[2])) ], header=['Algorithm', "L1", "L2", "L3"], searchers=[ uniform_cost_search, greedy_best_first_graph_search, astar_search ]) print("Euclidean") compare_searchers(problems=[ InstrumentedProblem(GraphProblem(L1[0], L1[1], L1[2])), InstrumentedProblem(GraphProblem(L2[0], L2[1], L2[2])), InstrumentedProblem(GraphProblem(L3[0], L3[1], L3[2])) ], header=['Algorithm', "L1", "L2", "L3"], searchers=[ uniform_cost_search, greedy_best_first_graph_search, astar_search ])
def blind_puzzle(seed, scrambles): compare_searchers(problems=[Puzzle(3,seed,scrambles)], header=['Searcher','8-puzzle'], searchers=[breadth_first_tree_search, breadth_first_graph_search, depth_first_graph_search, iterative_deepening_search, depth_limited_search])
def main(): firstGraphProblem = readMazeFromFile('L1.txt') secondGraphProblem = readMazeFromFile('L2.txt') thirdGraphProblem = readMazeFromFile('L3.txt') compare_searchers(problems=[firstGraphProblem, secondGraphProblem, thirdGraphProblem], header=['Algorithm', 'L1.txt', 'L2.txt', 'L3.txt'], searchers=[uniform_cost_search, greedy_best_first_graph_search, astar_search])
def wjsolve(capacities, start, goal, searchers=default_searchers): print("Solving WJ(%s,%s,%s)" % (capacities, start, goal)) for s in searchers: sol = s(wj.WJ(capacities, start, goal)) print(" %s cost %s: %s" % (s.__name__, sol.path_cost, ' '.join([str(n.state) for n in sol.path()]))) print("SUMMARY: successors/goal tests/states generated/solution") a.compare_searchers([wj.WJ(capacities, start, goal)], [], searchers)
def wjsolve(capacities, start, goal, searchers=default_searchers): problem = wj.WJ(capacities, start, goal) print("Solving {}".format(problem)) for alg in searchers: print('\n\nSearch algorithm:', alg.__name__) wj.print_solution(alg(problem)) print("\n\nSUMMARY: successors/goal tests/states generated/solution\n") #s.compare_searchers([wj2.WJ2(capacities, start, goal)], [], searchers) s.compare_searchers([problem], [], searchers)
def wj3solve(capacities, start, goal, searchers=default_searchers): problem = wj3.WJ3(capacities, start, goal) print(f"Solving {problem}\n") reachable = problem.reachable_states() potential = (capacities[0]+1) * (capacities[1]+1) * (capacities[2]+1) print(f"{len(reachable)} of {potential} reachable states: {reachable}") print() successful_searchers = [] for alg in searchers: print(f"• {alg.__name__}", end=': ') solution = apply_search_alg(alg, problem) if solution: wj3.print_solution(solution) successful_searchers.append(alg) print() print("\nSUMMARY: successors/goal tests/states generated/solution") if successful_searchers: s.compare_searchers([problem], [], searchers=successful_searchers)
def wj3solve(capacities, start, goal, searchers=default_searchers): problem = wj3.WJ3(capacities, start, goal) print(f"Solving {problem}\n") reachable = problem.reachable_states() potential = (capacities[0] + 1) * (capacities[1] + 1) * (capacities[2] + 1) print(f"{len(reachable)} of {potential} reachable states: {reachable}\n") successful_searchers = [] print('searchers', [s.__name__ for s in searchers]) for alg in searchers: print(f"• {alg.__name__}", end=': ') solution = alg(problem) if solution: wj3.print_solution(solution) successful_searchers.append(alg) print() print( "\nSUMMARY: algorithm <successors goal_tests states_generated solution>" ) if successful_searchers: s.compare_searchers([problem], [], searchers=successful_searchers)
def mainBrain(inistate, goal): #location=find_location(inistate) #print(location) problem = EightPuzzle(inistate, goal) #print(problem[inistate]) #print("---") #goalnode = search.breadth_first_graph_search(problem) goalnode = search.breadth_first_graph_search(problem) #goalnode = search.depth_first_graph_search(problem) #goalnode = search.iterative_deepening_search(problem) # sammud (tegevused, käigud) algolekust lõppolekuni print(goalnode.solution()) print(goalnode.path()) #print(len(goalnode.path())) #print("aa") #print(len(goalnode.solution())) #print("aa") print("----------------------") search.compare_searchers([problem], ["Strategy", "firstState"], searchers=[search.breadth_first_graph_search]) #pakun esimene number tähendab mitmes neuroni ringi. 3 tähendab kokku arvutust
inistate2 = (1,8,2,0,4,3,7,6,5) inistate3 = (5,4,0,6,1,8,7,3,2) inistate4 = (8,6,7,2,5,4,3,0,1) problem1 = EightPuzzle(inistate1) problem2 = EightPuzzle(inistate2) problem3 = EightPuzzle(inistate3) problem4 = EightPuzzle(inistate4) #goalnode = search.breadth_first_graph_search(problem) # sammud (tegevused, käigud) algolekust lõppolekuni #print(goalnode.solution()) # olekud algolekust lõppolekuni #print(goalnode.path()) # search.breadth_first_graph_search - lõpetab 10000 läbivaatamise järel # search.depth_first_graph_search - lõpetab 10000 läbivaatamise järel # search.iterative_deepening_search - maksimaalne sügavus: 10 # astar_h1 - lõpetab 10000 läbivaatamise järel search.compare_searchers([problem1], ["Probleem 1", "Samme lõpuni/Katseid/Olekuid/Lahendusi"], searchers=[search.astar_h1, search.breadth_first_graph_search, search.depth_first_graph_search, search.iterative_deepening_search]) search.compare_searchers([problem2], ["Probleem 2", "Samme lõpuni/Katseid/Olekuid/Lahendusi"], searchers=[search.astar_h1, search.breadth_first_graph_search, search.depth_first_graph_search, search.iterative_deepening_search]) search.compare_searchers([problem3], ["Probleem 3", "Samme lõpuni/Katseid/Olekuid/Lahendusi"], searchers=[search.astar_h1, search.breadth_first_graph_search, search.depth_first_graph_search, search.iterative_deepening_search]) search.compare_searchers([problem4], ["Probleem 4", "Samme lõpuni/Katseid/Olekuid/Lahendusi"], searchers=[search.astar_h1, search.breadth_first_graph_search, search.depth_first_graph_search, search.iterative_deepening_search]) # Tulemus: A* on kõige efektiivsem, breadth_first_graph_search järgmine. 3. ja 4. ei lahenda ükski otsing ette antud piirides. 3. lahendus on A*-l 64339 sammu
if action == Constants.up: return zero_place - row_length if action == Constants.down: return zero_place + row_length if action == Constants.right: return zero_place + 1 return zero_place - 1 problem = EightPuzzle(Constants.begin, Constants.Goal) goalnode = search.breadth_first_graph_search(problem) # sammud (tegevused, käigud) algolekust lõppolekuni print(goalnode.solution()) # olekud algolekust lõppolekuni print("path", goalnode.path()) print("path_len", len(goalnode.path())) problem = EightPuzzle(Constants.begin, Constants.Goal) goalnode = search.iterative_deepening_search(problem) # sammud (tegevused, käigud) algolekust lõppolekuni print(goalnode.solution()) # olekud algolekust lõppolekuni print("path", goalnode.path()) print("path_len", len(goalnode.path())) search.compare_searchers([problem], ["name", "result"], searchers=[ search.breadth_first_graph_search, search.iterative_deepening_search ])
def path_cost(self, c, state1, action, state2): return c + 1 goal = (1, 2, 3, 4, 5, 6, 7, 8, 0) inistate = (1, 2, 3, 7, 0, 5, 8, 4, 6) inistate2 = (1, 8, 2, 0, 4, 3, 7, 6, 5) inistate3 = (5, 4, 0, 6, 1, 8, 7, 3, 2) inistate4 = (8, 6, 7, 2, 5, 4, 3, 0, 1) problem = EightPuzzle(inistate, goal) print( search.compare_searchers([problem, EightPuzzle(inistate2, goal)], ["Strateegia", inistate, inistate2], searchers=[search.depth_first_graph_search])) #search.compare_searchers([problem], ["Strateegia", inistate], # searchers=[search.depth_first_graph_search]) #search.compare_searchers([problem], ["Strateegia", inistate3], # searchers=[search.iterative_deepening_search]) goalnode = search.breadth_first_graph_search(problem) # sammud (tegevused, käigud) algolekust lõppolekuni print(goalnode.solution()) # olekud algolekust lõppolekuni print(goalnode.path())
def crazy(seed, scrambles): compare_searchers(problems=[Puzzle(3,seed,scrambles)], header=['Searcher','8-puzzle'], searchers=[depth_first_tree_search])
def informed_puzzle15_over(seed,scrambles): compare_searchers(problems=[PuzzleManhattan(4,seed,scrambles), PuzzleManhattanOver(4,seed,scrambles)], header=['Searcher','15-puzzle Manhattan', '15-puzzle Manhattan over'], searchers=[astar_search,greedy_best_first_graph_search])
def legal(self, state, action): new_state = self.result(state,action) return (new_state[0] == 0 or new_state[0] == 3 or new_state[0] == new_state[1]) and (all(p in range(4) for p in new_state[:2])) def h(self, node): """number of trips""" return (node.state[0]+node.state[1])/2 sol = search.depth_first_graph_search(MissionareCannibalProblem()) print('DFS: ', sol.solution()) print('Final state (DFS): ', sol) print('') sol = search.breadth_first_graph_search(MissionareCannibalProblem()) print('BFS: ', sol.solution()) print('Final state (BFS): ', sol) sol = search.astar_search(MissionareCannibalProblem()) print('A*: ', sol.solution()) search.compare_searchers([MissionareCannibalProblem()], "Cannibal", [search.depth_first_graph_search, search.breadth_first_graph_search, search.iterative_deepening_search, search.astar_search])
def astar_with_h1(puzzle): search.astar_search(puzzle, puzzle.h1) def astar_with_h2(puzzle): search.astar_search(puzzle, puzzle.h2) #search.astar_search(puzzle, puzzle.h1) #search.astar_search(puzzle, puzzle.h2) # 8 6 7 # 2 5 4 # 3 . 1 hardPuzzle = EightPuzzle((8, 6, 7, 2, 5, 4, 3, 0, 1)) search.astar_search(hardPuzzle, hardPuzzle.h2) search.compare_searchers([puzzle], "EightPuzzle 2", searchers=[ search.breadth_first_search, search.iterative_deepening_search, astar_with_h0, astar_with_h1, astar_with_h2 ]) #def __main__(args):