new_contents: import random
  
  import numpy
  import functools
  
  from deap import base
  from deap import creator
  from deap import tools
  
  creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
  creator.create("Individual", list, fitness=creator.FitnessMin)
  
  toolbox = base.Toolbox()
  
  r = 3
  c = 3
  apartmentCount = r*c
  tenant = 4
  
  def draw_indices(n, k):
      return numpy.random.choice(n, k, replace=False)
  
  def draw_tenants(n, k):
      indices = draw_indices(n, k)
      x = 0
      while x < n:
          if x in indices: yield 1
          else: yield 0
          x += 1
  
  toolbox.register("tenants", draw_tenants, n=apartmentCount, k=tenant)
  toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.tenants)
  
  # define the population to be a list of 'individual's
  toolbox.register("population", tools.initRepeat, list, toolbox.individual)
  
  # the goal ('fitness') function to be maximized
  def evalUnhappiness(individual):
      print(individual)
      unhappiness = 0.0
      for x in range(len(individual)):
          if (individual[x]>0):
              if (x>=c and individual[x-c]>0): unhappiness += 0.5
              if (x<(r-1)*c and individual[x+c]>0): unhappiness += 0.5
              if (x%c>0 and individual[x-1]>0): unhappiness += 0.5
              if (x%c<c-1 and individual[x+1]>0): unhappiness += 0.5
      return int(unhappiness),
          
  #----------
  # Operator registration
  #----------
  # register the goal / fitness function
  toolbox.register("evaluate", evalUnhappiness)
  
  # register the crossover operator
  toolbox.register("mate", tools.cxTwoPoint)
  
  # register a mutation operator with a probability to
  # flip each attribute/gene of 0.05
  toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
  
  # operator for selecting individuals for breeding the next
  # generation: each individual of the current generation
  # is replaced by the 'fittest' (best) of three individuals
  # drawn randomly from the current generation.
  toolbox.register("select", tools.selTournament, tournsize=3)
  
  #----------
  
  def main():
      random.seed(64)
  
      # create an initial population of 300 individuals (where
      # each individual is a list of integers)
      pop = toolbox.population(10)
  
      # CXPB  is the probability with which two individuals
      #       are crossed
      #
      # MUTPB is the probability for mutating an individual
      #
      # NGEN  is the number of generations for which the
      #       evolution runs
      CXPB, MUTPB, NGEN = 0.8, 0.2, 40
      
      print("Start of evolution")
      
      # Evaluate the entire population
      fitnesses = list(map(toolbox.evaluate, pop))
      for ind, fit in zip(pop, fitnesses):
          ind.fitness.values = fit
      
      print("  Evaluated %i individuals" % len(pop))
      
      # Begin the evolution
      for g in range(NGEN):
          print("-- Generation %i --" % g)
          
          # Select the next generation individuals
          offspring = toolbox.select(pop, len(pop))
          # Clone the selected individuals
          offspring = list(map(toolbox.clone, offspring))
      
          # Apply crossover and mutation on the offspring
          for child1, child2 in zip(offspring[::2], offspring[1::2]):
  
              # cross two individuals with probability CXPB
              if random.random() < CXPB:
                  toolbox.mate(child1, child2)
  
                  # fitness values of the children
                  # must be recalculated later
                  del child1.fitness.values
                  del child2.fitness.values
  
          for mutant in offspring:
  
              # mutate an individual with probability MUTPB
              if random.random() < MUTPB:
                  toolbox.mutate(mutant)
                  del mutant.fitness.values
      
          # Evaluate the individuals with an invalid fitness
          invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
          fitnesses = map(toolbox.evaluate, invalid_ind)
          for ind, fit in zip(invalid_ind, fitnesses):
              ind.fitness.values = fit
          
          print("  Evaluated %i individuals" % len(invalid_ind))
          
          # The population is entirely replaced by the offspring
          pop[:] = offspring
          
          # Gather all the fitnesses in one list and print the stats
          fits = [ind.fitness.values[0] for ind in pop]
          
          length = len(pop)
          mean = sum(fits) / length
          sum2 = sum(x**2 for x in fits)
          std = abs(sum2 / length - mean**2)**0.5
          
          print("  Min %s" % min(fits))
          print("  Max %s" % max(fits))
          print("  Avg %s" % mean)
          print("  Std %s" % std)
      
      print("-- End of (successful) evolution --")
      
      best_ind = tools.selBest(pop, 1)[0]
      print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
  
  if __name__ == "__main__":
      main()
  
subject: Create noisy_neighbors_ml.py