22 lines
610 B
Python
22 lines
610 B
Python
import random
|
|
from typing import List, Dict, Any
|
|
|
|
def generate_knapsack_case(num_items: int = 10, max_weight: int = 50, max_value: int = 100, capacity_ratio: float = 0.5) -> Dict[str, Any]:
|
|
"""
|
|
Generates a case for 0/1 Knapsack.
|
|
"""
|
|
items = []
|
|
total_weight = 0
|
|
for i in range(num_items):
|
|
w = random.randint(1, max_weight)
|
|
v = random.randint(1, max_value)
|
|
items.append({"id": i, "weight": w, "value": v})
|
|
total_weight += w
|
|
|
|
capacity = int(total_weight * capacity_ratio)
|
|
|
|
return {
|
|
"items": items,
|
|
"capacity": capacity
|
|
}
|