def plan(self, grid_2D, agent_position, goal_position, save_maze=True): # current_position = {'z': int(self.zpos - self.origin_coord['z']), 'x': int(self.xpos - self.origin_coord['x'])} # print('current_position', current_position) frontier = [] maze_map_dict = {} for z in range(agent_position[0] - self.scanning_range, agent_position[0] + self.scanning_range + 1): # 0, self.envsize): for x in range(agent_position[1] - self.scanning_range, agent_position[1] + self.scanning_range + 1): if x >= 0 and x < self.envsize and z >= 0 and z < self.envsize: item = grid_2D[z][x] # if maze_map_dict.get(self.state_to_string(z, x)) is None: # maze_map_dict[self.state_to_string(z, x)] = set() # maze_map_dict[self.state_to_string(z, x)].add(self.get_passable_neighbours(grid_2D, z, x)) import ipdb; ipdb.set_trace() # import ipdb; ipdb.set_trace() if self.is_passable(item): maze_map_dict[self.state_to_string( z, x)] = self.get_passable_neighbours(grid_2D, z, x) print(maze_map_dict) print(maze_map_dict[self.state_to_string(*(agent_position))]) print(maze_map_dict[self.state_to_string(*(goal_position))]) # import ipdb; ipdb.set_trace() maze_map = search.UndirectedGraph(maze_map_dict) # initial_position = (self.range_z//2, self.range_x//2) maze_problem = search.GraphProblem( self.state_to_string(*(agent_position)), self.state_to_string(*(goal_position)), maze_map) # print('maze_map', maze_map) solution_node = search.uniform_cost_search(problem=maze_problem, display=True) #, h=None) print('solution_node', solution_node) if solution_node is not None: solution_path = [solution_node.state] current_node = solution_node.parent solution_path.append(current_node.state) while current_node.state != maze_problem.initial: current_node = current_node.parent solution_path.append(current_node.state) return solution_path
def main(problemID, mapID): problem = int(problemID) reward_hole = -1.0 stochastic = False episodes = 100 mapBase = mapID stats = {} # start from a known seed np.random.seed(12) # set up the environment env = LochLomondEnv(problem_id=problem, is_stochastic=stochastic, map_name_base=mapBase, reward_hole=reward_hole) state_space_locations, state_space_actions, state_initial_id, \ state_goal_id = env2statespace(env) # Insert the solution here to find and output the solution using A-star # define the states and actions in a table maze_map = search.UndirectedGraph(state_space_actions) maze_map.locations = state_space_locations maze_problem = search.GraphProblem(state_initial_id, state_goal_id, maze_map) for episode in range(episodes): # iterate over episodes env.reset() # reset the state of the env to the starting state iterations, node = my_astar_search_graph(problem=maze_problem, h=None) # -- Trace the solution --# solution_path = [node] cnode = node.parent solution_path.append(cnode) while cnode.state != state_initial_id: cnode = cnode.parent solution_path.append(cnode) print("----------------------------------------") print("Identified goal state:" + str(solution_path[0])) print("Solution trace:" + str(solution_path)) print("Iterations:" + str(iterations)) print("----------------------------------------") # log stats stats["solutiontrace"] = str(solution_path) stats["numberofiterations"] = str(iterations) return stats
sumner_map.locations = dict(Newtown=(525121, 33131), Machynlleth=(525903, 38535), Dolgellau=(527421, 38844), Conwy=(532829, 38295), Bangor=(532274, 41293), Caernarnfon=(531396, 42739), Betws_y_coed=(530931, 38010), Wrexham=(530430, 29925), Pwllheli=(528888, 44176), Llangollen=(529692, 31717), Welshpool=(526603, 31464), Aberystwyth=(524153, 40829)) #all instances run BestFS and A* #sumner_puzzle yields better solution for BestFS than DFS, and BFS better than BestFS sumner_puzzle = search.GraphProblem('Pwllheli', 'Conwy', sumner_map) #sumner1_puzzle adds nothing new sumner1_puzzle = search.GraphProblem('Pwllheli', 'Newtown', sumner_map) #sumner2_puzzle yields same solution with UCS and A*, but A* expands fewer nodes sumner2_puzzle = search.GraphProblem('Newtown', 'Wrexham', sumner_map) sumner_puzzle.description = ''' An abbreviated map of Sumner County, TN. This map is unique, to the best of my knowledge. ''' #cannot for the life of me remember how to get the table to print every problem instance. myPuzzles = [sumner_puzzle, sumner1_puzzle, sumner2_puzzle]
import search romania_problem = search.GraphProblem('Arad', 'Bucharest', search.romania_map) #vacumm_world = search.GraphProblemStochastic('State_1', ['State_7', 'State_8'], vacumm_world) #LRTA_problem = search.OnlineSearchProblem('State_3', 'State_5', one_dim_state_space) eight_puzzle = search.EightPuzzle((1, 2, 3, 0, 4, 6, 7, 5, 8)) nqueens = search.NQueensProblem(8) print("\n\nBreadth first search\n") def breadth_first_search(problem): """[Figure 3.11] Note that this function can be implemented in a single line as below: return graph_search(problem, FIFOQueue()) """ node = search.Node(problem.initial) if problem.goal_test(node.state): return node frontier = search.FIFOQueue() frontier.append(node) explored = set() while frontier: node = frontier.pop() explored.add(tuple(node.state)) for child in node.expand(problem): if child.state not in tuple(explored) and child not in frontier: if problem.goal_test(child.state): return child
Warsaw=(latitude(38.2431), longitude(38.2431,93.3819)), Clinton= (latitude(38.3686), longitude(38.3686, 93.7783)), RichHill= (latitude(38.0964), longitude(38.0964, 94.3611)), Ottawa= (latitude(38.6158), longitude(38.6158, 95.2686)), OsageCity= (latitude(39.0000), longitude(38.6339, 95.8258)), OverlandPark= (latitude(38.9822), longitude(38.9822, 94.6708)), Olathe=(latitude(38.8814), longitude(38.8814,94.8191)), Lawrence=(latitude(38.9717), longitude(38.9717,95.2353)), Atchison=(latitude(39.5631), longitude(39.5631,95.1216)), Topeka=(latitude(39.0558), longitude(39.0558,95.6890)), StMarys=(latitude(39.1942), longitude(39.1942,96.0711)), Holton=(latitude(39.4653), longitude(39.4653,95.7364)), ) sumner_puzzle = search.GraphProblem('Cottontown', 'Mitchellville', sumner_map) sumner_puzzle.label = 'Sumner Map' sumner_puzzle.description = ''' An abbreviated map of Sumner County, TN. This map is unique, to the best of my knowledge. ''' kcmapTopeka_puzzle = search.GraphProblem('OsageCity','Topeka', kc_map) kcmapTopeka_puzzle.label = 'Kansas City Map' kcmapTopeka_puzzle.description = ''' A map of the Kansas City area in the Missouri-Kansas Bistate Area. ''' kcmapStMarys_puzzle = search.GraphProblem('KansasCity','StMarys', kc_map) kcmapStMarys_puzzle.label = 'Kansas City Map'
Smithfield = (39.8013187,-79.827878), Normalville = (39.9986836,-79.4656011), Markleysburg =(39.7368018,-79.458878) ) #sumner_puzzle = search.GraphProblem('Cottontown', 'Mitchellville', sumner_map) #sumner_puzzle.label = 'Sumner' #sumner_puzzle.description = ''' #An abbreviated map of Sumner County, TN. #This map is unique, to the best of my knowledge. #''' fayette_puzzle = search.GraphProblem('Perryopolis', 'Markleysburg', fayette_map) fayette_puzzle.label = 'Fayette P2M' fayette_puzzle.description = ''' An abbreviated map of Fayette County, PA. This map is unique, to the best of my knowledge. ''' # added by whh fp2 = search.GraphProblem('Markleysburg', 'Connellsville', fayette_map) fp2.label = 'Fayette M2C' romania_map = search.UndirectedGraph(dict( A=dict(Z=75,S=140,T=118), Z=dict(O=71,A=75), S=dict(O=151,R=80,F=99), T=dict(A=118,L=111),
Wallkill=32, NewWindsor=7, Washingtonville=34, Montgomery=45, PortJervis=69), NewWindsor=dict(Washingtonville=11, Newburgh=7), Poughkeepsie=dict(Newburgh=30), Montgomery=dict(PortJervis=15, Newburgh=45), Wallkill=dict(Middletown=33, Newburgh=32), Washingtonville=dict(Warwick=25, NewWindsor=11, Middletown=12, Newburgh=34), )) orange_puzzle = search.GraphProblem('PortJervis', 'Newburgh', orange_map) orange_puzzle.label = 'Orange' orange_puzzle.description = ''' An abbreviated map of Orange County, NY. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111), O=dict(Z=71, S=151), L=dict(T=111, M=70),
gray_map = search.UndirectedGraph(dict( Boston=dict(Cambridge=14, Brookline=21, Chelsea=13, Arlington=3), Chelsea=dict(Boston=13, Winthrop=13, Somerville=16, Arlington=8), Winthrop=dict(Chelsea=13), Somerville=dict(Chelsea=16, Cambridge=10, Medford=5, Arlington=6), Cambridge=dict(Boston=14, Brookline=21, Watertown=16, Belmont=15, Medford=15, Somerville=10, Arlington=10), Medford=dict(Cambridge=15, Belmont=5, Somerville=5), Brookline=dict(Boston=21, Cambridge=21, Watertown=20), Watertown=dict(Belmont=7, Cambridge=16, Brookline=20), Belmont=dict(Watertown=7, Cambridge=15, Medford=5), Arlington=dict(Somerville=6, Cambridge=10, Boston=3, Chelsea=8) )) gray_puzzle = search.GraphProblem('Belmont', 'Chelsea', gray_map) gray_puzzle.label = 'Boston' gray_puzzle.description = ''' An abbreviated map of Sumner County, TN. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph(dict( A=dict(Z=75,S=140,T=118), Z=dict(O=71,A=75), S=dict(O=151,R=80,F=99), T=dict(A=118,L=111), O=dict(Z=71,S=151), L=dict(T=111,M=70), M=dict(L=70,D=75),
BoysRanch=(0, 100), Masterson=(30, 100), Fritch=(32, 75), Groom=(51, 70), Love=(42, 75), ) #sumner_puzzle = search.GraphProblem('Cottontown', 'Mitchellville', sumner_map) #sumner_puzzle.label = 'Sumner' # sumner_puzzle.description = ''' # An abbreviated map of Sumner County, TN. # This map is unique, to the best of my knowledge. # ''' potter_puzzle2 = search.GraphProblem('Arney', 'BoysRanch', potter_map) potter_puzzle2.label = 'Potter County - Arney to BoysRanch' potter_puzzle2.description = '''Instance where BFS does better than DFS ''' romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111), O=dict(Z=71, S=151), L=dict(T=111, M=70), M=dict(L=70, D=75), D=dict(M=75, C=120), R=dict(S=80, C=146, P=97), C=dict(R=146, P=138, D=120),
Dearborn=dict(Garden_City=16, Detroit=13, Lincoln_Park=21, Wayne=20, Taylor=15), Lincoln_Park=dict(Detroit=21, Dearborn=11, Wyandotte=8), Belleville=dict(Romulus=11), Romulus=dict(Belleville=11, Wayne=12, Taylor=13), Taylor=dict(Romulus=13, Dearborn=15, Woodhaven=11), Wyandotte=dict(Lincoln_Park=8, Trenton=15), Flat_Rock=dict(Woodhaven=8), Woodhaven=dict(Flat_Rock=8, Taylor=11, Trenton=13), Trenton=dict(Woodhaven=13, Wyandotte=15))) # sumner_puzzle = search.GraphProblem('Cottontown', 'Mitchellville', sumner_map) myPuzzle = search.GraphProblem('Livonia', 'Belleville', wayne_map) myPuzzle.label = 'Wayne County' myPuzzle.description = ''' An abbreviated map of Wayne County, MI. This map is unique, to the best of my knowledge. ''' from grid import distance wayne2map = dict(Plymouth=dict(Livonia=11, Westland=17), Livonia=dict(Plymouth=14, Hamtramck=26, Detroit=23, Garden_City=10), Hamtramck=dict(Livonia=26, Detroit=12),
#This map is unique, to the best of my knowledge. # My Northern Il and Indiana map northernIl_map = search.UndirectedGraph(dict( Rockford=dict(CrystalLake=42), CrystalLake=dict(Chicago=53, Aurora=35), Chicago=dict(Aurora=41, Hammond=27, Detriot=284), Detriot=dict(Indianapolis=287), Aurora=dict(Chicago=41, Kankakee=81), Hammond=dict(SouthBend=71), Kankakee=dict(Champaign=160), Champaign=dict(Lafayette=91), Lafayette=dict(Indianapolis=62), Indianapolis=dict(SouthBend=150), )) northernIl_puzzle2 = search.GraphProblem('Rockford', 'Indianapolis', northernIl_map) northernIl_map.locations = dict( Rockford = (42, 89), CrystalLake= (42, 88), Chicago= (41, 87), Detriot= (42, 83), Aurora= (41, 88), Hammond= (41, 87), Champaign= (40, 88), Lafayette = (40, 86), Indianapolis= (39, 86), ) northernIl_puzzle = search.GraphProblem('Rockford', 'SouthBend', northernIl_map) #northernIl_puzzle shows breadth_first_search is lower than depth_first_search
Fredonia=dict(Hamburg=10), SBuffalo=dict(Buffalo=3))) erie_map.locations = dict(Clarence=(27, 41), Amherst=(19, 41), Lancaster=(27, 30), GrandIsland=(4, 40), Cheektowaga=(23, 31), WestSeneca=(24, 22), OrchardPark=(26, 17), Buffalo=(9, 28), Hamburg=(20, 17), Fredonia=(10, 10), SBuffalo=(12, 25)) erie_puzzle = search.GraphProblem('GrandIsland', 'OrchardPark', erie_map) erie_puzzle.label = 'Erie' erie_puzzle.description = ''' An abbreviated map of Sumner County, TN. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111), O=dict(Z=71, S=151), L=dict(T=111, M=70),
Fribourg=94, Lausanne=129, Lugano=177, Schaffhausen=31, Sion=545, Thun=83, Winterhur=15, Zug=20))) ''' swiss_map.locations = dict( Basel=(476,76), Bern=(469,74), Biel=(471,72), Chur=(469,95),Fribourgh=(480,78), Geneva=(462,61), Lausanne=(465,66), Lugano=(460,90), Schaffhausen=(477,86), Sion=(462,74), Thun=(468,76), Winterhur=(475,87), Zug=(472,65), Zurich=(474,85)) ''' swiss_puzzle = search.GraphProblem('Chur', 'Biel', swiss_map) swiss_puzzle1 = search.GraphProblem('Zug', 'Geneva', swiss_map) swiss_puzzle2 = search.GraphProblem('Chur', 'Schaffhausen', swiss_map) swiss_puzzle3 = search.GraphProblem('Zurich', 'Zug', swiss_map) swiss_puzzle.description = ''' An abbreviated map of major cities in Switzerland. ''' # A trivial Problem definition #class Hex(search.Problem): # def actions(self, state): # return ['nw', 'ne', 'e', 'se','sw','w'] # All of the grid # ['(0,0)', '(0,1)', '(0,2)'], # ['(1,0)', '(1,1)', '(1,2)', '(1,3)'],
Germany_map.locations = dict(Bremen=(53.07, 8.8), Hamburg=(53.55, 9.99), Hanover=(52.37, 9.73), Berlin=(52.52, 13.45), Leipzig=(51.33, 12.37), Dresden=(51.05, 13.73), Dortmund=(51.51, 7.46), Essen=(51.45, 7.01), Dusseldorf=(51.22, 6.77), Cologne=(50.93, 6.96), Frankfurt=(50.11, 8.68), Nuremberg=(49.45, 11.07), Stuttgart=(48.77, 9.18), Munich=(48.13, 11.58)) Germany_puzzle = search.GraphProblem('Bremen', 'Munich', Germany_map) Germany_puzzle.label = 'Bremen to Munich' Germany_puzzle.description = 'go from Bremen to Munich...if you can' Germany_puzzle2 = search.GraphProblem('Munich', 'Essen', Germany_map) Germany_puzzle2.label = 'Munich to Essen' Germany_puzzle2.description = 'the most difficult one so far.' romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111), O=dict(Z=71, S=151), L=dict(T=111, M=70), M=dict(L=70, D=75),
Aylesbury=45, ), Aylesbury=dict(Buckingham=45, Watford=81), Watford=dict(Aylesbury=81, London=20))) uk_map.locations = dict(Birmingham=(52.4862, 1.8904), Oxford=(51.7520, 1.2577), London=(51.5074, 0.1278), Coventry=(52.4068, 1.5197), Luton=(51.8787, 0.4200), Wolverhampton=(52.5870, 2.1288), Cheltenham=(51.8994, 2.0783), Buckingham=(51.5014, 0.1419), Aylesbury=(51.8156, 0.808), Watford=(51.6565, 0.3903)) uk_puzzle = search.GraphProblem('Birmingham', 'Watford', uk_map) uk_puzzle.label = 'UK' uk_puzzle.description = ''' An abbreviated map of Sumner County, TN. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111), O=dict(Z=71, S=151), L=dict(T=111, M=70),
# ''' ashgabat_map = search.UndirectedGraph( dict( Kommunizm=dict(Bezmein=10, Bagyr=14, Pilmile=60), Pewrize=dict(Bagyr=10, Shirvan=100, Faruj=130), Bagyr=dict(Bezmein=8, Kipchak=9, Pewrize=10, Kommunizm=14), Bezmein=dict(Bagyr=8, Kipchak=5, Kommunizm=10), Kipchak=dict(Bezmein=5, Bagyr=9), Shirvan=dict(Pewrize=100, Bojnourd=50, Faruj=42), Faruj=dict(Shirvan=42, Pewrize=130, Bojnourd=98), Bojnourd=dict(Faruj=98, Shirvan=50, Pilmile=50), Pilmile=dict(Bojnourd=50, Kommunizm=60), )) ashgabat_puzzle = search.GraphProblem('Bojnourd', 'Kipchak', ashgabat_map) ashgabat_puzzle.label = 'Ashgabat' ashgabat_puzzle.description = ''' An abbreviated map of Ashgabat, Turkmenistan. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111), O=dict(Z=71, S=151), L=dict(T=111, M=70),
Mutare=dict(Harare=197, Nyanga=98), MountDarwin=dict(Harare=118), Nyanga=dict(Chikore=106, Mutare=98), Chikore=dict(Harare=138, Nyanga=106), )) zimbabwe_map.locations = dict(Harare=(613, 200), MountDarwin=(662, 101), Chikore=(731, 201), Nyanga=(769, 234), Mutare=(761, 311), Kadoma=(512, 248), Gweru=(502, 359), Lumbini=(271, 264), Bulawayo=(394, 423)) harare_mutare_puzzle = search.GraphProblem('Harare', 'Mutare', zimbabwe_map) harare_mutare_puzzle.label = 'Zimbabwe Map' harare_mutare_puzzle.description = ''' An abbreviated map of several cities in Zimbabwe. ''' # A trivial Problem definition # class HexDeadEnd(search.Problem): # def actions(self, state): # return ['e', 'se'] # # def result(self, state, action): # if action == 'e': # return 'on' # else:
#Cottontown=dict(Portland=18), #Fairfield=dict(Mitchellville=21, Portland=17), #Mitchellville=dict(Portland=7, Fairfield=21), SanLuis=dict(AvilaBeach=11, MorroBay=15, Atascadero=18), AvilaBeach=dict(SanLuis=11, MorroBay=28, SantaMaria=30), Atascadero=dict(Templeton=7, SanLuis=18, MorroBay=23, Cayucos=27), MorroBay=dict(Cayucos=8, SanLuis=15, AvilaBeach=28, Atascadero=23), SantaMaria=dict(AvilaBeach=30, Nipomo=11), Nipomo=dict(SantaMaria=11, SanLuis=25), Cayucos=dict(MorroBay=8, Atascadero=27, Templeton=31), Templeton=dict(Atascadero=7, Cayucos=31), )) slo_puzzle = search.GraphProblem('Cayucos', 'SantaMaria', slo_map) slo_puzzle.label = 'SLO' slo_puzzle.description = ''' An abbreviated map of San Luis Obispo, CA. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph(dict( A=dict(Z=75,S=140,T=118), Z=dict(O=71,A=75), S=dict(O=151,R=80,F=99), T=dict(A=118,L=111), O=dict(Z=71,S=151), L=dict(T=111,M=70), M=dict(L=70,D=75),
import search from math import (cos, pi) # A sample map problem sumner_map = search.UndirectedGraph( dict( Portland=dict(Mitchellville=7, Fairfield=17, Cottontown=18), Cottontown=dict(Portland=18), Fairfield=dict(Mitchellville=21, Portland=17), Mitchellville=dict(Portland=7, Fairfield=21), )) sumner_puzzle = search.GraphProblem('Cottontown', 'Mitchellville', sumner_map) sumner_puzzle.label = 'Sumner' sumner_puzzle.description = ''' An abbreviated map of Sumner County, TN. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111), O=dict(Z=71, S=151), L=dict(T=111, M=70), M=dict(L=70, D=75), D=dict(M=75, C=120), R=dict(S=80, C=146, P=97),
import search romania_problem = search.GraphProblem('Arad', 'Craiova', search.romania_map) #vacumm_world = search.GraphProblemStochastic('State_1', ['State_7', 'State_8'], vacumm_world) #LRTA_problem = search.OnlineSearchProblem('State_3', 'State_5', one_dim_state_space) eight_puzzle = search.EightPuzzle((1, 2, 3, 0, 4, 6, 7, 5, 8)) nqueens = search.NQueensProblem(8) print("\nBest first Search") print("\nSolution to Romania Problem Arad->Craiova") print(search.recursive_best_first_search(romania_problem).solution()) print("\nSolution to Eight Puzzle (1, 2, 3, 0, 4, 6, 7, 5, 8)") print(search.recursive_best_first_search(eight_puzzle).solution())
Bozeman=dict(Checkerboard=108, Helena=97, Butte=81), Polaris=dict(Dillon=41, Bannack=24, Jackson=24, Achlin=40), Jackson=dict(Sula=56, Polaris=24), Sula=dict(Jackson=56, Hamilton=38, Anaconda=116), Philipsburg=dict(Missoula=71), Cobalt=dict(Salmon=76), Bannack=dict(Salmon=128, Polaris=24, Butte=81), Checkerboard=dict(Bozeman=108), Astley=dict(Bentwood=32, Butte=110), Bentwood=dict(Astley=32, Achlin=70), Achlin=dict(Polaris=40, Bentwood=70), )) #Butte_map = search.GraphProblem('Butte', 'DeerLodge', Butte_map) #BFS > DFS Butte_map = search.GraphProblem( 'Butte', 'Anaconda', Butte_map) #UCS > BFS > DFS where ">" means better(faster) Butte_map.label = 'Butte Montana' Butte_map.description = ''' A map consisting of multiple counties near Butte, Montana. Reaches into Idaho. This map is unique, to the best of my knowledge. ''' # A trivial Problem definition class Twiddle(search.Problem): def actions(self, state): return [(0, 0, 'cc'), (0, 0, 'cw'), (0, 2, 'cc'), (0, 2, 'cw')] def result(self, state, action):
Z=dict(O=71,A=75), S=dict(O=151,R=80,F=99), T=dict(A=118,L=111), O=dict(Z=71,S=151), L=dict(T=111,M=70), M=dict(L=70,D=75), D=dict(M=75,C=120), R=dict(S=80,C=146,P=97), C=dict(R=146,P=138,D=120), F=dict(S=99,B=211), P=dict(R=97,C=138,B=101), B=dict(G=90,P=101,F=211), )) #sumner_puzzle = search.GraphProblem('Cottontown', 'Mitchellville', sumner_map) sumner_puzzle = search.GraphProblem('A', 'B', sumner_map) sumner_puzzle.label = 'Sumner Map' sumner_puzzle.description = ''' An abbreviated map of Sumner County, TN. This map is unique, to the best of my knowledge. ''' solarSystem_map = search.UndirectedGraph(dict( Mars=dict(Earth=75,Venus=140,Satellites=118), Earth=dict(Sun=59,Mars=75), Venus=dict(Sun=101,Saturn=80), Satellites=dict(Mars=118,Jupiter=111), Sun=dict(Venus=151,Uranus=75), Jupiter=dict(Satellites=111,Neptune=50), Neptune=dict(Jupiter=70,Mercury=75),
# A sample map problem il_map = search.UndirectedGraph( dict( # Portland=dict(Mitchellville=7, Fairfield=17, Cottontown=18), # Cottontown=dict(Portland=18), # Fairfield=dict(Mitchellville=21, Portland=17), # Mitchellville=dict(Portland=7, Fairfield=21), Chicago=dict(Springfield=204, Peoria=169), Jacksonville=dict(Springfield=38, Peoria=97, Quincy=79), Springfield=dict(Chicago=204, Jacksonville=38, Peoria=68), Peoria=dict(Chicago=169, Jacksonville=97, Springfield=68, Quincy=146), Quincy=dict(Jacksonville=79, Peoria=146), )) illinois_puzzle = search.GraphProblem('Chicago', 'Jacksonville', il_map) illinois_puzzle.label = 'Illinois Map' illinois_puzzle.description = ''' An abbreviated map of Illinois. This map is unique, to the best of my knowledge. ''' # A trivial Problem definition class LightSwitch(search.Problem): def actions(self, state): return ['up', 'down'] def result(self, state, action): if action == 'up':
explored = set() while frontier: node = frontier.pop() node_colors[node.state] = "red" iterations += 1 all_node_colors.append(dict(node_colors)) explored.add(node.state) for child in node.expand(problem): if child.state not in explored and child not in frontier: if problem.goal_test(child.state): node_colors[child.state] = "green" iterations += 1 all_node_colors.append(dict(node_colors)) return(iterations, all_node_colors, child) frontier.append(child) node_colors[child.state] = "orange" iterations += 1 all_node_colors.append(dict(node_colors)) node_colors[node.state] = "gray" iterations += 1 all_node_colors.append(dict(node_colors)) return None all_node_colors = [] romania_problem = s.GraphProblem('Arad', 'Bucharest', romania_map) display_visual(algorithm = breadth_first_search, problem = romania_problem)
Sunbury=120), Chesapeake=dict(Suffolk=35, Norfolk=15, VirginiaBeach=40, Moyock=120), VirginiaBeach=dict(Norfolk=35, Chesapeake=40), Hampton=dict(Norfolk=30, Suffolk=60, NewportNews=15), NewportNews=dict(Hampton=15, Jamestown=35, Williamsburg=30, Yorktown=15), Jamestown=dict(NewportNews=35, Williamsburg=15), Williamsburg=dict(Jamestown=15, NewportNews=30, Yorktown=20), Yorktown=dict(Williamsburg=20, Newportnews=15), Sunbury=dict(Suffolk=120, Moyock=45), Moyock=dict(Suffolk=150, Chesapeak=120), )) norfolk_puzzle = search.GraphProblem('Jamestown', 'Yorktown', norfolk_map) norfolk_puzzle.label = 'Norfolk' norfolk_puzzle.description = 'This is a map of the Norfolk, VA area.' \ 'This map is unique to the best of my' \ 'knowledge.' #========================================================================= #========================================================================= romania_map = search.UndirectedGraph( dict( A=dict(Z=75, S=140, T=118), Z=dict(O=71, A=75), S=dict(O=151, R=80, F=99), T=dict(A=118, L=111),
Azle=dict (Fortworth = 17, Reno = 10, Frisco = 33, Fubu = 41), Reno=dict (Azle = 10, Coppell = 47, Garland =19, Lakewood= 45), Coppell=dict (Richardson = 21, Reno = 47, Lakewood = 8), Frisco=dict (Azle = 33, Fortworth =45), Lakewood= dict( Reno = 45, Coppell=8), Garland= dict (Fortworth = 33, Reno = 19), Fubu= dict(Azle = 41, Nike = 51, Pfflyers = 38, Atoka =128), Pfflyers= dict(Fubu = 38), Nike= dict(Fubu = 51), Atoka= dict(Fubu = 128, Ada=18), Ada= dict(Atoka = 18), )) Dallas_puzzle = search.GraphProblem('Frisco', 'Richardson', dallas_map) Dallas_puzzle.label = 'Dallas' Dallas_puzzle.description = ''' An abbreviated map of Dallas, Tx. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph(dict( A=dict(Z=75,S=140,T=118), Z=dict(O=71,A=75), S=dict(O=151,R=80,F=99), T=dict(A=118,L=111), O=dict(Z=71,S=151), L=dict(T=111,M=70), M=dict(L=70,D=75),
Dublin=dict(Mullingar=79), Mullingar=dict(Naas=70, Dublin=79), Naas=dict(Mullingar=70, Carlow=58), Kells=dict(Dublin=65, Mullingar=42), Arklow=dict(Naas=97, Dublin=71), Carlow=dict(Naas=58), )) sumner_map.locations = dict(Dublin=(69, 42), Mullingar=(0, 22), Naas=(41, 53), Kells=(30, 0), Arklow=(75, 101), Carlow=(62, 25)) sumner_puzzle = search.GraphProblem('Mullingar', 'Carlow', sumner_map) sumner_puzzle.label = 'Sumner Map' sumner_puzzle.description = ''' An abbreviated map of Eastern Ireland. This map is unique, to the best of my knowledge. ''' sumner_puzzle2 = search.GraphProblem('Mullingar', 'Arklow', sumner_map) sumner_puzzle2.label = 'Sumner Map 2' sumner_puzzle2.description = ''' An abbreviated map of Eastern Ireland. This map is unique, to the best of my knowledge. ''' # A trivial Problem definition class LightSwitch(search.Problem):
#Fairfield=dict(Mitchellville=21, Portland=17), #Mitchellville=dict(Portland=7, Fairfield=21), Jackson=dict(Humboldt=27), Humboldt=dict(Jackson=27, ThreeWay=8), ThreeWay=dict(Humboldt=8, Medon=34), Medon=dict(Jackson=17, Humboldt=43, ThreeWay=34), SpringCreek=dict(ThreeWay=18, Medon=34, Humboldt=29))) # Coordinates for map. May not be entirely accurate but as close as possible madison_map.locations = (dict(Jackson=(485, 512), Humboldt=(482, 482), ThreeWay=(474, 474), Medon=(495, 501), SpringCreek=(474, 464))) madison_puzzle = search.GraphProblem('Jackson', 'ThreeWay', madison_map) madison_puzzle1 = search.GraphProblem('SpringCreek', 'Jackson', madison_map) madison_puzzle.label = 'Madison' madison_puzzle.description = ''' An abbreviated map of Madison County, TN. This map is unique, to the best of my knowledge. ''' madison_puzzle1.label = 'Madison1' madison_puzzle1.description = ''' An abbreviated map of Madison County, TN. This map is unique, to the best of my knowledge. ''' romania_map = search.UndirectedGraph( dict(
Q=dict(Mitchellville=6, R=6), R=dict(Q=6, S=6), S=dict(R=6, Cottontown=6), )) sumner_map.locations = dict( Portland=travelTime(36.581538,-86.6534912), Fairfield=travelTime(36.6182314,-86.3761194), Cottontown=travelTime(36.449575,-86.5697851), Graball=travelTime(36.4788454,-86.4750586), Mitchellville=travelTime(36.6334132,-86.5480556), Q=travelTime(36.49553455,-86.564352725), R=travelTime(36.5414941,-86.55892035), S=travelTime(36.58745365,-86.553487975), ) sumner_puzzle = search.GraphProblem('Cottontown', 'Mitchellville', sumner_map) sumner_puzzle.label = 'Sumner Map' # The sliding tile problem beloved of AI teachers, # because it has a decent heuristic. class SlidingTile(search.Problem): # initial and goal states are strings of the form # '1,2,3,4|5,6,7,8|9,10,11,_' # Where _ marks the missing tile. def __init__(self, initial, goal=None): super().__init__(initial, goal) iGrid = self.state2grid(initial) self.rows = len(iGrid) self.cols = len(iGrid[0]) if not goal: