Skip to content

Commit e37654c

Browse files
OOP code with comments
1 parent a0dd0a3 commit e37654c

File tree

2 files changed

+102
-76
lines changed

2 files changed

+102
-76
lines changed

boxes.py

+46-38
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
11
import random
22

3+
class BoxGenerator:
4+
"""
5+
A class to generate boxes with specified dimensions and constraints.
6+
"""
7+
def __init__(self, min_dim=11, split_dim_min=5):
8+
self.min_dim = min_dim
9+
self.split_dim_min = split_dim_min
310

4-
def generateboxes(container, num):
5-
retry = 500 * num
6-
while num > 1:
7-
cuboid = random.choice(container)
8-
while cuboid[3] <= 11 or cuboid[4] <= 11 or cuboid[5] <= 11:
9-
retry -= 1
10-
if retry == 0:
11-
print("Cannot partition into packages. Please try again")
12-
return
13-
cuboid = random.choice(container)
14-
container.remove(cuboid)
15-
prob = random.uniform(0, 1)
16-
x1 = cuboid[0]
17-
y1 = cuboid[1]
18-
z1 = cuboid[2]
19-
x2 = cuboid[3]
20-
y2 = cuboid[4]
21-
z2 = cuboid[5]
22-
if prob < 0.35:
23-
# Split in length
24-
t = random.randint(5, int(x2 / 2))
25-
package1 = [x1 + t, y1, z1, x2 - t, y2, z2]
26-
package2 = [x1, y1, z1, t, y2, z2]
27-
elif prob < 0.65:
28-
# Split in width
29-
t = random.randint(5, int(y2 / 2))
30-
package1 = [x1, y1 + t, z1, x2, y2 - t, z2]
31-
package2 = [x1, y1, z1, x2, t, z2]
32-
33-
else:
34-
# Split in height
35-
t = random.randint(5, int(z2 / 2))
36-
package1 = [x1, y1, z1 + t, x2, y2, z2 - t]
37-
package2 = [x1, y1, z1, x2, y2, t]
11+
def generate_boxes(self, container, num):
12+
"""
13+
Generates a list of boxes within a container based on the specified conditions.
3814
39-
container.append(package1)
40-
container.append(package2)
41-
num -= 1
15+
Parameters:
16+
- container (list): A list of cuboids where each cuboid is represented by six integers.
17+
- num (int): The number of boxes to generate.
4218
43-
return container
19+
Returns:
20+
- A list of generated boxes with their dimensions.
21+
"""
22+
retry = 500 * num
23+
while num > 1:
24+
cuboid = random.choice(container)
25+
while cuboid[3] <= self.min_dim or cuboid[4] <= self.min_dim or cuboid[5] <= self.min_dim:
26+
retry -= 1
27+
if retry == 0:
28+
print("Cannot partition into packages. Please try again")
29+
return
30+
cuboid = random.choice(container)
31+
container.remove(cuboid)
32+
prob = random.uniform(0, 1)
33+
x1, y1, z1, x2, y2, z2 = cuboid
34+
35+
# Splitting the cuboid based on the random probability generated
36+
if prob < 0.35:
37+
t = random.randint(self.split_dim_min, int(x2 / 2))
38+
package1 = [x1 + t, y1, z1, x2 - t, y2, z2]
39+
package2 = [x1, y1, z1, t, y2, z2]
40+
elif prob < 0.65:
41+
t = random.randint(self.split_dim_min, int(y2 / 2))
42+
package1 = [x1, y1 + t, z1, x2, y2 - t, z2]
43+
package2 = [x1, y1, z1, x2, t, z2]
44+
else:
45+
t = random.randint(self.split_dim_min, int(z2 / 2))
46+
package1 = [x1, y1, z1 + t, x2, y2, z2 - t]
47+
package2 = [x1, y1, z1, x2, y2, t]
48+
container.append(package1)
49+
container.append(package2)
50+
num -= 1
51+
return container

create_dataset.py

