A plant breeder describes each seed line by a string of 0s and 1s (one character per gene). A
line's fitness is the number of positions where it agrees with the ideal string target.
Write next_generation(pop, target, elite, tournaments, cuts, flips) that returns the next
generation, built with these rules:
- Keep the best. The
elitefittest lines ofpopare copied unchanged to the front of the new generation, fittest first. Equal fitness: the line that comes earlier inpopgoes first. - Pick parents. Each entry of
tournamentsis a list of indices intopop. Its winner is the fittest of those lines (equal fitness: the smaller index wins). - Cross. Winners are used in pairs: tournaments 0 and 1 give parents
aandbof pair 0, tournaments 2 and 3 give pair 1, and so on. Pairkis cut at positionc = cuts[k]and gives two children, firsta[:c] + b[c:], thenb[:c] + a[c:]. Children are appended in this order until the new generation haslen(pop)lines; a child that no longer fits is dropped. - Mutate. Each
(k, p)inflipsflips characterpof childk(kcounts the children that were kept, from 0; elite lines are never mutated). Flips are applied in the order given.
Return the new generation as a list of strings.
Examples
Input: pop = ["0110", "1010", "1111", "0001"], target = "1011", elite = 1,
tournaments = [[0, 3], [2, 0], [1, 3], [0, 2]], cuts = [1, 3], flips = [(1, 0)]
Output: ["1010", "0111", "0001", "1011"]
Explanation: fitnesses are 1, 3, 3, 2, so the elite is "1010" (it beats "1111" on index).
Pair 0 is "0001" x "1111" cut at 1: "0111" and "1001". Pair 1 is "1010" x "1111" cut at 3:
"1011" (its second child does not fit). Flipping character 0 of child 1 turns "1001" into "0001".
Constraints
1 <= len(pop) <= 60; all lines andtargethave the same lengthL,1 <= L <= 640 <= elite <= len(pop); withm = len(pop) - elitechildren needed,tournamentshas2 * ceil(m / 2)non-empty lists andcutshasceil(m / 2)values with0 <= c <= L- every
(k, p)inflipshas0 <= k < mand0 <= p < L
Goals
- Apply tournament selection, one-point crossover and bit-flip mutation by hand
- Keep the best individuals unchanged from one generation to the next
- Follow a precise specification with tie-breaking rules