-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
knapsack item settings ignores itemsInStock #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
bug
Something isn't working
Comments
the Unbounded knapsack problem ignores the total number of itemsInStock const possibleKnapsackItems = [
new KnapsackItem({ value: 1, weight: 1, itemsInStock: 10 }),
new KnapsackItem({ value: 100, weight: 100, itemsInStock: 100 }),
new KnapsackItem({ value: 50, weight: 50, itemsInStock: 2 }),
new KnapsackItem({ value: 1000, weight: 1000, itemsInStock: 5 }),
];
const maxKnapsackWeight = 76;
const knapsack = new Knapsack(possibleKnapsackItems, maxKnapsackWeight);
knapsack.solveUnboundedKnapsackProblem();
console.log(knapsack.totalValue);
console.log(knapsack.totalWeight);
console.log(knapsack.selectedItems.length);
console.log(knapsack.selectedItems[0]);
console.log(knapsack.selectedItems[1]);
76
76
2
KnapsackItem { value: 1, weight: 1, itemsInStock: 10, quantity: 26 }
KnapsackItem { value: 50, weight: 50, itemsInStock: 2, quantity: 1 } |
will fix as below , I will make e pr: solveUnboundedKnapsackProblem() {
this.sortPossibleItemsByValue();
this.sortPossibleItemsByValuePerWeightRatio();
for (let itemIndex = 0; itemIndex < this.possibleItems.length; itemIndex += 1) {
if (this.totalWeight < this.weightLimit) {
const currentItem = this.possibleItems[itemIndex];
// Detect how much of current items we can push to knapsack.
// if(currentItem.quantity == currentItem.itemsInStock) return;
const availableWeight = this.weightLimit - this.totalWeight;
const maxPossibleItemsCount = Math.floor(availableWeight / currentItem.weight);
if (maxPossibleItemsCount) {
if(maxPossibleItemsCount <= currentItem.itemsInStock){
currentItem.quantity = maxPossibleItemsCount;
}else{
currentItem.quantity = currentItem.itemsInStock;
}
this.selectedItems.push(currentItem);
}
}
}
} |
Nice catch @shsina. Waiting for PR. |
Thank you for the fix @shsina . It is now in master branch. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the algorithm ignores itemsInStock count
The text was updated successfully, but these errors were encountered: