【问题标题】:Why does my Dynamic Programming Approach to the SSP always work?为什么我的 SSP 动态编程方法总是有效?
【发布时间】:2017-01-06 01:01:05
【问题描述】:

为什么我的代码 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)

【问题讨论】:

  • 我的第一印象是,由于 SSP.__init__() 中存在默认值,因此出现了问题。但是,当我粘贴您的示例代码时,它会在 try_at_random if (S[n-1] > sum): TypeError: unorderable types: int() > builtin_function_or_method() 中抛出异常第 41 行,我猜“总和”应该是是总和(某事)?

标签: python dynamic-programming subset-sum


【解决方案1】:
  1. 我不认为 sum 是在您的方法 try_at_random(self, S, n, t) 中定义的,如关于第 41 行的评论中所述。

  2. 在“if”语句中使用“instance”的地方是指“self”吗?我也不确定那里是否定义了实例。

  3. 当你返回那个“或”(返回 instance.try_at_random(S, n-1, t) 或 instance.try_at_random(S, n-1, t-S[n-1]))时,你是什么期待?如果您在返回之前执行打印语句以查看其评估结果,您会得到什么。我知道您正在尝试递归调用它,但是在不使用“self.try_at_random”和返回“或”语句之间,我不确定这是在做什么。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 2011-04-24
    • 2019-08-02
    • 1970-01-01
    • 2016-11-11
    • 2011-12-08
    • 2012-09-22
    • 2020-08-10
    相关资源
    最近更新 更多