[Python] Fastest way to generate all combinations given variables, and their max,min and...

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I have a dictionary with ranges for a few variables [min,max,interval]:

    dict = { 'v1': [0,10,1], 'v2':[2,20,2], 'v3':[0,10,5]}


    What is the fastest way to generate all the combinations that exist? e.g. {'v1': 0,'v2':2,'v3':0}, {'v1':0,'v2':2,'v3':5}, {'v1':0,'v2':2,'v3':10}, {'v1':0,'v2':4,'v3':0}.......

    One caveat is that I have certain restrictions, for example, if v3 > 5: v2 < 4 must be satisfied so ['v1':0,'v2':6,'v3':10] is invalid and should not be generated.

    I have around 15-20 variables and around 5 restrictions, it will probably total about 1 billion different combinations (the more combos the better but needs to be done in reasonable time). What would be the fastest way to generate this?

    I have access to 20 CPU's so workload will be distributed.

    I know I can use np.mgrid or itertools.product as shown here: Numpy: efficient way to generate combinations from given ranges but with with the fact I have certain restrictions I am not sure how to speedily implement it

    Continue reading...

Compartilhe esta Página