code: def average_fitness_termination(population, num_generations, num_evaluations, args):
      
      tolerance = args.setdefault('tolerance', 0.001)
      avg_fit = sum([x.fitness for x in population]) / float(len(population))
      best_fit = max([x.fitness for x in population])
      return (best_fit - avg_fit) < tolerance
docs: Return True if the population's average fitness is near its best fitness.
      
      This function calculates the average fitness of the population, as well
      as the best fitness. If the difference between those values is less 
      than a specified tolerance, the function returns True. 
      
      .. Arguments:
         population -- the population of Individuals
         num_generations -- the number of elapsed generations
         num_evaluations -- the number of candidate solution evaluations
         args -- a dictionary of keyword arguments
      
      Optional keyword arguments in args:
      
      - *tolerance* -- the minimum allowable difference between average 
        and best fitness (default 0.001)