+56-38
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
1-
"""
2-
This function is used to create the data-set
3-
"""
4-
51
import json
62
import random
7-
import boxes as bx
3+
from boxes import BoxGenerator
4+
5+
class DatasetCreator:
6+
"""
7+
A class to create a dataset for the box packing problem, including truck dimensions and box parameters.
8+
"""
9+
def __init__(self, min_boxes=10, max_boxes=36, min_value=50, max_value=500,
10+
max_truck_dim=(600, 600, 600), min_truck_dim=(50, 50, 50)):
11+
"""
12+
Initializes the DatasetCreator with parameters for box and truck dimension ranges.
13+
14+
Parameters:
15+
- min_boxes, max_boxes (int): The minimum and maximum number of boxes.
16+
- min_value, max_value (int): The minimum and maximum value associated with each box.
17+
- max_truck_dim, min_truck_dim (tuple): The maximum and minimum dimensions for the trucks.
18+
"""
19+
self.min_boxes = min_boxes
20+
self.max_boxes = max_boxes
21+
self.min_value = min_value
22+
self.max_value = max_value
23+
self.max_truck_len, self.max_truck_wid, self.max_truck_ht = max_truck_dim
24+
self.min_truck_len, self.min_truck_wid, self.min_truck_ht = min_truck_dim
25+
self.box_generator = BoxGenerator()
826

9-
MIN_BOXES = 10
10-
MAX_BOXES = 36
11-
MIN_VALUE = 50
12-
MAX_VALUE = 500
13-
MAX_TRUCK_LEN = 600
14-
MIN_TRUCK_LEN = 50
15-
MAX_TRUCK_WID = 600
16-
MIN_TRUCK_WID = 50
17-
MAX_TRUCK_HT = 600
18-
MIN_TRUCK_HT = 50
27+
def generate_dataset(self):
28+
"""
29+
Generates a dataset of packing scenarios, each with a set of boxes and a truck.
1930
20-
truck_dim = [[random.randint(MIN_TRUCK_LEN, MAX_TRUCK_LEN), random.randint(MIN_TRUCK_WID, MAX_TRUCK_WID),
21-
random.randint(MIN_TRUCK_HT, MAX_TRUCK_HT)] for _ in range(5)]
22-
NUM_BOXES = [
23-
[random.randint(MIN_BOXES, MAX_BOXES), random.randint(MIN_BOXES, MAX_BOXES), random.randint(MIN_BOXES, MAX_BOXES),
24-
random.randint(MIN_BOXES, MAX_BOXES), random.randint(MIN_BOXES, MAX_BOXES)] for _ in range(5)]
25-
dataset = {}
26-
i = 0
27-
for cont, counts in zip(truck_dim, NUM_BOXES):
28-
for number in counts:
29-
packages = bx.generateboxes([[0, 0, 0] + cont], number)
30-
boxes = []
31-
total_value = 0
32-
for each in packages:
33-
l, w, h = each[3:]
34-
vol = l * w * h
35-
value = random.randint(MIN_VALUE, MAX_VALUE)
36-
total_value += value
37-
boxes.append([l, w, h, vol, value])
38-
dataset[i] = {'truck dimension': cont, 'number': number, 'boxes': boxes, 'solution': packages,
39-
'total value': total_value}
40-
i += 1
31+
Returns:
32+
- A dictionary representing the dataset, where each key is a scenario with truck dimensions,
33+
box parameters, and total value.
34+
"""
35+
truck_dim = [[random.randint(self.min_truck_len, self.max_truck_len),
36+
random.randint(self.min_truck_wid, self.max_truck_wid),
37+
random.randint(self.min_truck_ht, self.max_truck_ht)] for _ in range(5)]
38+
num_boxes = [[random.randint(self.min_boxes, self.max_boxes) for _ in range(5)] for _ in range(5)]
39+
dataset = {}
40+
i = 0
41+
origin = [0,0,0]
42+
for truck_dimensions, box_counts in zip(truck_dim, num_boxes):
43+
for number_of_boxes in box_counts:
44+
# Generate boxes within the truck's volume, defined by starting at the origin [0, 0, 0] and truck_dimensions [length, width, height]
45+
packages = self.box_generator.generate_boxes([origin + truck_dimensions], number_of_boxes)
46+
boxes = []
47+
total_value = 0
48+
for each in packages:
49+
l, w, h = each[3:]
50+
vol = l * w * h
51+
value = random.randint(self.min_value, self.max_value)
52+
total_value += value
53+
boxes.append([l, w, h, vol, value])
54+
dataset[str(i)] = {'truck dimension': truck_dimensions, 'number': number_of_boxes, 'boxes': boxes, 'solution': packages,
55+
'total value': total_value}
56+
i += 1
57+
return dataset
4158

42-
with open('input.json', 'w') as outfile:
43-
json.dump(dataset, outfile)
59+
def save_to_file(self, dataset, filename='input.json'):
60+
with open(filename, 'w') as outfile:
61+
json.dump(dataset, outfile)

0 commit comments

Comments
 (0)