def final_arch_gt_bd(self): """ This function returns the gt_bd of only the final archive :return: """ info = ['gt_bd'] # EXPLORATION ARCHIVE archive_gt_bd = utils.load_data( self.exp_path, info=info + ['reward'], generation=-1, params=self.params, container_type='archive', ) # REWARD ARCHIVE # Check if rew_archive exists filename = 'rew_archive_final.pkl' if os.path.exists(os.path.join(self.exp_path, filename)): rew_archive_gt_bd = utils.load_data( self.exp_path, info=info, generation=-1, params=self.params, container_type='rew_archive', ) else: info = ['reward', 'gt_bd'] rew_archive_gt_bd = utils.load_data( self.exp_path, info=info, generation=-1, params=self.params, container_type='archive', ) for gen in rew_archive_gt_bd: rew_archive_gt_bd[gen] = { 'gt_bd': [ rew_archive_gt_bd[gen]['gt_bd'][idx] for idx in range(len(rew_archive_gt_bd[gen]['gt_bd'])) if rew_archive_gt_bd[gen]['reward'][idx] > 0 ] } archive_gt_bd = np.stack(archive_gt_bd[-1]['gt_bd']) rew_archive_gt_bd = rew_archive_gt_bd[-1]['gt_bd'] try: rew_archive_gt_bd = np.stack(rew_archive_gt_bd) except: pass # Save everything gt_bd = {'archive': archive_gt_bd, 'rew archive': rew_archive_gt_bd} with open( os.path.join(self.exp_path, 'analyzed_data', 'final_gt_bd.pkl'), 'wb') as f: pkl.dump(gt_bd, f)
def calculate_cvg_unif_ns(self): """ Calculates CVG and UNIF for methods with archive and pop separated This function goes through the offs of all the generations to gather CVG and UNIF data. For the first generation, it starts also with the pop. :return: """ grid = utils.CvgGrid(self.params, self.grid_params) cvg = [] unif = [] evaluations = [] # Load first init population init_pop = utils.load_data(self.exp_path, generation=1, params=self.params, container_type='population', info=['evaluated', 'gt_bd']) # Store them in the CVG grid for eval, bd in zip(init_pop[1]['evaluated'], init_pop[1]['gt_bd']): grid.store({'evaluated': eval, 'gt_bd': bd}) cvg.append(utils.calculate_coverage(grid.grid)) unif.append(utils.calculate_uniformity(grid.grid)) evaluations.append(eval) # Now load all the offsprings offsprings = utils.load_data(self.exp_path, generation=None, params=self.params, container_type='offsprings', info=['evaluated', 'gt_bd']) # Now store all the offs in the grid and calculate cvg and unif gen = 1 while gen in offsprings: if gen % 100 == 0: print("Working on gen: {}".format(gen)) for eval, bd in zip(offsprings[gen]['evaluated'], offsprings[gen]['gt_bd']): grid.store({'evaluated': eval, 'gt_bd': bd}) cvg.append(utils.calculate_coverage(grid.grid)) unif.append(utils.calculate_uniformity(grid.grid)) evaluations.append(eval) gen += 1 # Subsample so to have only 1k points points = np.linspace(0, self.params.evaluation_budget, 1000) cvg = np.interp(points, evaluations, cvg) unif = np.interp(points, evaluations, unif) evaluations = np.interp(points, evaluations, evaluations) data = {'cvg': cvg, 'unif': unif, 'eval': evaluations} with open( os.path.join(self.exp_path, 'analyzed_data', 'cvg_unif_by_eval.pkl'), 'wb') as f: pkl.dump(data, f)
def calculate_cvg_unif_me(self): """ Calculates CVG and UNIF for methods with only archive :return: """ cvg = [] unif = [] evaluations = [] # Load archives archive = utils.load_data(self.exp_path, generation=-1, params=self.params, container_type='archive', info=['gt_bd']) archive = archive[-1] count = 0 for filled in archive['filled']: count += 1 cvg.append(count / self.grid_params['bins']**2) unif.append(1) evaluations.append(filled) # Interpolate arrays points = np.linspace(0, self.params.evaluation_budget, 1000) cvg = np.interp(points, evaluations, cvg) unif = np.interp(points, evaluations, unif) evaluations = np.interp(points, evaluations, evaluations) data = {'cvg': cvg, 'unif': unif, 'eval': evaluations} with open( os.path.join(self.exp_path, 'analyzed_data', 'cvg_unif_by_eval.pkl'), 'wb') as f: pkl.dump(data, f)
def save_ksampling(self, path_spectrum, path_ksampling=None, kmax=0.1): from analysis.utils import load_data self.logger.info('Loading power spectrum: {}.'.format(path_spectrum)) get_data = load_data(path_spectrum, estimator='spectrum') self.kedges = get_data('edges') self.logger.info('Using k-edges: {:.4g} - {:.4g} ({:d}).'.format( self.kedges[0], self.kedges[-1], len(self.kedges))) self.kedges = self.kedges[self.kedges < kmax] self.logger.info('Using kmax = {:.4g}.'.format(kmax)) axis = [0, 1, 2] Nmesh = get_data('Nmesh') BoxSize = get_data('BoxSize') kfun = 2. * constants.pi / BoxSize self.logger.info( 'Used grid {0[0]:d} x {0[1]:d} x {0[2]:d} with box size {1[0]:.4g} x {1[1]:.4g} x {1[2]:.4g} i.e. fundamental wavevector {2[0]:.4g} x {2[1]:.4g} x {2[2]:.4g}.' .format(Nmesh, BoxSize, kfun)) halfNmesh = [] for iaxis in axis: if Nmesh[iaxis] % 2 == 0: Nmesh[iaxis] += 1 halfNmesh.append(Nmesh[0] // 2) kcoords = [ scipy.arange(Nmesh[0]) - halfNmesh[0], scipy.arange(Nmesh[1]) - halfNmesh[1], scipy.arange(halfNmesh[2]) ] for iaxis in axis: kcoords[iaxis] = kfun[iaxis] * kcoords[iaxis] kgrid = scipy.meshgrid(*kcoords, sparse=False, indexing='ij') for iaxis in axis: kgrid[iaxis][:halfNmesh[0], :, 0] = scipy.inf kgrid[iaxis][:halfNmesh[0] + 1, :halfNmesh[1] + 1, 0] = scipy.inf kgrid[iaxis][halfNmesh[0], halfNmesh[1], 0] = 0. kgrid[iaxis] = kgrid[iaxis].flatten() kgrid = scipy.asarray(kgrid).T knorm = numpy.linalg.norm(kgrid, ord=2, axis=1) mask = knorm < self.kedges[-1] knorm = knorm[mask] kgrid = kgrid[mask] knorm[knorm == 0.] = 1. kgrid /= knorm[:, None] self.k, _, kbinnumber = stats.binned_statistic(knorm, knorm, statistic='mean', bins=self.kedges) wgrid = scipy.ones((kgrid.shape[0]), dtype=kgrid.dtype) wgrid[knorm == 0] = 0.5 #to avoid double counts of k=(0,0,0) self.logger.info('Keeping {:d} k-modes.'.format(len(wgrid))) catalogue = Catalogue( { 'Position': kgrid, 'Weight': wgrid, 'ikbin': kbinnumber - 1 }, k=self.k, kedges=self.kedges, los=get_data('attrs').get('los', None)) catalogue.save(path_ksampling)
def save_gt_bd(self): """ This function calculates the coverage and uniformity used for plotting. :param trajectories: of all the agents in the run :param generation: Generation from which the trajectories are from :return: """ info = ['gt_bd'] # POPULATION population_gt_bd = utils.load_data( self.exp_path, info=info, generation=None, params=self.params, container_type='population', ) max_gen = max(list(population_gt_bd.keys())) # Find max_gen population_gt_bd['final'] = population_gt_bd[ max_gen] # Save final like this as well, so it's easier to recover # OFFSPRINGS offsprings_gt_bd = utils.load_data( self.exp_path, info=info, generation=None, params=self.params, container_type='offsprings', ) offsprings_gt_bd['final'] = offsprings_gt_bd[ max_gen] # Save final like this as well, so it's easier to recover # EXPLORATION ARCHIVE archive_gt_bd = utils.load_data( self.exp_path, info=info + ['reward'], generation=None, params=self.params, container_type='archive', ) archive_gt_bd['final'] = archive_gt_bd[ max_gen] # Save final like this as well, so it's easier to recover # REWARD ARCHIVE # Check if rew_archive exists filename = 'rew_archive_final.pkl' if os.path.exists(os.path.join(self.exp_path, filename)): rew_archive_gt_bd = utils.load_data( self.exp_path, info=info, generation=None, params=self.params, container_type='rew_archive', ) else: info = ['reward', 'gt_bd'] rew_archive_gt_bd = utils.load_data( self.exp_path, info=info, generation=None, params=self.params, container_type='archive', ) for gen in rew_archive_gt_bd: rew_archive_gt_bd[gen] = { 'gt_bd': [ rew_archive_gt_bd[gen]['gt_bd'][idx] for idx in range(len(rew_archive_gt_bd[gen]['gt_bd'])) if rew_archive_gt_bd[gen]['reward'][idx] > 0 ] } try: max_gen = max(list(rew_archive_gt_bd.keys())) # Find max_gen rew_archive_gt_bd['final'] = rew_archive_gt_bd[ max_gen] # Save final like this as well, so it's easier to recover except: print("No reward found in run: {}".format(self.exp_path)) rew_archive_gt_bd['final'] = {'gt_bd': []} # Given that load_data returns a dict {gen:{info: [data]}} we remove the depth of the info cause we already know it's gt_bd population_gt_bd = { gen: population_gt_bd[gen]['gt_bd'] for gen in population_gt_bd } offsprings_gt_bd = { gen: offsprings_gt_bd[gen]['gt_bd'] for gen in offsprings_gt_bd } archive_gt_bd = { gen: archive_gt_bd[gen]['gt_bd'] for gen in archive_gt_bd } rew_archive_gt_bd = { gen: rew_archive_gt_bd[gen]['gt_bd'] for gen in rew_archive_gt_bd } # Save everything gt_bd = { 'population': population_gt_bd, 'offsprings': offsprings_gt_bd, 'archive': archive_gt_bd, 'rew archive': rew_archive_gt_bd } with open(os.path.join(self.exp_path, 'analyzed_data', 'gt_bd.pkl'), 'wb') as f: pkl.dump(gt_bd, f)
def rew_by_eval(self): """ This function extracts the performances at each evaluation step :return: """ # All the informations we need are in the final archives info = ['reward', 'evaluated', 'rew_area'] archive = utils.load_data(self.exp_path, generation=-1, params=self.params, container_type='archive', info=info) data = archive[-1] del archive if os.path.exists(os.path.join(self.exp_path, 'rew_archive_final.pkl')): rew_archive = utils.load_data(self.exp_path, generation=-1, params=self.params, container_type='rew_archive', info=info) rew_archive = rew_archive[-1] # Merge archive and rew_arch data for i in info: data[i] = data[i] + rew_archive[i] del rew_archive # Sort data according to evaluation step eval = deepcopy(data['evaluated']) data = { k: np.array([x for _, x in sorted(zip(eval, v), key=lambda x: x[0])]) for k, v in data.items() } data['rew_area'] = np.stack(data['rew_area']) # Init the array with zero reward at eval zero rewards = { "rew_area_{}".format(i + 1): [np.array([0, 0])] for i in range(self.goals) } for eval, rew, area in zip(data['evaluated'], data['reward'], data['rew_area']): if area is not None: rewards["rew_area_{}".format(area)].append( np.array([eval, rew])) interp_rew = {} points = np.linspace(0, self.params.evaluation_budget, 1000) for k in rewards: # Interpolates reward in steps rew = np.stack(rewards[k]) # [[evaluation, reward]] rew[:, 0] = rew[:, 0] * 1000 / self.params.evaluation_budget rew[:, 1] = np.maximum.accumulate(rew[:, 1]) max_rew = np.zeros_like(points) for idx in range(len(rew[:, 0])): e, r = rew[idx] bottom = int(rew[idx, 0]) # Calculate upper limit if idx < len(rew[:, 0]) - 1: upper = int(rew[idx + 1, 0]) else: upper = len(max_rew) # Update max_rew array max_rew[bottom:upper] = r interp_rew[k] = max_rew interp_rew['eval'] = points with open( os.path.join(self.exp_path, 'analyzed_data', 'rew_by_eval.pkl'), 'wb') as f: pkl.dump(interp_rew, f)