def apply(item, bins): """ Adds the item to the next available bin after the last insertion. :param item: The item to add. :param bins: The bins to choose from. :return: The list of bins after insertion. """ b = bins[-1] if not b.add_item(item): b = Bin(bins[0].capacity) bins.append(b) b.add_item(item) return bins
def apply(item, bins): """ Adds the item to the very first bin that it can fit it. :param item: The item to add. :param bins: The bins to choose from. :return: The list of bins after the insertion. """ b = next((b for b in bins if b.can_add_item(item)), None) if not b: b = Bin(bins[0].capacity) bins.append(b) b.add_item(item) return bins
def apply(item, bins): """ Adds the item to the bin for which the most amount of open space would be available after insertion. :param item: The item to add. :param bins: The bins to choose from. :return: The list of bins after insertion. """ valid_bins = (b for b in bins if b.can_add_item(item)) sorted_bins = sorted(valid_bins, key=lambda x: x.filled_space()) if sorted_bins: b = sorted_bins[0] else: b = Bin(bins[0].capacity) bins.append(b) b.add_item(item) return bins
def execDir(self): global pathh pat = pathh files = listdir(pathh) open("result_ff.csv", "w").close() print(1) print(pat) for f in files: l = pat + "/" + f print(f) j = self.InstanceReader2(f) print("hada", j) instance, n, C, w = self.InstanceReader(l) print("hadddd") items = [Item(size=int(i)) for i in w] shuffle(items) start = timeit.default_timer() bins = [Bin(capacity=C)] for item in items: bins = FirstFit.apply(item, bins) stop = timeit.default_timer() time = stop - start c = len(bins) resultatt = open("result_ff.csv", mode="a+") resultatt.write(f'{instance},{n},{C},{c},{j},{time * 1000}\n') # Ecrire les resultats dans un fichier csv resultatt.close() print(pathh) self.loadData_mod_csv('./result_ff.csv')
def apply(item, bins): """ Adds the item to the bin for which the least amount of open space would be available after insertion. :param item: The item to add. :param bins: The bins to choose from. :return: The list of bins after the insertion. """ valid_bins = (b for b in bins if b.can_add_item(item)) # Note that this method is exactly the same as for the BestFit heuristic except for the following line. sorted_bins = sorted(valid_bins, key=lambda x: x.filled_space(), reverse=True) if sorted_bins: b = sorted_bins[0] else: b = Bin(bins[0].capacity) bins.append(b) b.add_item(item) return bins
def generate_solution(self, items): """ Generates a candidate solution based on the pattern given. :param items: The items that need to be used when generating a solution. :return: A list of bins to serve as a solution. """ solution = [Bin(self.bin_capacity)] pattern_length = len(self.pattern) for idx, item in enumerate(items): h = self.pattern[idx % pattern_length] solution = self.heuristic_map[h].apply(item, solution) return solution
def exeFile(self): global pathh a_file = open(pathh, "r") print(pathh, 111) list_of_lists = [] for line in a_file: stripped_line = line.strip() # line_list = stripped_line.split() n_liste = int(stripped_line) list_of_lists.append(n_liste) a_file.close() weight = list_of_lists[2:] # print(weight) C = list_of_lists[1] print(C) items = [Item(size=int(i)) for i in weight] shuffle(items) start = timeit.default_timer() bins = [Bin(capacity=C)] print(1) for item in items: print(item) bins = BestFit.apply(item, bins) print(2) stop = timeit.default_timer() time = stop - start c = len(bins) #c, conf, time = self.next_fit(weight, C) print(c) print(time) c1 = str(c) self.nbr_bin.setText(c1) tex = time * 1000 tex1 = str(tex) self.t_exe.setText(tex1) print("nooo") l = list_of_lists[0] print(l) g = str(l) print("hi", g) self.items_nb.setText(g) r = str(C) print("hi", g, r) self.bin_cap.setText(r)