Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help in python Thanks Online BPP - Next Fit Online BPP - First Fit Online BPP - Best Fit Offline BPP - First

I need help in python Thanks

Online BPP - Next Fit

Online BPP - First Fit

Online BPP - Best Fit

Offline BPP - First Fit Decreasing (presorted)

Offline BPP - Best Fit Decreasing (presorted)

Implement each of the five approximation algorithms mentioned above. For the two "offline" versions that require presorting the data in non-increasing (decreasing) order, you may use Python's built-in sorting function or call any other sorting algorithm of your choosing. You may also use your choice of data structure for finding a bin with a suitable or best-fit capacity

My code :

import math import random import sys import time from os import listdir from os.path import isfile, join import pandas as pd import matplotlib.pyplot as plt from copy import deepcopy from heapq import heappop, heappush

cap = 100

def process_file(file_name, my_path): with open(my_path + file_name, 'r') as file: weights = [] lines = file.readlines() for line in lines: weights.append(int(line))

nbins = BPP_nextFit(weights) opt_bins = sum(weights) / cap

print(file_name, ' Number of weights: ', len(weights), "Bins for BPP next fit: ", nbins, 'Opt bins: ', opt_bins)

def BPP_nextFit(weights): nBins = 0 remCap = cap

for weight in weights: if weight > remCap: nBins += 1 remCap = cap remCap -= weight return nBins

def main(): my_path = 'C:\\Users\\faruk\\PycharmProjects\\Projects' files = [f for f in listdir(my_path) if isfile(join(my_path, f))] for file in files: process_file(file, my_path)

if __name__ == '__main__': main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions