def move_gene_in_chromosome(self, original_fitness: float) -> TSP_Chromosome: """ Move a random gene to the point in the chromosome that will produce the best result. """ (best_new_chrom, best_new_fitness) = (None, None) len_chrom = len(self) for i in sample(range(len_chrom), min(3, len_chrom)): gene_before = self[i - 1] removed_gene = self[i] i_p_1 = (i + 1) % len_chrom gene_after = self[i_p_1] fitness_after_removal = original_fitness - gene_before.distance_to(removed_gene) \ - removed_gene.distance_to(gene_after) \ + gene_before.distance_to(gene_after) # noinspection PyArgumentList partial_chromosome: TSP_Chromosome = GA_World.chromosome_class( self[:i] + self[i + 1:]) (new_chrom, new_fitness) = partial_chromosome.add_gene_to_chromosome( fitness_after_removal, removed_gene) if best_new_fitness is None or new_fitness < best_new_fitness: (best_new_chrom, best_new_fitness) = (new_chrom, new_fitness) return GA_World.chromosome_class(best_new_chrom)
def gen_individual(): chromosome_list: List = sample(GA_World.gene_pool, Cycle_World.cycle_length) Cycle_World.individuals += 1 individual = GA_World.individual_class( GA_World.chromosome_class(chromosome_list)) return individual
def gen_individual(self): # Use ceil to ensure we have enough genes. zeros = [0]*ceil(self.chromosome_length/2) ones = [1]*ceil(self.chromosome_length/2) mixture = sample(zeros + ones, self.chromosome_length) chromosome_tuple: Tuple[Gene] = GA_World.chromosome_class(Gene(id, val) for (id, val) in zip(count(), mixture)) individual = GA_World.individual_class(chromosome_tuple) return individual
def gen_individual(): path_methods = [TSP_Chromosome.random_path] if gui_get('Greedy'): path_methods.append(TSP_Chromosome.greedy_path) if gui_get('Min spanning tree'): path_methods.append(TSP_Chromosome.spanning_tree_path) gen_path_method = choice(path_methods) chromosome_list: List = gen_path_method() chromo = GA_World.chromosome_class(chromosome_list) individual = GA_World.individual_class(chromo, generator=gen_path_method, original_sequence=chromosome_list) return individual
def mutate(self) -> Individual: chromosome = self.chromosome if randint(0, 100) <= SimEngine.gui_get('replace_gene'): (chromosome, self.fitness, _) = chromosome.replace_gene_in_chromosome(self.fitness) elif randint(0, 100) <= SimEngine.gui_get('reverse_subseq'): chromosome = chromosome.reverse_subseq() self.fitness = self.compute_fitness() self.chromosome: Chromosome = GA_World.chromosome_class(chromosome) return self
def create_node(self): new_point = self.create_random_agent(color=Color('white'), shape_name='node', scale=1) new_point.set_velocity(TSP_World.random_velocity()) # If the same individual is in the population # multiple times, don't process it more than once. updated_chromos = set() for ind in self.population: old_chromo = ind.chromosome if old_chromo not in updated_chromos: # noinspection PyUnresolvedReferences (new_chromo, ind.fitness) = old_chromo.add_gene_to_chromosome(ind.fitness, new_point) # noinspection PyArgumentList ind.chromosome = GA_World.chromosome_class(new_chromo) updated_chromos.add(new_chromo)
def delete_node(self): # Can't use choice with a set. node = sample(World.agents, 1)[0] # If the same individual is in the population # multiple times, don't process it more than once. updated_chromos = set() for ind in self.population: old_chromo = ind.chromosome if old_chromo not in updated_chromos: new_chromo_list = list(old_chromo) new_chromo_list.remove(node) new_chromo = GA_World.chromosome_class(new_chromo_list) ind.chromosome = new_chromo updated_chromos.add(new_chromo) node.delete()
def update_cycle_lengths(self, cycle_length): for ind in self.population: # To keep PyCharm's type checker happy assert isinstance(ind.chromosome, Loop_Chromosome) chromosome: Loop_Chromosome = ind.chromosome if cycle_length < len(chromosome): new_chromosome = chromosome[:cycle_length] ind.fitness = ind.compute_fitness() else: available_genes = GA_World.agents - set(chromosome) new_genes = sample(available_genes, cycle_length - len(chromosome)) new_chromosome = chromosome for gene in new_genes: (new_chromosome, ind.fitness, _) = \ chromosome.add_gene_to_chromosome(ind.fitness, gene) ind.chromosome = GA_World.chromosome_class(new_chromosome)
def mutate(self) -> Individual: chromosome = self.chromosome if randint(0, 100) <= SimEngine.gui_get('exchange_genes'): assert isinstance(self.chromosome, Segregation_Chromosome) chromosome = chromosome.exchange_genes(self.satisfactions) elif randint(0, 100) <= SimEngine.gui_get('move_unhappy_gene'): candidate_indices = Segregation_Individual.unhappy_gene_value_indices(self.satisfactions) if not candidate_indices: return self chromosome = chromosome.move_unhappy_gene(candidate_indices) elif randint(0, 100) <= SimEngine.gui_get('move_gene'): chromosome = chromosome.move_gene() elif randint(0, 100) <= SimEngine.gui_get('reverse_subseq'): chromosome = chromosome.reverse_subseq() self.chromosome = GA_World.chromosome_class(chromosome) (self.satisfactions, self.fitness) = self.chromosome.compute_chromosome_fitness() return self
def gen_individual(): chromosome_tuple: Tuple[Gene] = GA_World.chromosome_class( Gene(id, val) for (id, val) in zip(count(), GA_World.gene_pool)) individual = GA_World.individual_class(chromosome_tuple) return individual
def gen_individual(self): chromosome_list: List = sample(World.agents, self.cycle_length) individual = GA_World.individual_class( GA_World.chromosome_class(chromosome_list)) return individual