コード例 #1
0
def solveGreedy(game, over, dir):
    estimation = manhattanDistance(game, over)
    max = 0
    caminho[AuxFunctions.returnString(game)] = AuxFunctions.returnString(game)
    distancias = []
    # Implementacao de heap em ordem das distancias
    heapq.heapify(distancias)
    heapq.heappush(distancias, (estimation, game))
    while not len(distancias) == 0:
        node = heapq.heappop(distancias)
        repetidos[AuxFunctions.returnString(node[1])] = 1
        index = node[1].index(0)
        for value in dir[index]:
            # Troca de elementos, acedendo a segunda posicao do tuplo
            node1 = node[1][:]
            node1[index + value], node1[index] = node1[index], node1[index + value]
            node1String = AuxFunctions.returnString(node1)
            if not caminho.get(node1String):
                caminho[node1String] = AuxFunctions.returnString(node[1])
            dis = manhattanDistance(node1, over)
            if dis == 0:
                print "Tiveram em dada altura um maximo de", max, "nos na fila."
                return node
            if not repetidos.get(node1String):
                heapq.heappush(distancias, (dis, node1[:]))
                if len(distancias) > max:
                    max = len(distancias)
コード例 #2
0
def solveAStar(game, over, dir):
  distancias = []
  heap.heapify(distancias)
  max = 0
  gameString = AuxFunctions.returnString(game)
  dis[gameString] = 0
  caminho[gameString] = gameString
  heap.heappush(distancias, (manhattanDistance(game, over), game))
  while not len(distancias) == 0:
    node = heap.heappop(distancias)
    repetidos[AuxFunctions.returnString(node[1])] = 1
    if AuxFunctions.isSolution(node[1], over):
      print "Tiveram em dada altura um maximo de", max, "nos na fila."
      return distancia
    index = node[1].index(0)
    for value in dir[index]:
      node1 = node[1][:]
      node1String = AuxFunctions.returnString(node1)
      distancia = dis.get(node1String) + 1
      distanciaTemp = dis.get(node1String)
      node1[index+value], node1[index] = node1[index], node1[index+value]
      node1String = AuxFunctions.returnString(node1)
      dis[node1String] = distanciaTemp + 1
      distancia += manhattanDistance(node1, over)
      if not caminho.get(node1String):
        caminho[node1String] = AuxFunctions.returnString(node[1])
      if not repetidos.get(node1String):
        heap.heappush(distancias, (distancia, node1))
        if len(distancias) > max: max = len(distancias)
コード例 #3
0
def manhattanDistance(game, over):
    gameString = AuxFunctions.returnString(game)
    overString = AuxFunctions.returnString(over)
    distancias = {0: 0, 1: 1, 2: 2, 3: 1, 4: 2, 5: 3, 6: 2, 7: 3, 8: 4}
    soma = 0
    for x in range(1, 9):
        subtracao = gameString.index(str(x)) - overString.index(str(x))
        d = math.fabs(subtracao)
        if subtracao == 1 and (gameString.index(str(x)) == 2 or gameString.index(str(x)) == 5):
            d = 5
        soma += distancias[int(d)]
    return soma
コード例 #4
0
def solveIDFS(game, over, dir):
    nivel_limite = 0
    max = 0
    while True:
        stack = [game]
        repetidos.clear()
        dis.clear()
        dis[game] = 0
        while not len(stack) == 0:
            node = stack.pop(0)
            if AuxFunctions.isSolution(list(node), list(over)):
                print "Tiveram em dada altura um maximo de", max, "nos na fila."
                return nivel_limite
            index = node.index('0')
            if dis.get(node) < nivel_limite:
                for value in dir[index]:
                    nodeTrader = list(node)[:]
                    nodeTrader[index+value], nodeTrader[index] = nodeTrader[index], nodeTrader[index+value]
                    nodeTraderString = AuxFunctions.returnString(nodeTrader)
                    if not caminho.get(nodeTraderString):
                        caminho[nodeTraderString] = node
                    if not dis.get(nodeTraderString) or dis.get(node) + 1 < dis.get(nodeTraderString):
                        dis[nodeTraderString] = dis.get(node) + 1
                        stack.insert(0, nodeTraderString)
                        print len(stack)
                        if len(stack) > max: max = len(stack)
        nivel_limite += 1
コード例 #5
0
def solveDFS(game, over, dir):
    tabGame = AuxFunctions.returnString(game)
    tabTemp = AuxFunctions.returnString(game)
    stack = [tabGame]
    max = 0
    dis[tabGame] = 0
    while not len(stack) == 0:
        game = list(stack.pop(0))
        tabGame = AuxFunctions.returnString(game)
        # printTable(game)
        index = game.index("0")
        if not repetidos.get(tabGame):
            repetidos[tabGame] = 1
            for value in dir[index]:
                tempGame = game[:]
                tempGame[index + value], tempGame[index] = tempGame[index], tempGame[index + value]
                tabTemp = AuxFunctions.returnString(tempGame)
                stack.insert(0, tabTemp)
                if max < len(stack):
                    max = len(stack)
                dis[tabTemp] = dis.get(tabGame) + 1
                if AuxFunctions.isSolution(tempGame, over):
                    print "Tiveram em dada altura um maximo de", max, "nos na fila."
                    return dis.get(tabTemp)
コード例 #6
0
def solveBFS(game, over, dir):
    tabGame = AuxFunctions.returnString(game)
    queue = [tabGame]
    dis[tabGame] = 0
    max = 0
    while not len(queue) == 0:
        game = list(queue.pop(0))
        tabGame = AuxFunctions.returnString(game)
        index = game.index('0')
        if not repetidos.get(tabGame):
            repetidos[tabGame] = 1
            for value in dir[index]:
                tempGame = game[:]
                tempGame[index+value], tempGame[index] = tempGame[index], tempGame[index+value]
                tabTemp = AuxFunctions.returnString(tempGame)
                dis[tabTemp] = dis[tabGame] + 1
                queue.append(tabTemp)
                if len(queue) > max:
                    max = len(queue)
                if not caminho.get(tabTemp):
                    caminho[tabTemp] = tabGame
                if AuxFunctions.isSolution(tempGame, over):
                    print "Tiveram em dada altura um maximo de", max, "nos na fila."
                    return dis.get(tabTemp)