Very large number of combinations never gives results

时间:2019-01-07 13:48:52

标签: python-3.x combinations itertools

I am using win 10 and Python 3.6.3. I have a list of dictionaries - there are approx 200 dictionaries in my list. It looks like this:

my_list = [{"name": "object_1", "price": 9, "reward": 5}, {"name": "object_2", "price": 9.3, "reward": 4.9},....]

I am using this code to get all possible combinations of 10 objects:

total_combinations = itertools.combinations(my_list, 10)

I will then use some criteria in order to arrive at the most efficient combination -for example, maximise total reward given a total cost 90.

The problem is that there is a huge number of combinations, in particular there are 22,451,004,309,013,280 combinations. This is a very large number and so far, when i tried applying my criteria, Python either crashed (i have 8GB RAM) or it never returns any results. For example, the below code never returns any results:

total_combinations = itertools.combinations(my_list, 10)
for i in total_combinations:
    total_cost = 0
    total_reward = 0
    for j in i:
        total_cost += j["price"]
        total_reward += j["reward"]
    if total_cost > 80 and total_cost < 85:
        print(total_cost, total_reward, i)

If however i only include 20 dictionaries in my_list (ie "only" 184,756 different combinations), Python needs approx 30 seconds to process my code and show me the results.

Can someone help me on the above, or suggest an alternative way to handle such big number of scenarios?

Thanks.

0 个答案:

没有答案