Combination of lengths to reach bounded length

Hello,
I call on your skills
You have probably already been faced with this problem.

Let me explain:
I am trying to understand the logic of creating this table to make it a sub-dynamo (a list which will serve as data) in order to place families afterwards

By choosing all possible combinations (I come up against a list of data which will become too large, I think)
I haven’t gone any further in this direction, I don’t think it’s the right method.

import sys
liste_longueurs = [0, 90, 110, 150, 180]
liste_possibilites = []

for val1 in liste_longueurs:
    for val2 in liste_longueurs:
        for val3 in liste_longueurs:
            for val4 in liste_longueurs:
                for val5 in liste_longueurs:
                    une_combinaison = [val1, val2, val3, val4,val5]
                    liste_possibilites.append(une_combinaison)
                    
OUT = len(liste_possibilites),liste_possibilites

Do you think that there is a python module or dynamo node which searches for the number of elements according to a distance to be reached and not exceeded (I think this is the route that was used)

I don’t ask for the result of an algorithm (rather to know if my thinking is going in the right direction or there is yet another direction that I don’t perceive)

Sincerely
christian.stan

Not quite sure I understand the use case, but not sounds like you have a bunch of linear measurements, and you want to make them fit into a container to ensure the least amount of unused capacity in the containers.

If so, look into the BIM4Struct package as there is a recent update to allow finding the optimal container size for a given list of lengths and a list of containers. The actual node uses a list of curves though, so you may want to modify the Python code.

Search for packing in your library once installed.

1 Like

Hi,
I don’t know how the table is read, but I think that using a DataFrame (Pandas) or DataTable (.Net) would be most suitable

1 Like

hi, thank you for guidance, I’m trying to create the table
here is an example

(for the dimension of 340cm no real combination found (first on figure), I base myself from the largest module to the smallest module without being able to manage the combinations, really not easy to integrate them into a python code for a beginner)

import sys
#Dimensions at the edge of the beams
L_nu=[1.8,1.5,1.1,0.9]

def nb_modules(L_bet,a,b,c,d):
    Laxe=L_bet-0.2
    nb_a=Laxe//a
    if nb_a>=0:
        Laxe=Laxe-nb_a*a
        nb_b=Laxe//b
        if nb_b>=0:
            Laxe=Laxe-nb_b*b
            nb_c=Laxe//c
            if nb_c>=0:
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d
            else:
                nb_c=0
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d
        else:
            nb_b=0
            Laxe=Laxe-nb_b*b
            nb_c=Laxe//c
            if nb_c >=0:
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d
            else:
                nb_c=0
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d      
    else:
        nb_a=0
        Laxe=Laxe-nb_a*a
        nb_b=Laxe//b
        if nb_b >=0:
            Laxe=Laxe-nb_b*b
            nb_c=Laxe//c
            if nb_c >=0:
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d
            else:
                nb_c=0
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d
        else:
            nb_b=0
            Laxe=Laxe-nb_b*b
            nb_c=Laxe//c
            if nb_c >=0:
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d
            else:
                nb_c=0
                Laxe=Laxe-nb_c*c
                nb_d=Laxe//d
    L_tot=round((nb_a*a+nb_b*b+nb_c*c+nb_d*d)+0.2,1)
    if L_bet==L_tot:
        return [nb_a,nb_b,nb_c,nb_d,L_tot]
    else:
        return ["no solution",L_tot]
res=[nb_modules(i,1.9,1.6,1.2,1) for i in IN[0]]
OUT = res

while 2 combinations
usable (see figure)

cordially
christian.stan

hi, thank you, I will check with the package if this is going in a positive direction

cordially
christian.stan

Hi @christian.stan ,

Are you looking for something similar to this?

1 Like

hi, thank you, I’ll look tomorrow, I have a terrible headache (I’m going to do a covid test, I think)
you have to be very mentally fresh to tackle this big piece (for me)

cordially
christian.stan

This is a version of the minimum change making problem.
As the combinations are not excessive it is possible to iterate through all the possible combinations to generate the optimal solution - in this case minimum number of items

from itertools import combinations_with_replacement as cwr

lengths = [1.9, 1.6, 1.2, 1.0]  # Adjust before calculating and after
total = 3.2  # Adjusted

solutions = []
for i in range(int(total / max(lengths)), int(total / min(lengths)) + 1):
    for j in cwr(lengths, i):
        if sum(j) == total:
            solutions.append(j)
if solutions:
    print(min(solutions, key=len))  # make adjustment if required
else:
    print("No solutions")
3 Likes

hello, thank you (did you have python training during your life, or 100% self-taught?) and to all the contributors for having solved this.
A big thank-you

cordially
christian.stan

1 Like

I’m 100% self taught, mainly started with free online university courses and e-books (Think Python is good).
Once I had the basics I worked through the HackerRank python challenges which helped understand common computer science algorithms and problem solving.
Since then, besides Revit & C3D for work, just my own projects on data viz, machine learning & API libraries like spotipy

1 Like

I would try to apply and consult this book (to establish the skills base)
In this regard I think that you will appreciate the new quality article (again) by Mr. C. Poupin (Just like you on board the Python rocket heading to Pluto, like many other people) (Don’t worry Mr. Poupin :wink:, I loves b _ _ bs)
Personally I am on a bus heading to the hotel based in Kourou (or Cape Canaveral), waiting for takeoff.
I am aware that the progression curve is specific to each person even if I would like it to be exponential (my brain capacity is not unfortunately)
Thank you for your feedback,
Sincerely
christian.stan