def crossover(population, parent1, parent2): crossover_point = random.randint(1, 7) print(f"Crossover will happen at point {crossover_point}") parent1_genes = parent1.get_genes() parent2_genes = parent2.get_genes() child1_genes = BitSet(8) child2_genes = BitSet(8) for i in range(crossover_point): child1_genes.set(i, parent1_genes.get(i)) child2_genes.set(i, parent2_genes.get(i)) for i in range(crossover_point, 8): child1_genes.set(i, parent2_genes.get(i)) child2_genes.set(i, parent1_genes.get(i)) child1 = Chromossome() child1.set_genes(child1_genes) print( f"1st child generated from crossover: {utils.format_chromossome(child1)}" ) child2 = Chromossome() child2.set_genes(child2_genes) print( f"2nd child generated from crossover: {utils.format_chromossome(child2)}" ) population.append(child1) population.append(child2)
def crossover(population, parent1, parent2): # K-point crossover # https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)#Two-point_and_k-point_crossover k = random.randint(1, problem.N_QUEENS - 1) crossover_points = [] for _ in range(k): point = random.randint(1, problem.N_QUEENS - 1) while point in crossover_points: point = random.randint(1, problem.N_QUEENS - 1) crossover_points.append(point) crossover_points.sort() crossover_points_str = ", ".join(map(str, crossover_points)) print(f"{k} crossover(s) will happen at point(s) {crossover_points_str}") crossover_points.append(problem.N_QUEENS) parents_genes = [parent1.get_genes(), parent2.get_genes()] child1_genes = [None] * problem.N_QUEENS child2_genes = [None] * problem.N_QUEENS last_point = 0 parent_control = 0 for point in crossover_points: child1_genes[last_point:point] = parents_genes[parent_control][last_point:point] child2_genes[last_point:point] = parents_genes[not parent_control][last_point:point] last_point = point parent_control = not parent_control child1 = Chromossome() child1.set_genes(child1_genes) print(f"1st child generated from crossover: {utils.format_chromossome(child1)}") child2 = Chromossome() child2.set_genes(child2_genes) print(f"2nd child generated from crossover: {utils.format_chromossome(child2)}") population.append(child1) population.append(child2)
def f(chromossome): route = Chromossome.get_fenotype(chromossome.get_genes()) total_weight = 0 total_vertexes = len(GRAPH) for i in range(total_vertexes): vertex = route[i] next_vertex = route[i + 1] edge_weight = GRAPH[vertex][next_vertex] if edge_weight is None: edge_weight = NON_ADJACENT_WEIGHT total_weight += edge_weight return total_weight
def f_chromossome(chromossome): x, y = Chromossome.get_fenotype(chromossome.get_genes()) return f(x, y)
import matplotlib.pyplot as plt import modules.problem as problem from modules.genetics import operators from modules.genetics import utils from modules.genetics.chromossome import Chromossome if __name__ == "__main__": population = [Chromossome() for _ in range(10)] generation = 0 population_score = problem.g_average(population) print( f"Generation # {generation} -> Average population score = {population_score:.3f}\n" ) generation_plot = [] generation_plot.append(generation) population_score_plot = [] population_score_plot.append(population_score) while generation < 50: parent1, parent2 = operators.selection(population) operators.crossover(population, parent1, parent2) operators.mutation(population) operators.elitism(population) generation += 1 population_score = problem.g_average(population)
import matplotlib.pyplot as plt import modules.problem as problem from modules.genetics import operators from modules.genetics import utils from modules.genetics.chromossome import Chromossome if __name__ == "__main__": population = [] population.append(Chromossome(4, 3)) population.append(Chromossome(2, 9)) population.append(Chromossome(9, 11)) population.append(Chromossome(0, 15)) population.append(Chromossome(5, 5)) population.append(Chromossome(14, 3)) generation = 0 population_score = problem.g_average(population) print( f"Generation # {generation} -> Average population score = {population_score:.3f}\n" ) generation_plot = [] generation_plot.append(generation) population_score_plot = [] population_score_plot.append(population_score) while generation < 50: parent1, parent2 = operators.selection(population)
def crossover(population, parent1, parent2): # Order crossover # http://mat.uab.cat/~alseda/MasterOpt/GeneticOperations.pdf parent1_genes = parent1.get_genes() parent2_genes = parent2.get_genes() genes_length = len(parent1_genes) child1_genes = [None] * genes_length child2_genes = [None] * genes_length parent1_subset_start = random.randint(0, genes_length - 1) parent2_subset_start = random.randint(0, genes_length - 1) parent1_subset_end = random.randint(parent1_subset_start + 1, genes_length) parent2_subset_end = random.randint(parent2_subset_start + 1, genes_length) print( f"1st parent subset indexes: {parent1_subset_start} (inclusive) to {parent1_subset_end} (exclusive)" ) print( f"2nd parent subset indexes: {parent2_subset_start} (inclusive) to {parent2_subset_end} (exclusive)" ) child1_genes[parent1_subset_start:parent1_subset_end] = parent1_genes[ parent1_subset_start:parent1_subset_end] child2_genes[parent2_subset_start:parent2_subset_end] = parent2_genes[ parent2_subset_start:parent2_subset_end] parent1_next_index = parent2_next_index = 0 for i in range(genes_length): if child1_genes[i] is None: for k in range(parent2_next_index, genes_length): gene = parent2_genes[k] if gene in child1_genes: continue child1_genes[i] = gene parent2_next_index = k + 1 break if child2_genes[i] is None: for k in range(parent1_next_index, genes_length): gene = parent1_genes[k] if gene in child2_genes: continue child2_genes[i] = gene parent1_next_index = k + 1 break child1 = Chromossome() child1.set_genes(child1_genes) print( f"1st child generated from crossover: {utils.format_chromossome(child1)}" ) child2 = Chromossome() child2.set_genes(child2_genes) print( f"2nd child generated from crossover: {utils.format_chromossome(child2)}" ) population.append(child1) population.append(child2)
import matplotlib.pyplot as plt import modules.problem as problem from modules.genetics import operators from modules.genetics import utils from modules.genetics.chromossome import Chromossome if __name__ == "__main__": population = [Chromossome() for _ in range(10)] generation = 0 population_score = problem.g_average(population) print(f"Generation # {generation} -> Average population score = {population_score:.3f}\n") generation_plot = [] generation_plot.append(generation) population_score_plot = [] population_score_plot.append(population_score) while generation < 50: # pylint: disable=unbalanced-tuple-unpacking parent1, parent2 = operators.selection(population) operators.crossover(population, parent1, parent2) operators.mutation(population) operators.elitism(population) generation += 1 population_score = problem.g_average(population)