solve_knapsack_problem_tool
Optimizes item selection to maximize value within weight and volume limits for cargo loading, portfolio management, or resource allocation.
Instructions
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
)
Input Schema
Name | Required | Description | Default |
---|---|---|---|
capacity | Yes | ||
items | Yes | ||
knapsack_type | No | 0-1 | |
max_items_per_type | No | ||
volume_capacity | No |
Input Schema (JSON Schema)
{
"properties": {
"capacity": {
"title": "Capacity",
"type": "number"
},
"items": {
"items": {
"additionalProperties": true,
"type": "object"
},
"title": "Items",
"type": "array"
},
"knapsack_type": {
"default": "0-1",
"title": "Knapsack Type",
"type": "string"
},
"max_items_per_type": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Max Items Per Type"
},
"volume_capacity": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"default": null,
"title": "Volume Capacity"
}
},
"required": [
"items",
"capacity"
],
"type": "object"
}