Exemplo n.º 1
0
def CalculatePathBasedOnCurrentAlgorithm(tree, start, end, window):
    isCurrentAlgorithmAStar = Commons.CurrentAlgorithm == Commons.A_ALGORITHM
    queue = PriorityQueue()
    exploredPositions = set([])
    queue.put((0, start, [start]))
    exploredPositions.add(start)
    pathWeight = start.weight.__neg__()
    positionDictionary = {}
    positionDictionary[start] = 0

    while queue:

        weight, position, currentPath = queue.get()
        position.ColorPosition(Commons.ORANGE)
        if position != start and isCurrentAlgorithmAStar:
            weight -= CalculateManhattanDistance(position, end)

        if position == end:
            for pos in currentPath:
                pos.ColorPosition(Commons.YELLOW)
                pathWeight += pos.weight
            visitedNodes = (exploredPositions.__len__() -
                            queue._qsize()).__str__()
            RenderTexts(window, visitedNodes, pathWeight.__str__())
            GameWindow.DrawWindow(window, tree)
            return

        for i in range(position.neighbors.__len__()):

            if isCurrentAlgorithmAStar:
                cost = weight + position.neighbors[
                    i].weight + CalculateManhattanDistance(
                        position.neighbors[i], end)
            else:
                cost = weight + position.neighbors[i].weight

            if position.neighbors[i] not in exploredPositions:
                queue.put((cost, position.neighbors[i],
                           currentPath + [position.neighbors[i]]))
                exploredPositions.add(position.neighbors[i])
                position.neighbors[i].ColorPosition(Commons.BLACK)
                positionDictionary[position.neighbors[i]] = cost
            elif positionDictionary[
                    position.neighbors[i]] > cost and isCurrentAlgorithmAStar:
                queue.put((cost, position.neighbors[i],
                           currentPath + [position.neighbors[i]]))
                position.neighbors[i].ColorPosition(Commons.BLACK)
                positionDictionary[position.neighbors[i]] = cost

        thread = threading.Thread(target=GameWindow.DrawWindow,
                                  args=[window, tree, False])
        thread.start()
        thread.join()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    Commons.QuitGame()

            if event.type == pygame.QUIT:
                Commons.QuitGame()
Exemplo n.º 2
0
 for strat in starting_strats:
     perms = list(set(itertools.permutations(tuple(strat))))
     set_of_strats_as_tuples.update(perms)
 list_of_strats_as_tuples = list(set_of_strats_as_tuples)
 best_strats = PriorityQueue()
 total_strats = 0
 for strat_as_tuple in list_of_strats_as_tuples:
     strat_as_array = np.asarray(strat_as_tuple)
     score = calc_score(strat_as_array, orig_strats)
     if score > cutoff_score:
         best_strats.put((-score, strat_as_tuple))
         total_strats += 1
 print('total strats to look at = ' + str(total_strats))
 starting_strats = set()
 strat_counter = 0
 for _ in range(best_strats._qsize()):  # all from the priority queue
     if strat_counter % 100 == 0:
         print('looking at strat ' + str(strat_counter))
     x = best_strats.get()
     strat_as_tuple = x[1]
     for l in range(3):  # different number of explorations
         new_strat = np.asarray(strat_as_tuple)
         best_new_strat = None
         for k in range(
                 5 + l * 10
         ):  # different number of initial random changes to the strat
             new_strat = change_to_random_new_strat(new_strat)
         current_score = calc_score(new_strat, orig_strats)
         for j in range(100):  # greedy algorithm to search better strat
             old_strat = np.copy(new_strat)
             new_strat = change_to_random_new_strat(new_strat)