Crossover chance is a genetic-algorithm setting that controls how often selected parents share genetic material. Depending on the design, it can also specify whether crossover occurs, where recombination happens, or how much sequence is exchanged. section 2.5.9 section 2.6
A simple crossover operation works like this:
- Copy both parent chromosomes to create initial children. section 2.5.6
- Draw a random value and perform crossover when it falls below crossover_rate. section 2.5.6
- Choose a point along the gene sequence, split both parents there, and exchange their suffixes. section 2.5.6
- Treat the rate differently when crossover is assumed: it may then select the crossover point or exchanged amount. section 2.5.9
Experiment with the rate
Rerun the evolutionary notebook after increasing or decreasing crossover_rate, then compare whether evolution performs better for the chosen problem. Also vary mutation rate to inspect the balance between population variety and specialization. section 6.2.1 section 2.5.9
Listing 2.17 EDL_2_5_Genetic_Algorithms.ipynb: Crossover and reproduction
def crossover(parent1, parent2, crossover_rate):
child1, child2 = parent1.copy(), parent2.copy() #1
if random.random() < crossover_rate: #2
pt = random.randint(1, len(parent1)-2) #3
child1 = np.concatenate((parent1[:pt], parent2[pt:])) #4
child2 = np.concatenate((parent2[:pt], parent1[pt:])) #4
return [child1, child2]
crossover(pop[parent[0]],pop[parent[1]], .5) #5
Inspect the Python crossover function, including its probability test, random split point, and NumPy child construction. section 2.5.6
