【发布时间】:2019-02-27 16:46:37
【问题描述】:
我有一个搜索算法,它查找加法和乘法函数的组合,以从某个数字范围内达到某个数字范围。它正在搜索最短的程序,一个类似于 AAMMA 的程序,其中初始数字相加、相加、相乘、相乘、相加,其中结束数字在 r 到 s 范围内。它必须适用于起始范围 p 到 q 中的每个数字。
输入是 a 和 m,每个函数的相加和乘以 (num+a), (num*m)。我正在做的是尝试每种功能组合,直到找到一个有效的组合,如果它变得太大,则停止该分支。如果我找到有效的“程序”,我会在起始范围内的所有其他数字上尝试该程序。它会这样做,直到它没有找到不超过范围的分支。
我知道搜索不是超级典型,但我认为没有重复的可能性,所以我没有包含找到的列表。
它适用于较小的范围和输入,例如
Problem3("1 2 2 3 10 20")
但是对于更大的范围,我的测试用例只需要永远
Problem3("8 13 28 91 375383947 679472915")
我什至还没有看到完整的。从这里我最好的方法是什么,多线程(希望不是),以某种方式使我的内部功能更快,或者只是刮掉这种方法。
def Problem3(s):
a,m,p,q,r,s = list(map(int, s.split(" ")))
print(str(a) + "-C-" + str(m) + " processor")
print("Input guarenteed between " + str(p) + " and " + str(q))
print("Output is real number between " + str(r) + " and " + str(s))
open_set = queue.Queue()
# curr path depth
open_set.put([p, "", 0])
while not open_set.empty():
subroot = open_set.get()
multiCurr = subroot[0] * m
addCurr = subroot[0] + a
depth = subroot[2] + 1
if r <= addCurr <= s:
truePath = True
#If we find a working path, we need to check if it works for the other things
path = subroot[1] + "A"
for x in range(p, q+1):
for op in path:
if op == "A":
x += a
if op == "M":
x *= m
if r <= x <= s:
pass
else:
truePath = False
break
if truePath:
print("Found " + path + " at depth " + str(depth) + " with starting number " + str(p) + ", output " + str())
if r <= multiCurr <= s:
truePath = True
path = subroot[1] + "M"
for x in range(p, q+1):
for op in path:
if op == "A":
x += a
if op == "M":
x *= m
if r <= x <= s:
pass
else:
truePath = False
break
if truePath:
print("Found " + path + " at depth " + str(depth) + " with starting number " + str(p) + ", output " + str())
if addCurr > s and multiCurr > s:
pass
elif multiCurr > s:
open_set.put([addCurr, subroot[1] + "A", depth])
elif addCurr > s:
open_set.put([multiCurr, subroot[1] + "M", depth])
else:
open_set.put([multiCurr, subroot[1] + "M", depth])
open_set.put([addCurr, subroot[1] + "A", depth])
【问题讨论】:
标签: python python-3.x performance search