def __test__(): # global Testobj sys.setrecursionlimit(30) file = 'pooltest5.pkl' filecls = 'pooltest_cls.pkl' cls = Testobj0 print(cls.param) if os.path.isfile(file): objs = ut.load(file) cls = ut.load(filecls) print(cls.param) else: pool = [] for i in range(1000): obj = cls(pool) objs = [x for x in pool] ut.save(file, objs) ut.save(filecls, cls) print(len(objs)) obj = objs[500] # print(pool[500], len(pool)) print(obj.id, obj.data) obj() print(cls.param)
def plot_loss_ex(trainer): fig, ax = plt.subplots() for d in ['top', 'right']: ax.spines[d].set_visible(False) ylim_low = float('inf') ylim_upp = 0 try: with ut.chdir(trainer.out): if not os.path.isfile('log.json'): return log = ut.load('log.json', from_json=True) for key in ('main/loss', 'val/main/loss'): a = np.array([l[key] for l in log]) a = np.clip(a, 0, 1e6) ax.plot(a, label=key) ylim_upp = max(np.ceil(np.max(a[min(len(a)-1,3):50]))/1000*1000, ylim_upp) ylim_low = min(np.min(a)//1000*1000, ylim_low) ax.set_ylim((ylim_low, ylim_upp)) ax.set_xlabel('epoch') ax.grid(True) fig.legend() fig.savefig('loss1.png') finally: plt.close(fig) sleep(10)
def get_result_file(root='.'): ''' 計算済み最適化ファイルを読み出す ''' with ut.chdir(root): files = glob.glob('optimize_*.pkl') for i, file in enumerate(files): print(f'[{i}] {file}') print('select file') n = int(input()) if n < 0: return file = files[n] print('file:', file) env, optimizer, history = ut.load(file) return env, optimizer, history
def get_model(out): # モデル読み込み # model_cls = {'nsga2':NSGA2, 'moead':MOEAD}[model] files = ut.fsort(glob.glob(os.path.join(out, f'*epoch*.pkl'))) for i, file in enumerate(files): print(f'[{i}]', file) print('select file') n = int(input()) if n == -1: pass elif n < 0: return file = files[n] print('file:', file) env, optimizer, history = ut.load(file) return env, optimizer, history
def main1(out='result', force=False, clear_directory=False): ''' GAテスト & プロット ''' ### def ln(ind, origin): if not origin: return [] return list(zip(origin[0].value, ind.value, origin[1].value)) def dist(ind, origin): if not origin: return 0 values = np.array([[ind.value, par.value] for par in origin]) diff = values[:, :, 0] - values[:, :, 1] dists = np.sqrt(np.sum(diff**2, axis=1)) return np.sum(dists) fig, ax = plt.subplots() def plot(pop): pairs = [(fit.data, fit.data.origin.origin or ()) for fit in pop] parents = list(chain(*(x[1] for x in pairs))) lines = [ln(ind, origin) for ind, origin in pairs] # print(lines) # exit() if lines and [x for x in lines if x]: dists = [dist(ind, origin) for ind, origin in pairs] print(sum(dists)) ax.cla() cm = plt.get_cmap('jet') # print(cm(10)) # exit() if parents: x_p, y_p = np.array([ind.value for ind in parents]).T ax.scatter(x_p, y_p, c='r') for i, l in enumerate(lines): if l: plt.plot(*l, c=cm(i / (len(lines) + 1)), linewidth=0.5) x, y = np.array([fit.data.value for fit in pop]).T ax.scatter(x, y, c='b') plt.pause(1e-10) ### def local_main(): n_dim = 30 popsize = 100 problem = zdt1 with Environment() as env: # 個体クラス indiv_type = Individual # 初期個体生成クラス indiv_pool = env.register(indiv_type) # 遺伝子生成クラス initializer = UniformInitializer(n_dim) creator = Creator(initializer, indiv_pool) # # 適応度クラス # fit_type = Fitness # # 初期個体生成クラス # evaluator = env.register(fit_type) ### # optimizer = NSGA2(problem=problem, pool=indiv_pool) optimizer = MOEAD(problem=problem, pool=indiv_pool, ksize=5) # optimizer.set_initializer(Initializer(3)) # optimizer.setup(problem) ### Additional setting ### # optimizer.initializer = initializer # optimizer.n_cycle = None # optimizer.alternation = 'replace' ########################## # indivs = [creator() for _ in range(popsize)] # population = Population([ind.evaluate(problem) for ind in indivs]) # optimizer.calc_fitness(population) population = optimizer.init_population(creator, popsize=popsize) history = [population] # print(population[0].data.origin) # return for i in range(100): population = optimizer(population) plot(population) origin = population[0].data.origin.origin or [] # print([x.id for x in origin], '->', population[0].data.id) history.append(population) if i % 50 == 50 - 1: ut.save(f'result/temp{i}.pkl', history) plt.show() return env, optimizer, history ### def resume_main(env, optimizer, history): print('resume_main') for i, population in enumerate(history): plot(population) origin = population[0].data.origin.origin or [] # print([x.id for x in origin], '->', population[0].data.id) plt.show() file = 'test_moead.pkl' if os.path.exists(file) and not force: env, optimizer, history = ut.load(file) resume_main(env, optimizer, history) else: env, optimizer, history = local_main() ut.save(file, (env, optimizer, history))
def __test__(): obj = TestClass('abc') ut.save('test.pkl', obj) obj = ut.load('test.pkl')