Esempio n. 1
0
 def doSearches(self, board, option):
     if option == 1:
         bfs.search(board)
     if option == 2:
         ucs.search(board)
     if option == 3:
         board.isAstar = True
         ass.search(board)
Esempio n. 2
0
def call_algorithm(method, table):
    solved_table = build_standard_solved_table(table.rows, table.columns)

    time_before = time.time()

    if method == BFS:
        order = build_order(args.order)
        final_node = bfs.search(begin_table=table, solved_table=solved_table, order=order, max_depth=args.max_depth, random_orders=order is None)
    elif method == DFS:
        order = build_order(args.order)
        final_node = dfs.search(begin_table=table, solved_table=solved_table, order=order, max_depth=args.max_depth, random_orders=order is None)
    elif method == IDFS:
        order = build_order(args.order)
        final_node = idfs.search(begin_table=table, solved_table=solved_table, order=order, min_limit=args.min_limit, max_depth=args.max_depth)
    elif method == BEST_FIRST_SEARCH:
        final_node = best_first_search.search(begin_table=table, solved_table=solved_table, heuristic=args.heuristic, max_depth=args.max_depth)
    elif method == A_STAR:
        final_node = a_star.search(begin_table=table, solved_table=solved_table, heuristic=args.heuristic, max_depth=args.max_depth)
    elif method == SMA_STAR:
        final_node = sma_star.search(begin_table=table, solved_table=solved_table, heuristic=args.heuristic, max_depth=args.max_depth, max_memory=args.max_memory)

    if final_node is None:
        print("Could not find a solution!")
        return

    final_node.table.print()
    print("Solution found in " + str(time.time()-time_before) + 's')

    moves = utils.create_list_of_moves(final_node)
    print("Moves to solve the puzzle: " + str(len(moves)))
    print(utils.convert_moves(moves))
def make_path():  # call bfs.py to generate the shortest path
    if start_vertex_key is not None and end_vertex_key is not None and end_vertex_key is not start_vertex_key:
        path = search(vertex_dict[start_vertex_key],
                      vertex_dict[end_vertex_key])
        for item in path:
            item.set_vertex_color(RED)
            item.set_highlighted(True)
Esempio n. 4
0
def main():

    start = None  # initialize start and end to None
    end = None

    draw_image(dartmouth_image, 0, 0)

    for place in dartmouth_places:
        dartmouth_places[place].start(
            sm_x,
            sm_y)  # check to see if the vertex has been clicked as 'start'
        dartmouth_places[place].rolled(
            m_x,
            m_y)  # check to see if the vertex has been rolled over as a 'goal'
        if dartmouth_places[
                place].vertex_start:  # if this is the start, save the object in a 'start' variable
            start = dartmouth_places[place]
        if dartmouth_places[
                place].vertex_path:  # if this is the goal, save the object in a 'end' variable
            end = dartmouth_places[place]

    if start is not None and end is not None:  # if there is a start and end, perform bfs

        path = search(start, end)  # create a list of visited locations

        for place in path:
            place.vertex_path = True  # for every place in the path, set the path boolean variable to True

    for place in dartmouth_places:  # draw all of the paths in the correct colors
        dartmouth_places[place].draw_paths()

    for place in dartmouth_places:  # draw all of the vertexes in the correct color on top of the paths
        dartmouth_places[place].draw_vertex()
Esempio n. 5
0
 def doSearches(self, board, option):
     if option == 1:
         bfs.search(board)
     if option == 2:
         dfs.search(board)
     if option == 3:
         ucs.search(board)
     if option == 4:
         gbfs.search(board)
     if option == 5:
         ass.search(board)
     if option == 6:  # all get executed
         bfs.search(board)
         dfs.search(board)
         ucs.search(board)
         gbfs.search(board)
         ass.search(board)
Esempio n. 6
0
def get_weight(adj_list):
	start=None
	for k in adj_list:
		if k.is_name("startNode"):
			start=k
			break
	group=bfs.search(adj_list,start)
	for gen in group:
		for k in gen:
			k.weight=gen.get(k)
