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()
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()
def repeat_k(): # 网络平均度不同,合作率曲线 e = Evolution(has_mut=False) k = 5 a = [0] * k for i in range(k): G = nx.random_regular_graph(i * 2 + 2, 1000) p = Population(G) e.set_population(p).set_game(g).set_rule(u) print('Control Variable k: %d' % (i * 2 + 2)) e.evolve(100000, restart=True, quiet=True) # TODO: if e is CoEvolution, population need re-copy # a[i] = e.cooperate[-1] e.show(fmt[i], label="k=%d" % (i * 2 + 2)) # plt.plot(range(2, k+1), a[1:], 'r-') # plt.plot([400+i*i for i in range(20)], 'ro--', label='k=4') # plt.plot([400 + i for i in range(20)], 'g^-.', label='k=6') # plt.plot([400 - i for i in range(20)], 'cx:', label='k=8') plt.legend(loc='lower right') plt.show()
def once(): e = Evolution(has_mut=True) e.set_population(p).set_game(g).set_rule(u) e.evolve(1000) e.show()