def main(): try: # num_Clients, clients, vehicles = Read_File().read_File("In/TWSPD/25C/IC101.txt", "In/In_Veiculos") num_Clients, clients, vehicles = Read_File().read_File("In/aleatorio", "In/In_Veiculos") Global._init(_num_Clients = num_Clients, _MAX_PARTICLES = int(math.ceil(num_Clients / 10.0)), _MAX_ITERATION = 1, _clients = clients, _vehicles = vehicles) # print("oi1") particles = [] for i in range(Global.MAX_PARTICLES): print("Criando Particula #{}".format(i + 1)) particles.append(Particle()) # particles = [Particle() for _ in range(Global.MAX_PARTICLES)] # print("oi2") update_GBest(particles) # print("oi3") for i in range(Global.MAX_ITERATION): # print("oi4") change = 0 for j in range(len(particles)): # print("oi5") particles[j].update_Velocity(i) # print("oi6") particles[j].update_Position() # print("oi7") change |= particles[j].update() if(change): update_GBest(particles) print("Iteracao #{}: {} | {}".format(i + 1, particles[0].gbest.fitness, particles[0].gbest.fitness_No_Cost)) finally: particles[0].gbest.show_VRP()
def main(): global NUM_PARTICLES, GBEST # try: NUM_ITERACOES = 1000 num_Clientes = 25 if (len(sys.argv) == 1) else int(sys.argv[1]) num_Arquivo = int(sys.argv[2]) NUM_PARTICLES = 100 # print(num_Clientes, num_Arquivo) num_Veiculos, veiculos_Capacidades, coords, demandas, coletas, time_Window, matriz_Distancia = LerArquivo( ).readFile("../entry-{}".format(num_Arquivo), 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, veiculos_Capacidades, 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) # print("Fitness atual: {}".format(GBEST.fitness)) 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("Iteracao #{}".format(itera)) # print("Melhor fitness: {}".format(particles[0].gbest.fitness)) # finally: # # for l in GBEST.rotas: # # print(' '.join([str(x.id) for x in l.clientes])) # Graph().draw(GBEST.rotas) print(GBEST.fitness)
def main(): global NUM_PARTICLES, GBEST NUM_ITERACOES = 1000 num_Clientes = 25 NUM_PARTICLES = num_Clientes num_Veiculos, capacidade, coords, demandas, time_Window, matriz_Distancia = LerArquivo( ).readFile("In/TW/{}C/C101.txt".format(num_Clientes), num_Clientes) clientes = [] for i in range(num_Clientes): clientes.append( Cliente(coords[i + 1], i + 1, demandas[i + 1], time_Window[i + 1])) Global.init(num_Veiculos, num_Clientes, capacidade, coords, demandas, 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("Iteracao #{}".format(itera)) print("Melhor fitness: {}".format(particles[0].gbest.fitness)) # for i, particle in enumerate(particles): # vel_Veiculo = particle.vel[Global.num_Clientes:] # print("Particle #{} = {}".format(i + 1, vel_Veiculo)) Graph().draw(GBEST.rotas)
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
def main(): try: num_Clients, num_Vehicles, clients, vehicles = Read_File().read_File( "In/TWSPD/100C/IC101.txt", "In/In_Veiculos") Global._init(_num_Clients=num_Clients, _num_Vehicles=num_Vehicles, _MAX_PARTICLES=100, _MAX_ITERATION=1000, _clients=clients, _vehicles=vehicles) for t in range(5): anterior = -1 contador = 0 with open("IC_101_{}.txt".format(t + 1), "w") as file: particles = [] for i in range(Global.MAX_PARTICLES): particles.append(Particle()) update_GBest(particles) for i in range(Global.MAX_ITERATION): change = 0 for j in range(len(particles)): particles[j].update_Velocity(i) particles[j].update_Position() change |= particles[j].update() if (change): update_GBest(particles) print("Iteracao #{}: {}".format( i + 1, particles[0].gbest.fitness)) file.write("{}:{}\n".format(i + 1, particles[0].gbest.fitness)) if (particles[0].gbest.fitness <= 830.5): break if (particles[0].gbest.fitness == anterior): contador += 1 if (contador >= 300): break else: contador = 0 anterior = particles[0].gbest.fitness finally: pass
def main(): global NUM_PARTICLES, GBEST NUM_ITERACOES = 1000 num_Clientes = 100 NUM_PARTICLES = 100 pastas = ["C101_Het_FC_0.95"] valores = [(0.95, 2.85)] # try: for (pasta, valor) in zip(pastas, valores): for arq in range(0, 11): with open("{}/{}_{}.txt".format(pasta, pasta, arq), "w") as file: num_Veiculos, veiculos_Capacidades, coords, demandas, coletas, time_Window, matriz_Distancia = LerArquivo().readFile("In/TWSPD/{}C/IC101.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, veiculos_Capacidades, coords, demandas, coletas, time_Window, matriz_Distancia, clientes, NUM_PARTICLES, NUM_ITERACOES) particles = [] for i in range(Global.NUM_PARTICLES): particles.append(Particle(1.0, valor[0], valor[1])) GBEST = get_GBest(particles) for i in range(Global.NUM_PARTICLES): particles[i].set_GBest(GBEST) print("Fitness atual: {}".format(GBEST.fitness)) anterior = -1 contador = 0 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() temp_inicial = 10.0 temp_final = 0.0000001 A = 1 / (float(NUM_ITERACOES)) * math.log(temp_inicial / temp_final) t_atual = temp_inicial * math.exp(-A * (itera + 1)) mapeado = t_atual / 10.0 # print(itera, mapeado) update = update or particles[i].update(-1) if(update): GBEST = get_GBest(particles) for i in range(Global.NUM_PARTICLES): particles[i].set_GBest(GBEST) qtd_Veiculos = sum([1 for x in GBEST.rotas if len(x.clientes) > 2]) file.write(str(itera) + ":" + str(particles[0].gbest.fitness) + ":" + str(qtd_Veiculos) + "\n") print("Iteracao #{}: {} com {} veiculos".format(itera, particles[0].gbest.fitness, qtd_Veiculos)) if(particles[0].gbest.fitness <= 829.0): break if(particles[0].gbest.fitness == anterior): contador += 1 if(contador >= 200): particles = [] contador = 0 for i in range(Global.NUM_PARTICLES): particles.append(Particle(1.0)) GBEST = get_GBest(particles) for i in range(Global.NUM_PARTICLES): particles[i].set_GBest(GBEST) else: contador = 0 anterior = particles[0].gbest.fitness
def main(): try: num_Clients, num_Vehicles, clients, vehicles = Read_File().read_File( "In/TWSPD/100C/IC101.txt", "In/In_Veiculos") Global._init(_num_Clients=num_Clients, _num_Vehicles=num_Vehicles, _MAX_PARTICLES=100, _MAX_ITERATION=1000, _clients=clients, _vehicles=vehicles) threads_2 = [] thread_1 = threading.Thread(target=initParticle, args=( 0, 24, )) thread_1.start() thread_2 = threading.Thread(target=initParticle, args=( 25, 49, )) thread_2.start() thread_3 = threading.Thread(target=initParticle, args=( 50, 74, )) thread_3.start() thread_4 = threading.Thread(target=initParticle, args=( 75, 99, )) thread_4.start() threads_2.append(thread_1) threads_2.append(thread_2) threads_2.append(thread_3) threads_2.append(thread_4) # for i in range(Global.MAX_PARTICLES): # print("Criando Particula #{}".format(i + 1)) # particles.append(Particle()) # for i in range(4): # threads_2[i].start() for i in range(4): threads_2[i].join() update_GBest() for i in range(Global.MAX_ITERATION): time_start = time.time() threads = [] thread_1 = threading.Thread(target=runParticle, args=(0, 24, i)) thread_1.start() thread_2 = threading.Thread(target=runParticle, args=(25, 49, i)) thread_2.start() thread_3 = threading.Thread(target=runParticle, args=(50, 74, i)) thread_3.start() thread_4 = threading.Thread(target=runParticle, args=(75, 99, i)) thread_4.start() threads.append(thread_1) threads.append(thread_2) threads.append(thread_3) threads.append(thread_4) # for i in range(4): # threads[i].start() for j in range(4): threads[j].join() # change = 0 # for j in range(len(particles)): # particles[j].update_Velocity(i) # particles[j].update_Position() # change |= particles[j].update() update_GBest() time_end = time.time() print("Time elapsed: {}".format(time_end - time_start)) print("Iteracao #{}: {} {}".format( i + 1, particles[0].gbest.fitness_No_Cost, particles[0].gbest.fitness)) print("-" * 25) finally: particles[0].gbest.show_VRP()