Bull's-eye 2020-04-28 00:48 采纳率: 0%
浏览 178

npython(numpy)中,怎么正确使用递归?

假设有一禁忌表(20行8列的矩阵),然后我定义了一个函数,用来随机生成一个向量。现在我想对每次生成的向量进行判断:如果新生成的向量在禁忌表中已经存在,那么就要重新生成。我想用递归来完成这个操作,下面是我写的代码的一部分。

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:
            x = 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 = x
        if x_new in tabu_list:
            create_new_x(t, x)
    return x_new

其中t是一个自然数,每次迭代后t+=1,x就是我生成的向量,这个函数也就是生成向量的函数。tabu_list也就是我说的那个禁忌表,初始情况下tabu_list=[]。
程序执行的时候就报错,报错内容如下:
Traceback (most recent call last):
File "C:/Users/dell/Desktop/SimulatedAnnealing/SA.py", line 1243, in
x_New = create_new_x(t, x)
File "C:/Users/dell/Desktop/SimulatedAnnealing/SA.py", line 1211, in create_new_x
create_new_x(t, x)
File "C:/Users/dell/Desktop/SimulatedAnnealing/SA.py", line 1211, in create_new_x
create_new_x(t, x)
File "C:/Users/dell/Desktop/SimulatedAnnealing/SA.py", line 1211, in create_new_x
create_new_x(t, x)
[Previous line repeated 990 more times]
File "C:/Users/dell/Desktop/SimulatedAnnealing/SA.py", line 1203, in create_new_x
a = np.random.randint(0, 8)
File "mtrand.pyx", line 726, in numpy.random.mtrand.RandomState.randint
File "C:\Program Files\Python37\lib\site-packages\numpy\core_dtype.py", line 347, in name_get
if _name_includes_bit_suffix(dtype):
File "C:\Program Files\Python37\lib\site-packages\numpy\core_dtype.py", line 326, in _name_includes_bit_suffix
elif np.issubdtype(dtype, np.flexible) and _isunsized(dtype):
File "C:\Program Files\Python37\lib\site-packages\numpy\core\numerictypes.py", line 392, in issubdtype
if not issubclass
(arg1, generic):
File "C:\Program Files\Python37\lib\site-packages\numpy\core\numerictypes.py", line 328, in issubclass_
return issubclass(arg1, arg2)
RecursionError: maximum recursion depth exceeded while calling a Python object

请问各位大佬,这样子的递归要怎么写才可以啊

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-09 19:43
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