def test_knapsack_unbounded(self): # wikipedia example for _unbounded i1 = Item(2, 1) i2 = Item(10, 4) i3 = Item(1, 1) i4 = Item(2, 2) i5 = Item(4, 12) self.assertEqual((36,[3,3,0,0,0]), knapsack_unbounded([i1,i2,i3,i4,i5], 15))
def test_knapsack_01(self): # wikipedia example for _01 # Item (Value, Weight) i1 = Item(2, 1) i2 = Item(10, 4) i3 = Item(1, 1) i4 = Item(2, 2) i5 = Item(4, 12) self.assertEqual((15, [1,1,1,1,0]), knapsack_01([i1,i2,i3,i4,i5], 15))
def test_other_example(self): # http://www.cs.princeton.edu/~wayne/cs423/lectures/approx-alg-4up.pdf # Item (Value, Weight) i1 = Item(1, 1) i2 = Item(6, 2) i3 = Item(18,5) i4 = Item(22,6) i5 = Item(28,7) self.assertEqual((40,[0,0,1,1,0]), knapsack_01([i1,i2,i3,i4,i5], 11))
def test_knapsack_unbounded_for_approx(self): # http://math.stackexchange.com/questions/720001/ # Item (Value, Weight) i1 = Item (90, 9) i2 = Item (19, 2) i3 = Item (1, 1) self.assertEqual((95,[0,5,0]), knapsack_unbounded([i1,i2,i3], 10)) # approximation chooses this one self.assertEqual((91,[1,0,1]), knapsack_approximate([i1,i2,i3], 10))
def trials(): """Conduct timing trials for sample values of W.""" # 103 207 4 2 523 a = 103 b = 207 c = 4 d = 2 items = [] # Item (Value, Weight) for i in range(a, b, c): items.append(Item(i, i)) items.append(Item(d * i + 1, d * i + 1)) maxBestTotal = 0 print('W', 'KnapsackUnboundedTime', 'KnapsackApproximateTime', 'ActualAnswer', 'ApproximateAnswer') diffTotal = 0 W = 10 numReps = 1 numTrials = 1 while W <= 65536: itemSet = 'items=[]\n' for item in items: itemSet = itemSet + 'items.append(Item(' + str( item.value) + ',' + str(item.weight) + '))\n' setup = ''' from adk.knapsack import knapsack_unbounded, knapsack_01, knapsack_approximate, Item, record_best import random\n''' + itemSet + ''' ''' executeUnbound = ''' record_best (knapsack_unbounded(items,''' + str(W) + ''')[0]) ''' totalUnbound = min( timeit.Timer(executeUnbound, setup=setup).repeat(numReps, numTrials)) executeApproximate = ''' record_best (knapsack_approximate(items,''' + str(W) + ''')[0]) ''' totalApproximate = min( timeit.Timer(executeApproximate, setup=setup).repeat(numReps, numTrials)) print(W, '{0:.5f}'.format(totalUnbound), '{0:.5f}'.format(totalApproximate), record_best()) W = W * 2 + 1 if diffTotal > maxBestTotal: print(a, b, c, d, diffTotal) maxBestTotal = diffTotal
def reportOne(): """ To see the generates matrices, modify the knapsack methods temporarily to print out the matrices. """ items = [Item(4, 4), Item(8, 8), Item(9, 9), Item(10, 10)] W = 33 print("Knapsack Unbounded") w_un = knapsack_unbounded(items[:], W) print(w_un) print("Knapsack Approximate") w_ap = knapsack_approximate(items[:], W) print(w_ap) print("Knapsack 0/1") w_01 = knapsack_01(items[:], W) print(w_01)
def trials(): """ Locate knapsack items that has different results for : 1) knapsack_unbounded 2) knapsack_01 3) knapsack_approximate Found this nice example: W = 33 (33, [6, 0, 0, 1]) (32, [8, 0, 0, 0]) (31, [1, 1, 1, 1]) (val= 4 , weight= 4 (val= 8 , weight= 8 (val= 10 , weight= 10 (val= 9 , weight= 9 """ # 103 207 4 2 523 for t in range(10000): items = [] # Item (Value, Weight) for _ in range(5): i = random.randint(3, 11) items.append(Item(i, i)) W = 33 w_un = knapsack_unbounded(items[:], W) w_ap = knapsack_approximate(items[:], W) w_01 = knapsack_01(items[:], W) if w_un[0] != w_ap[0] and w_ap[0] != w_01[0] and w_un[0] != w_01[0]: print(w_un, w_ap, w_01) for i in items: print('(val=', i.value, ', weight=', i.weight) return
def trials(): """ Search small space to determine input set to knapsack that offers greatest difference between dynamic programming and approximate. Once computed, use these values in adk.book.chapter11.py """ # 83 250 4 2 457 # 103 207 4 2 523 a = 23 b = 56 c = 8 d = 5 maxBestTotal = 0 for a in range(23, 113, 10): for b2 in range(1, 8): b = a * b2 + 1 for c in [4, 8, 16, 32, 64]: for d in range(2, 7): diffTotal = 0 W = 10 numReps = 1 numTrials = 1 while W <= 65536: items = [] # Item (Value, Weight) for i in range(a, b, c): items.append(Item(i, i)) items.append(Item(d * i + 1, d * i + 1)) itemSet = 'items=[]\n' for item in items: itemSet = itemSet + 'items.append(Item(' + str( item.value) + ',' + str(item.weight) + '))\n' #itemSet = itemSet + 'items.append(Item(' + str(W//4) + "," + str(W//4) + '))\n' setup = ''' from adk.knapsack import knapsack_unbounded, knapsack_01, knapsack_approximate, Item, record_best import random\n''' + itemSet + ''' ''' executeUnbound = ''' record_best (knapsack_unbounded(items,''' + str(W) + ''')[0]) ''' totalUnbound = min( timeit.Timer(executeUnbound, setup=setup).repeat( numReps, numTrials)) executeApproximate = ''' record_best (knapsack_approximate(items,''' + str(W) + ''')[0]) ''' totalApproximate = min( timeit.Timer(executeApproximate, setup=setup).repeat( numReps, numTrials)) #print (W, totalUnbound, totalApproximate, record_best()) best2 = record_best() if len(best2) > 0: diffTotal += best2[0] - best2[1] W = W * 2 + 1 if diffTotal > maxBestTotal: print(a, b, c, d, diffTotal) maxBestTotal = diffTotal