示例#1
0
文件: egt.py 项目: peihy/PyEGT
def cora():
    G = nx.barabasi_albert_graph(1000, 3)
    # G = nx.watts_strogatz_graph(1000, 5, 0.3)
    # G = nx.random_regular_graph(4, 1000)
    p = Population(G)
    e = Evolution()
    g = game.PDG(2)
    e.set_population(p).set_game(g)
    # p.strategy = np.ones(len(p), np.int)
    f, axs = plt.subplots(3, 4)
    axs = axs.reshape(12)
    for r in range(11):
        if r > 5:
            r_ = 10 - r
            p.strategy = np.zeros(len(p), dtype=np.int)
            s = 1
        else:
            r_ = r
            p.strategy = np.ones(len(p), dtype=np.int)
            s = 0
        n = int(round(len(p) / 10.0 * r_))
        selected_list = np.random.choice(range(len(p)), n)
        p.strategy[selected_list] = s
        # print p.strategy
        g.strategy = p.strategy
        g.play()
        p.show_degree(axs[r])
    plt.show()
示例#2
0
文件: egt.py 项目: peihy/PyEGT
def repeat_b():
    # 博弈收益参数不同,合作率曲线
    e = Evolution(has_mut=False)
    G = nx.random_regular_graph(4, 1000)
    p = Population(G)
    b = 5
    for i in range(b):
        g = game.PDG(i * 2 + 2)
        e.set_population(p).set_game(g).set_rule(u)
        print('Control Variable b: %d' % (i * 2 + 2))
        e.evolve(100000, restart=True, quiet=True, autostop=False)
        e.show(fmt[i], label="b=%d" % (i * 2 + 2))
    plt.legend(loc='lower right')
    plt.show()
示例#3
0
文件: egt.py 项目: peihy/PyEGT
def repeat2d():
    e = Evolution()
    bs = np.linspace(1, 10, 3)
    # fig, axes = plt.subplots()
    colors = 'brgcmykwa'
    symbs = '.ox+*sdph-'
    for i in range(1, 10):
        i = 4
        G = nx.random_regular_graph(i + 1, 1000)
        p = Population(G)
        a = [0] * len(bs)
        for j, b in enumerate(bs):
            g = game.PDG(b)
            e.set_population(p).set_game(g).set_rule(u)
            e.evolve(10000)
            a[j] = e.cooperate[-1]
            plt.plot(bs, a, colors[j] + symbs[j], label='b=%f' % b)
        break
    plt.show()
示例#4
0
文件: egt.py 项目: peihy/PyEGT
def repeat_start_pc():
    # 初始Pc不同的合作变化曲线
    # G = nx.watts_strogatz_graph(1000, 4, 0.2)
    G = nx.barabasi_albert_graph(1000, 3)
    p = Population(G)
    g = game.PDG(b=10)
    u = rule.DeathBirth()
    e = Evolution(has_mut=False)
    e.set_population(p).set_game(g).set_rule(u)
    for i in range(5):
        pc = (2 * i + 1) / 10.0
        p.init_strategies(g, [pc, 1 - pc])
        print('Initial P(C) is %.2f' % pc)
        e.evolve(100000, restart=True, quiet=True, autostop=False)
        e.show(fmt[i], label=r'start $P_C$=%.2f' % pc)
    plt.legend(loc='lower right')
    plt.title(r'Evolution under Different Start $P_C$')
    plt.xlabel('Number of generations')
    plt.ylabel(r'Fraction of cooperations, $\rho_c$')
    plt.show()
