0

I am wanting to develop a function that selects a board from the population "at random", however I want it to be biased towards boards with a lower fitness value.

# population = [(fitness, board),...] # population is a heapq in my program


def random_selection(population):        
    random_index = randint(0, len(population) - 1)
    return population[random_index][1] 

For example, (1 , board), (7, board). I want the first board to have a higher probability of being selected since it has a lower value.

How would I do this? I was thinking of using random() and creating a threshold of acceptance probability.

Additionally, would it be possible to keep track of what boards I have selected thus far? For my case, board within the heapq is unique.

Joshua
  • 139
  • 3
  • 12
  • 3
    Possible duplicate of [A weighted version of random.choice](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice) – FlyingTeller Mar 23 '18 at 09:57
  • 1
    By what percent you want the bias? – Rao Sahab Mar 23 '18 at 09:58
  • @RaoSahab I want the bias to be based on the heuristic value. Thus if it's a low h value the probability of selection is high. For example 1 = 0.9, 2 = 0.8 etc. – Joshua Mar 23 '18 at 10:32

1 Answers1

0

If you have access to numpy, numpy.random.choice() has exactly the feature you desire.

From the docs:

#Generate a non-uniform random sample from np.arange(5) of size 3:

>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])

You would just need to construct the weights, p, for each of the elements in your population to define how much more likely choice 1 is than 7.

Bonlenfum
  • 19,101
  • 2
  • 53
  • 56