def update_archive(pop, archive=None): if archive is None: combined_f = pop.get_f() else: combined_f = np.r_[archive, pop.get_f()] indices = pg.select_best_N_mo(combined_f, 50) new_archive = combined_f[indices, :] new_archive = np.unique(new_archive.round(4), axis=0) return new_archive
def plot_2d(popi,filename): magnet_dim = len(popi.get_x()[0]) hv = pg.hypervolume(popi) ref_point = hv.refpoint() best_point = (popi.get_f()[hv.greatest_contributor(ref_point)]) ndf, dl, dc, ndl = pg.fast_non_dominated_sorting(popi.get_f()) # print(pg.sort_population_mo(popi.get_f())) ndf_champ = [] # for j in range(2,200): # print(pg.select_best_N_mo(popi.get_f(),10)) best_10 = (pg.select_best_N_mo(popi.get_f(),10)) for i in best_10: print(i, np.power(np.zeros(magnet_dim)+2,popi.get_x()[i]), popi.get_f()[i]) x = np.linspace(pg.ideal(popi.get_f())[0],0) y = np.zeros(50)+(1) plot_x, plot_y = 0,0 objs = len(popi.get_f()[0]) fig, axs = plt.subplots(objs-1,sharex=True) fig.suptitle('ratios to Nom vs Resolution') log_res, log_x = [], [] for i in ndf[0]: log_res.append(np.log(popi.get_f()[i][0])/np.log(10)) log_x.append(np.log(popi.get_f()[i][1])/np.log(10)) if max(popi.get_f()[i]) < 1000000: print(i, np.power(np.zeros(magnet_dim)+2,popi.get_x()[i]), popi.get_f()[i]) if objs == 2: j = 1 axs.set_xlabel('resolution') axs.axvline(x=fNom[0],linestyle="dashed",color="red") axs.axvline(x=best_point[0],linestyle="dotted",color="blue") axs.axhline(y=1.0,linestyle="dashed",color="red") ndf_champ.append([popi.get_f()[i] for i in ndf[0]]) pg.plot_non_dominated_fronts(ndf_champ[0],comp=[0,j],axes=axs) # ax.plot df = (color="C{}".format(j)) # ax = pg.plot_non_dominated_fronts(popi.get_f()[0:j]) axs.set_ylabel(fNames[2]) # axs.set_ylim(0.9,1.1) axs.set_yscale('log') # axs.set_xlim(0.8,1.2) axs.set_xscale('log') else: axs[objs-2].set_xlabel('resolution') for j in range(1,2): # axs[plot_y].plot(x,y,linestyle="dashed",color="red") axs[plot_y].axvline(x=fNom[0],linestyle="dashed",color="red") axs[plot_y].axvline(x=best_point[0],linestyle="dotted",color="blue") axs[plot_y].axhline(y=1.0,linestyle="dashed",color="red") ndf_champ.append([popi.get_f()[i] for i in ndf[0]]) pg.plot_non_dominated_fronts(ndf_champ[0],comp=[0,j],axes=axs[plot_y]) # ax.plot(color="C{}".format(j)) # ax = pg.plot_non_dominated_fronts(popi.get_f()[0:j]) axs[plot_y].set_ylabel(fNames[2]) # axs[plot_y].set_ylim(1e-1,1e1) axs[plot_y].set_yscale('log') # axs[plot_y].set_xlim(0.1,10.0) # axs[plot_y].set_xscale('log') plot_y += 1 # print(ndf_champ, ndf[0]) # print(ndf) fig.tight_layout() fig.savefig(filename+"_ndf.png") plt.clf() # plt.plot(log_res, log_x,"o", linestyle="none", ) # fig.savefig(filename+"_logndf.png") return
def plot_4d(popi, filename): magnet_dim = len(popi.get_x()[0]) hv = pg.hypervolume(popi) # ref_point = hv.refpoint() ref_point = (1e10, 1e10, 1e10, 1e10) best_point = (popi.get_f()[hv.greatest_contributor(ref_point)]) ndf, dl, dc, ndl = pg.fast_non_dominated_sorting(popi.get_f()) # print(pg.sort_population_mo(popi.get_f())) ndf_champ = [] # for j in range(2,200): # print(pg.select_best_N_mo(popi.get_f(),10)) best_10 = (pg.select_best_N_mo(popi.get_f(), 10)) # for i in best_10: # print(i, np.power(np.zeros(magnet_dim)+2,popi.get_x()[i]), popi.get_f()[i]) x = np.linspace(pg.ideal(popi.get_f())[0], 0) y = np.zeros(50) + (1) plot_x, plot_y = 0, 0 fig, axs = plt.subplots(3, sharex=True) fig.suptitle('Pareto Fronts of each parameter vs. BeamSpotSize at FP4') axs[optimized_params - 2].set_xlabel(fNames[3]) # for i in ndf[0]: # if max(popi.get_f()[i]) < 10: # print(i, np.power(np.zeros(magnet_dim)+2,popi.get_x()[i]), popi.get_f()[i]) reduced_ndf = [] first = True check_val = 1e9 for j in range(0, 3): # axs[plot_y].plot(x,y,linestyle="dashed",color="red") axs[plot_y].axvline(x=fNom[0], linestyle="dashed", color="red") # axs[plot_y].axvline(x=best_point[0],linestyle="dotted",color="blue") axs[plot_y].axhline(y=1.0, linestyle="dashed", color="red") for i in ndf[0]: if np.all(np.array(popi.get_f()[i]) < check_val) == True: # if np.all(np.array(popi.get_f()[i]) < 1) == True and first: ndf_champ.append(popi.get_f()[i]) reduced_ndf.append(i) first = False try: pg.plot_non_dominated_fronts(ndf_champ, comp=[3, j], axes=axs[plot_y]) if j == 1: print(filename[-6:-4], len(ndf_champ)) except: print(filename[-6:-4], "no better than nominal solutions") return # ax.plot(color="C{}".format(j)) # ax = pg.plot_non_dominated_fronts(popi.get_f()[0:j]) axs[plot_y].set_ylabel(fNames[j]) # axs[plot_y].set_ylim(1e-1,1e1) axs[plot_y].set_yscale('log') # axs[plot_y].set_xlim(0.1,10.0) axs[plot_y].set_xscale('log') plot_y += 1 # print(ndf_champ, ndf[0]) # print(ndf) fig.tight_layout() fig.savefig(filename + "_ndf.png") plt.cla() fig2, axs2 = plt.subplots(3, 4) plot_x, plot_y = 0, 0 reduced_qs = np.array(popi.get_x())[reduced_ndf] # print(reduced_qs) df = pd.DataFrame(reduced_qs, columns=[ 'y0', 'y1', 'y2', 'y3', 'y4', 'y5', 'y6', 'y7', 'y8', 'y9', 'y10' ]) qNom = np.zeros(magnet_dim) for i in range(magnet_dim): if plot_x > 3: plot_x, plot_y = 0, plot_y + 1 axs2[plot_y, plot_x] = df['y{0}'.format(i)].plot.hist(ax=axs2[plot_y, plot_x], bins=20, range=(-1, 1)) axs2[plot_y, plot_x].axvline(x=qNom[i], ymin=0, ymax=20, color='red', linestyle='dashed') # axs[plot_y,plot_x].axvline( x = max_y[i], ymin=0,ymax=20,color='green',linestyle='dashed') axs2[plot_y, plot_x].axes.yaxis.set_visible(False) axs2[plot_y, plot_x].axes.set_xlim(-1, 1) axs2[plot_y, plot_x].set_title("q{0}".format(i + 1)) plot_x += 1 fig2.delaxes(axs2[plot_y, plot_x]) fig2.tight_layout() plt.savefig(filename + ".png") return