![](/img/trans.png)
[英]Efficient way to sample N points from a 3D space with the constraint of a minimum distance between two points in Python
[英]Fast 3d distance between N points python using networkx nodes in python
我一直試圖解決這一問題,但我做對了。
我目前正在使用站點到站點的滲濾進行殖民化模擬。 我正在嘗試將其放大到大數〜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計算方式計算距離來繪制這些邊緣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.