def fill(self, network, filepath=None, max_iter=int(1e10)): filename = os.path.basename(filepath) if os.path.isfile(filepath): curr_caching = Caching.load(filepath) curr_score = curr_caching.score(network) print('montecarlo partial best - {:20s} SCORE: {}'.format( filename, curr_score), flush=True) else: curr_score = 0 begin_time = datetime.datetime.now() j = 0 while j < max_iter: min_video_size = np.min(network.videos) self.caches = [ _random_cache(network.videos, network.cache_size, min_video_size) for i in range(self.num_caches) ] score = self.score(network) end_time = datetime.datetime.now() print('montecarlo - {:20s} SCORE: {:7d} ({}) j={}, t={}'.format( filename, score, curr_score, j, end_time - begin_time), flush=True) begin_time = end_time if score > curr_score: curr_score = score best_caches = self.caches print('montecarlo partial best - {:20s} SCORE: {}'.format( filename, score), flush=True) self.save(filepath) j += 1
def test_score(in_dirpath=IN_DIRPATH, out_dirpath=OUT_DIRPATH, source='example'): in_filepath = os.path.join(in_dirpath, source + '.in') network = Network.load(in_filepath) print(network) out_filepath = os.path.join(out_dirpath, source + '.out') caching = Caching.load(out_filepath) print(caching.score(network))
def test_caching_output(in_dirpath=OUT_DIRPATH, source='example'): in_filepath = os.path.join(in_dirpath, source + '.out') caching = Caching.load(in_filepath) print(caching) out_filepath = os.path.join(in_dirpath, 'test_' + source) caching.save(out_filepath)
def __init__(self, *args, **kwargs): Caching.__init__(self, *args, **kwargs)
def fill(self, network, filepath=None, max_generations=int(1e7 - 1), pool_size=400, selection=0.5, crossover=0.6, mutation_rate=0.05, mutation=0.1, elitism=0.005, multiproc=True): dirpath = os.path.dirname(filepath) filename = os.path.basename(filepath) basename = os.path.splitext(filename)[0] evo_dirpath = os.path.join(dirpath, basename) old_evo_dirpath = os.path.join(dirpath, '_old_' + basename) if not os.path.isdir(evo_dirpath): os.makedirs(evo_dirpath) pool_filenames, pool_dirpath = [], '' if os.path.isdir(evo_dirpath): pool_filenames = os.listdir(evo_dirpath) pool_dirpath = evo_dirpath if len(pool_filenames) != pool_size and os.path.isdir(old_evo_dirpath): pool_filenames = os.listdir(old_evo_dirpath) pool_dirpath = old_evo_dirpath if len(pool_filenames) != pool_size: pool = [] if os.path.isfile(filepath): caching = Caching.load(filepath) pool.append((caching.score(network), caching)) for _ in range(pool_size - len(pool)): caching = Caching(network.num_caches) caching.fill(network) pool.append((caching.score(network), caching)) else: pool = [(int(name.split('_')[0]), Caching.load(os.path.join(pool_dirpath, name))) for name in pool_filenames] pool = sorted(pool, key=operator.itemgetter(0), reverse=True) begin_time = datetime.datetime.now() generation = 0 best_score = pool[0][0] mp_pool = multiprocessing.Pool(multiprocessing.cpu_count()) \ if multiproc else None while generation < max_generations: # selection selected = pool[:int(pool_size * selection)] # elitism elite = copy.deepcopy(pool[:int(pool_size * elitism) + 1]) # crossover and mutate num_generators = 2 if multiproc: results = [ mp_pool.apply_async(_breeding, ([ selected[i] for i in sorted( random.sample(range(len(selected)), num_generators)) ], network, crossover, mutation_rate, mutation)) for _ in range(pool_size - len(elite)) ] offspring = [result.get() for result in results] else: offspring = [ _breeding([ selected[i] for i in sorted( random.sample(range(len(selected)), num_generators)) ], network, crossover, mutation_rate, mutation) for _ in range(pool_size - len(elite)) ] pool = elite + offspring # delete old generation if os.path.isdir(old_evo_dirpath): shutil.rmtree(old_evo_dirpath, ignore_errors=True) shutil.rmtree(old_evo_dirpath, ignore_errors=True) shutil.move(evo_dirpath, old_evo_dirpath) os.makedirs(evo_dirpath) # save new generation pool = sorted(pool, key=operator.itemgetter(0), reverse=True) [ caching.save( os.path.join( evo_dirpath, '{:07d}_id{:04d}_gen{:06d}__'.format( score, i, generation) + filename)) for i, (score, caching) in enumerate(pool) ] if pool[0][0] > best_score: pool[0][1].save(filepath) best_score = pool[0][0] end_time = datetime.datetime.now() print('evolution - {:20s} SCORE: {:7d}, gen={}, t={}'.format( filename, best_score, generation, end_time - begin_time), flush=True) begin_time = end_time generation += 1 # return best result self.caches = pool[0][1].caches