Solve knapsack optimization problems using OR-Tools.
This tool solves knapsack problems where items need to be selected
to maximize value while staying within capacity constraints.
Use cases:
- Cargo loading: Optimize loading of trucks, ships, or planes by weight and volume
- Portfolio selection: Choose optimal set of investments within budget constraints
- Resource allocation: Select projects or activities with limited budget or resources
- Advertising planning: Choose optimal mix of advertising channels within budget
- Menu planning: Select dishes for a restaurant menu considering costs and popularity
- Inventory optimization: Decide which products to stock in limited warehouse space
Args:
items: List of items, each with 'name', 'value', 'weight', and optionally 'volume', 'quantity'
capacity: Weight capacity constraint
volume_capacity: Volume capacity constraint (optional)
knapsack_type: Type of knapsack problem ('0-1', 'bounded', 'unbounded')
max_items_per_type: Maximum items per type for bounded knapsack
Returns:
Knapsack result with total value and selected items
Example:
# Select items to maximize value within weight limit
solve_knapsack_problem(
items=[
{"name": "Item1", "value": 10, "weight": 5, "volume": 2},
{"name": "Item2", "value": 15, "weight": 8, "volume": 3},
{"name": "Item3", "value": 8, "weight": 3, "volume": 1}
],
capacity=10,
volume_capacity=5
)