Skip to content
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

knapsack item settings ignores itemsInStock #63

Closed
sshbio opened this issue Jun 12, 2018 · 4 comments
Closed

knapsack item settings ignores itemsInStock #63

sshbio opened this issue Jun 12, 2018 · 4 comments
Labels
bug Something isn't working

Comments

@sshbio
Copy link
Contributor

sshbio commented Jun 12, 2018

the algorithm ignores itemsInStock count

@sshbio sshbio closed this as completed Jun 12, 2018
@sshbio
Copy link
Contributor Author

sshbio commented Jun 12, 2018

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 } 

@sshbio sshbio reopened this Jun 12, 2018
@sshbio
Copy link
Contributor Author

sshbio commented Jun 12, 2018

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);
        }
      }
    }
  }

@trekhleb trekhleb added the bug Something isn't working label Jun 13, 2018
@trekhleb
Copy link
Owner

Nice catch @shsina. Waiting for PR.

@trekhleb
Copy link
Owner

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
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants