Let's take an example list of lists like this:
li=[[0.99, 0.002],
[0.98, 0.0008, 0.0007],
[0.97, 0.009, 0.001],
[0.86, 0.001]]
Note that elements inside each sublist are sorted in descending order and their sum is always less than or equal to 1. Also, the sublists themselves are sorted in descending order of their first elements.
I am interested to find combinations, taking one element from each sublist such that the product of the elements of the combination is above a certain threshold, say 1e-5. One way that I found of doing this is by using itertools.product.
a = list(itertools.product(*li))
[item for item in a if np.prod(item)>1e-5]
But, this procedure is not feasible for me since my actual list has too many sublists and so the number of possible combinations to check is too big.
Instead of first finding all combinations and checking for the threshold condition, I must do the opposite i.e. only find combinations that satisfy the given condition. For example: since 0.002*0.0008*0.009 is already less than 1e-5, I can ignore all other combinations that start with (0.002, 0.0008,0.009,...).
I could not find an easy way to implement this. What I have in mind is a tree data structure, where I build a tree such that each node will keep track of the product and as soon as a node value is below 1e-5, I stop building further the tree on that node and also on nodes that are to it's right (since the nodes on the right will be smaller than the current node).
A simple tree skeleton to get started:
class Tree(object):
def __init__(self, node=None):
self.node = node
self.children = []
def add_child(self, child):
self.children.append(child)
Once, the tree is built, I would then extract the combination that reached the depth = len(li)
Any help to build such a tree or any other ideas towards solving the problem would be highly appreciated. Thanks!