**Fitness Proportionate Selection** is a common method in Evolutionary Algorithms, which is also known to us as **Roulette Wheel Selection** in Evolutionary Algorithms[1] such as Genetic Algorithms.

The basic idea here is to get a random value out of a sample based on a pre-defined bias.

Following is the python implementation (using numpy library) of the Roulette Wheel selection. The *wheel_vector* should be provided in the following format [….value, bias…..], values can be added as much as required. Bias value is relative, higher the bias value higher the change of getting selected the corresponding value.

For an example, if it is required to declare the wheel vector for a bias dice where no. 6 is more likely to be appeared than other values, it should be defined as follow: note that value 6 is prone to be appeared 5 times more than any other value.

wheel_vector = np.array([1,1,2,1,3,1,4,1,5,1,6,5])

#### “all Python codes in this article are released under Creative Commons — Public Domain“

#!/usr/bin/python import numpy as np import random as rand roulette_wheel = np.array((0)) slot_count = 0 def init_roul_wheel(value_array): slot_count = 0 i=0 arrsize = value_array.size while i < arrsize/2: slot_count = slot_count + value_array[2*i+1] i = i + 1 roulette_wheel = np.zeros((slot_count),dtype=np.int) #print(roulette_wheel) i = 0 while i < arrsize/2: rv = value_array[2*i] bv = value_array[2*i+1] j = 0 while j<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span><span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span><span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span><bv: t = rand.randint(0,slot_count-1) wheel_alloc = roulette_wheel[t] if wheel_alloc == 0: roulette_wheel[t] = rv j = j + 1 i = i + 1 return (roulette_wheel) def spin(rw): slot_count = rw.size randno = rand.randint(0,10000) rot_degree = randno%360 rot_unit = 360/slot_count rol_no = (rot_degree - (rot_degree%(rot_unit)))/rot_unit rol_value = rw[int(rol_no)] return rol_value wheel_vector = np.array([10,1,20,2,30,2,40,4,50,5,60,4,70,3,80,2,90,2]) x = init_roul_wheel(wheel_vector) print (spin(x))

Check how the above code works (online python demo)

The above code is quite lengthy as I purposely omitted shortcuts to slice the arrays hoping that it improves the readability of the code.

The next step is to check if the function is really bias. In order to check that the code has been altered as follow. This version iterates the wheel 1,000,000 times and check the results.

#!/usr/bin/python import numpy as np import random as rand roulette_wheel = np.array((0)) slot_count = 0 def init_roul_wheel(value_array): slot_count = 0 i=0 arrsize = value_array.size while i < arrsize/2: slot_count = slot_count + value_array[2*i+1] i = i + 1 roulette_wheel = np.zeros((slot_count),dtype=np.int) #print(roulette_wheel) i = 0 while i < arrsize/2: rv = value_array[2*i] bv = value_array[2*i+1] j = 0 while j<bv: t = rand.randint(0,slot_count-1) wheel_alloc = roulette_wheel[t] if wheel_alloc == 0: roulette_wheel[t] = rv j = j + 1 i = i + 1 return (roulette_wheel) def spin(rw): slot_count = rw.size randno = rand.randint(0,10000) rot_degree = randno%360 rot_unit = 360/slot_count rol_no = (rot_degree - (rot_degree%(rot_unit)))/rot_unit rol_value = rw[int(rol_no)] return rol_value wheel_vector = np.array([10,1,20,2,30,2,40,4,50,5,60,4,70,3,80,2,90,2]) x = init_roul_wheel(wheel_vector) #print (spin(x)) cal_rounds = 1000000 results = np.zeros((cal_rounds),dtype=np.int); i =0 while i<cal_rounds: value = spin(x) results[i] = value i = i +1 unique, counts = np.unique(results, return_counts=True) print ("RW Vector", wheel_vector, "\n") print ("Roulette Wheel", x) print ("Results: ",unique,"\n\t" ,counts) i = 0 while i<counts.size: #print (unique[i], "occured " + str(counts[i])) precentage = (counts[i]*100)/np.sum(counts) print (unique[i]," precentage - ",str(precentage) + ' %','('+str(round(precentage))+')') i = i +1

Check the output of the code above, it shows number of times the values has been selected and the values have been selected based on the bias (check the percentages). Therefore it can be concluded that the function is working as expected.

**References**

[1] Lipowskia, Adam and Lipowskab, Dorota *Roulette-wheel selection via stochastic acceptance* – http://www.sciencedirect.com/science/article/pii/S0378437111009010

To the extent possible under law,

this article, the author

has waived all copyright and related or neighboring rights to “Implementing Roulette Wheel in Python”.