-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathknapsack.js
More file actions
143 lines (116 loc) · 3.28 KB
/
knapsack.js
File metadata and controls
143 lines (116 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var inventory = new Array();
var limit = 0;
var upperBoundWorth = 0;
var mode = 0;
var nodeData = function (bitset, totalWeight, totalWorth) {
this.bitset = bitset;
this.totalWeight = totalWeight;
this.totalWorth = totalWorth;
}
var processNode = function(currentNode, take) {
var newBitset = currentNode.bitset.slice(0);
newBitset.push(take);
var newCurrentWeight = currentNode.totalWeight;
var newCurrentWorth = currentNode.totalWorth;
if(take == 1) {
newCurrentWeight += inventory[newBitset.length-1].weight;
newCurrentWorth += inventory[newBitset.length-1].worth;
}
var returnNode = new nodeData(newBitset, newCurrentWeight, newCurrentWorth);
if(newBitset.length == inventory.length) {
if(newCurrentWeight > limit) {
returnNode = new nodeData(newBitset, newCurrentWeight, -1);
}
return returnNode;
} else {
//Mode determines optimization type
if(mode == 1) {
//Optimization #1
//Only go for right children when weight exceeds limit
if(newCurrentWeight > limit) {
return processNode(returnNode, 0);
}
} else if(mode == 2) {
//Optimization #2
//Only go for right children when worth exceeds upperbound
if(newCurrentWeight > limit || newCurrentWorth > upperBoundWorth) {
return processNode(returnNode, 0);
}
}
var t = processNode(returnNode, 1);
var i = processNode(returnNode, 0);
if( t.totalWorth > i.totalWorth) {
return t;
} else {
return i;
}
}
}
exports.clearInventory = function() {
inventory.length = 0;
}
exports.getLimit = function() {
return limit;
}
exports.setNewLimit = function(newLimit) {
limit = newLimit;
}
exports.resetLimit = function() {
setNewLimit(0);
}
exports.setInventory = function(newInventory) {
inventory = newInventory;
}
exports.item = function (weight, worth, name) {
this.weight = weight;
this.worth = worth;
this.ratio = worth/weight;
this.name = name;
}
exports.solveKnapsack = function(newMode) {
// MODES
// 0 - Brute Force Tree
// 1 - Tree Optimized w/ weight optimizations
// 2 - Tree Optimized w/ weight optimizations + greedy upper bound optimizations
// 3 - Greedy
mode = newMode;
if(mode == 2) {
var greedyNode = calculateGreedy();
var factor = limit/greedyNode.totalWeight;
upperBoundWorth = greedyNode.totalWorth * factor;
} else if (mode == 3) {
return calculateGreedy();
}
var firstNodeBitset = new Array();
var firstNode = new nodeData(firstNodeBitset, 0, 0)
var firstNodeTake = processNode(firstNode, 1);
var firstNodeIgnore = processNode(firstNode, 0);
var winningNode;
if(firstNodeTake.totalWorth > firstNodeIgnore.totalWorth) {
winningNode = firstNodeTake;
} else {
winningNode = firstNodeIgnore;
}
return winningNode;
}
//Greedy Implementation
var calculateGreedy = function () {
var totalWeight = 0;
var totalWorth = 0;
var totalBitset = new Array();
inventory.sort(function(a,b) { return parseFloat(b.ratio) - parseFloat(a.ratio)} );
for(var i = 0; i < inventory.length; i++) {
totalWeight += inventory[i].weight;
totalWorth += inventory[i].worth;
totalBitset[i] = 1;
if(totalWeight > limit) {
totalWeight -= inventory[i].weight;
totalWorth -= inventory[i].worth;
totalBitset[i] = 0;
} else if(totalWeight == limit) {
break;
}
}
var returnNode = new nodeData(totalBitset, totalWeight, totalWorth);
return returnNode;
}