Example #1
0
def a_star(estadoInicial):
    inicio = time.time()
    raiz = stn.STNode(estadoInicial, -1, 0, "")
    searchTree = mh.MinHeap()
    searchTree.insere(raiz)
    solucoes = []
    explorados = []
    caminhoAteSolucao = []
    nosVisitados = []

    while len(solucoes) < 2:
        nodeAtual = searchTree.remove()
        explorados.append(nodeAtual.estado)
        if (nodeAtual.custo == 31):
            solucoes.append(nodeAtual)
            fim = time.time()
            print("Solucao encontrada em %fs; Estados expandidos = %d - %d" %
                  (fim - inicio, len(explorados), len(nosVisitados)))

        for node in tb.calcularMovimentosPossiveis(nodeAtual.estado):
            nodeNovo = stn.STNode(node[0], nodeAtual.custo, nodeAtual.id,
                                  node[1])
            if (nodeNovo.estado not in explorados
                    and nodeNovo.estado not in searchTree.estados):
                searchTree.insere(nodeNovo)
                searchTree.estados.append(nodeNovo.estado)
                nosVisitados.append(nodeNovo)

    caminhoAteSolucao = utils.procurarCaminhos(solucoes, nosVisitados)
    utils.escreverSolucao(caminhoAteSolucao)
Example #2
0
def kthSmallestElem(arr, k):
    heap = minHeap.MinHeap()
    for i in arr:
        for j in i:
            heap.insert(j)
    for z in range(k - 1):
        heap.delete(0)

    return heap.delete(0)
Example #3
0
def astar(start, goal, blocked_dict, dim, tie_break):

    # evaluated nodes
    closed_dict = {}

    # map of points associated with the point it came from
    came_from = {}

    # map of points and thier costs
    g_score = {start: 0}

    # nodes waiting to be evaluated
    open_list = mh.MinHeap(tie_break)
    open_list.insert(State(start, 0, heuristic(start, goal)), g_score)

    # number of nodes expanded
    cells_expanded = 0

    while len(open_list.heap) > 0:

        current = open_list.heap[0]

        # if the goal is found, get shortest path
        if current.point == goal:
            #print(reconstruct_path(came_from, current.point))
            return reconstruct_path(came_from, current.point), cells_expanded

        if current in open_list.heap:
            open_list.pop()

        closed_dict[current.point] = True

        # iterates through all neighboring nodes
        neighborhood = neighborsOf(current.point, blocked_dict, dim)

        for neighbor in neighborhood:

            if closed_dict.get(neighbor.point, False) == True:
                continue

            cells_expanded += 1

            # The distance from current node to a neighbor plus the past cost from start to current node
            new_g_score = g_score[current.point] + 1

            came_from[neighbor.point] = current.point
            g_score[neighbor.point] = new_g_score
            neighbor.g = g_score[neighbor.point]
            neighbor.f = g_score[neighbor.point] + heuristic(
                neighbor.point, goal)

            if neighbor not in open_list.heap:
                open_list.insert(neighbor, g_score)
    # TODO here
    return -1
Example #4
0
def requests(file):
	queueA = minHeap.MinHeap()
	queueB = minHeap.MinHeap()

	with open(file, "r") as f:
		n = int(f.readline().strip())
		for i in range(n):
			ip_addr, tier, estimate = f.readline().strip().split(" ")
			node = queueNode(str(ip_addr), str(tier), int(estimate), i)
			if tier == 'A':
				queueA.insert(node)
			elif tier == 'B':
				queueB.insert(node)
			i += 1

	for i in range(queueA.bhsize):
		print(queueA.remove().ip_addr)

	for i in range(queueB.bhsize):
		print(queueB.remove().ip_addr)
Example #5
0
 def __init__(self):
     self.heap = minHeap.MinHeap()
 def __init__(self):
     arr = []
     self.minHeap = minHeap.MinHeap(arr)
     self.maxHeap = maxHeap.MaxHeap(arr)