棒(线段)渗流算法 - 图论?

时间:2013-08-23 22:00:43

标签: algorithm ram

我写了一个用于研究棒渗透的算法(即:在它们之间相交的线段之间的网络)。在我的算法中,在“b”和“h”的矩形框内创建N个(线段),然后逐个算法探索所有线段之间的交集。这是蒙特卡罗模拟,因此“实验”执行多次(不少于100次)。这样写,消耗了很多RAM:

array_x1=uniform.rvs(loc=-b/2, scale=b, size=N)  
array_y1=uniform.rvs(loc=-h/2, scale=h, size=N)
array_x2=uniform.rvs(loc=-b/2, scale=b, size=N)  
array_y2=uniform.rvs(loc=-h/2, scale=h, size=N)  

M = np.zeros([N,N])  

for u in xrange(100):  ----> This '100' is the number of experiments.
    for j in xrange(N):
       if j>0:
         x_A1B1 = array_x2[j]-array_x1[j]
        y_A1B1 = array_y2[j]-array_y1[j]
        x_A1A2 = array_x1[0:j]-array_x1[j]
        y_A1A2 = array_y1[0:j]-array_y1[j]      
        x_A2A1 = -1*x_A1A2
        y_A2A1 = -1*y_A1A2
        x_A2B2 = array_x2[0:j]-array_x1[0:j]
        y_A2B2 = array_y2[0:j]-array_y1[0:j]
        x_A1B2 = array_x2[0:j]-array_x1[j]
        y_A1B2 = array_y2[0:j]-array_y1[j]
        x_A2B1 = array_x2[j]-array_x1[0:j]
        y_A2B1 = array_y2[j]-array_y1[0:j]

        p1 = x_A1B1*y_A1A2 - y_A1B1*x_A1A2
        p2 = x_A1B1*y_A1B2 - y_A1B1*x_A1B2
        p3 = x_A2B2*y_A2B1 - y_A2B2*x_A2B1
        p4 = x_A2B2*y_A2A1 - y_A2B2*x_A2A1

        condition_1=p1*p2
        condition_2=p3*p4                         

        for k in xrange (j):
            if condicion_1[k]<=0 and condicion_2[k]<=0:
                M[j,k]=1

    if j+1<N+4:
        x_A1B1 = array_x2[j]-array_x1[j]
        y_A1B1 = array_y2[j]-array_y1[j]
        x_A1A2 = array_x1[j+1:]-array_x1[j]
        y_A1A2 = array_y1[j+1:]-array_y1[j]     
        x_A2A1 = -1*x_A1A2
        y_A2A1 = -1*y_A1A2
        x_A2B2 = array_x2[j+1:]-array_x1[j+1:]
        y_A2B2 = array_y2[j+1:]-array_y1[j+1:]
        x_A1B2 = array_x2[j+1:]-array_x1[j]
        y_A1B2 = array_y2[j+1:]-array_y1[j]
        x_A2B1 = array_x2[j]-array_x1[j+1:]
        y_A2B1 = array_y2[j]-array_y1[j+1:]

        p1 = x_A1B1*y_A1A2 - y_A1B1*x_A1A2
        p2 = x_A1B1*y_A1B2 - y_A1B1*x_A1B2
        p3 = x_A2B2*y_A2B1 - y_A2B2*x_A2B1
        p4 = x_A2B2*y_A2A1 - y_A2B2*x_A2A1

        condicion_1=p1*p2
        condicion_2=p3*p4                         

        for k in xrange (N-j-1):
            if condicion_1[k]<=0 and condicion_2[k]<=0:
                M[j,k+j+1]=1

这里,如果棒i与棒j相交,则元素Mij = 1,否则Mij = 0。

如何优化算法?在这种情况下,图论是有用的吗?怎么样?

等待你的回答。

非常感谢!

祝你好运

0 个答案:

没有答案