类似的问题: Generating N random points with certain predefined distance between them
choose n most distant points in R
但他们无论是在MATLAB或不符合要求的任务。
我要创建长度的盒子的任何两个点之间的距离是大于δ较大内部的点的N个。
例如:比方说,我有长度为10埃对X,Y,Z轴的盒子。 我想有这样的盒子内200个随机点,以便任意两点之间的最小距离大于3埃大。
尝试:
#!python
# -*- coding: utf-8 -*-#
import numpy as np
np.random.seed(100)
np.set_printoptions(2)
box_length = 10
d = box_length
threshold = 6
num_points = 5
x1, y1, z1 = np.random.random(num_points)* box_length, np.random.random(num_points)* box_length, np.random.random(num_points)* box_length
x2, y2, z2 = np.random.random(num_points)* box_length, np.random.random(num_points)* box_length, np.random.random(num_points)* box_length
# print(len(x1))
# just for checking make ponts integers
for i in range(len(x1)):
x1[i] = int(x1[i])
x2[i] = int(x2[i])
y1[i] = int(y1[i])
y2[i] = int(y2[i])
z1[i] = int(z1[i])
z2[i] = int(z2[i])
print(x1)
print(y1)
print(z1)
print("\n")
pt1_lst = []
pt2_lst = []
for i in range(len(x1)):
a, b, c = x1[i], y1[i], z1[i]
a2, b2, c2 = x2[i], y2[i], z2[i]
dist2 = (a-a2)**2 + (b-b2)**2 + (c-c2)**2
print("\n")
print(a,b,c)
print(a2,b2,c2)
print(dist2)
if dist2 > threshold**2:
pt1 = (a,b,c)
pt2 = (a2,b2,c2)
pt1_lst.append(pt1)
pt2_lst.append(pt2)
print("points")
print(pt1_lst)
print(pt2_lst)
上的代码问题:该代码points1比较点points2但里面points1和points2内本身不比较。
可能有更好的算法来解决这个问题,并脱帽向那些谁配有高招来解决问题的家伙。
谢谢。
PS:我做了一些研究,并试图找到相关链接,但是无法来解决这个问题。不过一些相关链接:
更新::
我试图Stefans'代码的下方,它适用于N = 10,但我尝试了对于N = 200,它是使用非常大的时间(I停止10分钟后的代码)。
是否有这样做的任何有效的方法?
帮助将真正的赞赏!
All paths of length L from node n using python Create Random Points Inside Defined Rectangle with Python
比方说,我有长度为10埃对X,Y,Z轴的盒子。我想有这个盒子里面10个随机点,因此任何两点之间的最小距离大于3埃大。
我想这样的作品,在该框中重复产生10个随机点,直到距离都足够大:
>>> import numpy as np
>>> from itertools import combinations
>>> while True:
P = np.random.rand(10, 3) * 10
if all(np.linalg.norm(p - q) > 3
for p, q in combinations(P, 2)):
break
>>> P
array([[ 9.02322366, 6.13576854, 3.1745708 ],
[ 6.48005836, 7.5280536 , 4.66442095],
[ 5.78306167, 1.83922896, 9.48337683],
[ 0.70507032, 0.20737532, 5.31191608],
[ 3.71977864, 6.40278939, 3.81742814],
[ 0.03938102, 6.7705456 , 6.28841217],
[ 3.27845597, 2.98811665, 4.81792286],
[ 7.74422021, 9.30027671, 8.1770998 ],
[ 0.28544716, 0.35155801, 9.77847352],
[ 4.84536373, 4.21378476, 0.4456017 ]])
大约需要50试图找到一个很好的点的集合。在这里,我尝试1000次,20次,这是很好:
>>> sum(all(np.linalg.norm(p - q) > 3
for p, q in combinations(np.random.rand(10, 3) * 10, 2))
for _ in range(1000))
20
这种分配通用名称是泊松球采样。已知有O(N)的做到这一点 - 请here