Skip to content

Commit

Permalink
removed redundant list comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
obucklin committed Apr 26, 2024
1 parent c6e3e23 commit 15f3f8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
10 changes: 1 addition & 9 deletions src/compas_timber/ghpython/components/CT_Beam_Nesting/code.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand All @@ -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
23 changes: 12 additions & 11 deletions src/compas_timber/planning/beam_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -141,26 +140,28 @@ 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)
if results_dict["done"]:
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
Expand All @@ -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


0 comments on commit 15f3f8d

Please sign in to comment.