def crossover(self, other_chromosome, every_other=True): new_path = list([]) if every_other: # Combines chromosome by alternating allels. self_index = 0 other_index = 1 my_turn = True while len(new_path) < len(self.route.vertices) - 1: if my_turn and self_index < len( self.route.vertices) - 1: if self.route.vertices[ self_index].vertex_id not in new_path: new_path.append( self.route.vertices[self_index].vertex_id) my_turn = False self_index += 1 else: if other_chromosome.route.vertices[ other_index].vertex_id not in new_path: new_path.append( other_chromosome.route. vertices[other_index].vertex_id) my_turn = True if other_index >= len( other_chromosome.route.vertices) - 2: my_turn = True else: other_index += 1 else: # Splits the two chromosomes down the middle index = 0 while len(new_path) < len(self.route.vertices) - 1: if index < len(self.route.vertices) // 2: if self.route.vertices[ index].vertex_id not in new_path: new_path.append( self.route.vertices[index].vertex_id) index += 1 else: remaining_vertices = [ vertex for vertex in self.route.vertices if vertex.vertex_id not in new_path ] for remainining_vertex in remaining_vertices: new_path.append(remainining_vertex.vertex_id) new_route = Route(self.route.graph) new_route.walk_complete_path(new_path) resultant_chromosome = TravelingSalesman.GeneticAlgorithm.Chromosome( None, new_route) return resultant_chromosome
def initialize_population(self): city_range = list(range(1, 1 + len(self.graph.vertices))) for chromosome_index in range(self.population_size): random_city_order = deepcopy(city_range) random.shuffle(random_city_order) random_route = Route(graph) random_route.walk_complete_path(random_city_order) chromosome = TravelingSalesman.GeneticAlgorithm.Chromosome( chromosome_index, random_route) self.population.append(chromosome) self.population = np.array(self.population)
def initialize_population(self): city_range = list(range(1, 1 + len(self.graph.vertices))) for chromosome_index in range(self.population_size): random_city_order = deepcopy(city_range, memo={}) random.shuffle(random_city_order) random_route = Route(self.graph) random_route.walk_complete_path(random_city_order) chromosome = GeneticAlgorithm.Chromosome( chromosome_index, random_route, crossover_method=self.crossover_method, mutation_method=self.mutation_method) self.population.append(chromosome) self.population = np.array(self.population)
def build_chromosome_from_path_and_graph(chromosome_id, path, graph, crossover_method, mutation_method): route = Route(graph) route.walk_complete_path(path) return GeneticAlgorithm.Chromosome(chromosome_id, route, crossover_method, mutation_method)
def crossover(self, other_chromosome): new_path = list([]) if self.crossover_method == GeneticAlgorithm.Chromosome.CrossoverMethods.UNIFORM: self_turn = True while len(new_path) < len(self.route.vertices) - 1: if self_turn: remaining_vertex_ids = [ vertex.vertex_id for vertex in self.route.vertices if vertex.vertex_id not in new_path ] if len(remaining_vertex_ids) > 0: new_path.append( random.choice(remaining_vertex_ids)) self_turn = False else: remaining_vertex_ids = [ vertex.vertex_id for vertex in other_chromosome.route.vertices if vertex.vertex_id not in new_path ] if len(remaining_vertex_ids) > 0: new_path.append( random.choice(remaining_vertex_ids)) self_turn = True elif self.crossover_method == GeneticAlgorithm.Chromosome.CrossoverMethods.PARTIALLY_MAPPED: p1 = random.randint(1, len(self.route.vertices) - 3) p2 = random.randint(p1 + 1, len(self.route.vertices) - 2) self_ids = [ vertex.vertex_id for vertex in self.route.vertices ][:-1] self_s1 = self_ids[:p1] self_s2 = self_ids[p1:p2] self_s3 = self_ids[p2:] other_ids = [ vertex.vertex_id for vertex in other_chromosome.route.vertices ][:-1] other_s1 = other_ids[:p1] other_s2 = other_ids[p1:p2] other_s3 = other_ids[p2:] new_path = self_s1 s2_left = list([]) for vertex_id in other_s2: if vertex_id not in self_s1 and vertex_id not in self_s3: s2_left.append(vertex_id) s3_left = list([]) for vertex_id in other_s3: if vertex_id not in self_s1 and vertex_id not in self_s3: s3_left.append(vertex_id) s1_left = list([]) for vertex_id in other_s1: if vertex_id not in self_s1 and vertex_id not in self_s3: s1_left.append(vertex_id) remaining_vertex_ids = s2_left + s3_left + s1_left new_path += remaining_vertex_ids[:p2 - p1] new_path += self_s3 elif self.crossover_method == GeneticAlgorithm.Chromosome.CrossoverMethods.ORDERED_CROSSOVER: p1 = random.randint(1, len(self.route.vertices) - 3) p2 = random.randint(p1 + 1, len(self.route.vertices) - 2) j_1 = p1 + 1 j_2 = j_1 k = j_1 to_p1 = self.route.vertices[:p1] from_p1 = self.route.vertices[p1:] mid = other_chromosome.route.vertices[p1:p2 + 1] for vertex in to_p1: if vertex not in mid: new_path.append(vertex.vertex_id) for vertex in mid: new_path.append(vertex.vertex_id) for vertex in from_p1: if vertex.vertex_id not in new_path: new_path.append(vertex.vertex_id) else: # Splits the two chromosomes down the middle index = 0 while len(new_path) < len(self.route.vertices) - 1: if index < len(self.route.vertices) // 2: if self.route.vertices[ index].vertex_id not in new_path: new_path.append( self.route.vertices[index].vertex_id) index += 1 else: remaining_vertices = [ vertex for vertex in self.route.vertices if vertex.vertex_id not in new_path ] for remainining_vertex in remaining_vertices: new_path.append(remainining_vertex.vertex_id) new_route = Route(self.route.graph) new_route.walk_complete_path(new_path) resultant_chromosome = GeneticAlgorithm.Chromosome( None, new_route, self.crossover_method, self.mutation_method) return resultant_chromosome