def forwardPathRelink(OPA, OPC): piorSolucao = [] melhorSolucao = [] melhorCusto = 0 solucaoSaida = [] # VERIFICA QUAL DAS DUAS PILHAS É A MAIOR OU MENOR pilhaA = np.max(hc.PilhasAbertas(OPA)) pilhaC = np.max(hc.PilhasAbertas(OPC)) if(pilhaA < pilhaC): melhorSolucao = OPA piorSolucao = OPC melhorCusto = pilhaA else: melhorSolucao = OPC piorSolucao = OPA melhorCusto = pilhaC solucaoSaida = melhorSolucao while (list(piorSolucao) != list(melhorSolucao)): # CRIA-SE UM VETOR COM INDICE DOS ELEMENTOS DIFERENTES vetNaoSimetricos = [i for i in range(len(piorSolucao)) if piorSolucao[i] != melhorSolucao[i]] # REALIZA A TROCA for i in range(len(vetNaoSimetricos) - 1): piorSolucao[vetNaoSimetricos[i]], piorSolucao[vetNaoSimetricos[i + 1]] = piorSolucao[vetNaoSimetricos[i + 1]], piorSolucao[vetNaoSimetricos[i]] custoAtual = np.max(hc.PilhasAbertas(piorSolucao)) if custoAtual <= melhorCusto: solucaoSaida = piorSolucao melhorCusto = custoAtual return solucaoSaida
def FirstImprovementMethod(ordemDasPilhas): resultadoBom = np.max(hc.PilhasAbertas(ordemDasPilhas), 0) - 1 itinerator = 0 while resultadoBom < np.max(list(hc.PilhasAbertas(ordemDasPilhas)), 0): ordemDasPilhas = hc.trocarPosicao(ordemDasPilhas) if itinerator >= 100: break else: itinerator = itinerator+1 return ordemDasPilhas
def MixedPathRelink(OPA, OPC): piorSolucao = [] melhorSolucao = [] melhorCusto = 0 solucaoSaida = [] # VERIFICA QUAL DAS DUAS PILHAS É A MAIOR OU MENOR pilhaA = np.max(hc.PilhasAbertas(OPA)) pilhaC = np.max(hc.PilhasAbertas(OPC)) if(pilhaA < pilhaC): melhorSolucao = OPA piorSolucao = OPC melhorCusto = pilhaA else: melhorSolucao = OPC piorSolucao = OPA melhorCusto = pilhaC solucaoSaida = melhorSolucao # VAI REALIZAR A TROCA ENTRE MOVER PARA DIREITA, OU PARA A ESQUERDA switch = True custoAtual = 0 while (list(piorSolucao) != list(melhorSolucao)): # MENOR PARA O MAIOR if(switch): # CRIA-SE UM VETOR COM INDICE DOS ELEMENTOS DIFERENTES vetNaoSimetricos = [i for i in range(len(piorSolucao)) if piorSolucao[i] != melhorSolucao[i]] # REALIZA A TROCAs for i in range(len(vetNaoSimetricos) - 1): piorSolucao[vetNaoSimetricos[i]], piorSolucao[vetNaoSimetricos[i + 1]] = piorSolucao[ vetNaoSimetricos[i + 1]], piorSolucao[vetNaoSimetricos[i]] custoAtual = np.max(hc.PilhasAbertas(piorSolucao)) switch = False # MAIOR PARA O MENOR else: # CRIA-SE UM VETOR COM INDICE DOS ELEMENTOS DIFERENTES vetNaoSimetricos = [i for i in range(len(melhorSolucao)) if melhorSolucao[i] != piorSolucao[i]] # REALIZA A TROCA for i in reversed(range(len(vetNaoSimetricos) - 1)): melhorSolucao[vetNaoSimetricos[i]], melhorSolucao[vetNaoSimetricos[i + 1]] = melhorSolucao[vetNaoSimetricos[i + 1]], melhorSolucao[vetNaoSimetricos[i]] custoAtual = np.max(hc.PilhasAbertas(melhorSolucao)) switch = True if custoAtual <= melhorCusto: solucaoSaida = piorSolucao melhorCusto = custoAtual return solucaoSaida
def graspRum(ordemDasPilhas): resultadoBom = np.max(hc.PilhasAbertas(ordemDasPilhas)) # ORDENA A MATRIZ DE FORMA CRESCENTE matOrd = gerarMatrizOrdenada() for counter in range(100): ordemDasPilhasAtual = construtivaGrasp(matOrd) ordemDasPilhasAtual = hr.RandonUpHillMethod(list(ordemDasPilhasAtual), 100) resultadoMelhor = np.max(hc.PilhasAbertas(ordemDasPilhasAtual)) if resultadoMelhor < resultadoBom: ordemDasPilhas = ordemDasPilhasAtual resultadoBom = resultadoMelhor return ordemDasPilhas
def RandonUpHillMethod(ordemDasPilhas, iMax): resultadoBom = np.max(hc.PilhasAbertas(ordemDasPilhas)) ordemDasPilhasAtual = ordemDasPilhas i = 0 while i < iMax: ordemDasPilhasAtual = hc.embaralhar(ordemDasPilhasAtual) resultadoMelhor = np.max(hc.PilhasAbertas(ordemDasPilhasAtual)) if resultadoMelhor < resultadoBom : ordemDasPilhas = ordemDasPilhasAtual resultadoBom = resultadoMelhor i = -1 i = i+1 return ordemDasPilhas
def graspFim(ordemDasPilhas): resultadoBom = np.max(hc.PilhasAbertas(ordemDasPilhas)) # ORDENA A MATRIZ DE FORMA CRESCENTE matOrd = gerarMatrizOrdenada() i = 0 while i < 150: ordemDasPilhasAtual = construtivaGrasp(matOrd) ordemDasPilhasAtual = hr.FirstImprovementMethod(ordemDasPilhasAtual) resultadoMelhor = np.max(hc.PilhasAbertas(ordemDasPilhasAtual)) if resultadoMelhor < resultadoBom: ordemDasPilhas = ordemDasPilhasAtual resultadoBom = resultadoMelhor i = -1 i = i + 1 return ordemDasPilhas
def IteratedLocalSearch(ordemDasPilhas, Method): resultadoBom = np.max(hc.PilhasAbertas(ordemDasPilhas)) ordemDasPilhasFinal = ordemDasPilhas historico = [list(ordemDasPilhas)] i = 0 while True: perturbacoes(historico, ordemDasPilhas) if (Method == 'FIM'): ordemDasPilhas = hr.FirstImprovementMethod(ordemDasPilhas) elif (Method == 'RUM'): ordemDasPilhas = hr.RandonUpHillMethod(ordemDasPilhas, 100) resultadoMelhor = np.max(hc.PilhasAbertas(ordemDasPilhas)) if resultadoBom > resultadoMelhor: resultadoBom = resultadoMelhor ordemDasPilhasFinal = list(ordemDasPilhas) i = i + 1 if i >= 50: break return ordemDasPilhasFinal
def graspPathRelinkForwardFim(ordemDasPilhas): resultadoBom = np.max(hc.PilhasAbertas(ordemDasPilhas)) # ORDENA A MATRIZ DE FORMA CRESCENTE matOrd = grasp.gerarMatrizOrdenada() # LISTA D CANDIDATOS ls = [] i = 0 while i < 150: ordemDasPilhasAtual = grasp.construtivaGrasp(matOrd) ordemDasPilhasAtual = hr.FirstImprovementMethod(ordemDasPilhasAtual) #ADICIONA A LISTA DE CANDIDATOS O RESULTADO ATUAL if(len(ls) < 2): # VERIFICA REPETICAO tem = [np.all(ordemDasPilhasAtual == x) for x in ls] # ADICIONA SE NAO TIVER REPETIDO if (not np.any(tem)): ls.append(list(ordemDasPilhasAtual)) else: # ESCOLHER UM VETOR orderPilhasCandidata = random.choice(ls) ordemDasPilhasAtual = forwardPathRelink(list(ordemDasPilhasAtual), list(orderPilhasCandidata)) if(len(ls) < 20): # VERIFICA REPETICAO tem = [np.all(ordemDasPilhasAtual == x) for x in ls] # ADICIONA SE NAO TIVER REPETIDO if (not np.any(tem)): ls.append(list(ordemDasPilhasAtual)) else: # indices = list(range(0, len(ordemDasPilhasAtual))) # matPilhasAbertas = [ [np.max(hc.PilhasAbertas(i)), i] for i in ls] removeIten = 21 last = np.max(hc.PilhasAbertas(ordemDasPilhasAtual)) atual = last k = 0 for j in ls: temp = np.max(hc.PilhasAbertas(j)) if (temp > last and temp > atual): removeIten = k last = temp k = k + 1 if (removeIten < 20): ls.pop(removeIten) tem = [np.all(ordemDasPilhasAtual == x) for x in ls] # ADICIONA SE NAO TIVER REPETIDO if (not np.any(tem)): ls.append(list(ordemDasPilhasAtual)) # matPilhasAbertas = np.array(matPilhasAbertas) # print(matPilhasAbertas[matPilhasAbertas[:,0].argsort()]) resultadoMelhor = np.max(hc.PilhasAbertas(ordemDasPilhasAtual)) if resultadoMelhor < resultadoBom : ordemDasPilhas = ordemDasPilhasAtual resultadoBom = resultadoMelhor i = -1 i = i+1 return ordemDasPilhas
def main(FILENAME, SELECT): print( "\nMOSP - MINIMIZATION OF OPEN STACKS PROBLEM (PROBLEMA DE MINIMIZAÇÃO DE PILHAS ABERTAS)" ) try: # CARREGA OS DADOS DO ARQUIVO df.dataRead(FILENAME) # ------------------------- MÉTODO CONSTRUTIVO ---------------------------------------# #---------------------------- RandonShuffle ------------------------------------------# print("\nRandonShuffle - Método Construtivo") ordemDasPilhas = list(range(0, g.nrows)) # CRIA A LISTA INICIAL timeCounter = time.time() # INICIA O CONTADOR ordemDasPilhas = hc.RandonShuffle(ordemDasPilhas) # METODO CONSTRUTOR timeCounter = time.time() - timeCounter # ENCERRA O CONTADOR qtdPilhasAbertas = hc.PilhasAbertas( ordemDasPilhas) # REALIZA A CONTAGEM DAS PILHAS MatrizDePilhas = g.matPaPe[ ordemDasPilhas, :] # GERA A MATRIZ A PARTIR DOS INDICES nomeMetodo = 'RandonShuffle' print("\n[INFO]: Ordem das pilhas: ", end="") print(ordemDasPilhas) print("\n[INFO]: Quantidade de pilhas abertas: ", end="") print(qtdPilhasAbertas) print('\n[INFO]: O tempo total de execução foi: ', end="") print(timeCounter) mmosp = hc.MMOSP(ordemDasPilhas) print("MMOSP: " + str(mmosp)) df.dataWrite(FILENAME, nomeMetodo, timeCounter, MatrizDePilhas, qtdPilhasAbertas, mmosp) # GRAVA NO DISCO AS INFORMAÇÕES # ---------------------------- FirstImprovementMethod --------------------------------------# if (SELECT == 1): print("\nFirstImprovementMethod - Método de Refinamento\n") # METODO REFINAMENTO timeCounter = time.time() ordemDasPilhas = hr.FirstImprovementMethod(ordemDasPilhas) timeCounter = time.time() - timeCounter nomeMetodo = 'FirstImprovementMethod' # ---------------------------- RandonUpHillMethod --------------------------------------# elif (SELECT == 2): print("\nRandonUpHillMethod - Método de Refinamento\n") timeCounter = time.time() ordemDasPilhas = hr.RandonUpHillMethod(ordemDasPilhas, 100) timeCounter = time.time() - timeCounter nomeMetodo = 'RandonUpHillMethod' # ---------------------------- IteratedLocalSearch FirstImprovementMethod --------------------------------------# elif (SELECT == 3): print("[INFO] Iterated Local Search - First Improvement Method") timeCounter = time.time() ordemDasPilhas = ils.IteratedLocalSearch(ordemDasPilhas, 'FIM') timeCounter = time.time() - timeCounter nomeMetodo = 'IteratedLocalSearchFirstImprovement' # ---------------------------- IteratedLocalSearch RandonUpHillMethod --------------------------------------# elif (SELECT == 4): print("[INFO] Iterated Local Search - Randon Uphill Method") timeCounter = time.time() ordemDasPilhas = ils.IteratedLocalSearch(ordemDasPilhas, 'RUM') timeCounter = time.time() - timeCounter nomeMetodo = 'IteratedLocalSearchRandonUphill' elif (SELECT == 5): print("[INFO] GRASP - First Improvement Method") timeCounter = time.time() ordemDasPilhas = grasp.graspFim(ordemDasPilhas) timeCounter = time.time() - timeCounter nomeMetodo = 'GraspFirstImprovement' elif (SELECT == 6): print("[INFO] GRASP - Randon Uphill Method") timeCounter = time.time() ordemDasPilhas = grasp.graspRum(ordemDasPilhas) timeCounter = time.time() - timeCounter nomeMetodo = 'GraspRandonUphill' elif (SELECT == 7): print("[INFO] GRASP PathRelink Forward - First Improvement Method") timeCounter = time.time() ordemDasPilhas = grasprf.graspPathRelinkForwardFim(ordemDasPilhas) timeCounter = time.time() - timeCounter nomeMetodo = 'GraspFirstImprovementPathRelinkForward' elif (SELECT == 8): print( "[INFO] GRASP PathRelink Backward - First Improvement Method") timeCounter = time.time() ordemDasPilhas = grasprb.graspPathRelinkBackwardFim(ordemDasPilhas) timeCounter = time.time() - timeCounter nomeMetodo = 'GraspFirstImprovementPathRelinkBackward' elif (SELECT == 9): print("[INFO] GRASP PathRelink Mixed - First Improvement Method") timeCounter = time.time() ordemDasPilhas = grasprm.graspPathRelinkMixedFim(ordemDasPilhas) timeCounter = time.time() - timeCounter nomeMetodo = 'GraspFirstImprovementPathRelinkMixed' # --------------------------------------- FIM DOS METODOS ---------------------------------------------------# # IMPRESSÃO E GRAVAÇÃO NO DISCO qtdPilhasAbertas = hc.PilhasAbertas( ordemDasPilhas) # REALIZA A CONTAGEM DAS PILHAS MatrizDePilhas = g.matPaPe[ ordemDasPilhas, :] # GERA A MATRIZ A PARTIR DOS INDICES print("\n[INFO]: Ordem das pilhas: ", end="") print(ordemDasPilhas) print("\n[INFO]: Quantidade de pilhas abertas: ", end="") print(qtdPilhasAbertas) print('\n[INFO]: O tempo total de execução foi: ', end="") print(timeCounter) mmosp = hc.MMOSP(ordemDasPilhas) print("MMOSP: " + str(mmosp)) df.dataWrite(FILENAME, nomeMetodo, timeCounter, MatrizDePilhas, qtdPilhasAbertas, mmosp) except ValueError as err: raise (err)