diff --git a/src/calculation_budget_constrains/service.py b/src/calculation_budget_constrains/service.py index d05b9e5..d16d7b5 100644 --- a/src/calculation_budget_constrains/service.py +++ b/src/calculation_budget_constrains/service.py @@ -150,7 +150,7 @@ def knapsack_selection(equipments: List[dict], budget: float, scale: int = 10_00 for i in range(n): cost, value = costs[i], values[i] for w in range(W, cost - 1, -1): - if dp[w - cost] + value > dp[w]: + if dp[w - cost] + value >= dp[w]: # <= FIXED HERE dp[w] = dp[w - cost] + value keep[i][w] = True @@ -164,5 +164,15 @@ def knapsack_selection(equipments: List[dict], budget: float, scale: int = 10_00 else: excluded.append(equipments[i]) + # Optional: fill leftover budget with zero-priority items + remaining_budget = budget - sum(eq["total_cost"] for eq in selected) + if remaining_budget > 0: + for eq in excluded[:]: + if eq["total_cost"] <= remaining_budget: + selected.append(eq) + excluded.remove(eq) + remaining_budget -= eq["total_cost"] + return selected, excluded + \ No newline at end of file