Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def makeTheVectors (subjects):
- subjectNames = subjects.keys()
- workvector = []
- valuevector = []
- subjectDic = {}
- subnumber = 0
- for subject in subjectNames:
- subjectDic[subnumber] = subject
- subnumber += 1
- for subject in subjects.values():
- workvector.append(subject[1])
- valuevector.append(subject[0])
- return subjectDic, valuevector, workvector
- def maximumList1 (subjects, maxWork):
- x = {}
- subjectDic, v, w = makeTheVectors(subjects)
- i = len(v) - 1
- subjectsToTake = {}
- print "Value vector:",v
- print "Work vector:",w
- print "initial i =",i
- print "___________________"
- print
- maxValue, subjectVector = maximumList0 (i, x, v, w, maxWork)
- print "Maximum possible class value =",maxValue
- print subjectVector
- def maximumList0 (i, x, v, w, maxWork):
- """Returns a vector of subjects and the value resulting from that vector."""
- print "i=",i
- if i == 0:#then we're at the end of the list.
- if w[i] < maxWork:#then we take it.
- x[i] = 1
- return v[i], x
- else:#then we don't take it.
- x[i] = 0
- return 0, x
- #defining the don't take branch:
- without_i, x = maximumList0 (i-1, x, v, w, maxWork)
- if w[i] > maxWork:#then we can't add this one, nor proceed any further down the take branch.
- x[i] = 0
- return without_i, x
- else:#we choose between the take and don't take branches.
- with_i, x = maximumList0 (i-1, x, v, w, maxWork-w[i])
- with_i += v[i]#this is the value of our take branch.
- if with_i > without_i:#then the "take" branch is better, so we take i.
- x[i] = 1
- return with_i, x
- else:#then the "don't take" branch is better, so we don't take i.
- x[i] = 0
- return without_i, x
Advertisement
Add Comment
Please, Sign In to add comment