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 ground_nodes(): """ It plots the solution""" k_values = [0.4, 0.6, 0.8, 1] list_x_global = [list() for i in range(len(k_values))] list_y_global = [list() for i in range(len(k_values))] scenarios.generate_victim_positions_traces() for i, k in enumerate(k_values): #list_victims_partical_victims = [] #list_partial_victims = [] list_x = list() list_y = list() scenarios.partial_knowledge_generation(k) for j in global_variables.list_partial_victims: list_x.append(j.node_x) list_y.append(j.node_y) list_x_global[i].append(list_x) list_y_global[i].append(list_y) # plotting drones connectivity fig = plt.figure(figsize=(13, 10), dpi=300) ax1 = fig.add_subplot(2, 2, 1) ax1.scatter(list_x_global[0], list_y_global[0], c='k', label="known victims", marker='o') ax1.set_title("a) 50 ground nodes", fontsize=12) ax1.set_xlabel("X [m]", fontsize=11) ax1.set_ylabel("Y [m]", fontsize=11) ax1.axis([-50, 1050, -50, 1050]) ax1.grid(True) ax2 = fig.add_subplot(2, 2, 2) ax2.scatter(list_x_global[1], list_y_global[1], c='k', label="known victims", marker='o') ax2.set_title("b) 75 ground nodes", fontsize=12) ax2.set_xlabel("X [m]", fontsize=11) ax2.set_ylabel("Y [m]", fontsize=11) ax2.axis([-50, 1050, -50, 1050]) ax2.grid(True) ax3 = fig.add_subplot(2, 2, 3) ax3.scatter(list_x_global[2], list_y_global[2], c='k', label="known victims", marker='o') ax3.set_title("c) 100 ground nodes", fontsize=12) ax3.set_xlabel("X [m]", fontsize=11) ax3.set_ylabel("Y [m]", fontsize=11) ax3.axis([-50, 1050, -50, 1050]) ax3.grid(True) ax4 = fig.add_subplot(2, 2, 4) ax4.scatter(list_x_global[3], list_y_global[3], c='k', label="known victims", marker='o') ax4.set_title("d) 125 ground nodes", fontsize=12) ax4.set_xlabel("X [m]", fontsize=11) ax4.set_ylabel("Y [m]", fontsize=11) ax4.axis([-50, 1050, -50, 1050]) ax4.grid(True) fig.savefig("Positions.png")
def positions_from_file(fil, num, k, nodes, UAVs, fig): """ It plots the solution""" #global list_victim #global list_partial_victims f = open(fil, 'r') scenarios.generate_victim_positions_traces() scenarios.partial_knowledge_generation(k) #print list_victims list_drones = list() for line in f: fields = line.split(",") list_drones.append( global_variables.Drone(int(fields[0]), float(fields[1]), float(fields[2]))) for i in list_drones: print(i.id, i.node_x, i.node_y) #CO= quality.evaluate_coverage(list_drones) #G= quality.create_drones_graph(list_drones) #print quality.check_graph_connectivity(G) #for i in list_drones: # print i.neighbordrones #F= quality.evaluate_weighted(list_drones) CO = quality.evaluate_coverage(list_drones) print("coverage", CO) #FTO= quality.evaluate_tolerance(list_drones) #print FTO #RO= quality.evaluate_redundancy(list_drones) #print "redundancy", RO ground_x = list() # victims' positions ground_y = list() ground_x_covered = list() # victims' positions ground_y_covered = list() UAV_x = list() # drones' positions UAV_y = list() UAV_ground_x = list() # links among drones and victims UAV_ground_y = list() UAV_UAV_x = list() # links among drones UAV_UAV_y = list() for i in global_variables.list_partial_victims: ground_x.append(i.node_x) ground_y.append(i.node_y) for j in list_drones: UAV_x.append(j.node_x) UAV_y.append(j.node_y) #print UAV_x, UAV_y #print ground_x, ground_y UAV_ground_x, UAV_ground_y, ground_x_covered, ground_y_covered = plot_links( list_drones, 1) UAV_UAV_x, UAV_UAV_y = plot_links_drones(list_drones) #print UAV_ground_x # plotting drones connectivity #fig = plt.figure(figsize= (10,20), dpi = 300) ax1 = fig.add_subplot(4, 2, num) #s1= ax1.scatter(ground_x, ground_y, c = 'y', label = "ground_nodes", marker = 'o') s1 = ax1.scatter(UAV_x, UAV_y, c='r', label="UAV", marker='x') for i in range(0, len(UAV_UAV_x)): if i % 2 == 0: x1 = UAV_UAV_x[i] x11 = UAV_UAV_x[i + 1] y1 = UAV_UAV_y[i] y11 = UAV_UAV_y[i + 1] ax1.plot([x1, x11], [y1, y11], 'r--', label="UAV-UAV links", marker='_') ax1.set_xlabel("X [m]", fontsize=11) ax1.set_ylabel("Y [m]", fontsize=11) ax1.axis([-50, 1050, -50, 1050]) ax1.set_title(UAVs, fontsize=12) ax1.grid("on") ax2 = fig.add_subplot(4, 2, num + 1) s2 = ax2.scatter(ground_x, ground_y, c='c', label="ground_nodes", marker='x') s3 = ax2.scatter(ground_x_covered, ground_y_covered, c='m', label="ground_nodes", marker='x') ax2.scatter(UAV_x, UAV_y, c='r', label="UAV", marker='x') for i in range(0, len(UAV_ground_x)): if i % 2 == 0: x1 = UAV_ground_x[i] x11 = UAV_ground_x[i + 1] y1 = UAV_ground_y[i] y11 = UAV_ground_y[i + 1] ax2.plot([x1, x11], [y1, y11], 'g--', label="UAV-ground links", marker='_') ax2.set_xlabel("X [m]", fontsize=11) ax2.set_ylabel("Y [m]", fontsize=11) ax2.axis([-50, 1050, -50, 1050]) ax2.set_title(nodes, fontsize=12) ax2.grid("on") #l = ["unknown victims", "known victims", "drones"] #c = ['b', 'k', 'r', 'y', 'g', 'r'] #m = ['o', 'o', 'o', '_', '_', '_'] UAV_links = mlines.Line2D([], [], color='red', linestyle='--', label='Blue stars') GN_links = mlines.Line2D([], [], color='green', linestyle='--', label='Blue stars') if num == 1: fig.legend(handles=[s1, s2, s3, UAV_links, GN_links], labels=['UAV', 'GN', 'Cov-GN', 'UAV-links', 'GN-links'], loc='upper center') #fig.legend(handles=[s1, s2, s3, s4, s5, s6], ('UAV', 'GN','Cov-GN','UAV-links', 'GN-GN.links'), loc= 'upper right') fig.savefig("Positions.png")
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)