Brittney P.

asked • 10/25/21

Best Fit Algorithm Help

def BestFit():
boxes = readCSV()
num_of_boxes = len(boxes)
bin_area = 40
n_bin = 0
area_remaining_in_bins = []
lists = []
for x in range(num_of_boxes):
area_remaining_in_bins.append(bin_area)
lists.append([])
print(boxes)
#for i in lists:
for box in boxes:
l = box[0]
w = box[1]
area = l * w
for i in range(len(area_remaining_in_bins)):
if area <= area_remaining_in_bins[i]:
area_remaining_in_bins[i] = area_remaining_in_bins[i] - area
lists[i].append((l,w))
break
else:
i+=1

for i in lists[:]:
if len(i) == 0:
lists.remove(i)
for j in area_remaining_in_bins[:]:
if j == 40:
area_remaining_in_bins.remove(j)

num_of_boxes_packed = []
b = []
for i in lists:
if len(i) >= 1:
n_bin += 1
na = len(i)
num_of_boxes_packed.append(na)

I want to alter this part of the code:

for box in boxes:
l = box[0]
w = box[1]
area = l * w
for i in range(len(area_remaining_in_bins)):
for i in range(len(area_remaining_in_bins)):
minimum = area_remaining_in_bins[i]
if (area <= area_remaining_in_bins[i]) and (area_remaining_in_bins[i] - area <= minimum):
area_remaining_in_bins[i] = area_remaining_in_bins[i] - area
minimum = area_remaining_in_bins[i]
lists[i].append((l,w))
break

I want it to place the boxes, not in previous boxes that need to be filled, but more specifically, to be placed into the box that will result in the less area left overall (tightest spot).


I'm don't necessarily know how to translate it into code, where the area left will be less than just doing the first-fit algorithm.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.