for _ in range(10000): population.next_generation() # resultado final final_population = population.get_population() best_solution = final_population[0] best_chromosome = best_solution[0] best_distance = best_solution[1] print("Tamanho da população: {}".format(len(final_population))) print("Taxa de mutação: 5%") print("Número de cidades: {}".format(cities_location.get_cities_count())) print("Melhor solução: {}".format(best_distance)) plt.plot(cities_location.get_cities()[0], cities_location.get_cities()[1], 'ro') best_chromosome_cities = best_chromosome.get_cities() best_path_x = [] best_path_y = [] for i in range(len(best_chromosome_cities)): city = cities_location.get_city(best_chromosome_cities[i]) best_path_x.append(city.x) best_path_y.append(city.y) first_city = cities_location.get_city(best_chromosome_cities[0]) best_path_x.append(first_city.x) best_path_y.append(first_city.y) plt.plot(best_path_x, best_path_y)
def handler(event, context): body_cities = None body_distances = None population_size = 20 mutation_rate = 1 # 1% - taxa de mutação generations = 1000 # critério de parada time_distances = [] if event: event = json.loads(event['body']) population_size = int(event['populationSize']) mutation_rate = int(event["mutationRate"]) generations = int(event["generations"]) body_cities = event['cities'] body_distances = event["distances"] try: c = Cities() if body_cities and body_distances: c.set_cities(body_cities, body_distances) else: c.test() # carrega cidades para testes cities_list = c.get_cities() for city in cities_list: print("Distâncias da cidade: %s\n******" % city.name) time_distances.append(city.distances) print(city.distances) for index, distance in enumerate(city.distances): print("De %s --> %s = %s" % (city.name, cities_list[index].name, distance)) ga = GeneticAlgorithm(population_size) result = ga.resolve(mutation_rate, generations, time_distances, cities_list) print({ 'generation': result[0], 'travelled_distance': result[1], 'chromosome': result[2], 'cities': c.chromose_to_cities(result[2]) }) return { 'statusCode': 200, 'headers': { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }, 'body': json.dumps({ 'generation': result[0], 'travelled_distance': result[1], 'chromosome': result[2], 'cities': c.chromose_to_cities(result[2]) }) } except ImportError: print(ImportError) return { 'statusCode': 500, 'body': json.dumps({'message': "Erro ao executar"}) }
population.next_generation() # resultado final final_population = population.get_population() best_solution = final_population[0] best_chromosome = best_solution[0] best_distance = best_solution[1] print("Tamanho da população: {}".format(len(final_population))) print("Taxa de mutação: 10%") print("Número de cidades: {}".format(cities_location.get_cities_count())) print("Melhor custo: {}".format(best_distance)) print("Melhor solução: {}".format(best_chromosome.get_cities())) # Exibe os dados no matplot plt.plot(cities_location.get_cities()[0], cities_location.get_cities()[1], 'ro') best_chromosome_cities = best_chromosome.get_cities() best_path_x = [] best_path_y = [] for i in range(len(best_chromosome_cities)): city = cities_location.get_city(best_chromosome_cities[i]) best_path_x.append(city.x) best_path_y.append(city.y) first_city = cities_location.get_city(best_chromosome_cities[0]) best_path_x.append(first_city.x) best_path_y.append(first_city.y) plt.plot(best_path_x, best_path_y)