|
| 1 | +import MergeSort from '../../sorting/merge-sort/MergeSort'; |
| 2 | + |
| 3 | +export default class Knapsack { |
| 4 | + /** |
| 5 | + * @param {KnapsackItem[]} possibleItems |
| 6 | + * @param {number} weightLimit |
| 7 | + */ |
| 8 | + constructor(possibleItems, weightLimit) { |
| 9 | + this.selectedItems = []; |
| 10 | + this.weightLimit = weightLimit; |
| 11 | + this.possibleItems = possibleItems; |
| 12 | + // We do two sorts because in case of equal weights but different values |
| 13 | + // we need to take the most valuable items first. |
| 14 | + this.sortPossibleItemsByValue(); |
| 15 | + this.sortPossibleItemsByWeight(); |
| 16 | + } |
| 17 | + |
| 18 | + sortPossibleItemsByWeight() { |
| 19 | + // Sort possible items by their weight. |
| 20 | + // We need them to be sorted in order to solve knapsack problem using |
| 21 | + // Dynamic Programming approach. |
| 22 | + this.possibleItems = new MergeSort({ |
| 23 | + /** |
| 24 | + * @var KnapsackItem itemA |
| 25 | + * @var KnapsackItem itemB |
| 26 | + */ |
| 27 | + compareCallback: (itemA, itemB) => { |
| 28 | + if (itemA.weight === itemB.weight) { |
| 29 | + return 0; |
| 30 | + } |
| 31 | + |
| 32 | + return itemA.weight < itemB.weight ? -1 : 1; |
| 33 | + }, |
| 34 | + }).sort(this.possibleItems); |
| 35 | + } |
| 36 | + |
| 37 | + sortPossibleItemsByValue() { |
| 38 | + // Sort possible items by their weight. |
| 39 | + // We need them to be sorted in order to solve knapsack problem using |
| 40 | + // Dynamic Programming approach. |
| 41 | + this.possibleItems = new MergeSort({ |
| 42 | + /** |
| 43 | + * @var KnapsackItem itemA |
| 44 | + * @var KnapsackItem itemB |
| 45 | + */ |
| 46 | + compareCallback: (itemA, itemB) => { |
| 47 | + if (itemA.value === itemB.value) { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + return itemA.value > itemB.value ? -1 : 1; |
| 52 | + }, |
| 53 | + }).sort(this.possibleItems); |
| 54 | + } |
| 55 | + |
| 56 | + // Solve 0/1 knapsack problem using dynamic programming. |
| 57 | + solveZeroOneKnapsackProblem() { |
| 58 | + this.selectedItems = []; |
| 59 | + |
| 60 | + // Create knapsack values matrix. |
| 61 | + const numberOfRows = this.possibleItems.length; |
| 62 | + const numberOfColumns = this.weightLimit; |
| 63 | + const knapsackMatrix = Array(numberOfRows).fill(null).map(() => { |
| 64 | + return Array(numberOfColumns + 1).fill(null); |
| 65 | + }); |
| 66 | + |
| 67 | + // Fill the first column with zeros since it would mean that there is |
| 68 | + // no items we can add to knapsack in case if weight limitation is zero. |
| 69 | + for (let itemIndex = 0; itemIndex < this.possibleItems.length; itemIndex += 1) { |
| 70 | + knapsackMatrix[itemIndex][0] = 0; |
| 71 | + } |
| 72 | + |
| 73 | + // Fill the first row with max possible values we would get by just adding |
| 74 | + // or not adding the first item to the knapsack. |
| 75 | + for (let weightIndex = 1; weightIndex <= this.weightLimit; weightIndex += 1) { |
| 76 | + const itemIndex = 0; |
| 77 | + const itemWeight = this.possibleItems[itemIndex].weight; |
| 78 | + const itemValue = this.possibleItems[itemIndex].value; |
| 79 | + knapsackMatrix[itemIndex][weightIndex] = itemWeight <= weightIndex ? itemValue : 0; |
| 80 | + } |
| 81 | + |
| 82 | + // Go through combinations of how we may add items to knapsack and |
| 83 | + // define what weight/value we would receive using Dynamic Programming |
| 84 | + // approach. |
| 85 | + for (let itemIndex = 1; itemIndex < this.possibleItems.length; itemIndex += 1) { |
| 86 | + for (let weightIndex = 1; weightIndex <= this.weightLimit; weightIndex += 1) { |
| 87 | + const currentItemWeight = this.possibleItems[itemIndex].weight; |
| 88 | + const currentItemValue = this.possibleItems[itemIndex].value; |
| 89 | + |
| 90 | + if (currentItemWeight > weightIndex) { |
| 91 | + // In case if item's weight is bigger then currently allowed weight |
| 92 | + // then we can't add it to knapsack and the max possible value we can |
| 93 | + // gain at the moment is the max value we got for previous item. |
| 94 | + knapsackMatrix[itemIndex][weightIndex] = knapsackMatrix[itemIndex - 1][weightIndex]; |
| 95 | + } else { |
| 96 | + // Else we need to consider the max value we can gain at this point by adding |
| 97 | + // current value or just by keeping the previous item for current weight. |
| 98 | + knapsackMatrix[itemIndex][weightIndex] = Math.max( |
| 99 | + currentItemValue + knapsackMatrix[itemIndex - 1][weightIndex - currentItemWeight], |
| 100 | + knapsackMatrix[itemIndex - 1][weightIndex], |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Now let's trace back the knapsack matrix to see what items we're going to add |
| 107 | + // to the knapsack. |
| 108 | + let itemIndex = this.possibleItems.length - 1; |
| 109 | + let weightIndex = this.weightLimit; |
| 110 | + |
| 111 | + while (itemIndex > 0) { |
| 112 | + const currentItem = this.possibleItems[itemIndex]; |
| 113 | + const prevItem = this.possibleItems[itemIndex - 1]; |
| 114 | + |
| 115 | + // Check if matrix value came from top (from previous item). |
| 116 | + // In this case this would mean that we need to include previous item |
| 117 | + // to the list of selected items. |
| 118 | + if ( |
| 119 | + knapsackMatrix[itemIndex][weightIndex] && |
| 120 | + knapsackMatrix[itemIndex][weightIndex] === knapsackMatrix[itemIndex - 1][weightIndex] |
| 121 | + ) { |
| 122 | + // Check if there are several items with the same weight but with the different values. |
| 123 | + // We need to add highest item in the matrix that is possible to get the highest value. |
| 124 | + const prevSumValue = knapsackMatrix[itemIndex - 1][weightIndex]; |
| 125 | + const prevPrevSumValue = knapsackMatrix[itemIndex - 2][weightIndex]; |
| 126 | + if ( |
| 127 | + !prevSumValue || |
| 128 | + (prevSumValue && prevPrevSumValue !== prevSumValue) |
| 129 | + ) { |
| 130 | + this.selectedItems.push(prevItem); |
| 131 | + } |
| 132 | + } else if (knapsackMatrix[itemIndex - 1][weightIndex - currentItem.weight]) { |
| 133 | + this.selectedItems.push(prevItem); |
| 134 | + weightIndex -= currentItem.weight; |
| 135 | + } |
| 136 | + |
| 137 | + itemIndex -= 1; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + get totalValue() { |
| 142 | + /** @var {KnapsackItem} item */ |
| 143 | + return this.selectedItems.reduce((accumulator, item) => { |
| 144 | + return accumulator + item.totalValue; |
| 145 | + }, 0); |
| 146 | + } |
| 147 | + |
| 148 | + get totalWeight() { |
| 149 | + /** @var {KnapsackItem} item */ |
| 150 | + return this.selectedItems.reduce((accumulator, item) => { |
| 151 | + return accumulator + item.totalWeight; |
| 152 | + }, 0); |
| 153 | + } |
| 154 | +} |
0 commit comments