为什么我对SSP的动态编程方法始终有效?

时间:2017-01-06 01:01:05

标签: python dynamic-programming subset-sum

为什么我的代码在100%的时间内都能成功?我想生成此代码输出的100个随机样本来测量速度,但每次运行时我都会收到100个真实结果和0个错误结果。有人可以给我一些建议吗?

import random
from random import randint, sample
from itertools import chain, combinations
import time

class SSP():
    def __init__(self, S=[], t=0):
        self.S = S
        self.t = t
        self.n = len(S)
        #
        self.decision = False
        self.total    = 0
        self.selected = []

    def __repr__(self):
        return "SSP instance: S="+str(self.S)+"\tt="+str(self.t)

    def random_instance(self, n, bitlength=10):
        max_n_bit_number = 2**bitlength-1
        self.S = sorted([randint(0,max_n_bit_number) for i in range(n)], reverse=True)
        self.t = randint(0,n*max_n_bit_number)
        self.n = len(self.S)

    def random_yes_instance(self, n, bitlength=10):
        max_n_bit_number = 2**bitlength-1
        self.S = sorted([randint(0,max_n_bit_number) for i in range(n)], reverse=True)
        self.t = sum(sample(self.S, randint(0,n)))
        self.n = len(self.S)

    def try_at_random(self, S, n, t):
        #if sum is 0, use empty set as our solution
        if (t == 0):
          return True
          print("Found a subset with given sum")
        #if n is 0 and sum is not 0, no solution possible
        if (n == 0 and t != 0):
          return False
          print("No subset within given sum")

        if (S[n-1] > sum):
          return instance.try_at_random(S, n-1, t)
        else:
          return instance.try_at_random(S, n-1, t) or instance.try_at_random(S, n-1, t-S[n-1])

i=0
tr = 0
fa = 0
instance = SSP()

for i in range (0, 100):
    instance.random_yes_instance(4)
    print(instance)

    start_time = time.time()

    if (instance.try_at_random(instance.S, instance.n, instance.t) == True):
      print("Found a subset with given sum")
      tr += 1
    else:
      print("No subset within given sum")
      fa += 1

    time_after = time.time() - start_time
    print ("Time taken: " +str(time_after)+"s")
    i+=1
print ("Times succeeded: ", tr)
print ("Times failed: ", fa)

下面是示例输出:

SSP instance: S=[754, 429, 232, 131]    t=131
Found a subset with given sum
Time taken: 0.0s
SSP instance: S=[954, 903, 768, 184]    t=0
Found a subset with given sum
Time taken: 0.0s
SSP instance: S=[871, 532, 495, 337]    t=0
Found a subset with given sum
Time taken: 0.0s
SSP instance: S=[1011, 837, 599, 559]   t=599
Found a subset with given sum
Time taken: 0.0s
SSP instance: S=[571, 306, 181, 121]    t=0
Found a subset with given sum
Time taken: 0.0s
SSP instance: S=[807, 284, 220, 71] t=1162
Found a subset with given sum
Time taken: 0.0s
('Times succeeded: ', 100)
('Times failed: ', 0)

1 个答案:

答案 0 :(得分:0)

  1. 我不认为sum是在你的方法try_at_random(self,S,n,t)中定义的,正如关于第41行的评论所述。

  2. 如果你在“if”语句中使用“instance”,你的意思是“自我”吗?我也不确定是否在那里定义了实例。

  3. 当你返回“或”(返回instance.try_at_random(S,n-1,t)或instance.try_at_random(S,n-1,tS [n-1]))时,你是什么期待?如果你在返回之前做一个打印声明,看看正在评估什么,你会得到什么。我知道你试图递归地调用它,但是在不使用“self.try_at_random”并返回“或”语句之间我不确定它在做什么。

  4. 祝你好运!