Zavax
Zavax

Reputation: 59

Python Genetic Algorithm "Natural" Selection

How can I perform selection (i.e. deletion of elements) in an array that tends towards lower numbers.

If I have an array of fitnesses sorted lowest to highest, how can I use random number generation that tends towards the smaller numbers to delete those elements at random.

pop_sorted_by_fitness = [1, 4, 10, 330]

I want to randomly delete one of those smaller elements, where it's most of the time 1, sometimes 4, and rarely 10, with almost never 330. How can I achieve this sort of algorithm.

Upvotes: 0

Views: 284

Answers (1)

Unni
Unni

Reputation: 5794

How about making use of exponential distribution for sampling your indexes using numpy.random.exponential

import numpy as np 

s = [1, 4, 10, 330] 
limit = len(s)
scale = 10**int(np.log10(limit))
index = int(np.random.exponential()*scale)%limit

Test it

In [37]: sorted([int(np.random.exponential()*scale)%limit for _ in xrange(20)]) 
Out[37]: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]

Upvotes: 2

Related Questions