From 15f3f8dee010f9af8f9c005c39a365c664f6abb6 Mon Sep 17 00:00:00 2001 From: oliver bucklin Date: Fri, 26 Apr 2024 12:43:24 +0200 Subject: [PATCH] removed redundant list comparison --- .../components/CT_Beam_Nesting/code.py | 10 +------- src/compas_timber/planning/beam_nesting.py | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/compas_timber/ghpython/components/CT_Beam_Nesting/code.py b/src/compas_timber/ghpython/components/CT_Beam_Nesting/code.py index 07b23eaec..bbc084496 100644 --- a/src/compas_timber/ghpython/components/CT_Beam_Nesting/code.py +++ b/src/compas_timber/ghpython/components/CT_Beam_Nesting/code.py @@ -1,5 +1,4 @@ from ghpythonlib.componentbase import executingcomponent as component -from Grasshopper.Kernel.GH_RuntimeMessageLevel import Error from Grasshopper.Kernel.GH_RuntimeMessageLevel import Warning from Grasshopper import DataTree from Grasshopper.Kernel.Data import GH_Path @@ -8,7 +7,7 @@ class NestingComponent(component): - def RunScript(self, assembly, stock_length, tolerance=1, iterations=10): + def RunScript(self, assembly, stock_length, tolerance, iterations): if not assembly: self.AddRuntimeMessage(Warning, "input Assembly failed to collect data") @@ -30,11 +29,4 @@ def RunScript(self, assembly, stock_length, tolerance=1, iterations=10): info.append("bin count = {0}".format(len(nesting_dict.items()))) info.append("cutoff waste = {:.2f}".format(nester.total_space(nesting_dict))) - beam_list_out = [] - for val in nesting_dict.values(): - beam_list_out.extend(val) - - if set(beam_list_out) != set(assembly.beams): - self.AddRuntimeMessage(Error, "beams inputs and outputs dont match") - return data_tree, info diff --git a/src/compas_timber/planning/beam_nesting.py b/src/compas_timber/planning/beam_nesting.py index 8f83a14a4..0e56c7a54 100644 --- a/src/compas_timber/planning/beam_nesting.py +++ b/src/compas_timber/planning/beam_nesting.py @@ -44,7 +44,7 @@ def total_space(self, bin_dict): return sum([self.space_remaining(bin) for bin in bin_dict.values()]) def get_bins_basic(self, beams): - """returns a dictionary of bins with beams nested in them""" + """returns a dictionary of bins with beams nested in them""" beams_sorted = sorted(beams, key=lambda z: z.length, reverse=True) bins = OrderedDict([(0, [])]) for beam in beams_sorted: @@ -109,7 +109,7 @@ def validate_bin_results(self, bins, beams): beam_list_out.extend(val) if set(beam_list_out) != set(beams): - raise Exception("beams inputs and outputs dont match") + raise Exception("Beams input and nesting output dont match") def get_bins(self, beams, stock_length, tolerance=None, iterations=0): """returns a dictionary of bins with beams nested in them @@ -126,7 +126,6 @@ def get_bins(self, beams, stock_length, tolerance=None, iterations=0): number of iterations to run the nesting algorithm, the algorithm will stop when the total waste is less than the stock length """ - self.stock_length = stock_length if tolerance is None: self.tolerance = stock_length / 100 @@ -141,18 +140,20 @@ def get_bins(self, beams, stock_length, tolerance=None, iterations=0): if iterations == 0: return self.get_bins_basic(beams) - + else: - for i in range(iterations): #try with different shuffling + for i in range(iterations): # try with different shuffling these_beams = beams these_bins = self.get_bins_basic(these_beams) results_dict = self.parse_bins(these_bins) - if results_dict["done"]: #if the nesting is successful + if results_dict["done"]: # if the nesting is successful bins_out = results_dict["finished_bins"] else: sort = False shuffle = True - for x in range(iterations): #tries to repack the beams that don't fit in the bins within the cutoff tolerance + for x in range( + iterations + ): # tries to repack the beams that don't fit in the bins within the cutoff tolerance these_beams = results_dict["recycled_beams"] temp_bins = self.fill_bins(results_dict["temporary_bins"], these_beams, sort, shuffle) results_dict = self.parse_bins(temp_bins) @@ -160,7 +161,7 @@ def get_bins(self, beams, stock_length, tolerance=None, iterations=0): bins_out = results_dict["finished_bins"] print("success after {0} iterations.".format(x)) break - elif x == iterations - 1: #if the last iteration is reached, the best result is taken + elif x == iterations - 1: # if the last iteration is reached, the best result is taken all_bins.append(results_dict["finished_bins"]) else: sort = not sort @@ -169,10 +170,10 @@ def get_bins(self, beams, stock_length, tolerance=None, iterations=0): break if not bins_out: - bins_out = min(all_bins, key=lambda x: len(x)) #if no successful nesting is found, the one with the least bins is taken + bins_out = min( + all_bins, key=lambda x: len(x) + ) # if no successful nesting is found, the one with the least bins is taken self.validate_bin_results(bins_out, beams) return bins_out - -