def _load_init_population(self, intcodedir, size=None, copy=True): # make sure we are at the beginning of experiment assert self._istep == 0, 'initialization only allowed at the beginning' # make sure size <= population size if size is not None: assert size <= self._popsize, f'size {size} too big for population of size {self._popsize}' else: size = self._popsize # load codes init_population, genealogy = utils.load_codes2(intcodedir, size, self._random_generator) init_population = init_population.reshape( (len(init_population), *self._code_shape)) # fill the rest of population if size==len(codes) < population size if len(init_population) < self._popsize: remainder_size = self._popsize - len(init_population) remainder_pop, remainder_genealogy = \ self._mate(init_population, genealogy, np.ones(len(init_population)), remainder_size) remainder_pop, remainder_genealogy = self._mutate( remainder_pop, remainder_genealogy) init_population = np.concatenate((init_population, remainder_pop)) genealogy = genealogy + remainder_genealogy # apply self._init_population = init_population self._init_population_fns = genealogy # a list of '*.npy' file names self._curr_samples = self._init_population.copy() self._genealogy = [f'[init]{g}' for g in genealogy] # no update for idc, idx, ids because popsize unchanged if copy: self._copy_init_population() if self._generator.loaded: self._prepare_images()
def _load_init_code(self, initcodedir, copy=True): # make sure we are at the beginning of experiment assert self._istep == 0, 'initialization only allowed at the beginning' init_code, init_codefns = utils.load_codes2(initcodedir, 1, self._random_generator) self._curr_center = init_code.reshape(self._code_shape) self._init_codefn = init_codefns[0] self._best_code = self._curr_center.copy() if copy: self._copy_init_code()
('caffe-net', 'fc8', 1), ] Optim_arr = ["Genetic", "CholCMA"] #%% Genetic Algorithm Parameters AND CMA-ES Parameters population_size = 40 mutation_rate = 0.25 mutation_size = 0.75 kT_multiplier = 2 n_conserve = 10 parental_skew = 0.75 # Genetic(population_size, mutation_rate, mutation_size, kT_multiplier, recorddir, # parental_skew=0.5, n_conserve=0) code_length = 4096 init_sigma = 3 Aupdate_freq = 10 # use the mean code of the texture patterns as the initcode codes, _ = utils.load_codes2(initcodedir, 40) initcode = np.mean(codes, axis=0, keepdims=True) # CholeskyCMAES(recorddir=recorddir, space_dimen=code_length, init_sigma=init_sigma, # Aupdate_freq=Aupdate_freq, init_code=np.zeros([1, code_length])) #%% from time import time import matplotlib.pylab as plt for unit in unit_arr[:]: savedir = join(recorddir, "optim_cmp", "%s_%s_%d" % (unit[0], unit[1], unit[2])) os.makedirs(savedir, exist_ok=True) for Optim_str in Optim_arr: for triali in range(10): t0 = time() if Optim_str == "Genetic": optim = Genetic(population_size, mutation_rate, mutation_size, kT_multiplier, recorddir,