numpy 满足一定条件的随机向量生成

4szc88ey  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(141)

设G是(100,20)的矩阵,h是(100)的向量。
我如何随机生成一个大小为100的向量x,满足条件G x <= h?
显然,我可以做类似以下的事情.但是,如果我想产生成千上万的这些点,这是低效的.谢谢.

import numpy as np

# Define G and h
G = np.random.rand(100, 20)
h = np.random.rand(100)

def is_feasible(x):
  return np.all(np.dot(G, x) <= h)

while True:
  # Generate random vector x
  x = np.random.rand(20)

  # Check if x is feasible
  if is_feasible(x):
    break

字符串

nkcskrwz

nkcskrwz1#

我相信你可以试着跟着。你有不平等
G x <= h
Xi < 1,对任意i
对于任意i,Xi >= 0
您可以找到满足上述所有条件凸多边形交点,然后从该多边形中,您可以得到
Xi和MinXi,显然,这些值小于1并且大于或等于0。
然后,对由最小/最大值定义的超立方体中的点进行采样,然后才应用矩阵检查
伪代码
为真时:
对于在范围(0,100)中的i:
Xi =最小xi +(最大xi -最小xi)U 01
如果G x <= h:断开

mkh04yzy

mkh04yzy2#

这段代码生成一个向量,随机检查可行性。如果失败,从向量的每个值中减去随机的小数量,然后再试一次。这样我就能够在3秒内生成1000个非零向量:

import numpy as np

def is_feasible(x):
    return np.all(np.dot(G, x) <= h)

def generate(n=1000):
    out = []
    for _ in range(n):
        x = np.random.rand(20)

        while True:
            to_substract = np.random.uniform(0.01, 0.00001, size=20)
            x = np.clip(x - to_substract, 0, 1)

            # discard all-zero vector
            if np.all(x == 0):
                x = np.random.rand(20)
                continue

            if is_feasible(x):
                out.append(x)
                break

    return out

# Define G and h
G = np.random.rand(100, 20)
h = np.random.rand(100)

# generate and show first 20 vectors with most elements non-zero
out = sorted(generate(), key=lambda a: -np.sum(a > 0))
for row in out[:20]:
    print(*map(lambda v: "{:.5f}".format(v) if v != 0 else "       ", row), sep="|")

字符串
打印(例如):

|       |0.01784|0.00423|       |       |       |       |       |0.01945|       |       |       |       |       |       |       |0.00355|       |       
       |       |       |0.01594|0.01037|       |       |       |       |       |       |       |0.00797|       |       |0.01140|       |       |       |       
0.01059|       |       |       |0.02336|       |       |       |       |       |       |       |       |       |       |       |       |0.01277|       |       
       |       |0.03377|       |       |0.00754|       |       |       |       |0.02260|       |       |       |       |       |       |       |       |       
       |       |0.04551|       |       |       |       |0.01515|       |       |       |       |       |0.00466|       |       |       |       |       |       
       |0.02042|       |       |       |       |       |       |       |       |       |       |       |       |       |       |0.00385|0.00374|       |       
0.02001|       |0.00690|       |       |       |       |       |       |       |       |       |       |       |       |0.01926|       |       |       |       
       |       |       |0.01995|       |       |       |       |       |0.02459|0.00761|       |       |       |       |       |       |       |       |       
       |       |       |       |       |       |       |       |       |0.01517|       |       |0.00088|       |       |       |0.01188|       |       |       
       |       |       |       |       |       |       |       |       |0.01883|       |0.00382|       |0.00260|       |       |       |       |       |       
       |       |       |       |       |       |       |       |0.02289|0.00556|       |       |       |       |       |       |       |0.00122|       |       
       |       |0.00943|       |       |       |       |       |       |       |       |0.00766|       |       |0.01971|       |       |       |       |       
0.01891|       |0.00308|       |       |       |       |       |       |       |       |0.00916|       |       |       |       |       |       |       |       
       |       |       |       |       |       |       |0.00428|       |       |       |       |       |0.00848|       |       |0.00597|       |       |       
       |       |       |0.03437|       |       |       |       |0.01687|       |       |       |       |       |       |0.00748|       |       |       |       
       |       |       |0.02499|       |       |       |       |       |0.01509|       |       |0.00726|       |       |       |       |       |       |       
       |       |       |       |       |       |       |       |       |       |0.01138|       |       |       |       |       |0.00951|       |       |0.01423
       |       |0.00362|       |       |       |       |0.02474|       |       |       |       |       |       |       |       |       |0.00115|       |       
       |       |       |0.00695|       |       |0.01630|       |       |       |       |       |       |       |       |0.00665|       |       |       |       
       |       |       |0.00485|       |0.00809|0.00404|       |       |       |       |       |       |       |       |       |       |       |       |       

real    0m2,585s
user    0m0,007s
sys     0m0,008s

mwkjh3gx

mwkjh3gx3#

另一种可能的方法是使用线性规划,正如@pjs在评论中建议的那样,线性规划的解是随机生成的样本。目标函数必须被扰动,以便在每次运行时得到不同的解。

from scipy.optimize import linprog

G = np.random.rand(100, 20)
h = np.random.rand(100)

# Perturbed objective function
c = np.random.normal(0, 0.01, 20)

# Using linear programming
z = linprog(c, A_ub=G, b_ub=h, method='highs')

if z.success:
    x = z.x
    print(x)

字符串

相关问题