Guest User

Untitled

a guest
Mar 6th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. def makeTheVectors (subjects):
  2.     subjectNames = subjects.keys()
  3.     workvector = []
  4.     valuevector = []
  5.     subjectDic = {}
  6.     subnumber = 0
  7.     for subject in subjectNames:
  8.         subjectDic[subnumber] = subject
  9.         subnumber += 1
  10.     for subject in subjects.values():
  11.         workvector.append(subject[1])
  12.         valuevector.append(subject[0])
  13.     return subjectDic, valuevector, workvector
  14.  
  15. def maximumList1 (subjects, maxWork):
  16.     x = {}
  17.     subjectDic, v, w = makeTheVectors(subjects)
  18.     i = len(v) - 1
  19.     subjectsToTake = {}
  20.     print "Value vector:",v
  21.     print "Work  vector:",w
  22.     print "initial i =",i
  23.     print "___________________"
  24.     print
  25.     maxValue, subjectVector = maximumList0 (i, x, v, w, maxWork)
  26.     print "Maximum possible class value =",maxValue
  27.     print subjectVector
  28.  
  29. def maximumList0 (i, x, v, w, maxWork):
  30.     """Returns a vector of subjects and the value resulting from that vector."""
  31.     print "i=",i
  32.     if i == 0:#then we're at the end of the list.
  33.         if w[i] < maxWork:#then we take it.
  34.             x[i] = 1
  35.             return v[i], x
  36.         else:#then we don't take it.
  37.             x[i] = 0
  38.             return 0, x
  39.     #defining the don't take branch:
  40.     without_i, x = maximumList0 (i-1, x, v, w, maxWork)
  41.     if w[i] > maxWork:#then we can't add this one, nor proceed any further down the take branch.
  42.         x[i] = 0
  43.         return without_i, x
  44.     else:#we choose between the take and don't take branches.
  45.         with_i, x = maximumList0 (i-1, x, v, w, maxWork-w[i])
  46.         with_i += v[i]#this is the value of our take branch.
  47.     if with_i > without_i:#then the "take" branch is better, so we take i.
  48.         x[i] = 1
  49.         return with_i, x
  50.     else:#then the "don't take" branch is better, so we don't take i.
  51.         x[i] = 0
  52.         return without_i, x
Advertisement
Add Comment
Please, Sign In to add comment