Guest User

Untitled

a guest
May 4th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #
  2. # Problem 4: Subject Selection By Dynamic Programming
  3. #
  4.  
  5. bSubset = []
  6. bValue = 0
  7.  
  8.  
  9. def dpAdvisorHelper(w,v,i,aW,m):
  10. ##Go down branch, hold subjects, compare to bestSubset.
  11. ##Let's hope this works.
  12. ##Also, this is proof that programming is best late at night, lol
  13. global bSubset
  14. global bValue
  15.  
  16. try: return m[(i,aW)]
  17. except KeyError:
  18.  
  19. if i == 0: #Base case
  20. if bSubset == []: bSubset = [0]
  21. if w[i] <= aW: #Take
  22. m[(i,aW)] = v[i],[i]
  23. return v[i],[i]
  24. else: #Don't take
  25. m[(i,aW)] = 0,[]
  26. return 0,[]
  27.  
  28. ##Going down the branches
  29. without_iS = []
  30. with_iS = []
  31. without_i,without_iS = dpAdvisorHelper(w,v,i-1,aW,m)
  32. if w[i] > aW: #No space left, can't include i
  33. m[(i,aW)] = without_i, without_iS[:]
  34. return without_i,without_iS[:]
  35. with_i,with_iS = dpAdvisorHelper(w,v,i-1,aW-w[i],m)
  36. with_i += v[i] #Adds the value of i
  37. with_iS.append(i) #Adds i to subset
  38.  
  39. ##Choose new bSubset, if a larger value
  40. if max(with_i,without_i) >= bValue:
  41. print bSubset,bValue
  42. if with_i >= without_i:
  43. m[(i,aW)] = with_i, with_iS[:]
  44. bValue = with_i
  45. bSubset = with_iS[:]
  46. return with_i,with_iS[:]
  47. else:
  48. m[(i,aW)] = without_i,without_iS[:]
  49. bValue = without_i
  50. bSubset = without_iS[:]
  51. return without_i,without_iS[:]
  52. else: #Subset is less than bSubset
  53. if with_i >= without_i:
  54. m[(i,aW)] = with_i,with_iS[:]
  55. return with_i,with_iS[:]
  56. else:
  57. m[(i,aW)] = without_i,with_iS[:]
  58. return without_i,without_iS[:]
  59.  
  60. def dpAdvisor(subjects, maxWork):
  61. """
  62. Returns a dictionary mapping subject name to (value, work) that contains a
  63. set of subjects that provides the maximum value without exceeding maxWork.
  64.  
  65. subjects: dictionary mapping subject name to (value, work)
  66. maxWork: int >= 0
  67. returns: dictionary mapping subject name to (value, work)
  68. """
  69. # TODO...
  70. global bSubset
  71. global bValue
  72.  
  73. v = []
  74. w = []
  75. subjectList = []
  76. for a in subjects:
  77. v.append(subjects[a][VALUE])
  78. w.append(subjects[a][WORK])
  79. subjectList.append(a)
  80. i = len(subjectList)-1
  81. aW = maxWork
  82. m = {}
  83. value,bSubset = dpAdvisorHelper(w,v,i,aW,m)
  84. print bSubset
  85. print bValue
  86.  
  87. schedule = {}
  88. for a in bSubset:
  89. schedule[subjectList[a]] = subjects[subjectList[a]]
  90. return schedule
  91.  
  92. #
  93. # Problem 5: Performance Comparison
  94. #
  95. def dpTime():
  96. """
  97. Runs tests on dpAdvisor and measures the time required to compute an
  98. answer.
  99. """
  100. # TODO...
  101.  
  102. #lol = bruteForceAdvisor(SUBJECTS, 8)
  103. ## 8 = 9.5 seconds
  104. ## 9 = 26 seconds
  105. ## 10 = 71 seconds
  106. ## 11 = 184 seconds
  107. #printSubjects(lol)
  108. lol = dpAdvisor(SUBJECTS, 8)
  109. printSubjects(lol)
Advertisement
Add Comment
Please, Sign In to add comment