python 仿真实验_生成仿真的演化网络实验【Python版】

''' 绘制动画

'''

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

import networkx as nx

import random

fig, ax = plt.subplots()

G = nx.Graph()

G.add_edge(0,1)

def init_network():

# nx.draw(G, node_size=10, node_color='c')

return G,

def animate_network(i):

# 定义网络动态变化的几种形式,分别代表删除边,删除节点,维持不动,增加节点,增加边

d = [-2, -1, 0, 1, 2]

# 从中随机选择一种操作

c = random.randint(0,len(d)-1)

edges = list(G.edges())

nodes = list(G.nodes())

if d[c] == -2:

if edges:

u,v = random.choice(edges)

G.remove_edge(u,v)

if d[c] == -1:

if nodes:

node = random.choice(nodes)

G.remove_node(node)

if d[c] == 0:

# 网络不发生变化

pass

if d[c] == 1:

G.add_node(len(nodes))

if d[c] == 2:

if nodes:

G.add_edge(len(nodes), random.choice(nodes))

nx.draw(G, pos = nx.spring_layout(G), node_size=10, node_color='c',edge_color='grey')

return G,

ani = animation.FuncAnimation(fig=fig, func=animate_network, frames=30, init_func=init_network, interval=100, blit=False,)

ani.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值