-
Notifications
You must be signed in to change notification settings - Fork 0
/
knapsack_dataset.py
55 lines (45 loc) · 1.3 KB
/
knapsack_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import scipy.stats as stats
np.random.seed(0)
stats.truncnorm.random_state = np.random.RandomState(0)
def generate_dataset(num_items, weight_range, limit, filename):
weights = np.random.randint(
low=weight_range[0], high=weight_range[1] + 1, size=num_items
)
mu, sigma = 1, 1
lower, upper = 0.5, 5
truncated_normal = stats.truncnorm(
(lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma
)
x_values = truncated_normal.rvs(num_items)
values = np.round(weights * x_values).astype(int)
items = np.column_stack((values, weights))
np.save(filename, items)
print(f"Dataset saved to {filename} with shape {items.shape}")
datasets = [
{
"num_items": 100,
"weight_range": (1, 100),
"limit": 3500,
"filename": "knapsack_dataset_1.npy",
},
{
"num_items": 1000,
"weight_range": (1, 100),
"limit": 35000,
"filename": "knapsack_dataset_2.npy",
},
{
"num_items": 3000,
"weight_range": (1, 100),
"limit": 75000,
"filename": "knapsack_dataset_3.npy",
},
]
for dataset in datasets:
generate_dataset(
dataset["num_items"],
dataset["weight_range"],
dataset["limit"],
dataset["filename"],
)