create N random points all more distant than given length L (python and N = 200)

牧云@^-^@ 提交于 2020-01-13 11:33:32

问题


Similar questions:
Generating N random points with certain predefined distance between them

choose n most distant points in R

But they are either in matlab or does not fullfill the required task.

I have to create N number of points inside a box of length that the distance between any two points is larger than delta.

For example: Let's say I have a box of length 10 Angstrom on x,y,z axis.
I want to have 200 random points inside this box so that minimum distance between any two points is larger than 3 Angstrom.

Attempt:

#!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)

Problem on Code: This code compares points from points1 to points2 but does not compare within itself inside points1 and points2.

There might be better algorithms to solve this problem and hats off to those guys who come with the brilliant idea to solve the problem.

Thanks.

PS: I did some research and try to find related links, however was unable to solve the problem. Still some related links are:

Update::

I attempted Stefans' code below, It works for N= 10 but I tried it for N = 200 and it is using extremely large time ( I stopped the code after 10 minutes).

Is there any efficient way of doing this?

Help will be truly appreciated!!

All paths of length L from node n using python
Create Random Points Inside Defined Rectangle with Python


回答1:


Let's say I have a box of length 10 Angstrom on x,y,z axis. I want to have 10 random points inside this box so that minimum distance between any two points is larger than 3 Angstrom.

I think this works, repeatedly generating ten random points in that box until the distances are all large enough:

>>> 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 ]])

Takes about 50 attempts to find a good set of points. Here I try 1000 times, 20 times it was good:

>>> 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



回答2:


Common name for such distribution is Poisson spheres sampling. There is known O(n) which does this - please check here



来源:https://stackoverflow.com/questions/47041784/create-n-random-points-all-more-distant-than-given-length-l-python-and-n-200

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!