Esempio n. 7
0
  def getDistribution( self, state ):
    global CHOKE_DECISION
    # Read variables from state
    ghostState = state.getGhostState( self.index )
    legalActions = state.getLegalActions( self.index )
    pos = state.getGhostPosition( self.index )
    isScared = ghostState.scaredTimer > 0
    choke_points = self.getChockingPoints(state)
    target_point = None

    for index in CHOKE_DECISION.keys():
      if index != self.index:
        try:
          choke_points.remove(CHOKE_DECISION[index])
        except:
          pass

    # Select best actions given the state
    max_distance = 10000
    if choke_points == None:
        target_point = state.getPacmanPosition()
        
    for c in choke_points:
      path = bfs.search(pos, c, state.getWalls())
      distance = 0
      if path != None:
        distance = len(path)

      if distance < max_distance:
        max_distance = distance
        target_point = c

    CHOKE_DECISION[self.index] = target_point

    if target_point == None:
      target_point = state.getPacmanPosition()

    speed = 1
    if isScared: speed = 0.5
    
    actionVectors = [Actions.directionToVector( a, speed ) for a in legalActions]
    newPositions = [( int(pos[0]+a[0]), int(pos[1]+a[1]) ) for a in actionVectors]
    
    distancesToTarget = []
    for pos in newPositions:
      path = bfs.search(pos, target_point, state.getWalls())
      if path:
        distancesToTarget.append(len(path))

    if isScared:
      bestScore = max( distancesToTarget )
      bestProb = self.prob_scared_flee
    else:
      bestScore = min( distancesToTarget )
      bestProb = self.prob_attack
    bestActions = [action for action, distance in zip( legalActions, distancesToTarget ) if distance == bestScore]
    
    # Construct distribution
    dist = util.Counter()
    for a in bestActions: dist[a] = bestProb / len(bestActions)
    for a in legalActions: dist[a] += ( 1-bestProb ) / len(legalActions)
    dist.normalize()
    return dist
Esempio n. 8
0
	def test_bfs_should_not_find_invalid_element(self):
		start_node = self.nodes[0]
		self.assertIsNone(bfs.search(self.g, 'borat', start_node))
Esempio n. 9
0
	def test_bfs_should_find_shortest_path(self):
		start_node = self.nodes[0]
		self.assertEqual(bfs.search(self.g, 'casper', start_node)[1], self.shortest_path)
Esempio n. 10
0
	def test_bfs_should_find_element(self):
		start_node = self.nodes[0]
		self.assertEqual(bfs.search(self.g, 'casper', start_node)[0], self.nodes[1])
Esempio n. 11
0
 def get_sub_graph(self, central_node_text, directions = "all"):
   if directions == "all":
     directions = [self.graph.UPSTREAM, self.graph.DOWNSTREAM]
   central_node = self.graph.get_node("text", central_node_text)
   visited = bfs.search(self.graph.get_adjacency_dict(), central_node.name, directions)
   return self.get_sub_graph_from_visted_node_set(visited)
Esempio n. 12
0
    print('1. Search for Item')
    print('2. List Names')
    print('3. List Items')
    print('4. Quit')
    print('')


menu_choice = 0
print_menu()
while menu_choice != 4:
    menu_choice = int(input("Type in a number (1-4): "))
    if menu_choice == 1:
        print("Search for Item")
        name = raw_input("Who to start search with: ")
        item = raw_input("Item: ")
        person = bfs.search(name, item)
        if person:
            print person + " is the closest " + item + " seller to " + name + " !"
        else:
            print "There are no " + item + " sellers accessible to " + name + "."

    elif menu_choice == 2:
        print("List Names")
        print(bfs.get_names())

    elif menu_choice == 3:
        print("List Items")
        print(bfs.get_items())

    elif menu_choice != 4:
        print_menu()
Esempio n. 13
0
 def test_bfs_should_not_find_invalid_element(self):
     start_node = self.nodes[0]
     self.assertIsNone(bfs.search(self.g, 'borat', start_node))
Esempio n. 14
0
 def test_bfs_should_find_shortest_path(self):
     start_node = self.nodes[0]
     self.assertEqual(
         bfs.search(self.g, 'casper', start_node)[1], self.shortest_path)
Esempio n. 15
0
 def test_bfs_should_find_element(self):
     start_node = self.nodes[0]
     self.assertEqual(
         bfs.search(self.g, 'casper', start_node)[0], self.nodes[1])