Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started