Genetic Algorithm: Crossover, Mutation, and Selection
Classified in Mathematics
Written on in
English with a size of 7.56 KB
Genetic Algorithm: Crossover, Mutation, and Selection
This document outlines several key functions used in genetic algorithms, focusing on crossover, mutation, and selection techniques.
Crossover Functions
crossover_n(parent1, parent2)
This function performs a crossover between two parents, parent1 and parent2, by selecting two random cut points and swapping the segments between them.
def crossover_n(parent1, parent2): cortes = random.sample(range(len(parent1)), 2) cortes = sorted(cortes) hijo1 = parent1[:cortes[0]] + parent2[cortes[0]:cortes[1]] + parent1[cortes[1]:] hijo2 = parent2[:cortes[0]] + parent1[cortes[0]:cortes[1]] + parent2[cortes[1]:] return hijo1, hijo2cross(parent1, parent2)
This function implements a