示例#5
0
文件: egt.py 项目: peihy/PyEGT
def repeat_ss_rewire():
    # 策略固定,连接倾向进行演化
    from network import LatticeWithLongTie
    p = LatticeWithLongTie(30)
    g = game.PDG(b=8)
    # u = rule.DeathBirth()
    u = rule.Fermi(0.1)
    a = adapter.Preference(3)
    e = StaticStrategyEvolution()
    e.set_game(g).set_rule(u).set_adapter(a)
    e.set_population(p)
    e.bind_process()
    p.init_longtie()
    # p.degree_distribution(loglog=False)
    p_copy1 = p.copy()
    plt.figure()
    for i in range(6):
        i = 5
        pc = i / 5.0
        # 复制演化前的状态
        p_copy = p.copy()
        e.set_population(p)
        a.dynamic = p.dynamics
        a.bind(p)
        # if i > 0:
        #     p.is_equal(p_copy1)
        #     break
        p.init_strategies(g, [pc, 1 - pc])
        # p.check_cache()
        # 重置网络连接,重置节点的连接策略
        p = p_copy
        print('static P(C) is %.2f' % pc)
        e.evolve(100, restart=True, quiet=True, autostop=False)
        e.show(fmt[i], label=r'static $P_C$=%.2f ' % pc)
        break
    plt.legend(loc='upper left')
    plt.title('Static Strategy Evolution')
    plt.xlabel('Number of generations')
    plt.ylabel('Rewire Strategies')
    plt.ylim(0, len(p))
    plt.show()
示例#6
0
文件: egt.py 项目: peihy/PyEGT
def repeat_ll_rewire():
    # 策略固定,连接倾向进行演化
    from network import LatticeWithLongTie
    # G = nx.random_regular_graph(5, 1000)
    p = LatticeWithLongTie(30)
    g = game.PDG(b=8)
    u = rule.DeathBirth()
    # u = rule.Fermi(.1)
    a = adapter.Preference(3)
    e = CoEvolution()
    e.set_game(g).set_rule(u).set_adapter(a)
    e.set_population(p)
    e.bind_process()
    p.degree_distribution()
    print('start evolving')
    e.evolve(10000, restart=True, quiet=True, autostop=False)
    plt.figure()
    e.show(fmt[0], label='LLN Co-Evolution')
    plt.legend(loc='upper left')
    plt.title('LLN Co-Evolution')
    plt.xlabel('Number of generations')
    plt.ylabel('Rewire Strategies')
    plt.show()
    p.degree_distribution()
示例#7
0
    def evolve(self, turns, **kwargs):
        self.rewire = None
        self.prefer = np.zeros((turns, self.adapter.category), dtype=np.int)
        super(self.__class__, self).evolve(turns, **kwargs)

    def show(self, fmt, label, wait=False, *args, **kwargs):
        super(self.__class__, self).show(True)
        f = plt.figure(2)
        color = 'brgcmykw'
        # symb = '.ox+*sdph'
        label = ['random', 'popularity', 'knn', 'pop*sim', 'similarity']
        for i in xrange(self.adapter.category):
            plt.plot(self.prefer[:, i], color[i], label=label[i])
        plt.title('CoEvolutionary Game')
        plt.xlabel('Step')
        plt.ylabel('Strategies')
        plt.legend()
        # plt.show()
        f.show()


if __name__ == '__main__':
    G = nx.random_regular_graph(5, 10)
    pp = Population(G)
    gg = game.PDG()
    rr = rule.BirthDeath()
    e = Evolution()
    e.set_population(pp).set_game(gg).set_rule(rr)
    e.evolve(10000)
    e.show()
示例#8
0
文件: egt.py 项目: peihy/PyEGT
# G = nx.star_graph(10)
# G = nx.watts_strogatz_graph(1000, 5, 0.2)
G = nx.barabasi_albert_graph(1000, 3)
# G = nx.powerlaw_cluster_graph(1000, 10, 0.2)
# G = nx.convert_node_labels_to_integers(nx.davis_southern_women_graph())
# G = ["/../../DataSet/ASU/Douban-dataset/data/edges.csv", ',']
# G = {"path":"/../wechat/barabasi_albert_graph(5000,100)_adj.txt", "fmt":"adj"}
# G = "/../wechat/facebook.txt"

# 网络结构
p = Population(G)
# print nx.info(p)
# p.degree_distribution()

# 博弈类型
g = game.PDG(b=5)
# g = game.PGG(3)
# g = game.PGG2(3)

# 学习策略
# u = rule.BirthDeath()
u = rule.DeathBirth()
# u = rule.Fermi()
# u = rule.HeteroFermi(g.delta)

# 连接策略
a = adapter.Preference(3)

# 绘图参数
colors = 'bgrcmykw'
markers = '.,ov^v<>1234sp*hH+xDd|-'