def create_new_population(self, operations): #print("\nCreating initial population...") genome = Genome(operations[:]) genome.score = calculate_fitness(genome.operations) self.genomes.append(genome) for _ in range(self.population_size): genome = Genome(shuffle_valid_genome(operations[:])) genome.score = calculate_fitness(genome.operations) self.genomes.append(genome)
def crossover(parent_one, parent_two): if randint(0, 100) > crossover_rate * 100: return (parent_one, parent_two) midpoint = randint(1, task_count - 1) offspring_one, offspring_two = Genome(False), Genome(False) offspring_one.genes = parent_one.genes[:midpoint] + parent_two.genes[ midpoint:] offspring_two.genes = parent_two.genes[:midpoint] + parent_one.genes[ midpoint:] offspring_one.update_duration() offspring_two.update_duration() return (offspring_one, offspring_two)
def make_child(self, mom, dad): child = Genome() for n in range(child.node_count): giver = (mom, dad)[random() > 0.5] child.node_net[n] = giver.node_net[n] child.mutate() return child
def __init__(self, genGenome=False): self.position = None self.prevPosition = None self.sensorInput = [] # should be fixed size self.vector = None self.brain: InsectBrain = None self.fitness = 1 self.canvas_sphere = None self.canvas_line = None # create random genome if genGenome: self.genome = Genome() self.genome.new_gene(size=25, value_bounds=(-1, 1), name="W_inputs") self.genome.new_gene(size=5, value_bounds=(-1, 1), name="B_inputs") self.genome.new_gene(size=25, value_bounds=(-1, 1), name="W_one") self.genome.new_gene(size=5, value_bounds=(-1, 1), name="B_one") self.genome.new_gene(size=25, value_bounds=(-1, 1), name="W_two") self.genome.new_gene(size=5, value_bounds=(-1, 1), name="B_two") self.genome.new_gene(size=5, value_bounds=(-1, 1), name="W_out") self.genome.new_gene(size=1, value_bounds=(-1, 1), name="B_out") self.brain: InsectBrain = InsectBrain(self.genome) else: self.genome = None
def parse_dir(dataset1, dataset2): genomes = [] for dirName, subdirList, fileList in os.walk(".."): # if dataset1 and dataset2 are in VirLab if dirName in ("../genomes" + dataset1, "../genomes" + dataset2): vector = dirName.rsplit("/")[-1] for filename in fileList: # Split file name to obtain file type disease, filetype = filename.rsplit(".", 1) if filetype == "fna": filetype = "fasta" elif filetype == "gbff": filetype = "genbank" # records = list of genomes. Each one having traits records = list(SeqIO.parse(dirName + "/" + filename, filetype)) chars = set('NWKMRYSBVHDX') for genome in records: # handle ambiguous genomic data, throw all ambigous files out if not any((c in chars) for c in genome): my_gen = Genome(vector, disease, str(genome.seq)) genomes.append(my_gen) return genomes
def __init__(self, genome=None, parents=None): if genome != None: self.genome = genome return if parents != None: self.genome = Genome(gametes=(parents[0].genome.gamete(), parents[1].genome.gamete()))
def generate_genome_list(all_possible_genes): """Generate a list of all possible networks. Args: all_possible_genes (dict): The parameter choices Returns: networks (list): A list of network objects """ genomes = [] # This is silly. for nbn in all_possible_genes['nb_neurons']: for nbl in all_possible_genes['nb_layers']: for a in all_possible_genes['activation']: for o in all_possible_genes['optimizer']: # Set the parameters. genome = { 'nb_neurons': nbn, 'nb_layers': nbl, 'activation': a, 'optimizer': o, } # Instantiate a network object with set parameters. genome_obj = Genome() genome_obj.set_genes_to(genome, 0, 0) genomes.append(genome_obj) return genomes
def run(config_file): # Load configuration. config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # Create the population, which is the top-level object for a NEAT run. p = neat.Population(config) # Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5)) # Run for up to 300 generations. winner = p.run(eval_genomes, 300) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) # Show output of the most fit genome against training data. print('\nOutput:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) genome = Genome(winner_net) print(f'\nFitness winner: {genome.get_fitness()}') genome.visualize()
def test_setting_layers(self): """ Tests correctness of layer formation operation. """ # Setup genome = Genome() genome.initialize(2, 1) inputs = [1, 2] outputs = [6] connections = [ Connection(1, 5), Connection(2, 5), Connection(4, 6), Connection(3, 6), Connection(5, 4), Connection(5, 3) ] genome.input_nodes = inputs genome.output_nodes = outputs genome.connection_genes = connections # Run graph = Graph() layers = graph.set_up_layers(genome.get_inputs(), genome.get_outputs(), genome.get_connections()) # Assert self.assertEqual(layers[0], {5}) # first non-input layer self.assertEqual(layers[1], {3, 4}) # second non-input layer self.assertEqual(layers[2], {6}) # output layer
def crossover(genome_a: Genome, genome_b: Genome): child = Genome() maxInn = max(genome_a.connectionGenes.maxInnovationNumber, genome_b.connectionGenes.maxInnovationNumber) child.nodeGenes.nodes += genome_a.nodeGenes.get_nodes_by_type( NodeType.INPUT) child.nodeGenes.nodes += genome_a.nodeGenes.get_nodes_by_type( NodeType.OUTPUT) for i in range(1, maxInn + 1): if (not genome_a.connectionGenes.get_connection_by_innovation(i) and genome_b.connectionGenes.get_connection_by_innovation(i)) or\ (genome_b.connectionGenes.get_connection_by_innovation(i) and genome_b.fitness > genome_a.fitness): child.add_gene( copy.deepcopy( genome_b.connectionGenes.get_connection_by_innovation(i))) elif (not genome_b.connectionGenes.get_connection_by_innovation(i) and genome_a.connectionGenes.get_connection_by_innovation(i)) or\ (genome_a.connectionGenes.get_connection_by_innovation(i) and genome_a.fitness > genome_b.fitness): child.add_gene( copy.deepcopy( genome_a.connectionGenes.get_connection_by_innovation(i))) elif genome_a.connectionGenes.get_connection_by_innovation(i) and genome_b.connectionGenes.get_connection_by_innovation(i) and\ genome_a.fitness == genome_b.fitness: child.add_gene( copy.deepcopy( random.choice([ genome_a, genome_b ]).connectionGenes.get_connection_by_innovation(i))) return child
def __init__(self, row, column, state_size=24, action_size=13, genome=None, memories=None): self.id = str(uuid.uuid4()) self.color = ORGANISM_COLOR self.width = ORGANISM_WIDTH self.height = ORGANISM_HEIGHT # Set position in game grid self._set_position(row, column) self.energy = INITIAL_ENERGY self.alive = True if not genome: self.genome = Genome(organism_id=self.id) else: self.genome = genome self.genome.organism_id = self.id # DQN Stuff self.state_size = state_size self.action_size = action_size self.memory = deque(maxlen=2000) self.gamma = 0.95 # discount rate self.epsilon = 1.0 # exploration rate self.epsilon_min = 0.01 self.epsilon_decay = 0.995 self.learning_rate = 0.001 self.model = self._build_model(self.genome.geneparam) self.past_memories = memories if self.past_memories: self.train_from_initial(self.past_memories)
def create_offspring(self, genome: Genome) -> Genome: neuron_gene_list = [] neuron_gene_dict = {} for i in range(len(genome.neuron_gene_list)): original_gene = genome.neuron_gene_list[i] neuron_gene = NeuronGene(original_gene.inno_id, original_gene.activation_fn, original_gene.type) neuron_gene_list.append(neuron_gene) neuron_gene_dict[neuron_gene.inno_id] = neuron_gene connection_genes = [] for conn in genome.connection_gene_list: connection_genes.append( ConnectionGene(conn.inno_id, conn.from_id, conn.to_id, conn.weight)) from_neuron = neuron_gene_dict[conn.from_id] to_neuron = neuron_gene_dict[conn.to_id] from_neuron.target_neurons.append(to_neuron) to_neuron.source_neurons.append(from_neuron) new_genome = Genome(neuron_gene_dict, neuron_gene_list, connection_genes, self.current_generation) self.mutate_genome(new_genome) return new_genome
def crossover(parent1: dict, parent2: dict, config: Config): assert parent1 != parent2 if parent1.fitness < parent2.fitness: parent1, parent2 = deepcopy(parent2), deepcopy(parent1) # Setup p1_genome = parent1.genome p2_genome = parent1.genome child = Individ(id= next(config.global_individ_id_counter), genome= Genome()) # Mix the genes chromosomes_that_can_average_values = ["weight_chromosome", "bias_chromosome"] # crossover neruons for chromosome_key, parent1_chromosome in vars(p1_genome).items(): parent2_chromosome = p2_genome.__getattribute__(chromosome_key) child_chromsome = deepcopy(parent1_chromosome) for gene_id in child_chromsome: if gene_id in parent2_chromosome: # cross over crossover_event = random.random() if crossover_event < config.crossover_mix_genes_rate and chromosome_key in chromosomes_that_can_average_values: child_chromsome[gene_id] = (parent1_chromosome[gene_id].value + parent2_chromosome[gene_id].value) * 0.5 elif crossover_event < config.crossover_mix_genes_rate + (1 - config.crossover_mix_genes_rate) / 2: # keep gene from parent 2 child_chromsome[gene_id] = parent2_chromosome[gene_id] else: # keep gene from parent 1 pass child.genome.__setattr__(chromosome_key, child_chromsome) return child
def _combine_genomes(self, mom, dad): child_gene = {} mom_gene = mom.genome.geneparam dad_gene = dad.genome.geneparam # Choose the optimizer child_gene['optimizer'] = random.choice( [mom_gene['optimizer'], dad_gene['optimizer']]) # Combine the layers max_len = max(len(mom_gene['layers']), len(dad_gene['layers'])) child_layers = [] for pos in range(max_len): from_mom = bool(random.getrandbits(1)) # Add the layer from the correct parent IF it exists. Otherwise add nothing if from_mom and len(mom_gene['layers']) > pos: child_layers.append(mom_gene['layers'][pos]) elif not from_mom and len(dad_gene['layers']) > pos: child_layers.append(dad_gene['layers'][pos]) child_gene['layers'] = child_layers child = Genome(child_gene, mom_id=mom.id, dad_id=dad.id) # Randomly mutate one gene if MUTATE_CHANCE > random.random(): child.mutate_one_gene() return child
def __init__(self, population: int, input_nodes: int, output_nodes: int): self.population: int = population self.input_nodes: int = input_nodes self.output_nodes = output_nodes self.species: List[Species] = [] for _ in range(population): self.add_to_species(Genome(input_nodes, output_nodes))
def ga(): scores = [] best = None average = None worst = None data = {} agents = [Genome(GRID_SIZE) for _ in range(POPULATION)] for generation in range(GENERATIONS): print("Genomeration: %d Population: %d" % (generation, len(agents))) fitness = [- x.get_fitness() for x in agents] stat = (max(fitness), np.mean(fitness), min(fitness)) scores.append(stat) print("Best: %.2f Avg: %.2f Worst: %.2f" % stat) # Fitness pre-calculated adn stored in genome # selection next_generation = selection(agents) # crossover next_generation += crossover(agents) # mutation next_generation = [agent.mutate() for agent in next_generation] agents = sorted(next_generation, key=lambda x: x.get_fitness()) best = agents[0] average = agents[int(POPULATION/2)] worst = agents[-1] # print(np.mean([x.get_fitness() for x in agents])) # pprint.pprint(best.array_representation()) if best.get_fitness() == 0: print("Found optimal!") break # best = min(agents, key=lambda x: x.get_fitness()) # pprint.pprint(best.array_representation()) generations = [x for x in range(GENERATIONS)] data_preproc = pd.DataFrame({ 'Generation': generations, 'Best': [x[0] for x in scores], 'Average': [x[1] for x in scores], 'Worst': [x[2] for x in scores]}) sns.lineplot(x='Generation', y='value', hue='variable', data=pd.melt(data_preproc[data_preproc.index % 5 == 0], ['Generation'])) plt.show() data['scores'] = scores data['best'] = best.array_representation() data['average'] = average.array_representation() data['worst'] = worst.array_representation() with open('data.json', 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4)
def meiosis(in1, in2, N=4, v=2, oc=True): '''v defines the shape of the gamma distribution, it is required to have a non-zero shape parameter if v = 0, we assume user means no crossover model v =1 corresponds to no interference obligate crossover means use the obligate crossover version''' if N > 4: raise IndexError( 'Maximum of four distinct meiotic products to sample.') genomes = [genome.reference_genome() for _ in range(4)] if v != 0: if oc: crossover_fn = oc_get_crossover_points else: crossover_fn = get_crossover_points else: crossover_fn = lambda x, y: [] for idx, (start, end) in enumerate(utils.pairwise(Genome.chrom_breaks)): c1, c2 = in1.genome[start:end], in2.genome[start:end] xpoints = crossover_fn(v, len(c1)) #log.debug('Chr %d, xpoints=%s',chrom_names[idx],xpoints) c1, c2, c3, c4 = crossover(c1, c2, xpoints) #independent assortment outputs = sorted([c1, c2, c3, c4], key=lambda *args: random.random()) for j in range(4): genomes[j][start:end] = outputs[j] return [Genome(genomes[j]) for j in range(N)]
def test_adding_node(self): """ Tests adding node operation. """ # Setup genome = Genome() genome.initialize(3, 1) genome_connections = genome.get_connections() # List of enabled (respectively, disabled) connections only. init_true_connections = list( filter(lambda a: a.is_enabled() == True, genome_connections)) init_false_connections = list( filter(lambda a: a.is_enabled() == False, genome_connections)) init_connections_length = len(genome_connections) # Run genome.add_node() # Assert self.assertTrue( len( list( filter(lambda x: x.is_enabled() == True, genome.get_connections()))), len(init_true_connections) + 1) self.assertTrue( len( list( filter(lambda x: x.is_enabled() == False, genome.get_connections()))), len(init_false_connections) + 1) self.assertEqual(len(genome.get_connections()), init_connections_length + 2)
def test_genome_get_gc_content(sequence, gc_content): global genome passed = True try: genome = Genome(sequence) except Exception: genome = None passed = False assert passed, 'Error while creating Genome.' check_init() try: gc = genome.get_gc_content() except Exception: passed = False assert passed, 'Error in Genome.get_gc_content().' try: passed = isclose(gc, gc_content, rel_tol=0, abs_tol=1e-5) except Exception: passed = False assert passed, 'Incorrect GC content.'
def __init__(self, P, eco_type): self.num_loci = P.num_loci self.sigma = P.SIGMA # strength of selection for local adaptation self.eco_type = eco_type # the ecosystem the ind is currently living in self.k = P.num_env_fac self.genome = Genome(self.k, P.num_loci) self.fitness = self.find_fitness(self.eco_type) self.juvenile = True # flag for if individual is juvenile. All inds begin as juveniles
def create_blank_neuron(self) -> Genome: neuron_list = [ NeuronGene(a.inno_id, a.activation_fn, a.type) for a in self.basic_neurons ] neurons = {a.inno_id: a for a in neuron_list} return Genome(neurons, neuron_list, [], self.current_generation)
def __init__(self, init_pos, init_vel, size, color, genome=None): self.genome = Genome(**dict(outer_vision_rad=int(size / 2) + 100, inner_vision_rad=int(size / 2) + 30, food_pref=1, poison_pref=-0.25, max_speed=8)) super().__init__(init_pos, size, self.get_sprite_img(size, color), init_vel) self.health = self.MAX_HEALTH
def parse(self): # initialise empty dictionary of haplotype blocks: blocks = {0: Genome(self._sample), 1: Genome(self._sample)} # keep list of chromosomes visited already, to detect phasing chroms_seen = [] # loop on hapfile and parse informative lines (skipping display-only lines) for line in self.file: if not self._is_chromline(line): continue else: chrom, newblocks = self._parse_chromline(line) phase = self._get_phase(chrom, chroms_seen) chroms_seen.append(chrom) for b in newblocks: blocks[phase].add_segment(chrom, b[1], b[2], phase, b[0]) self.hapblocks = blocks
def __init__(self, size): self.size = size self.cur = 0 self.gen = 0 self.individuals = [] for _ in range(self.size): g = Genome(10) g.randomize() self.individuals.append(g)
def crossover(genome1, genome2, more_fit_crossover_rate=0.8, less_fit_crossover_rate=0.2): """ Input: genome1: the more fit genome genome2: the less fit genome more_fit_crossover_rate: the rate at which genes only occurring in the more fit gene are used less_fit_crossover_rate: -"- ----- Combine the genome to get a child-genome. Mutate the child genome. """ if genome1 == genome2: return genome1.copy() population = genome1.population child_genes = [] child_nodes = [] disabled_ids = [] # Genes ids_1, ids_2 = map(lambda x: set(x.genes_by_id.keys()), [genome1, genome2]) for _id in ids_1 | ids_2: if _id in ids_1: if _id in ids_2: gene = genome1.genes_by_id[_id].copy() if not gene.enabled and genome2.genes_by_id[_id].enabled: gene = genome2.genes_by_id[_id].copy() child_genes += [gene] else: gene = genome1.genes_by_id[_id].copy() if random.random() > more_fit_crossover_rate: disabled_ids += [_id] child_genes += [gene] else: gene = genome2.genes_by_id[_id].copy() if random.random() > less_fit_crossover_rate: disabled_ids += [_id] child_genes += [gene] # Nodes node_ids_1, node_ids_2 = map(lambda x: set(x.nodes_by_id.keys()), [genome1, genome2]) for _id in node_ids_1 | node_ids_2: if _id in node_ids_1: child_nodes += [genome1.nodes_by_id[_id].copy()] else: child_nodes += [genome2.nodes_by_id[_id].copy()] child_genome = Genome(population, optimizer=genome1.optimizer.copy(), nodes_and_genes=[child_nodes, child_genes]) random.shuffle(disabled_ids) for _id in disabled_ids: child_genome.disable_edge(child_genome.genes_by_id[_id]) return child_genome
def reproduce_population(self): """ The miracle of life This method will take two random parents and create two children from them. """ first_child, second_child = self.mate() mutate_genome(first_child) mutate_genome(second_child) first_genome = Genome(first_child) second_genome = Genome(second_child) first_genome.score = calculate_fitness(first_child) second_genome.score = calculate_fitness(second_child) self.genomes.append(first_genome) self.genomes.append(second_genome) self.sort_population() self.reap_population()
def random_genome(self): arrays = self.model.get_weights() array_new = [] for i in arrays: reshaped = i.reshape(-1) for n in range(len(reshaped)): reshaped[n] = random.uniform(-10, 10) array_new.append(i) return Genome(array_new)
def init_genomes(self, amount_input, amount_output, amount_genomes): node_genes = NodeGenes() node_genes.add_new_node(NodeType.INPUT, amount_input) node_genes.add_new_node(NodeType.OUTPUT, amount_output) self.genomes = [] for i in range(amount_genomes): new_genome = Genome() new_genome.nodeGenes = copy.deepcopy(node_genes) self.__init_new_genome(new_genome) self.genomes.append(new_genome)
def found(self, n=50, genome_size=10, ambient_fitness=1.5): """ Generate n individuals and create population """ for _ in range(n): a_new_genome = Genome() a_new_genome.expand(genome_size) # genome initilization self.individuals.append( Individual( genome=a_new_genome, fitness=ambient_fitness, ))
def __init__(self, nb_entrees, nb_sorties, idInd): self.nb_e = nb_entrees self.nb_s = nb_sorties self.id = idInd self.espece = None self.genome = Genome(self.nb_e, self.nb_s) self.phenotype = Phenotype(self.nb_e, self.nb_s) self.idToPos = { } #Ce tableau fera l'interface entre le genome et l'individu self.fitness = None self.sharedFitness = None