【发布时间】:2020-06-13 13:17:43
【问题描述】:
我正在尝试为最简单可行的Golomb Ruler 构建一个启发式算法。从 0 到 n,找到 n 个数,使得它们之间的所有差异都不同。这种启发式方法包括每次将标尺增加 1。如果列表中已经存在差异,则跳转到下一个整数。所以标尺以 [0,1] 开头,差异列表 = [ 1 ]。然后我们尝试将 2 加到标尺 [0,1,2] 上,但这不可行,因为差异 (2-1 = 1) 已经存在于差异列表中。然后我们尝试将 3 添加到标尺 [0,1,3] 上,这是可行的,因此差异列表变为 [1,2,3] 等等。到目前为止,这是我所做的:
n = 5
positions = list(range(1,n+1))
Pos = []
Dist = []
difs = []
i = 0
while (i < len(positions)):
if len(Pos)==0:
Pos.append(0)
Dist.append(0)
elif len(Pos)==1:
Pos.append(1)
Dist.append(1)
else:
postest = Pos + [i] #check feasibility to enter the ruler
difs = [a-b for a in postest for b in postest if a > b]
if any (d in difs for d in Dist)==True:
pass
else:
for d in difs:
Dist.append(d)
Pos.append(i)
i += 1
但是,我无法使差异检查起作用。有什么建议吗?
【问题讨论】:
标签: python python-3.x heuristics