我定义了一个根据原向量生成新向量的函数,然后也定义了一个禁忌表,我想让每次新生成的向量都不属于禁忌表中,否则就要重新去生成新的向量。
下面代码就是我定义的函数:
import numpy as np
def create_new_x(t, x):
if 0 <= t < 30:
s = []
while len(s) < 6:
a = np.random.randint(0, 8)
if a not in s:
s.append(a)
x[s[0]], x[s[1]] = x[s[1]], x[s[0]]
x[s[2]], x[s[3]] = x[s[3]], x[s[2]]
x[s[4]], x[s[5]] = x[s[5]], x[s[4]]
x_new = x
if x in tabu_list:
create_new_x(t, x)
elif 30 <= t < 70:
s = []
while len(s) < 4:
a = np.random.randint(0, 8)
if a not in s:
s.append(a)
x[s[0]], x[s[1]] = x[s[1]], x[s[0]]
x[s[2]], x[s[3]] = x[s[3]], x[s[2]]
x_new = x
if x in tabu_list:
create_new_x(t, x)
else:
s = []
while len(s) < 4:
a = np.random.randint(0, 8)
if a not in s:
s.append(a)
x[s[0]], x[s[1]] = x[s[1]], x[s[0]]
x[s[2]], x[s[3]] = x[s[3]], x[s[2]]
x_new = x
if x in tabu_list:
create_new_x(t, x)
return x_new
其中,t只是一个自然增长的数,根据迭代次数来变化,x是一向量,比如x = [1,2,3,4,5,6,7,8],“tabu_list”就是我定义的禁忌表(一个20行8列的矩阵,每一行也是类似于x的向量,每行中的各个元素互不相同),然后执行的时候,就会报错,报错的最后一行如下。
RecursionError: maximum recursion depth exceeded while calling a Python object。
然而我用dubug时,好像又不会报错,请问各位大佬,这种情况是因为什么造成的,是我的递归写的不对还是因为其他的?