def mutate(self) -> Individual: chromosome = self.chromosome satisfied = self.satisfied no_mutation = gui_get('no_mutation') move_unsatisfied = gui_get('move_unsatisfied_gene') exchange_unsatisfied_genes = gui_get('exchange_unsatisfied_genes') reverse_subseq = gui_get('reverse_subseq') mutations_options = move_unsatisfied + exchange_unsatisfied_genes + reverse_subseq + no_mutation mutation_choice = randint(0, mutations_options) if mutation_choice <= move_unsatisfied: unsatisfied_indices = [ i for i in range(len(satisfied)) if not satisfied[i] ] if not unsatisfied_indices: return self new_chromosome = chromosome.move_unsatisfied_gene( unsatisfied_indices) elif mutation_choice <= move_unsatisfied + exchange_unsatisfied_genes: new_chromosome = chromosome.exchange_unsatisfied_genes(satisfied) elif mutation_choice <= move_unsatisfied + exchange_unsatisfied_genes + reverse_subseq: new_chromosome = chromosome.reverse_subseq() else: return self new_individual = GA_World.individual_class(new_chromosome) return new_individual
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 mutate(self) -> Individual: if randint(0, 100) <= gui_get('invert selection'): new_chromosome = self.chromosome.invert_a_gene() new_individual = GA_World.individual_class(new_chromosome) return new_individual else: return self
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: mutation_operations = [] chromo = self.chromosome assert isinstance(chromo, TSP_Chromosome) if gui_get('Move element'): mutation_operations.append(chromo.move_gene_in_chromosome) if gui_get('2-Opt'): mutation_operations.append(chromo.two_opt) # If no mutation operations have been selected, return the individual unchnged. if not mutation_operations: return self else: # Otherwise, apply one of the selected mutation operation. operation = choice(mutation_operations) # Both of the current mutation operations require self.fitness as their argument. new_chromosome = operation(self.fitness) new_individual = GA_World.individual_class(new_chromosome) return new_individual
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
def mutate(self) -> Individual: new_chromosome = (self.chromosome).replace_gene_in_chromosome( self.fitness) new_individual = GA_World.individual_class(new_chromosome) return new_individual