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 crossover operation by flipping values between two parents based on randomly selected indices.
def cross(parent1, parent2): flips_num = 1 + random.randrange(1, len(parent1) - 1) indices_in = random.sample(range(0, len(parent1)), flips_num) child1, child2 = parent1, parent2 child1 = [parent2[i] if i in indices_in else parent1[i] for i in range(len(parent1))] child2 = [parent1[i] if i in indices_in else parent2[i] for i in range(len(parent1))] return child1, child2Mutation Functions
mutation1(individual, p10, p01)
This function mutates an individual by flipping bits based on probabilities p10 (probability of changing 1 to 0) and p01 (probability of changing 0 to 1).
def mutation1(individual, p10, p01): mutated_individual = individual.copy() for i in range(len(individual)): if individual[i] == 0: if random.random() <= p01: mutated_individual[i] = 1 else: if random.random() <= p10: mutated_individual[i] = 0 return mutated_individualmutacion_insercion(individuo)
This function performs an insertion mutation on an individual by randomly selecting two positions and inserting the element at the first position into the second position.
def mutacion_insercion(individuo): pos_1 = random.randint(0,len(individuo)) pos_2 = random.randint(0,len(individuo)) cambiar=individuo[pos_1] individuo.remove(cambiar) individuo.insert(pos_2,cambiar) return(individuo)
Individual and Population Creation
create_individual(n_features)
This function creates an individual with a specified number of features, where each feature is a random integer (0 or 1).
def create_individual(n_features): individual = [0, [random.randint(0, 1) for x in range(n_features)]] return individual[1]create_individual_permu(n_features)
This function creates an individual with a permutation of features.
def create_individual_permu(n_features): individual = [0, list([np.random.permutation(n_features)][0])] return individual[1]crear_poblacion(population_size)
This function creates a population of individuals with a given population_size.
def crear_poblacion(population_size): population = [] for i in range(population_size): ind = create_individual(10) population.append(ind) return populationSelection Functions
roulette_wheel_selection(population, population_fitness)
This function implements roulette wheel selection, where individuals are selected based on their fitness relative to the total fitness of the population.
def roulette_wheel_selection(population, population_fitness): ranked_population, ranked_fitness = rank_population(population, population_fitness) probabilities = [population_fitness[i]/sum(population_fitness) for i in range(len(population_fitness))] distributed_probabilities = list(np.cumsum(probabilities)) alpha = random.random() selected_pos = np.argmin([alpha > distributed_probabilities[i] for i in range(len(distributed_probabilities))]) return population[selected_pos]tournament_selection(population, population_fitness, percentile=10)
This function implements tournament selection, where a subset of the population is selected, and the fittest individual from that subset is chosen.
def tournament_selection(population, population_fitness, percentile=10): tournament_size = int(len(population) * 0.01 * percentile) tournament_pos = random.sample(range(0, len(population)), tournament_size) members, members_fitness = rank_population([population[i] for i in tournament_pos], [population_fitness[i] for i in tournament_pos]) return members[0]