【发布时间】:2017-06-26 17:04:30
【问题描述】:
我一整天都在想办法解决这个问题,但我就是做错了。
我目前正在使用站点到站点的渗透进行殖民模拟。我正在尝试将其扩展到较大的数字〜10 ^ 6,但是用于计算距离的传统 numpy 方法我正在使用二次缩放,因此对于如此大的运行,程序运行超过一天。我真的希望这更快。我一直在寻找解决方案,但我找不到任何可以帮助我解决这个问题的方法,因为我有一个在模拟中使用的自定义类。
所以我想要每个节点到所有其他节点的距离,如果节点在彼此的 D_max 范围内,则绘制一条边,允许两个节点之间迁移。
`density = 0.14 #Stellar density per cubic parsec
L = 100
Patches = int(0.056*density*L**3+15)
Distance = 5
nearand = np.genfromtxt('/Users/Skippy/nearand.csv', delimiter = ',',usecols=np.arange(0, 3)).astype('float32') # a csv of 3d cartesian co-ordinates
G = nx.Graph()
xcoord = nearand[:,0]
ycoord = nearand[:,1]
zcoord = nearand[:,2]
class patch:
def __init__(self,status=0,pos=(0,0,0)):
self.status = status
self.pos = pos
def __str__(self):
return(str(self.status))
for i in xrange(Patches):
Stat = 1 if np.random.uniform() < P_init else 0 # a parameter used in the algorithm later
Pos = (xcoord[i], ycoord[i], zcoord[i])
G.add_node(patch(Stat,Pos))
for p1 in G.nodes():
for p2 in G.nodes():
Dist = np.sqrt((p1.pos[2] - p2.pos[2])**2 + (p1.pos[1]-p2.pos[1])**2+(p1.pos[0]-p2.pos[0])**2)
if Dist <= Distance:
G.add_edge(p1,p2)`
在此之后,算法运行,但较大的运行在距离计算中被搁置,因此只有距离需要优化。谁能帮我解决这个问题?它看起来和其他问题类似,但我需要能够以常规 numpy 计算计算距离的方式绘制这些边缘。
【问题讨论】:
标签: python python-2.7 simulation networkx