示例#1
0
def plot_2d_evo(popi):

    magnet_dim = len(popi.get_x()[0])
    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.ideal(popi.get_f())[0])
    x = np.linspace(pg.ideal(popi.get_f())[0],0)
    y = np.zeros(50)+(1)
    for j in range(2,100):
        plt.cla()
        plt.plot(x,y,linestyle="dashed",color="red")
        print(ndf[0])
        ndf_champ.append([popi.get_f()[i] for i in ndf[j]])
#        ax = pg.plot_non_dominated_fronts(ndf_champ[0],comp=[0,j])
#    ax.plot(color="C{}".format(j))
        ax = pg.plot_non_dominated_fronts(popi.get_f()[0:j])
        ax.set_xlabel('resolution')
        ax.set_ylabel('xangle_e_min')
        ax.set_ylim(1e-3,1000)
        ax.set_yscale('log')
#    print(ndf_champ, ndf[0])
#    print(ndf)
        plt.savefig("popi{}".format(j))
    return
示例#2
0
def findMinAndMaxPoints(objectivesList):
    '''Returns the min and max points for a list of sets of objectives'''    
    minPoint = pg.ideal(objectivesList)
    maxPoint = pg.nadir(objectivesList)
    
    print(f"MinPoint {minPoint}")
    print(f"MaxPoint {maxPoint}\n")
    
    return minPoint, maxPoint
print('Running: ', filenameNPZ[:-4])

# algorithm
algo = po.algorithm(po.nsga2(gen=generations))
algo.set_verbosity(1)
# problem
prob = po.problem(myUDP.KAOnodesMultiObj())
# population
pop = po.population(prob=prob, size=sizePop)
# evolution
start = time.time()
popE = algo.evolve(pop)
print('time evolution: ', time.time() - start)

# save TXT fie with general description of the optimization
bestFstr = 'ideal found fit: ' + str(po.ideal(
    popE.get_f())) + '; best fit possible: -1'
#bestChamp  = 'champion decission vector'
#bestXstr  = 'velocity: ' + str(popE.champion_x[0]) + ', kL:' + str(popE.champion_x[1]),', kG: ' + str(popE.champion_x[2])
popStr = popE.__str__()
algoStr = algo.__str__()
localtime = time.localtime(time.time())
dateStr = str(localtime)
with open(pathsave + filenameTXT, "w") as text_file:
    print(dateStr, end='\n\n', file=text_file)
    print(bestFstr, end='\n', file=text_file)
    #print(bestChamp, end='\n', file=text_file)
    #print(bestXstr, end='\n\n', file=text_file)
    print(algoStr, end='\n', file=text_file)
    print(popStr, end='\n', file=text_file)

# Get logs from the algorithm
示例#4
0
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
示例#5
0
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