def __init__(self, path_apf, name_exp, log): self._path_apf = path_apf self._name_exp = name_exp self._logger = log # need to load the controller from file saved -> path -> name I already know # decide which generation to use -> last? (0) or something before? (-1,....,-N) # load the controller and assign it to the generator # random position and taac, generate tra and save it self._list_genome = None self._loader_apf = LoadAPF(path=self._path_apf, logger=self._logger) self._loader_apf.load_apf_only_routing_system() self._loader_apf.match_index_with_coordinates() self._loader_genome_meaning = GenomeMeaning(logger=self._logger) self._loader_genome_meaning.load_data(test=False) self._sub_matrix = SubMatrix( log=self._logger, list_points=self._loader_genome_meaning.name_typologies, values_matrix=(self._loader_apf.x_values, self._loader_apf.y_values)) self._sub_matrix.divide_into_cells() self._pre_loaded_points = FindPlacesOnRoutes(logger=self._logger) self._pre_loaded_points.load_preloaded_position()
def __init__(self, log): super().__init__(log) self._loader_apf = LoadAPF(path=args.data_path + args.apf_name, logger=self._log) self._loader_apf.load_apf_only_routing_system()
class PrintTrajectories(DataLoader): def __init__(self, log): super().__init__(log) self._loader_apf = LoadAPF(path=args.data_path + args.apf_name, logger=self._log) self._loader_apf.load_apf_only_routing_system() def print_paths(self, path=None, name=None, apf=False): """ Print generated path on disk :param path: folder where to save the graphs :param name: name of the folder where to save the graphs :param apf: True if graph with Artificial Potential Field, False if only graph with trajectory :return: """ if path is not None: path = "{}/{}/".format(path, name) os.mkdir(path) for idx, attraction_variant in enumerate(self._paths_generated): if path is not None: path_here = "{}/set_attraction_{}/".format(path, idx) os.mkdir(path_here) else: path_here = None for tra_idx, tra in tqdm( enumerate(attraction_variant), desc="printing trajectories variant {}".format(idx)): x = [] y = [] combinations = {} for points in tra: x.append(int(points.x)) y.append(int(points.y)) combinations["{}-{}".format(int(points.x), int(points.y))] = 1 max_x = max(x) + 1 min_x = min(x) - 1 max_y = max(y) + 1 min_y = min(y) - 1 max_value_x = ((max_x - min_x) + 10) * 2 max_value_y = ((max_y - min_y) + 10) * 2 matrix = np.zeros((max_value_x, max_value_y)) if apf: difference_to_move_x = int( (max_value_x - (max_x - min_x)) / 2) difference_to_move_y = int( (max_value_y - (max_y - min_y)) / 2) real_min_x = min_x - difference_to_move_x real_max_x = max_x + difference_to_move_x real_min_y = min_y - difference_to_move_y real_max_y = max_y + difference_to_move_y for j in range(real_min_x, real_max_x): for q in range(real_min_y, real_max_y): value = self._loader_apf.apf.iloc[j, q] matrix_pos_x = j - real_min_x matrix_pos_y = q - real_min_y if "{}-{}".format(j, q) in combinations: matrix[matrix_pos_x, matrix_pos_y] = 2.0 elif value != 0: matrix[matrix_pos_x, matrix_pos_y] = 1.0 else: mdlx = int(max_value_x / 2) mdly = int(max_value_y / 2) for i in range(1, len(x)): diffx = x[i] - x[i - 1] diffy = y[i] - y[i - 1] mdlx += diffx mdly += diffy matrix[mdlx, mdly] = 2.0 dataframe_apf = pd.DataFrame.from_records(matrix) sns.heatmap(dataframe_apf, fmt="d", vmin=-1.0, vmax=2.0) if path_here is None: plt.show() else: plt.savefig("{}/trajectory_{}.png".format( path_here, tra_idx), dpi=500) plt.close()
class Controller(object): def __init__(self, path_apf, name_exp, log): self._path_apf = path_apf self._name_exp = name_exp self._logger = log # need to load the controller from file saved -> path -> name I already know # decide which generation to use -> last? (0) or something before? (-1,....,-N) # load the controller and assign it to the generator # random position and taac, generate tra and save it self._list_genome = None self._loader_apf = LoadAPF(path=self._path_apf, logger=self._logger) self._loader_apf.load_apf_only_routing_system() self._loader_apf.match_index_with_coordinates() self._loader_genome_meaning = GenomeMeaning(logger=self._logger) self._loader_genome_meaning.load_data(test=False) self._sub_matrix = SubMatrix( log=self._logger, list_points=self._loader_genome_meaning.name_typologies, values_matrix=(self._loader_apf.x_values, self._loader_apf.y_values)) self._sub_matrix.divide_into_cells() self._pre_loaded_points = FindPlacesOnRoutes(logger=self._logger) self._pre_loaded_points.load_preloaded_position() def set_vector_data(self, vector_data): """ Read data from file and load the controller :param vector_data: :return: """ self._list_genome = vector_data def initialise_individual_and_run(self, save_path, how_many, version="0", debug=False): """ Initialise algorithm and generate the trajectories :param save_path: path to save the result to :param how_many: number of trajectories to generate :param version: :param debug: serial execution or not :return: """ individual = TrajectoryGeneration( x_value=args.x_value, genotype=self._list_genome, values_matrix=(self._loader_apf.x_values, self._loader_apf.y_values), apf=self._loader_apf.apf, genome_meaning=self._loader_genome_meaning, pre_matrix=self._sub_matrix, type_of_generator=args.type_generator, type_astar=args.type_astar, pre_loaded_points=self._pre_loaded_points, total_distance_to_travel=args.total_distance_to_travel) self._logger.debug("Generating Trajectories") results = [] number_of_processes = multiprocessing.cpu_count() if how_many < number_of_processes: number_of_processes = how_many if debug: # serial execution number_of_processes = 1 with Parallel(n_jobs=number_of_processes, verbose=30) as parallel: res = parallel( delayed(worker_job_lib)(individual, i) for i in range(how_many)) results.append(res) # distances, tra, real_tra, path, preloaded_points total_tra = [] # total_vector_distances = [] total_real_tra = [] total_paths = [] for trial in results[0]: total_tra.append(trial[1]) # total_vector_distances.append(trial[0]) total_real_tra.append(trial[2]) total_paths.append(trial[3]) save_data(vector_data=total_real_tra, save_path=save_path, name="real_tra", version=version) save_data(vector_data=total_tra, save_path=save_path, name="tra", version=version) save_data(vector_data=total_paths, save_path=save_path, name="paths", version=version) self._logger.debug("Trajectories generated")