0

I have a cartesian product obtained from itertools.product. The number of combinations is 353,736,000 or even bigger. I can't afford to convert this result to a list because of time constraints.

I want to obtain 10 random combinations from those millions to become the population of a genetic algorithm and I need it to be done in a matter of milliseconds.

Is it possible?

wim
  • 338,267
  • 99
  • 616
  • 750
  • You can do a random cartesian product multiple times instead of doing the full cartesian product then picking from that. – Aplet123 Nov 15 '20 at 00:02
  • 1
    Why not just randomly sample from each axis? – wim Nov 15 '20 at 00:20
  • Exactly, randomly sample from each of the axis (vectors). I was misled by my initial implementation, which aims at obtaining the entire search space to obtain optimality. When the order of magnitude is 10**8, then problem has to be solved by a heuristic and getting the entire search space is of no use. Each Chrommosome will get each of its component values by sampling randomly from each of the vectors. – Francisco Lemos Nov 15 '20 at 00:38

1 Answers1

0

Actually it is not necessary to obtain the cartesian product; for each of the vectors it is only necessary to randomly choose a value. Since each vector is a list:

import random

numberList = [111,222,333,444,555]
print("random item from list is: ", random.choice(numberList))

this will return a random element for a list. The same principle can be applied to other vectors to build the chromosome.