def only_local(option, f): knowledge = 0 iterations_local = 50 # number of trials scenarios.generate_victim_positions_traces() scenarios.partial_knowledge_generation(knowledge) list_best_individuals = list() list_best_fitness = list() list_evolution_max = list() list_best_time = list() for i in range(0, iterations_local): list_drones = quality.init( global_variables.num_drones) # random selection global_variables.partial = 1 result_global = quality.evaluate(list_drones) global_variables.partial = 1 # we evaluate the hill climbing type_global = "Genetic Algorithm" if option == "Hill": type_local = "Hill Climbing" list_drones_climbing, records = local.hill_climbing( list_drones, result_global) list_best_individuals.append(list_drones_climbing) #list_best_time.append(best_time) q, = quality.evaluate(list_drones_climbing) list_best_fitness.append(q) list_evolution_max.append(records) length = len(list_best_fitness) mean = sum(list_best_fitness) / length sum2 = sum(x * x for x in list_best_fitness) std = abs(sum2 / length - mean**2)**0.5 index = miscelleneous.find_max(list_best_fitness) f.write(type_local) f.write("\n") f.write("Results \n") f.write("Max, Min, Mean, Std \n") f.write( str(max(list_best_fitness)) + "," + str(min(list_best_fitness)) + "," + str(mean) + "," + str(std)) f.write(str(list_best_fitness)) plots.print_drones_data(list_best_individuals[index], f) plots.evolution_local(list_evolution_max[index], type_local) plots.positions(list_drones, list_best_individuals[index], type_global, type_local)
def hill_climbing(lista_drones, global_max): """ Hill climbing algorithm. It receives the list of drones and the global maximum""" #global partial #global_variables.partial = 1 # we evaluate hill climbing for the initial deployment problem global_variables.partial = 0 records = [] records.append(global_max) first_max = global_max max_list = copy.deepcopy(lista_drones) # watch up max_speed = 10 # meters/seconds simulation_time = 50000 # seconds #choose_directions(lista_drones, max_speed) # here we selec the type of movement selected = choose_directions_single(lista_drones, max_speed) current_quality = 0.0 prueba = lista_drones best_time = 0 print("---------- START CLIMBING OPTIMIZATION ----------") for i in range(0, simulation_time): update_drones_positions(lista_drones, 0) #update_drones_positions_single(lista_drones, selected) current_quality = quality.evaluate(lista_drones) if (current_quality <= global_max): #selected= step_back_single(lista_drones, selected, max_speed) step_back(lista_drones, max_speed) #print("go back") else: print("----------------previous quality %f" % global_max) global_max = current_quality #records.append(global_max) prueba = copy.deepcopy(lista_drones) print("----------------new quality %f" % global_max) plots.print_drones_data(lista_drones, 0) best_time = i # we update the time at which we find the best solution so far print("-----------------------------------------------") print("-----------------------------------------------") # we print some information about how the positions are updated. print("quality before hill climbing %f" % first_max) print("List of victims covered %s" % quality.calc_victims_covered(max_list)) print("total number of victims covered %d" % len(quality.calc_victims_covered(max_list))) print("-------INITIAL POSITIONS----------------------") plots.print_drones_data(max_list, 0) print("----------------------------------------------") print("----------------------------------------------") print("final quality hill climbing %f" % global_max) print("List of victims covered %s" % quality.calc_victims_covered(prueba)) print("total number of victims covered %d" % len(quality.calc_victims_covered(prueba))) print("-------FINAL POSITIONS----------------------") plots.print_drones_data(prueba, 0) print("----------------------------------------------") print("----------------------------------------------") return prueba, records
def random_deployment(): list_drones = quality.init(global_variables.num_drones) global_variables.partial = 0 result, = quality.evaluate(list_drones) return list_drones, result
def simple_grid(): list_drones = quality.init_grid(global_variables.num_drones) global_variables.partial = 0 result, = quality.evaluate(list_drones) return list_drones, result
def simulated_annealing(lista_drones, global_max, deviation): """ Simulated annealing algorithm. It receives the list of drones and the global maximum""" max_speed = 10 # meters/seconds simulation_time = 10000 # seconds global_variables.partial = 0 temperature = float(deviation) / 0.0953 records = [] records_positions = [] records_probability = [] records_temperature = [] records_aux = [] global_max2, = global_max first_max = global_max max_list = copy.deepcopy(lista_drones) # watch up records.append(global_max2) records_positions.append(max_list) choose_directions(lista_drones, max_speed) print("---------- START SIMULATED ANNEALING OPTIMIZATION ----------") for i in range(0, simulation_time): update_drones_positions(lista_drones) current_quality, = quality.evaluate(lista_drones) if current_quality <= global_max2 and current_quality > 0: probability = calculate_annealing_probability( temperature, current_quality, global_max2) print(probability) records_probability.append(probability) #records_aux.append(aux) if (probability >= random.random()): # we should avoid non valid solutions print("----------------accepting a WORSE solution--------") print("----------------previous quality %f" % global_max2) global_max2 = current_quality records.append(global_max2) prueba = copy.deepcopy(lista_drones) records_positions.append(prueba) print("----------------new quality %f" % global_max2) plots.print_drones_data(lista_drones, 0) print("-----------------------------------------------") print("-----------------------------------------------") else: step_back(lista_drones, max_speed) else: if (current_quality > 0): print("----------------accepting a BETTER solution--------") print("----------------previous quality %f" % global_max2) global_max2 = current_quality prueba = copy.deepcopy(lista_drones) records.append(global_max2) records_positions.append(prueba) print("----------------new quality %f" % global_max2) plots.print_drones_data(prueba, 0) print("-----------------------------------------------") print("-----------------------------------------------") temperature = update_annealing_temperature(temperature, deviation, simulation_time) records_temperature.append(temperature) print("temperature %f" % temperature) #plots.evolution_local(records_aux, "probability") index = miscelleneous.find_max(records) print("quality before simulated annealing %f" % first_max) print("List of victims covered %s" % quality.calc_victims_covered(max_list)) print("total number of victims covered %d" % len(quality.calc_victims_covered(max_list))) print("-------INITIAL POSITIONS----------------------") plots.print_drones_data(max_list, 0) print("----------------------------------------------") print("----------------------------------------------") print("final quality simulated annealing %f" % records[index]) print("List of victims covered %s" % quality.calc_victims_covered(records_positions[index])) print("total number of victims covered %d" % len(quality.calc_victims_covered(records_positions[index]))) print("-------FINAL POSITIONS----------------------") plots.print_drones_data(records_positions[index], 0) print("----------------------------------------------") print("----------------------------------------------") return records_positions[index], records
def tabu_search(lista_drones, global_max): """ Tabu search algorithm. It receives the list of drones and the global maximum""" #global partial global_variables.partial = 1 records = [] records.append(global_max) movement_list = [] tabu_list = [] # store the positions that have already visited first_max = global_max # we store the first maximum gloabal max_list = copy.deepcopy( lista_drones) # make a copy of the original postions of the drones max_speed = 10 # meters/seconds simulation_time = 10000 # seconds choose_directions(lista_drones, max_speed) current_quality = 0.0 repetido = 0 print("---------- START TABU SEARCH OPTIMIZATION ----------") for i in range(0, simulation_time): update_drones_positions(lista_drones, 0) current_quality = quality.evaluate(lista_drones) current_positions = miscelleneous.get_positions(lista_drones) if current_positions in tabu_list: repetido = repetido + 1 print("REPETIDOOOOOOOOOOOOOOOOOOOOOOOO") else: if (current_quality <= global_max): step_back(lista_drones, max_speed) #print("go back") else: print(current_positions) update_movements(lista_drones) movement_list.append(current_positions) print("----------------previous quality %f" % global_max) global_max = current_quality records.append(global_max) prueba = copy.deepcopy(lista_drones) print("----------------new quality %f" % global_max) plots.print_drones_data(lista_drones, 0) print("-----------------------------------------------") print("-----------------------------------------------") tabu_list.append(current_positions) print("repetido %d" % repetido) print("quality before tabu search %f" % first_max) print("List of victims covered %s" % quality.calc_victims_covered(max_list)) print("total number of victims covered %d" % len(quality.calc_victims_covered(max_list))) print("-------INITIAL POSITIONS----------------------") plots.print_drones_data(max_list, 0) print("----------------------------------------------") print("----------------------------------------------") print("final quality tabu search %f" % global_max) print("List of victims covered %s" % quality.calc_victims_covered(prueba)) print("total number of victims covered %d" % len(quality.calc_victims_covered(prueba))) print("-------FINAL POSITIONS----------------------") plots.print_drones_data(prueba, 0) print("----------------------------------------------") print("----------------------------------------------") plots.plot_movements(lista_drones) return prueba, records
def global_plus_local(f): """ This function runs the whole approach global + local search """ iterations_genetic = 1 # iterations genetic algorithm knowledge = 0.8 # level of knowledge list_best_individuals = list( ) # to store the best individual of each genetic algorithm run list_best_fitness = list( ) # to store the best fitness of the best individual of each run list_generations = list() # to store the generations list_drones_climbing = list() list_evolution_max = list() type_global = "Genetic" type_local = "Hill Climbing" f.write(type_global) f.write("\n") scenarios.generate_victim_positions_traces() scenarios.partial_knowledge_generation(knowledge) for i in range(0, iterations_genetic): individual, fitness, generation, evol_max = genetic.genetic_algorithm( "global_plus_local", i, knowledge) list_best_individuals.append(individual) list_best_fitness.append(fitness) list_generations.append(generation) list_evolution_max.append(evol_max) length = len(list_best_fitness) mean = sum(list_best_fitness) / length sum2 = sum(x * x for x in list_best_fitness) std = abs(sum2 / length - mean**2)**0.5 f.write("Results \n") f.write("Max, Min, Mean, Std \n") f.write( str(max(list_best_fitness)) + "," + str(min(list_best_fitness)) + "," + str(mean) + "," + str(std)) f.write("\n") global_max = max(list_best_fitness) index = miscelleneous.find_max(list_best_fitness) plots.print_drones_data(list_best_individuals[index], f) # LOCAL f.write(type_local) f.write("\n") #list_dr = quality.init_modified(list_best_individuals[index], global_variables.num_drones) # To simulate different number of drones for the initial deployment and for the adaptation to the real conditions #list_drones_climbing, records = local.hill_climbing(list_dr, list_best_individuals[index].fitness.values) list_drones_climbing, records = local.hill_climbing( list_best_individuals[index], list_best_individuals[index].fitness.values) f.write("Results \n") f.write(str(quality.evaluate(list_drones_climbing))) plots.print_drones_data(list_drones_climbing, f) plots.positions(list_best_individuals[index], list_drones_climbing, type_global, type_local) plots.evolution_local(records, type_local) plots.evolution_global(list_evolution_max[index], type_global) print("######### FIRST DEPLOYMENT STATISTICS ################") print(" Min %s" % min(list_best_fitness)) print(" Max %s" % max(list_best_fitness)) print(" Avg %s" % mean) print(" Std %s" % std)