DavidNorgren

Untitled

Jan 29th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. from collections import namedtuple
  2. import queue
  3. import time;
  4.  
  5. Item = namedtuple("Item", "benefit weight")
  6. Node = namedtuple("Node", "addedItems totalBenefit totalWeight")
  7.  
  8. # Appends a value to the end of a list and returns the new list
  9. def apnd(lst, item):
  10.     nlst = list(lst)
  11.     nlst.append(item)
  12.     return nlst
  13.  
  14. # Breadth-first search
  15. def solveKnapsackBFS(items, maxWeight):
  16.     root = Node([], 0, 0)
  17.     bestSolution = root
  18.     nodeQueue = queue.Queue()
  19.     nodeQueue.put(root)
  20.  
  21.     while not nodeQueue.empty():
  22.         n = nodeQueue.get()
  23.         if (n.totalBenefit > bestSolution.totalBenefit):
  24.             bestSolution = n
  25.         for i in items:
  26.             if i not in n.addedItems and n.totalWeight + i.weight < maxWeight:
  27.                 nodeQueue.put(Node(apnd(n.addedItems, i), n.totalBenefit + i.benefit, n.totalWeight + i.weight))
  28.  
  29.     return bestSolution
  30.  
  31. # Depth-first search
  32. def solveKnapsackDFS(items, maxWeight):
  33.     root = Node([], 0, 0)
  34.     global bestSolution
  35.     bestSolution = root
  36.  
  37.     def rec(n):
  38.         global bestSolution
  39.         if (n.totalBenefit > bestSolution.totalBenefit):
  40.             bestSolution = n
  41.         for i in items:
  42.             if i not in n.addedItems and n.totalWeight + i.weight < maxWeight:
  43.                 rec(Node(apnd(n.addedItems, i), n.totalBenefit + i.benefit, n.totalWeight + i.weight))
  44.  
  45.     rec(root)
  46.  
  47.     return bestSolution
  48.  
  49. # Run both functions on a set of items and measure time
  50. items = [
  51.     Item(20, 15),
  52.     Item(40, 32),
  53.     Item(50, 60),
  54.     Item(36, 80),
  55.     Item(25, 43),
  56.     Item(64, 120),
  57.     Item(54, 77),
  58.     Item(18, 6),
  59.     Item(46, 93),
  60.     Item(28, 35)
  61.     ]
  62.  
  63. print("BFS...")
  64. startTime = time.time()
  65. solutionBFS = solveKnapsackBFS(items, 420)
  66. endTime = time.time()
  67. print("Best solution was the following sequence")
  68. for i in solutionBFS.addedItems:
  69.     print("%d %d" % (i.benefit, i.weight))
  70. print("Total benefit was %d, total weight %d" % (solutionBFS.totalBenefit, solutionBFS.totalWeight))
  71. print("Time taken: %.2fs" % (endTime - startTime))
  72.  
  73. print("DFS...")
  74. startTime = time.time()
  75. solutionDFS = solveKnapsackDFS(items, 420)
  76. endTime = time.time()
  77. print("Best solution was the following sequence")
  78. for i in solutionDFS.addedItems:
  79.     print("%d %d" % (i.benefit, i.weight))
  80. print("Total benefit was %d, total weight %d" % (solutionDFS.totalBenefit, solutionDFS.totalWeight))
  81. print("Time taken: %.2fs" % (endTime - startTime))
Advertisement
Add Comment
Please, Sign In to add comment