예제 #1
0
def community(x,
              y,
              edges=499,
              n_bots=0,
              seeking=False,
              initial_graph="barabasi_albert"):
    m = weighted_balance_bots(d=3,
                              n_vertices=500,
                              n_edges=edges,
                              phi=y,
                              alpha=0.4,
                              epsilon=x,
                              initial_graph=initial_graph,
                              n_bots=n_bots,
                              seeking_bots=seeking)

    i = 0
    while m.convergence() == False and i < 100000:

        for counter in range(1000):
            m.step()
            i += 1

    return H(m.vertices[m.n_bots:, :-1], m.d - 1)
예제 #2
0
def bot_plots_shifted(recover=False, both_sides= False, neutral_bots=False, edges=None, fontsize = 20,t_lim=None,initial_opinion_range=[0,1],report_fig=""):
    '''effect on hyperpolarization and mean opinion with bots when constraining initial_opinion_range (and initial mean !=0)'''
    name = "bots"+both_sides*"_double"+neutral_bots*"_neutral"+(edges!=None)*("_edges_"+str(edges))+(t_lim!=None)*("_tlim_"+str(t_lim))+"shifted_"+str(int(100*initial_opinion_range[0]))+"_"+str(int(100*initial_opinion_range[1]))+"fig_"+report_fig
    if t_lim == None:
        t_lim = t_lim_default
    colors = [(1, 0, 0), (0.5, 0, 0.5), (0, 0, 1)]
    if not recover:
        metrics = {
            "time_to_convergence": lambda x: x.t,
            "maximal absolute opinion": lambda x:
            np.max(np.abs(np.mean(x.vertices[x.n_bots:-x.n_bots,:-1],axis=0))) if (x.both_sides and x.n_bots>0) else np.max(np.abs(np.mean(x.vertices[x.n_bots:,:-1],axis=0))),
            "H": lambda x: H(x.vertices[x.n_bots:-x.n_bots,:-1],x.d-1) if (x.both_sides and x.n_bots>0) else H(x.vertices[x.n_bots:,:-1],x.d-1)
        }
        if not both_sides:
            loop = ("n_bots", n_bots2side)
        else:
            loop = ("n_bots", n_bots1side)
        output_1 = experiment_loop(
            {"n_vertices":n_vertices,"d": 3, "alpha": 0.4,"f":lambda x: np.sign(x)*np.abs(x)**(0.5),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_opinion_range":initial_opinion_range}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_2 = experiment_loop(
            {"n_vertices":n_vertices, "d": 3, "alpha": 0.4, "f": lambda x: np.sign(x) * np.abs(x),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_opinion_range":initial_opinion_range}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots", verbose=True,t_lim=t_lim)
        output_3 = experiment_loop(
            {"n_vertices":n_vertices, "d": 3, "alpha": 0.4, "f": lambda x: np.sign(x) * np.abs(x) ** (2),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_opinion_range":initial_opinion_range}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots", verbose=True,t_lim=t_lim)
        with open(name+".pickle", "wb") as fp:
            pickle.dump([output_1, output_2, output_3],fp)
    else:
        with open(name+".pickle", "rb") as fp:
            output_1,output_2,output_3 = pickle.load(fp)


    median_plus_percentile_plot(output_1["variation"][1], output_1["time_to_convergence"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["time_to_convergence"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["time_to_convergence"],color=colors[2])

    plt.ylim(0,t_lim*101001/99999)
    plt.yticks(ticks=[0,20000,40000,60000,80000,100000],labels=["0","2e4","4e4","6e4","8e4","1e5"],fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots" +both_sides*" per side",fontsize=fontsize)
    plt.ylabel("Steps until convergence",fontsize=fontsize)
    plt.legend(["0.5","0","-1"],title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"t_conv_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["maximal absolute opinion"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["maximal absolute opinion"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["maximal absolute opinion"],color=colors[2])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Maximal absolute mean opinion",fontsize=fontsize)
    plt.legend(["0.5", "0",  "-1"], title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"maxabsop_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["H"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["H"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["H"],color=colors[2])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Hyperpolarization H(O)",fontsize=fontsize)
    plt.legend(["0.5",  "0",  "-1"], title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"H_"+name)
    plt.close()
예제 #3
0
def bot_plots_ba_dynamic(recover=False, both_sides= False, neutral_bots=False, edges=None, fontsize = 20,bot_positions = None,t_lim=None,epsilon=1,seeking_bots=False,report_fig=""):
    name = "bots_ba_dyn"+seeking_bots*"_seeking_"+(bot_positions!=None)*str(bot_positions)+both_sides*"_double"+neutral_bots*"_neutral"+(edges!=None)*("_edges_"+str(edges))+"_epsilon_"+str(int(epsilon*10))+(t_lim!=None)*("_tlim_"+str(t_lim))+"fig_"+report_fig
    if t_lim == None:
        t_lim = t_lim_default
    colors = [(1, 0, 0), (0.5, 0, 0.5), (0, 0, 1)]
    if not recover:
        metrics = {
            "time_to_convergence": lambda x: x.t,
            "maximal absolute opinion": lambda x:
            np.max(np.abs(np.mean(x.vertices[np.logical_not(x.bot_indices)][:,:-1],axis=0))),
            "H": lambda x: H(x.vertices[np.logical_not(x.bot_indices)][:,:-1],x.d-1)
        }
        if not both_sides:
            loop = ("n_bots", n_bots2side)
        else:
            loop = ("n_bots", n_bots1side)
        output_1 = experiment_loop(
            {"n_vertices":n_vertices,"d": 3, "alpha": 0.4,"f":lambda x: np.sign(x)*np.abs(x)**(0.5),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_graph":"barabasi_albert","bot_positions":bot_positions,"epsilon":epsilon,"phi":0.5,"seeking_bots":seeking_bots}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_2 = experiment_loop(
            {"n_vertices":n_vertices, "d": 3, "alpha": 0.4, "f": lambda x: np.sign(x) * np.abs(x),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_graph":"barabasi_albert","bot_positions":bot_positions,"epsilon":epsilon,"phi":0.5,"seeking_bots":seeking_bots}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots", verbose=True,t_lim=t_lim)
        output_3 = experiment_loop(
            {"n_vertices":n_vertices, "d": 3, "alpha": 0.4, "f": lambda x: np.sign(x) * np.abs(x) ** (2),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_graph":"barabasi_albert","bot_positions":bot_positions,"epsilon":epsilon,"phi":0.5,"seeking_bots":seeking_bots}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots", verbose=True,t_lim=t_lim)
        with open(name+".pickle", "wb") as fp:
            pickle.dump([output_1, output_2, output_3],fp)
    else:
        with open(name+".pickle", "rb") as fp:
            output_1,output_2,output_3 = pickle.load(fp)


    median_plus_percentile_plot(output_1["variation"][1], output_1["time_to_convergence"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["time_to_convergence"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["time_to_convergence"],color=colors[2])

    plt.ylim(0,t_lim*101001/99999)
    plt.yticks(ticks=[0,20000,40000,60000,80000,100000],labels=["0","2e4","4e4","6e4","8e4","1e5"],fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots" +both_sides*" per side",fontsize=fontsize)
    plt.ylabel("Steps until convergence",fontsize=fontsize)
    plt.legend(["0.5","0","-1"],title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"t_conv_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["maximal absolute opinion"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["maximal absolute opinion"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["maximal absolute opinion"],color=colors[2])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Maximal absolute mean opinion",fontsize=fontsize)
    plt.legend(["0.5", "0",  "-1"], title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"maxabsop_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["H"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["H"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["H"],color=colors[2])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Hyperpolarization H(O)",fontsize=fontsize)
    plt.legend(["0.5",  "0",  "-1"], title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"H_"+name)
    plt.close()
예제 #4
0
def bot_plots_altf(recover=False, both_sides= False, neutral_bots=False, edges=None, fontsize = 20,t_lim=None,report_fig=""):
    '''bots in models with alternative functions for evaluating the attitude: f = x +- 0.1, max or min(x,0)
    '''
    name = "bots_altf"+both_sides*"_double"+neutral_bots*"_neutral"+(edges!=None)*("_edges_"+str(edges))+(t_lim!=None)*("_tlim_"+str(t_lim))+"fig_"+report_fig
    if t_lim == None:
        t_lim = t_lim_default
    colors = [(1, 0, 0), (0,1,1),(1,1,0),(0,0,1)]
    if not recover:
        metrics = {
            "time_to_convergence": lambda x: x.t,
            "maximal absolute opinion": lambda x:
            np.max(np.abs(np.mean(x.vertices[x.n_bots:-x.n_bots,:-1],axis=0))) if (x.both_sides and x.n_bots>0) else np.max(np.abs(np.mean(x.vertices[x.n_bots:,:-1],axis=0))),
            "H": lambda x: H(x.vertices[x.n_bots:-x.n_bots,:-1],x.d-1) if (x.both_sides and x.n_bots>0) else H(x.vertices[x.n_bots:,:-1],x.d-1)
        }
        if not both_sides:
            loop = ("n_bots", n_bots2side)
        else:
            loop = ("n_bots", n_bots1side)
        output_1 = experiment_loop(
            {"n_vertices":n_vertices,"d": 3, "alpha": 0.4,"f":lambda x: x+0.1,"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_2 = experiment_loop(
            {"n_vertices":n_vertices,"d": 3, "alpha": 0.4,"f":lambda x: x-0.1,"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_3 = experiment_loop(
            {"n_vertices":n_vertices,"d": 3, "alpha": 0.4,"f":lambda x: max(0,x),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_4 = experiment_loop(
            {"n_vertices":n_vertices,"d": 3, "alpha": 0.4,"f":lambda x: min(0,x),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots}, loop, metrics, n=n_iterations,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)



        with open(name+".pickle", "wb") as fp:
            pickle.dump([output_1, output_2, output_3,output_4],fp)
    else:
        with open(name+".pickle", "rb") as fp:
            output_1,output_2,output_3,output_4 = pickle.load(fp)


    median_plus_percentile_plot(output_1["variation"][1], output_1["time_to_convergence"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["time_to_convergence"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["time_to_convergence"],color=colors[2])
    median_plus_percentile_plot(output_4["variation"][1], output_4["time_to_convergence"], color=colors[3])

    plt.ylim(0,t_lim*101001/99999)
    plt.yticks(ticks=[0,20000,40000,60000,80000,100000],labels=["0","2e4","4e4","6e4","8e4","1e5"],fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots" +both_sides*" per side",fontsize=fontsize)
    plt.ylabel("Steps until convergence",fontsize=fontsize)
    plt.legend(["x+0.1","x-0.1","max(x,0)","min(x,0)"],title="f(x)",fontsize=fontsize-4,title_fontsize=fontsize-4)
    plt.savefig(image_folder+"t_conv_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["maximal absolute opinion"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["maximal absolute opinion"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["maximal absolute opinion"],color=colors[2])
    median_plus_percentile_plot(output_4["variation"][1], output_4["maximal absolute opinion"], color=colors[3])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Maximal absolute mean opinion",fontsize=fontsize)
    plt.legend(["x+0.1","x-0.1","max(x,0)","min(x,0)"],title="f(x)",fontsize=fontsize-4,title_fontsize=fontsize-4)
    plt.savefig(image_folder+"maxabsop_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["H"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["H"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["H"],color=colors[2])
    median_plus_percentile_plot(output_4["variation"][1], output_4["H"], color=colors[3])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Hyperpolarization H(O)",fontsize=fontsize)
    plt.legend(["x+0.1","x-0.1","max(x,0)","min(x,0)"],title="f(x)",fontsize=fontsize-4,title_fontsize=fontsize-4)
    plt.savefig(image_folder+"H_"+name)
    plt.close()
     neutral_bots=neutral_bots,
     n_edges=n_edges)
 for i in range(200000):
     A.step()
     if A.convergence():
         break
 if n_bots > 0 and both_sides == True:
     submeans.append(
         np.mean(
             A.vertices[n_bots:-n_bots, :-1],
             axis=0))
     subvars.append(
         np.var(A.vertices[n_bots:-n_bots, :-1],
                axis=0))
     subH.append(
         H(A.vertices[n_bots:-n_bots, :-1],
           d=d))
 elif n_bots == 0:
     submeans.append(
         np.mean(A.vertices[:, :-1], axis=0))
     subvars.append(
         np.var(A.vertices[:, :-1], axis=0))
     subH.append(H(A.vertices[:, :-1], d=d))
 else:
     submeans.append(
         np.mean(
             A.vertices[n_bots + n_bots *
                        (not both_sides):, :-1],
             axis=0))
     subvars.append(
         np.var(
             A.vertices[n_bots + n_bots *
예제 #6
0
def bot_plots(recover=False, both_sides= False, neutral_bots=False, edges=None, fontsize = 20,t_lim=None):
    ''' calculates and plots: (time)steps to convergence, mean opinion and hyperpolarization
            x-axis n_bots, for evaluative extremeness e= 0.5, 0 and -1 
            '''
    
    name = "bots"+both_sides*"_double"+neutral_bots*"_neutral"+(edges!=None)*("_edges_"+str(edges))+(t_lim!=None)*("_tlim_"+str(t_lim))
    if t_lim == None:
        t_lim = 99999
    colors = [(1, 0, 0), (0.5, 0, 0.5), (0, 0, 1)]
    if not recover:
        metrics = {
            "time_to_convergence": lambda x: x.t,
            "maximal absolute opinion": lambda x:
            np.max(np.abs(np.mean(x.vertices[x.n_bots:-x.n_bots,:-1],axis=0))) if (x.both_sides and x.n_bots>0) else np.max(np.abs(np.mean(x.vertices[x.n_bots:,:-1],axis=0))),
            "H": lambda x: H(x.vertices[x.n_bots:-x.n_bots,:-1],x.d-1) if (x.both_sides and x.n_bots>0) else H(x.vertices[x.n_bots:,:-1],x.d-1)
        }
        if not both_sides:
            loop = ("n_bots", [0,2,6,10,20,30,40,50,100,150,200,250])
        else:
            loop = ("n_bots", [0, 1, 3, 5, 10, 15, 20, 25, 50, 75, 100, 125])
        output_1 = experiment_loop(
            {"n_vertices":500,"d": 3, "alpha": 0.4,"f":lambda x: np.sign(x)*np.abs(x)**(0.5),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots}, loop, metrics, n=10,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_2 = experiment_loop(
            {"n_vertices": 500, "d": 3, "alpha": 0.4, "f": lambda x: np.sign(x) * np.abs(x),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots}, loop, metrics, n=10,
            model_type="Weighted Balance Bots", verbose=True,t_lim=t_lim)
        output_3 = experiment_loop(
            {"n_vertices": 500, "d": 3, "alpha": 0.4, "f": lambda x: np.sign(x) * np.abs(x) ** (2),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots}, loop, metrics, n=10,
            model_type="Weighted Balance Bots", verbose=True,t_lim=t_lim)
        with open(name, "wb") as fp:
            pickle.dump([output_1, output_2, output_3],fp)
    else:
        with open(name, "rb") as fp:
            output_1,output_2,output_3 = pickle.load(fp)


    median_plus_percentile_plot(output_1["variation"][1], output_1["time_to_convergence"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["time_to_convergence"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["time_to_convergence"],color=colors[2])

    plt.ylim(0,t_lim*101001/99999)
    plt.yticks(ticks=[0,20000,40000,60000,80000,100000],labels=["0","2e4","4e4","6e4","8e4","1e5"],fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots" +both_sides*" per side",fontsize=fontsize)
    plt.ylabel("Steps until convergence",fontsize=fontsize)
    plt.legend(["0.5","0","-1"],title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"t_conv_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["maximal absolute opinion"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["maximal absolute opinion"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["maximal absolute opinion"],color=colors[2])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Maximal absolute mean opinion",fontsize=fontsize)
    plt.legend(["0.5", "0",  "-1"], title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"maxabsop_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["H"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["H"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["H"],color=colors[2])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Hyperpolarization H(O)",fontsize=fontsize)
    plt.legend(["0.5",  "0",  "-1"], title="Value of e",fontsize=fontsize,title_fontsize=fontsize)
    plt.savefig(image_folder+"H_"+name)
    plt.close()
예제 #7
0
def bot_plots_altf_ba(recover=False, both_sides= False, neutral_bots=False, edges=None, fontsize = 20,t_lim=None,bot_positions = None):
    name = "bots_altf_ba_"+(bot_positions!=None)*str(bot_positions)+both_sides*"_double"+neutral_bots*"_neutral"+(edges!=None)*("_edges_"+str(edges))+(t_lim!=None)*("_tlim_"+str(t_lim))
    if t_lim == None:
        t_lim = 99999
    colors = [(1, 0, 0), (0,1,1),(1,1,0),(0,0,1)]
    if not recover:
        metrics = {
            "time_to_convergence": lambda x: x.t,
            "maximal absolute opinion": lambda x:
            np.max(np.abs(np.mean(x.vertices[np.logical_not(x.bot_indices)][:, :-1], axis=0))),
             "H": lambda x: H(x.vertices[np.logical_not(x.bot_indices)][:,:-1],x.d-1)
        }
        if not both_sides:
            loop = ("n_bots", [0,2,6,10,20,30,40,50,100,150,200,250])
        else:
            loop = ("n_bots", [0, 1, 3, 5, 10, 15, 20, 25, 50, 75, 100, 125])
        output_1 = experiment_loop(
            {"n_vertices":500,"d": 3, "alpha": 0.4,"f":lambda x: x+0.1,"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_graph":"barabasi_albert","bot_positions":bot_positions}, loop, metrics, n=10,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_2 = experiment_loop(
            {"n_vertices":500,"d": 3, "alpha": 0.4,"f":lambda x: x-0.1,"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_graph":"barabasi_albert","bot_positions":bot_positions}, loop, metrics, n=10,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_3 = experiment_loop(
            {"n_vertices":500,"d": 3, "alpha": 0.4,"f":lambda x: max(0,x),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_graph":"barabasi_albert","bot_positions":bot_positions}, loop, metrics, n=10,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)
        output_4 = experiment_loop(
            {"n_vertices":500,"d": 3, "alpha": 0.4,"f":lambda x: min(0,x),"n_edges":edges,
             "both_sides":both_sides,"neutral_bots":neutral_bots,"initial_graph":"barabasi_albert","bot_positions":bot_positions}, loop, metrics, n=10,
            model_type="Weighted Balance Bots",verbose=True,t_lim=t_lim)



        with open(name, "wb") as fp:
            pickle.dump([output_1, output_2, output_3,output_4],fp)
    else:
        with open(name, "rb") as fp:
            output_1,output_2,output_3,output_4 = pickle.load(fp)


    median_plus_percentile_plot(output_1["variation"][1], output_1["time_to_convergence"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["time_to_convergence"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["time_to_convergence"],color=colors[2])
    median_plus_percentile_plot(output_4["variation"][1], output_4["time_to_convergence"], color=colors[3])

    plt.ylim(0,t_lim*101001/99999)
    plt.yticks(ticks=[0,20000,40000,60000,80000,100000],labels=["0","2e4","4e4","6e4","8e4","1e5"],fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots" +both_sides*" per side",fontsize=fontsize)
    plt.ylabel("Steps until convergence",fontsize=fontsize)
    plt.legend(["x+0.1","x-0.1","max(x,0)","min(x,0)"],title="f(x)",fontsize=fontsize-4,title_fontsize=fontsize-4)
    plt.savefig(image_folder+"t_conv_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["maximal absolute opinion"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["maximal absolute opinion"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["maximal absolute opinion"],color=colors[2])
    median_plus_percentile_plot(output_4["variation"][1], output_4["maximal absolute opinion"], color=colors[3])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Maximal absolute mean opinion",fontsize=fontsize)
    plt.legend(["x+0.1","x-0.1","max(x,0)","min(x,0)"],title="f(x)",fontsize=fontsize-4,title_fontsize=fontsize-4)
    plt.savefig(image_folder+"maxabsop_"+name)
    plt.close()

    median_plus_percentile_plot(output_1["variation"][1], output_1["H"],color=colors[0])
    median_plus_percentile_plot(output_2["variation"][1], output_2["H"],color=colors[1])
    median_plus_percentile_plot(output_3["variation"][1], output_3["H"],color=colors[2])
    median_plus_percentile_plot(output_4["variation"][1], output_4["H"], color=colors[3])
    plt.ylim(-0.01,1.01)
    plt.yticks(fontsize=fontsize-4)
    plt.xticks(fontsize=fontsize-4)
    plt.xlabel("Number of bots",fontsize=fontsize)
    plt.ylabel("Hyperpolarization H(O)",fontsize=fontsize)
    plt.legend(["x+0.1","x-0.1","max(x,0)","min(x,0)"],title="f(x)",fontsize=fontsize-4,title_fontsize=fontsize-4)
    plt.savefig(image_folder+"H_"+name)
    plt.close()
예제 #8
0
algo_name = 'DQN-TAMER'
env = gym.make('LunarLander-v2')
max_ep = 1000
epsilon = .3
gamma = .99
human = Human(1388420)
alpha_q = 1
alpha_h = alpha_q

#Proportion of network you want to keep
tau = .995

q = Q(env)
q_target = deepcopy(q)

h = H(env)
h_target = deepcopy(h)

q_optim = torch.optim.Adam(q.parameters(), lr=1e-3)
h_optim = torch.optim.Adam(h.parameters(), lr=1e-3)

batch_size = 128
rb = ReplayBuffer(1e6)

h_batch = 16
human_rb = HumanReplayBuffer(1e6)
local_batch = History(1e3)


#Training the network
def train():