0
votes
import sys
import random
import numpy as np
import matplotlib.pyplot as plt
sys.setrecursionlimit(10**7)

nL,nC=100,100
a,b=50,50
c = 5
n=0
L=np.zeros((nL,nC))
T = []
def case_prox(L1:list,i,j):   
    l=len(L1)
    return [[L1[(i+1)%l][j],(i+1)%l,j],[L1[(i-1)%l][j],(i-1)%l,j],[L1[i][(j+1)%l],i,(j+1)%l],[L1[i][(j-1)%l],i,(j-1)%l]]

def avancer(L,i,j):
    global n,S,T,p
    S = []
    p = 0
    while n<150:#global dans le while
        a = random.randrange(0,len(case_prox(L,i,j)))
        
        if L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] == 0:
            L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] = 1
            n+=1
            avancer(L,50,50)
            S.append(p+1)
        else:
            avancer(L,case_prox(L,i,j)[a][1],case_prox(L,i,j)[a][2])
            n+=1
            p+=1
    print(L)
                
                

L[50][50]=1

print(case_prox(L,30,30))

print(case_prox(L,30,30)[0][1],case_prox(L,30,30)[0][2])

avancer(L,50,50)
plt.plot(S)

I made an algorithm based on Internal Diffusion Limited Aggregation. The principal function is recursive so I have to call the function a lot of time. I decide to increase the recursion limitnevertheless the problem is not solved. The function works for a long time and do a role. Please Can you help me?

1
What do you mean by "do a role"? When you say "the problem is not solved," do you mean that you run out of stack? Or does it simply take indefinite time? (In the latter case, the stack depth limit does not matter.) - DYZ
The base case for the recursion is missing. In the while loop, you call the recursive method if or else, in any case. And there is no base case to stop recursion before the while loop. - Adam Nyx

1 Answers

0
votes

I think you want to iterate n prior to calling avancer in the else, otherwise you risk an infinite loop...

def avancer(L,i,j):
    global n,S,T,p
    S = []
    p = 0
    while n<150:#global dans le while
        a = random.randrange(0,len(case_prox(L,i,j)))
        
        if L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] == 0:
            L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] = 1
            n+=1
            avancer(L,50,50)
            S.append(p+1)
        else:
            n+=1
            avancer(L,case_prox(L,i,j)[a][1],case_prox(L,i,j)[a][2])
            p+=1
    print(L)