title: Why does the all the generated populations becomes the same?
body: <p>So I am making genetic algorithms where the fittest chromosome is the one that is all ones [1,1,1,1...]. I coded the whole thing below while debugging I found that crossover, mutation, generate_population works and I went through the code for my other functions and they all make sense. But whenever I run it I find that after the second or third generation all my other generations are the same. so my questions are that why is that happening because I went through my code and every new generation should have a different and "fitter" population and there should be variety among each population. Any help would be really appreciated. <a href="https://i.stack.imgur.com/JxzFc.png" rel="nofollow noreferrer">result image</a></p>
  
  <pre><code>import random
  
  def generate_population(size):
      population = []
      for i in range(size):
          individual = []
          for g in range(64):
            x = random.randrange(0,2)
            individual.append(x)
          population.append(individual)
      return population
  
  def fitnessFunc(individual):
    fit = 0
    for i in individual:
      if i == 1:
        fit += 1
      else:
        fit = fit 
    return fit
  
  
  def choice_by_roulette(sorted_population, fitness_sum):
      offset = 0
      normalized_fitness_sum = fitness_sum
      lowest_fitness = fitnessFunc(sorted_population[0])
      if lowest_fitness &lt; 0:
          offset = lowest_fitness
          normalized_fitness_sum += offset * len(sorted_population)
  
      draw = random.uniform(0, 1)
  
      accumulated = 0
      for individual in sorted_population:
          fitness = fitnessFunc(individual)+offset
          probability = fitness / normalized_fitness_sum
          accumulated += probability
  
          if draw &lt;= accumulated:
              return individual
  
  def sort_population_by_fitness(population):
      return sorted(population, key=fitnessFunc)
  
  def crossover(individual_a, individual_b):
    for i in range(64):
      pop = random.randint(1,2)
      if pop == 1:
        individual_a[i] = individual_b[i]
      else:
        individual_a = individual_a
    return individual_a
  
  def mutate(individual):
      rand = random.randrange(0,10)
      if rand == 5:
        if individual[rand]==1:
          individual[rand]=0
        else:
          individual[rand]=1
      return individual
  
  def make_next_generation(previous_population):
      next_generation = []
      sorted_by_fitness_population = sort_population_by_fitness(previous_population)
      population_size = len(previous_population)
      fitness_sum = sum(fitnessFunc(individual) for individual in population)
      for i in range(population_size):
          choice = choice_by_roulette(sorted_by_fitness_population, fitness_sum)
          schoice = choice_by_roulette(sorted_by_fitness_population, fitness_sum)
          if choice != None:
            first_choice = choice
          if schoice != None:
            second_choice = schoice
          individual = crossover(first_choice, second_choice)
          individual = mutate(individual)
          next_generation.append(individual)
      return next_generation
  
  
  population = generate_population(size=10)
  generations = 1000
  
  i = 1
  while True:
      print(f" GENERATION {i}")
  
      for individual in population:
          print(individual, fitnessFunc(individual))
      if i == generations:
          break
      i += 1
      population = make_next_generation(population)
  
  best_individual = sort_population_by_fitness(population)[-1]
  print(" FINAL RESULT")
  print(best_individual, fitnessFunc(best_individual))
  </code></pre>