from libs.LerArquivo import LerArquivo from libs.Particula import Particula from libs.Graph import Graph MAX_ITERACOES = 1000 NUM_PARTICLES = 100 if __name__ == "__main__": import copy fuckingBestFit = 1e9 veiculos = None numClientes, capacidade, coords, demandas, timeWindow, matrizDistancia = LerArquivo().readFile("In/TW/25C/C101.txt") particulas = [Particula(numClientes, capacidade, coords, demandas, timeWindow, matrizDistancia) for _ in range(NUM_PARTICLES)] try: codificacao = None for particula in particulas: if(particula.fitness < fuckingBestFit): fuckingBestFit = particula.fitness codificacao = copy.deepcopy(particula.posicao) for i in range(NUM_PARTICLES): particulas[i].gbest = copy.deepcopy(codificacao) particulas[i].fitness = fuckingBestFit # print(p.posicao - p.gbest) atualizou = False for iteracao in range(MAX_ITERACOES): # print("Iteracao #{}: {:.5f}".format(iteracao, particulas[0].fitness)) atualizou = False for i in range(NUM_PARTICLES): fitness = particulas[i].calculaFitness() if(fitness < particulas[i].fitness): particulas[i].fitness = fitness
def main(): global NUM_PARTICLES, GBEST, tot tot = 0 NUM_ITERACOES = 100 num_Clientes = 25 if (len(sys.argv) == 1) else int(sys.argv[1]) for _ in range(5): NUM_PARTICLES = 50 num_Veiculos, capacidade, coords, demandas, coletas, time_Window, matriz_Distancia = LerArquivo( ).readFile("In/TWSPD/{}C/IC102.txt".format(num_Clientes), num_Clientes) clientes = [] for i in range(num_Clientes): clientes.append( Cliente(coords[i + 1], i + 1, demandas[i + 1], coletas[i + 1], time_Window[i + 1])) Global.init(num_Veiculos, num_Clientes, capacidade, coords, demandas, coletas, time_Window, matriz_Distancia, clientes, NUM_PARTICLES, NUM_ITERACOES) particles = [] for i in range(Global.NUM_PARTICLES): particles.append(Particle()) GBEST = get_GBest(particles) for i in range(Global.NUM_PARTICLES): particles[i].set_GBest(GBEST) for itera in range(Global.NUM_ITERACOES): update = False for i in range(Global.NUM_PARTICLES): particles[i].update_Velocidade(itera) particles[i].update_Posicao() update = update or particles[i].update() if (update): GBEST = get_GBest(particles) for i in range(Global.NUM_PARTICLES): particles[i].set_GBest(GBEST) print("Melhor fitness #{}: {}".format( itera, particles[0].gbest.fitness)) print("Melhor fitness: {}".format(particles[0].gbest.fitness)) tot += particles[0].gbest.fitness