def grid_deployment(f): scenarios.generate_victim_positions_traces() iteractions = 50 list_solutions = list() list_fitness = list() for i in range(0, iteractions): sol, fit = algorithms.simple_grid() list_solutions.append(sol) list_fitness.append(fit) length = len(list_fitness) mean = sum(list_fitness) / length sum2 = sum(x * x for x in list_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_fitness)) + "," + str(min(list_fitness)) + "," + str(mean) + "," + str(std)) f.write("\n") global_max = max(list_fitness) index = miscelleneous.find_max(list_fitness) plots.print_drones_data(list_solutions[index], f) f.write("simple grid deployment") f.write("\n")
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 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)
def only_global(f, type_algorithm, knowledge, fil, argument): #iterations_genetic = 120 #iterations_genetic = 30 # how many times we run the algorithm iterations_genetic = 30 iterations_pso = 1 list_best_individuals = list() # list of best individuals list_best_fitness = list() # list of best fitnesses list_generations = list() # list of generations list_evolution_max = list() list_covergence = list() list_best_individuals_f1 = list() # list of best individuals list_best_fitness_f1 = list() # list of best fitnesses list_generations_f1 = list() # list of generations list_evolution_max_f1 = list() list_id_f1 = list() list_covergence_f1 = list() list_best_individuals_f2 = list() # list of best individuals list_best_fitness_f2 = list() # list of best fitnesses list_generations_f2 = list() # list of generations list_evolution_max_f2 = list() list_id_f2 = list() list_covergence_f2 = list() list_best_individuals_f3 = list() # list of best individuals list_best_fitness_f3 = list() # list of best fitnesses list_generations_f3 = list() # list of generations list_evolution_max_f3 = list() list_id_f3 = list() list_covergence_f3 = list() list_best_individuals_f4 = list() # list of best individuals list_best_fitness_f4 = list() # list of best fitnesses list_generations_f4 = list() # list of generations list_evolution_max_f4 = list() list_id_f4 = list() list_covergence_f4 = list() results_f1 = list() results_f2 = list() results_f3 = list() results_f4 = list() res = list() # scenario generation ## basically, the victims are splitted into quadrants (4 :1.up-right,2.left-down,3.up-left,4.down-right) ## given the preferred number of ground nodes to display, the quadrants will appear scenarios.generate_victim_positions_traces() scenarios.partial_knowledge_generation(knowledge) if type_algorithm == "genetic": for i in range(0, iterations_genetic): # individual, fitness, generation, evol_max, convergence = genetic.genetic_algorithm( "genetic", i, knowledge) list_best_individuals.append(individual) list_best_fitness.append(fitness) list_generations.append(generation) list_evolution_max.append(evol_max) list_covergence.append(convergence) #print convergence print("list_covergence STEP ", list_covergence) stat = miscelleneous.statistics(list_best_fitness) stat_convergence = miscelleneous.statistics(list_covergence) data_results = pd.DataFrame( [stat], columns=["maximum", "minimum", "mean", "std", "index"]) data_convergence = pd.DataFrame( [stat_convergence], columns=["maximum", "minimum", "mean", "std", "index"]) data_results.to_csv(fil + "results.csv") data_convergence.to_csv(fil + "convergence.csv") f.write("The best solution of population 1\n") plots.print_drones_data(list_best_individuals[stat["index"]], list_evolution_max[stat["index"]], f) if type_algorithm == "pso": for i in range(0, iterations_pso): individual, fitness, = pso.pso_algorithm() list_best_individuals.append(individual) list_best_fitness.append(fitness) if type_algorithm == "multi_population": for i in range(0, iterations_genetic): ## print the iterations print("Iteration genetic : ", i) if len(argument) > 1: ## input : initial positions res = ga_multi_population.ga_multi_population( argument, type_algorithm, i, knowledge) else: ## classic search with no individuals to start with res = ga_multi_population.ga_multi_population( None, type_algorithm, i, knowledge) ## 4 * iterations_genetic number of ga_multi_population : each node in the queue (ring schema) ## has THE SAME : iterations_genetic (30) results for i, r in enumerate(res): if i == 0: results_f1.append(r) if i == 1: results_f2.append(r) if i == 2: results_f3.append(r) if i == 3: results_f4.append(r) if type_algorithm == "multi_population": for r in results_f1: list_best_fitness_f1.append(r.best_fitness) list_best_individuals_f1.append(r.best) list_evolution_max_f1.append(r.best_evolution) list_id_f1.append(r.id) list_covergence_f1.append(r.convergence_generation) for r in results_f2: list_best_fitness_f2.append(r.best_fitness) list_best_individuals_f2.append(r.best) list_evolution_max_f2.append(r.best_evolution) list_id_f2.append(r.id) list_covergence_f2.append(r.convergence_generation) for r in results_f3: list_best_fitness_f3.append(r.best_fitness) list_best_individuals_f3.append(r.best) list_evolution_max_f3.append(r.best_evolution) list_id_f3.append(r.id) list_covergence_f3.append(r.convergence_generation) for r in results_f4: list_best_fitness_f4.append(r.best_fitness) list_best_individuals_f4.append(r.best) list_evolution_max_f4.append(r.best_evolution) list_id_f4.append(r.id) list_covergence_f4.append(r.convergence_generation) stat1 = miscelleneous.statistics(list_best_fitness_f1) stat2 = miscelleneous.statistics(list_best_fitness_f2) stat3 = miscelleneous.statistics(list_best_fitness_f3) stat4 = miscelleneous.statistics(list_best_fitness_f4) ''' x=0 for v in (list_covergence_f1,list_covergence_f2,list_covergence_f3,list_covergence_f4) : print(x," : ",v) x=x+1 ''' stat_convergence1 = miscelleneous.statistics(list_covergence_f1) stat_convergence2 = miscelleneous.statistics(list_covergence_f2) stat_convergence3 = miscelleneous.statistics(list_covergence_f3) stat_convergence4 = miscelleneous.statistics(list_covergence_f4) data_results = pd.DataFrame( [stat1, stat2, stat3, stat4], columns=["maximum", "minimum", "mean", "std", "index"]) data_results.to_csv(fil + "results.csv") data_convergence = pd.DataFrame( [ stat_convergence1, stat_convergence2, stat_convergence3, stat_convergence4 ], columns=["maximum", "minimum", "mean", "std", "index"]) data_convergence.to_csv(fil + "convergence.csv") f.write("The best solution of population 1\n") plots.print_drones_data(list_best_individuals_f1[stat1["index"]], list_evolution_max_f1[stat1["index"]], f) f.write("The best solution of population 2\n") plots.print_drones_data(list_best_individuals_f2[stat2["index"]], list_evolution_max_f2[stat2["index"]], f) f.write("The best solution of population 3\n") plots.print_drones_data(list_best_individuals_f3[stat3["index"]], list_evolution_max_f3[stat3["index"]], f) f.write("The best solution of population 4\n") plots.print_drones_data(list_best_individuals_f4[stat4["index"]], list_evolution_max_f4[stat4["index"]], f) if type_algorithm == "multi_objective": pareto_global = tools.ParetoFront() for i in range(0, iterations_genetic): pareto = ga_multi_objective.genetic_algorithm( "multi_objective", i, knowledge) pareto_global.update(pareto) plots.print_pareto(pareto_global, f)