So at the moment, I have a large dictionary of items. Might be a little confusing, but each of these keys have different values, and the values themselves correspond to another dictionary.
I need to make sure that my random selection from the first dict covers all possible values in the second dict. I'll provide a rudimentary example:
Dict_1 = {key1: (A, C), key2: (B, O, P), key3: (R, T, A)} # and so on
Dict_2 = {A: (1, 4, 7), B: (9, 2, 3), C: (1, 3)} # etc
I need a random selection of Dict_1
to give me a coverage of all numbers from 1 - 10 in Dict_2
values.
At the moment, I am selecting 6 random keys from Dict_1
, taking all the numbers that those letters would correspond with, and comparing that set to a set of the numbers from 1 - 10. If the selection isn't a subset of 1 - 10, select 6 more random ones and try again, until I have 1 - 10.
Now, this works, but I know it's far from efficient. How can I improve this method?
I am using Python.
In case your solution doesn't fully cover 1-10, you're erasing the whole solution and restarting completely from scratch.This is what's inefficient.
Instead, you could use an approach inspired by simulated annealing or random nearest neighbour search. The idea is that if your solution doesn't fully cover 1-10, then instead of erasing it, you try to incrementally make it better.
One way to do this is to attribute a score to each of the six keys in your solution. This score should reflect how useful that key is in the solution; i.e., how many numbers in 1-10 are covered thanks to this key that are not already covered by another key.
Then, instead of picking six new random keys, you keep the five best keys and only pick one new random key. The solution should become incrementally better, until hopefully it covers the whole range 1-10.