def crossover_and_mutate(population, toolbox, cxpb, mutpb):
    """Part of an evolutionary algorithm applying only the variation part
    (crossover and mutation). The modified individuals have their
    fitness invalidated. The individuals are cloned so the returned population
    is independent of the input population.
    """
    offspring = [toolbox.clone(ind) for ind in population]
    num_crossovers = int(cxpb * len(offspring))
    num_mutations = int(mutpb * len(offspring))
    for i in range(1, num_crossovers + 1, 2):
        offspring[i - 1], offspring[i] = toolbox.mate(offspring[i - 1], offspring[i])
        del offspring[i - 1].fitness.values, offspring[i].fitness.values
    for i in range(num_mutations):
        offspring[i], mutated = toolbox.mutate(offspring[i])
        if mutated:
            del offspring[i].fitness.values
    return offspring