我尝试制作一种实现模式搜索(优化)的算法。我差不多完成了。运行我的算法我看到值如何收敛到函数的最小值。我遇到的唯一问题是这个臭名昭着的错误:'NoneType' object is not iterable
。我的主要功能如下:
def exploresearch(xk, yk, hx, hy, lamb, theta, curval):
if hx < theta and hy < theta:
return [xk, yk]
else:
while hx > theta and hy > theta:
# шаг вправо
if broyden(xk + hx, yk) < curval:
# шаг вверх
if broyden(xk + hx, yk + hy) < curval:
xn = xk + lamb * hx
yn = yk + lamb * hy
xk = xk + hx
yk = yk + hy
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
#repeatexplore(xk, yk, hx, hy, lamb, theta, "+", "+")
# шаг вниз
elif broyden(xk + hx, yk - hy) < curval:
xn = xk + lamb * hx
yn = yk - lamb * hy
xk = xk + hx
yk = yk - hy
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
#repeatexplore(xk, yk, hx, hy, lamb, theta, "+", "-")
# остаемся
else:
#поиск по образцу
# (xn, yn) - вершина, в которой делаем поиск по образцу
hy = hy / 2
xn = xk + lamb * hx
yn = yk
xk = xk + hx
yk = yk
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
#repeatexplore(xk, yk, hx, hy, lamb, theta, "+", "")
# шаг влево
elif broyden(xk - hx, yk) < curval:
# шаг вверх
if broyden(xk - hx, yk + hy) < curval:
xn = xk - lamb * hx
yn = yk + lamb * hy
xk = xk - hx
yk = yk + hy
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
#repeatexplore(xk, yk, hx, hy, lamb, theta, "-", "+")
# шаг вниз
elif broyden(xk - hx, yk - hy) < curval:
xn = xk - lamb * hx
yn = yk - lamb * hy
xk = xk - hx
yk = yk - hy
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
#repeatexplore(xk, yk, hx, hy, lamb, theta, "-", "-")
# остаемся
else:
#поиск по образцу
# (xn, yn) - вершина, в которой делаем поиск по образцу
hy = hy / 2
xn = xk - lamb * hx
yn = yk
xk = xk - hx
yk = yk
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
#repeatexplore(xk, yk, hx, hy, lamb, theta, "-", "")
# шаг вверх
elif broyden(xk, yk + hy) < curval:
hx = hx / 2
xn = xk
yn = yk + lamb * hy
xk = xk
yk = yk + hy
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
#repeatexplore(xk, yk, hx, hy, lamb, theta, "", "+")
# шаг вниз
elif broyden(xk, yk - hy) < curval:
hx = hx / 2
xn = xk
yn = yk - lamb * hy
xk = xk
yk = yk - hy
newval = broyden(xk, yk)
xs, ys, flag = simpleexplore(xn, yn, hx, hy, lamb, theta, newval)
if flag:
xk, yk = repeatpattern(xk, yk, hx, hy, xs, ys, lamb, theta)
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
else:
hx = hx / 2
hy = hy / 2
xk, yk = exploresearch(xk, yk, hx, hy, lamb, theta, broyden(xk, yk))
将一些print语句调试,我看到这个错误在while循环之前弹出。就在此之前,我打印hy
和hx
值 - 它们看起来很正常,只是普通的浮点数。所以,此时我不知道该怎么做。
答案 0 :(得分:1)
如果您的代码未通过第一次测试并进入while循环,则您永远不会返回任何内容。如果你没有返回任何东西,那么这样的陈述:
xk, yk = exploresearch(...)
...会抛出你说的错误。这是因为exploresearch(...)
返回None
,而xk, yk = None
正在尝试迭代None
(NoneType
类型的对象)。
您需要确保代码的else
部分返回None
以外的其他内容。