def ga_result1(out='result'): ''' アニメーション表示 ''' file = ut.fsort(glob.glob(os.path.join(out, f'epoch*.pickle')))[-1] optimizer = MOEAD.load(file=file) print('epoch:', len(optimizer)) elite = optimizer.get_elite() print('elite:', len(elite)) imax = len(optimizer) - 1 for epoch in range(0, imax, 20): print(epoch, end='\n') # plt.cla() population = optimizer[epoch] # population = optimizer.calc_rank(population) front = [x for x in population] x, y = np.array([x.data.value for x in front]).T plt.scatter(x, y, color='blue', alpha=epoch / imax, label=f'epoch{epoch}') # plt.legend() plt.xlim((0, 50)) # plt.ylim((0, 50)) plt.pause(0.2) plt.show()
def get_model(): # モデル読み込み # 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 < 0: return file = files[n] print('file:', file) env, optimizer, history = ut.load(file) return env, optimizer, history
def ga_result14(model, out='result', show=False): ''' エリート個体数を数える ''' # モデル読み込み model_cls = {'nsga2': NSGA2, 'moead': MOEAD}[model] files = ut.fsort(glob.glob(os.path.join(out, f'*epoch*.pickle'))) 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) optimizer = model_cls.load(file=file) imax = len(optimizer) # imax = 100 idx = 0 cache = file.replace('.pickle', '_ndom.npy') if os.path.isfile(cache) and not force: data = np.load(cache) else: data = [] for epoch in range(0, imax, 1): print(epoch, end='\r') # plt.cla() population = optimizer[epoch] population = optimizer.calc_rank(population) front = [x for x in population if x.rank == 1] indivs = [fit.get_indiv() for fit in population] if hasattr(population[0], 'id'): idx = max(*map(attrgetter('id'), population), idx) else: idx = max(*map(attrgetter('id'), indivs), idx) data.append([idx, len(front)]) np.save(cache, data)
def ga_result2(out='result'): file = ut.fsort(glob.glob(os.path.join(out, f'epoch*.pickle')))[-1] optimizer = MOEAD.load(file=file) population = optimizer[-1] front = [x for x in population if x.rank == 1] front.sort(key=attrgetter('data.value')) for fit in front: # print(ind.value, ind.data.value) print(fit.get_indiv().get_gene().get_ivalue(), fit.value) crowdings = [ind.value[1] for ind in front] fig, axes = plt.subplots(2) axes[0].plot(crowdings) x, y = np.array([x.data.value for x in front]).T im = axes[1].scatter(x, y, c=crowdings, cmap='jet') # plt.xlim((0, 1)) # plt.ylim((0, 1)) fig.colorbar(im) plt.show()
def ga_result13(model, out='result', show=False): ''' HVプロット ''' # モデル読み込み model_cls = {'nsga2': NSGA2, 'moead': MOEAD}[model] files = ut.fsort(glob.glob(os.path.join(out, f'*epoch*.pickle'))) 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) optimizer = model_cls.load(file=file) ref_point = [100, 0] hypervolume = HyperVolume(ref=ref_point) # モデルパラメータ表示 print('[PARAMS]') print('popsize:', optimizer.popsize) print('epoch:', len(optimizer)) print('alternation:', optimizer.alternation) print('Indiv:', optimizer.indiv_type.__name__) optimizer.calc_rank(optimizer.population) elite = optimizer.get_elite() print('elite:', len(elite)) imax = len(optimizer) # imax = 100 idx = 0 force = False cache = file.replace('.pickle', '_hv.npy') if os.path.isfile(cache) and not force: hvs = np.load(cache) else: hvs = [] population = [] for epoch in range(0, imax, 1): # print(epoch, end='\r') # plt.cla() population = optimizer[epoch] + population optimizer.calc_rank(population) population = [fit for fit in population if fit.rank == 1] hv = hypervolume(population) print('epoch:', epoch, 'HV=', hv) if hasattr(population[0], 'id'): idx = max(*map(attrgetter('id'), population), idx) else: idx = max(*map(attrgetter('id'), indivs), idx) hvs.append([idx, hv]) np.save(cache, hvs) i, v = np.array(hvs).T fig, ax = plt.subplots() im = ax.plot(i, v, label=model) ax.legend() ax.set_xlabel('Evaluation Step') ax.set_ylabel('Hypervolume') if show: plt.show() else: basename = os.path.splitext(os.path.basename(file))[0] plt.savefig(os.path.join('result', f'front_{model}_{basename}_hv.png'))
def ga_result12(model, out='result', show=False): ''' 最適値プロット ''' # モデル読み込み model_cls = {'nsga2': NSGA2, 'moead': MOEAD}[model] files = ut.fsort(glob.glob(os.path.join(out, f'*epoch*.pickle'))) 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) optimizer = model_cls.load(file=file) # モデルパラメータ表示 print('[PARAMS]') print('popsize:', optimizer.popsize) print('epoch:', len(optimizer)) print('alternation:', optimizer.alternation) print('Indiv:', optimizer.indiv_type.__name__) optimizer.calc_rank(optimizer.population) elite = optimizer.get_elite() print('elite:', len(elite)) imax = len(optimizer) # imax = 100 idx = 0 cache = file.replace('.pickle', '_data.npy') if os.path.isfile(cache) and not force: data = np.load(cache) else: data = [] for epoch in range(0, imax, 1): print(epoch, end='\r') # plt.cla() population = optimizer[epoch] indivs = [fit.get_indiv() for fit in population] dv = min(map(itemgetter(0), indivs)) rcs = max(map(itemgetter(1), indivs)) if hasattr(population[0], 'id'): idx = max(*map(attrgetter('id'), population), idx) else: idx = max(*map(attrgetter('id'), indivs), idx) data.append([idx, dv, rcs]) np.save(cache, data) i, x, y = np.array(data).T fig, ax = plt.subplots() # color_list = [(0, '#AADDFF'), (0.33, '#002288'), (0.66, '#EEDD22'), (1, '#CC0000')] # color_list = [(0, '#0022AA'), (0.3, '#11AA00'), (0.5, '#EEDD22'), (1, '#CC0000')] # color_list = [(0, 'blue'), (0.3, 'green'), (0.5, 'yellow'), (1, 'red')] # cmap = plc.LinearSegmentedColormap.from_list('custom_cmap', color_list, N=25) im = ax.plot(i, x) im = ax.plot(i, y * 100) print('min dV=', x.min()) print('max RCS=', y.max()) # plt.legend() # ax.set_xlabel('$\\Delta\\it{V}$, $km/s$') # ax.set_ylabel('$RCS$, $m^2$') # ax.set_xlim((0, 100)) # ax.set_ylim((0, 0.45)) # cbar = fig.colorbar(im, label='generation') if show: plt.show() else: basename = os.path.splitext(os.path.basename(file))[0] plt.savefig(os.path.join('result', f'front_{model}_{basename}.png'))
def ga_result11(model, out='result', show=False): ''' 一括表示 ''' # モデル読み込み model_cls = {'nsga2': NSGA2, 'moead': MOEAD}[model] files = ut.fsort(glob.glob(os.path.join(out, f'*epoch*.pickle'))) 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) optimizer = model_cls.load(file=file) # モデルパラメータ表示 print('[PARAMS]') print('popsize:', optimizer.popsize) print('epoch:', len(optimizer)) print('alternation:', optimizer.alternation) if hasattr(optimizer, 'ksize'): print('ksize:', optimizer.ksize) print('Indiv:', optimizer.indiv_type.__name__) optimizer.calc_rank(optimizer.population) elite = optimizer.get_elite() print('elite:', len(elite)) imax = len(optimizer) # imax = 51 data = [] for epoch in range(0, imax, 1): print(epoch, end='\r') # plt.cla() population = optimizer[epoch] front = [x for x in population] data.extend([[*x.data.value, epoch] for x in front]) x, y, c = np.array(data).T fig, ax = plt.subplots() # color_list = [(0, '#AADDFF'), (0.33, '#002288'), (0.66, '#EEDD22'), (1, '#CC0000')] color_list = [(0, '#0022AA'), (0.3, '#11AA00'), (0.5, '#EEDD22'), (1, '#CC0000')] color_list = [(0, 'blue'), (0.3, 'green'), (0.5, 'yellow'), (1, 'red')] cmap = plc.LinearSegmentedColormap.from_list('custom_cmap', color_list, N=25) im = ax.scatter(x, y, c=c, cmap=cmap, marker='o', alpha=1) print('min dV=', x.min()) print('max RCS=', y.max()) # plt.legend() ax.set_xlabel('$\\Delta\\it{V}$, $km/s$') ax.set_ylabel('$RCS$, $m^2$') ax.set_xlim((0, 100)) ax.set_ylim((0, 0.45)) # plt.pause(0.2) cbar = fig.colorbar(im, label='Generation') cbar.set_label('Generation', size=24) plt.tight_layout() # show or save if show: plt.show() else: basename = os.path.splitext(os.path.basename(file))[0] plt.savefig(os.path.join('result', f'front_{model}_{basename}.png'))