Hello! I am currently trying to create a script (node - sequence) able to select the optimal combination of formwork panels dimensions in order to cover both width and length of a concrete beam. I designed this script in python, hoping to be able to use it in Dynamo, but I have not been able to do so so far.
If anyone could help me understand how to actually get the desired input and output from the code, I would greatly appreciate it. Below, the code in python syntax, in which the input parameters will be in bold case and the output in italics.
#Get all combinations from list def combinations(self, elements): allCombs =  for i in range (0 , len(elements) + 1): for comb in itertools.combinations(elements, i): allCombs.append(comb) return allCombs[1:] #Remove greater than target or return if the same def purge (self,elements, target): purged =  for i in range (0, len(elements)): if elements[i] < target: purged.append(elements[i]) elif elements[i] == target: return target return purged #Add list sublists def sums (self , elements): added =  for i in range (0, len(elements)): add = 0 for j in range (0, len(elements[i])): add += elements[i][j] added.append(add) return added #Get distances from elements to target def distances (self, elements,target): diffs =  for i in range (0, len(elements)): diffs.append(target - elements[i]) return diffs #Get the closest element index to zero def closest (self, elements): positives =  for i in range ( 0 , len (elements)): if elements[i] > 0: positives.append(elements[i]) closeTozero = min (positives) return elements.index ( closeTozero ) #return combination at index def getCombElement ( self, index , combs): return combs[index] #return the closest dimension to a target def getClosest (self , dims, target): purged = self.purge(dims, target) if isinstance(purged, float) or isinstance(purged, int): return [purged] combs = self.combinations(purged) added = self.sums(combs) diffs = self.distances(added, target) close = self.closest(diffs) comb = self.getCombElement(close , combs) return comb #return the closest dimension to a list of targets def getClosestForAll ( self, dims , targets): closests =  for i in range (0, len(targets)): closests.append([targets[i], self.getClosest(dims, targets[i])]) return closests
if name == ‘main’:
mynums = nums() allCombs = mynums.getClosestForAll([**0.3,0.45,0.60,0.75,0.90,1.2,1.4**], [**1.4, 0.80,2**] ) _print allCombs_