def init_pop(dim, pop_size): qNom = np.zeros(dim) p_optimizeRes = pg.problem(optimizeRes(magnet_dim)) pop = pg.population(prob=optimizeRes(dim, out=outputFile)) pop.push_back(x=qNom, f=np.zeros(p_optimizeRes.get_nobj())) #[0:dim]) print(pop) print(pop.problem.get_fevals()) pop.set_x(0, qNom) print(pop) for i in range(pop_size - 1): pop.push_back(pop.random_decision_vector()) return pop
def main(pop_init=None): # start timer startTime = timeit.default_timer() # initialize algorithm with hyperparameters alg = pg.moead(gen=generations, neighbours=neighbors, seed=seed) # there should be a way to run batch_fitness evaluations, haven't gotten it to work on NSCL #b = pg.bfe() #alg.set_bfe(b) #alg.set_verbosity(1) # initialize problem p_optimizeRes = pg.problem(optimizeRes(magnet_dim, outputFile)) # relic from old evolutions, which launched all islands internally # this can allow for interconnectivity between islands # now each run of this script calls its own island n_islands = 1 # if more than one island and want to exchange info need to define a topology to connect them #top = pg.topology(pg.fully_connected(n_islands,1.0)) # when running 5 objectives, pop needed to be 70 pop_n = 70 #84 if p_optimizeRes.get_nobj() == 4: # with 4 objs need pop=84 pop_n = 84 # check if we are using an input population or need to initialize one pop_new = None if (pop_init == None): # randomly create a new population of size pop_n pop_new = pg.population(p_optimizeRes, size=pop_n) print("initialize pop") else: pop_new = pop_init print("provided pop") # create archipelago from algorithm (of a single island) archi = pg.algorithm(alg) # evolve archipelago archi.evolve(pop_new) # check total time print('Running time (sec): %f' % (timeit.default_timer() - startTime)) # when I tried to use multiprocessing I needed this #pg.mp_island.shutdown_pool() #pg.mp_bfe.shutdown_pool() return pop_new
def read_pop_df(filename, pop=None): df = pd.read_hdf(filename) max_val = 1e9 df = df.loc[(df['FP2_res'] < max_val) & (df['FP3_res'] < max_val) & (df['MaxBeamWidth'] < max_val) & (df['FP4_BeamSpot'] < max_val)] df = df.reindex() magnet_dim = len(df.columns) - optimized_params # df = df.loc[(df['FP2_res'] < 1.0) & (df['FP2_e_xangle'] < 1.0) & (df['FP3_res'] < 1.0) & (df['FP3_e_xangle'] <1.0)] # costs = df[['FP2_res','FP2_e_xangle','FP3_res','FP3_e_xangle']] # costs = np.array(costs) # pareto = is_pareto_efficient_simple(costs) # df['pareto'] = pareto # print(np.count_nonzero(pareto) ) # df = (df.loc[(df['pareto']==True)]) # df = df.sort_values(by='FP2_res',ignore_index=True) # df["f0"] = df['f0'] * 1.0 / 4.419411469795324 # df['f1'] = df['f1'] * 1.0 / 2.7701491204695756 # print(magnet_dim) p_optimizeRes = pg.problem(optimizeRes(magnet_dim)) if pop == None: pop = pg.population(p_optimizeRes) # df_2 = df.loc[(df["f0"] < 10) & (df["f1"] < 10) & (df["f2"] < 10) & (df["f3"] < 10)] # df = df_2 nrow, ncol = df.shape # print(df.index) for i in df.index: if i > 26000: break # print(i) append = True xs = [] for j in range(1, magnet_dim + 1): xs.append(df["q" + str(j)][i]) xs = np.asarray(xs) fs = [] for j in range(magnet_dim, ncol + 0): # if j > 0: # fs.append(df["f"+str(j)][i]/fNom[j]) # else: # if df["f"+str(j)][i] >= 1.0: # append=False # if df["f"+str(j)][i] < 0 or np.isnan(df["f"+str(j)][i]): # print(df["f"+str(j)][i], i) fs.append(df.iloc[i, j]) if append: pop.push_back(xs, f=fs) # print(pop) return pop
def read_pop(filename, pop=None): df = pd.read_csv(filename, names=[ "x0", "x1", "x2", "x3", "x4", "x5", "x6", "f0", "f1", "f2", "f3" ]) magnet_dim = len(df.columns) - optimized_params quads = [] for i in range(magnet_dim): quads.append("x{}".format(i)) columns = quads columns.append("f0") columns.append("f1") columns.append("f2") columns.append("f3") # df["f0"] = df['f0'] * 1.0 / 4.419411469795324 # df['f1'] = df['f1'] * 1.0 / 2.7701491204695756 # print(magnet_dim) p_optimizeRes = pg.problem(optimizeRes(magnet_dim)) if pop == None: pop = pg.population(p_optimizeRes) # df_2 = df.loc[(df["f0"] < 10) & (df["f1"] < 10) & (df["f2"] < 10) & (df["f3"] < 10)] # df = df_2 nrow, ncol = df.shape # print(df) for i in df.index: append = True xs = [] for j in range(magnet_dim): xs.append(df["x" + str(j)][i]) xs = np.asarray(xs) fs = [] for j in range(ncol - magnet_dim): # if j > 0: # fs.append(df["f"+str(j)][i]/fNom[j]) # else: # if df["f"+str(j)][i] >= 1.0: # append=False if df["f" + str(j)][i] < 0 or np.isnan(df["f" + str(j)][i]): print(df["f" + str(j)][i], i) fs.append(df["f" + str(j)][i]) if append: pop.push_back(xs, f=fs) # print(pop) return pop
def read_pop(filename): df = pd.read_csv(filename) p_optimizeRes = pg.problem(optimizeRes(magnet_dim, out=outputFile)) pop = pg.population(p_optimizeRes) nrow, ncol = df.shape print(df) for i in range(nrow): xs = [] for j in range(magnet_dim): xs.append(df["x" + str(j)][i]) xs = np.asarray(xs) fs = [] for j in range(ncol - magnet_dim): fs.append(df["f" + str(j)][i]) pop.push_back(xs, f=fs) return pop
def read_pop(filename, pop=None): # df = pd.read_csv(filename,names=["x0","x1","x2","x3","x4","x5","x6","f0","f1","f2","f3"]) magnet_dim = len(pd.read_csv(filename).columns) - optimized_params p_optimizeRes = pg.problem(optimizeRes(magnet_dim)) obj_dim = p_optimizeRes.get_nobj() quads = [] for i in range(magnet_dim): quads.append("x{}".format(i)) columns = quads for i in range(obj_dim): columns.append("f{}".format(i)) df = pd.read_csv(filename, names=columns) # df["f0"] = df['f0'] * 1.0 / 4.419411469795324 # df['f1'] = df['f1'] * 1.0 / 2.7701491204695756 # print(magnet_dim) if pop == None: pop = pg.population(p_optimizeRes) # df_2 = df.loc[(df["f0"] < 10) & (df["f1"] < 10) & (df["f2"] < 10) & (df["f3"] < 10)] # df = df_2 nrow, ncol = df.shape # print(df) for i in df.index: # if i in [0,102,76]: # continue # for i in range(nrow): xs = [] for j in range(magnet_dim): xs.append(df["x" + str(j)][i]) xs = np.asarray(xs) fs = [] # print(ncol, magnet_dim) for j in range(ncol - magnet_dim): # if j > 0: # fs.append(df["f"+str(j)][i]/fNom[j]) # else: if df["f" + str(j)][i] < 0 or np.isnan(df["f" + str(j)][i]): print(df["f" + str(j)][i], i) df["f" + str(j)][i] = 1e10 fs.append(df["f" + str(j)][i]) if np.all(np.array(fs) < 1) == True or True: pop.push_back(xs, f=fs) # print(pop) return pop
def read_pop_df(filename, pop=None): df = pd.read_hdf(filename) magnet_dim = 15 p_optimizeRes = pg.problem(optimizeRes(magnet_dim)) nobj = p_optimizeRes.get_nobj() if pop == None: pop = pg.population(p_optimizeRes) nrow, ncol = df.shape for i in df.index: append=True xs = [] for j in range(1,magnet_dim+1): xs.append(df["q"+str(j)][i]) xs = np.asarray(xs) fs = [] for j in range(magnet_dim,magnet_dim+nobj): # if i == 0: # print(df.iloc[i,magnet_dim:magnet_dim+p_optimizeRes.get_nobj()]) fs.append(df.iloc[i,j]) if append: pop.push_back(xs,f=fs) return pop, df
def read_pop(filename,pop=None): # get magnet dimensions magnet_dim = len(pd.read_csv(filename).columns)-optimized_params # init problem for creating pop p_optimizeRes = pg.problem(optimizeRes(magnet_dim)) obj_dim = p_optimizeRes.get_nobj() quads = [] # construct columns to read from csv for i in range(magnet_dim): quads.append("x{}".format(i)) columns = quads for i in range(obj_dim): columns.append("f{}".format(i)) # read df from csv df = pd.read_csv(filename,names=columns) # initialize pop if none if pop == None: pop = pg.population(p_optimizeRes) # construct pop from df nrow, ncol = df.shape for i in df.index: xs = [] for j in range(magnet_dim): xs.append(df["x"+str(j)][i]) xs = np.asarray(xs) fs = [] for j in range(ncol-magnet_dim): # if not a valid obj value throw out point if df["f"+str(j)][i] < 0 or np.isnan(df["f"+str(j)][i]): print("error: ", df["f"+str(j)][i], i) df["f"+str(j)][i] = 1e10 fs.append(df["f"+str(j)][i]) # add point to population pop.push_back(xs,f=fs) # return population to main() return pop
def plot_4d(popi,filename,df): pop = None for i_cluster in range(10): df_i = df.loc[(df['kcluster'] == i_cluster)] magnet_dim = 15 p_optimizeRes = pg.problem(optimizeRes(magnet_dim)) nobj = p_optimizeRes.get_nobj() if pop == None: pop = pg.population(p_optimizeRes) nrow, ncol = df_i.shape for i in df_i.index: append=True xs = [] for j in range(1,magnet_dim+1): xs.append(df_i["q"+str(j)][i]) xs = np.asarray(xs) fs = [] for j in range(magnet_dim,magnet_dim+nobj): # if i == 0: # print(df.iloc[i,magnet_dim:magnet_dim+p_optimizeRes.get_nobj()]) fs.append(df.iloc[i,j]) if append: pop.push_back(xs,f=fs) popi = pop sort_param = 3 good_results=0 magnet_dim = len(popi.get_x()[0]) hv = pg.hypervolume(popi) ref_point = np.zeros(optimized_params)+1e10 ndf, dl, dc, ndl = pg.fast_non_dominated_sorting(popi.get_f()) plot_x, plot_y = 0,0 fig, axs = plt.subplots(optimized_params-1,sharex=True) fig.suptitle('Pareto Fronts of each parameter vs. BeamSpotSize at FP4') axs[optimized_params-2].set_xlabel(fNames[sort_param]) reduced_ndf = [] first = True # df_closest = df.loc[(df['kcluster'] == i_cluster)] for j in range(0,optimized_params-1): ndf_champ = [] axs[plot_y].axvline(x=fNom[0],linestyle="dashed",color="red") axs[plot_y].axhline(y=1.0,linestyle="dashed",color="red") for i in ndf[0]: check_val=1e9 if np.all(np.array(popi.get_f()[i]) < check_val) == True: good_results+=1 # print(filename[-6:-4], good_results, popi.get_f()[i]) ndf_champ.append(popi.get_f()[i]) reduced_ndf.append(i) try: pg.plot_non_dominated_fronts(ndf_champ,comp=[sort_param,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 if "closest" in df_i.columns: df_closest = df_i.loc[df['closest']==True] df_closest = df_closest.reset_index(drop=True) # print(df_closest.iloc[:,15:19]) for i_closest in df_closest.index: axs[plot_y].text(df_closest.iloc[:,18][i_closest],df_closest.iloc[:,15+j][i_closest],str(df_closest['kcluster'][0]+1),color='red') axs[plot_y].set_ylabel(fNames[j]) axs[plot_y].set_yscale('log') axs[plot_y].set_xscale('log') plot_y += 1 fig.tight_layout() fig.savefig(filename+"{}_paretos.png".format(i_cluster)) plt.cla() fig2, axs2 = plt.subplots(4,4) plot_x, plot_y = 0,0 reduced_qs = np.array(popi.get_x())[reduced_ndf] ycolumns = [] for i in range(magnet_dim): ycolumns.append('y{}'.format(i)) df_i = pd.DataFrame(reduced_qs, columns = ycolumns) 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_i['y{0}'.format(i)].plot.hist(ax=axs2[plot_y,plot_x],bins=100,range=(-2,2)) # 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_yscale("log") # axs2[plot_y,plot_x].axes.set_ylim(0.1,20) xlower, xupper = popi.problem.get_bounds() xlower, xupper = np.min(xlower), np.max(xupper) axs2[plot_y,plot_x].axes.set_xlim(xlower,xupper) axs2[plot_y,plot_x].set_title("q{0}".format(i+1)) y_min, y_max = axs2[plot_y,plot_x].get_ylim() df_closest['yplot'] = pd.Series(df_closest['kcluster'][0]).apply(lambda x: x/(10)*(y_max-y_min)+y_min) # print(df_closest.iloc[:,:15]) # if "closest" in df.columns: for i_closest in df_closest.index: axs2[plot_y,plot_x].text(df_closest["q{0}".format(i+1)][i_closest],df_closest['yplot'][i_closest],str(df_closest['kcluster'][0]+1),color='red') plot_x += 1 fig2.delaxes(axs2[plot_y,plot_x]) fig2.tight_layout() plt.savefig(filename + "{}_magnet_hists.png".format(i_cluster)) return