title: Roulette Wheel Selection for non-ordered fitness values
body: <p>I need to have a fitness proportionate selection approach to a GA, however my population cant loose the structure (order), in this case while generating the probabilities, I believe the individuals get the wrong weights, the program is: </p>
  
  <pre><code>population=[[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [6], [0]], 
  [[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [4], [1]], 
  [[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [6], [2]],
  [[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [4], [3]]]
  
  popultion_d={'0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1': 6, 
  '0,0,1,1,1,0,0,1,1,0,1,1,0,0,0,1': 4, 
  '0,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0': 6, 
  '1,0,0,1,1,1,0,0,1,1,0,1,1,0,0,0': 4}
  
  def ProbabilityList(population_d):
      fitness = population_d.values()
      total_fit = (sum(fitness))
      relative_fitness = [f/total_fit for f in fitness]
      probabilities = [sum(relative_fitness[:i+1]) for i in range(len(relative_fitness))]
      return (probabilities)
  
  def FitnessProportionateSelection(population, probabilities, number):
      chosen = []
      for n in range(number):
          r = random.random()
          for (i, individual) in enumerate(population):
              if r &lt;= probabilities[i]:
                  chosen.append(list(individual))
                  break
      return chosen
  
  number=2
  </code></pre>
  
  <p>The population element is: [[individual],[fitness],[counter]]</p>
  
  <p>The probabilities function output is: <code>[0.42857142857142855, 0.5714285714285714, 0.8571428571428571, 1.0]</code></p>
  
  <p>What I notice here is that the previous weight is summed up to the next one, not necessarily being in crescent order, so a think a higher weight is given to the cromosome with a lowest fitness.</p>
  
  <p>I dont want to order it because I need to index the lists by position later, so I think I will have wrong matches. </p>
  
  <p>Anyone knows a possible solution, package or different approach to perform a weighted the selection in this case?</p>
  
  <p>p.s: I know the dictionary may be redundant here, but I had several other problems using the list itself.</p>
  
  <p>Edit: I tried to use <code>random.choices()</code> as you can see below (using relative fitness):</p>
  
  <pre><code>def FitnessChoices(population, probabilities, number):
      return random.choices(population, probabilities, number)
  </code></pre>
  
  <p>But I get this error: <code>TypeError: choices() takes from 2 to 3 positional arguments but 4 were given</code></p>
  
  <p>Thank you!</p>