def __init__(self, genome, twobit_file=None, remote_fn=None, groups=None, trackdb=None, genome_file_obj=None, html_string=None, **kwargs): """ Represents a genome stanza within a "genomes.txt" file for a non-UCSC genome. The file itself is represented by a :class:`GenomesFile` object. """ HubComponent.__init__(self) Genome.__init__(self, genome, trackdb=trackdb, genome_file_obj=genome_file_obj) self.local_fn = twobit_file self.remote_fn = remote_fn self.html_string = html_string if groups is not None: self.add_groups(groups) else: self.groups = None self._orig_kwargs = kwargs self.add_params(**kwargs)
class Organism(object): """ Wrapper class that provides fitness metrics. """ def __init__(self, genome): self.genome = Genome(genome) self.policy = Network(self.genome) self.evals = list() def __cmp__(self, other): return cmp(self.fitness, other.fitness) def __str__(self): return '%.3f' % self.fitness def crossover(self, other): """ Return a new organism by recombining the parents. """ return Organism(self.genome.crossover(other.genome)) def mutate(self, frac=0.1, std=1.0, repl=0.25): """ Mutate the organism by mutating its genome. """ self.genome.mutate(frac, std, repl) self.policy = Network(self.genome) def copy(self): """ Return a deep copy of this organism. """ org = Organism(self.genome) org.evals = list(self.evals) return org @property def fitness(self): """ Average return. """ try: return sum(self.evals, 0.) / len(self.evals) except ZeroDivisionError: return 0.
class GenomeHub(object): """ Hub for a specific genome such as hg18, mm9 """ def __init__(self, name): self._name = name self._genome = Genome(self._name) self._dbroot = TrackDbRoot() self._genome.add_trackdb(self._dbroot) @property def name(self): return self._name @property def genome(self): return self._genome def add_trackdb(self, name): """ create a trackdb with given name for this genome """ trackdb = TrackDb() trackdb.local_fn = os.path.join(self.name, 'trackDb.%s.txt' % name) datadir = os.path.join(self.name, name) if not os.path.exists(datadir): os.mkdir(datadir) self._dbroot.add_trackdbs(trackdb) return trackdb
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 __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 generateGenome(self): """Create a partially connected genome and add it to our species.""" max_genome = 0 if self.genomes: max_genome = max(self.genomes) + 1 genome = Genome(INPUTS,OUTPUTS,self.name,max_genome) for node1 in genome.nodes: genome.mutateAddConnection() self.genomes[max_genome] = genome
def __init__(self, genome=None, ploida=None, ploidb=None, **kw): if genome: self.genome = genome elif ploida and ploidb: self.genome = Genome(ploida, ploidb) else: self.genome = Genome.random(self.traits, self.mutation_rate) self.phenotype = self.genome.make_phenotype(self.trait_ranges) self.__dict__.update(self.phenotype) self.subclass_init( **kw)
def make_random_4genome_process(e1, e2, block_number, chromosome_number, chromosome_constructor): left_ancestor = Genome(block_number, chromosome_number, chromosome_constructor) right_ancestor = left_ancestor.clone().perform_random_inversions(e1) ancestors = dict() ancestors['Left'] = left_ancestor ancestors['Right'] = right_ancestor random_genomes = dict() topology = random.choice(TOPOLOGIES) (a, b), (c, d) = topology random_genomes[a] = left_ancestor.clone().perform_random_inversions(e2) random_genomes[b] = left_ancestor.clone().perform_random_inversions(e2) random_genomes[c] = right_ancestor.clone().perform_random_inversions(e2) random_genomes[d] = right_ancestor.clone().perform_random_inversions(e2) return random_genomes, topology, ancestors
def crossover(self): new = [] self.individuals = sorted(self.individuals, key = lambda ind: ind.score, reverse = True) for _ in range(self.size): p1 = self.select() p2 = self.select() g = Genome(10) g.crossover(p1, p2) new.append(g) self.individuals = new
def default_hub(hub_name, genome, short_label, long_label, email): """ Returns a fully-connected set of hub components using default filenames. """ hub = Hub( hub=hub_name, short_label=short_label, long_label=long_label, email=email) genome = Genome(genome) genomes_file = GenomesFile() trackdb = TrackDb() hub.add_genomes_file(genomes_file) genomes_file.add_genome(genome) genome.add_trackdb(trackdb) return hub, genomes_file, genome, trackdb
class Creature(object): mutation_rate = MutationRate(mutation_rate=0.0, new_gene_chance=0.0, multi_chance=0.0) trait_ranges={"example_trait":(-1,1)} def __init__(self, genome=None, ploida=None, ploidb=None, **kw): if genome: self.genome = genome elif ploida and ploidb: self.genome = Genome(ploida, ploidb) else: self.genome = Genome.random(self.traits, self.mutation_rate) self.phenotype = self.genome.make_phenotype(self.trait_ranges) self.__dict__.update(self.phenotype) self.subclass_init( **kw) def subclass_init(self, **kw): pass @property def traits(self): if not getattr(self, '_traits', None): self._traits = self.trait_ranges.keys() return self._traits def __str__(self): if options.show_genes: return str(self.genome) else: return str(self.phenotype)
def breed_pop(self): """Breed the current population, a better score rank means probably more children.""" pop_size = len(self.goombas) num_clones = ceil(pop_size * World.CLONE_BEST_FRACTION) num_bred = ceil(pop_size * World.BREED_FRACTION) num_rand = ceil(pop_size * World.NEW_RANDOM_FRACTION) # Order the population by final score ordered_pop = sorted([gmba for gmba in self.goombas], key=lambda g: g.score(), reverse=True) # The top few will be cloned into the next generation unchanged top_dogs = ordered_pop[:num_clones] new_goombas = [Goomba.from_sequences(dog.genome.sequences()) for dog in top_dogs] # The bottom fraction is thrown out entirely breeders = ordered_pop[:num_bred] # The remaining goombas breed, with a higher likelihood as they rank higher breed_weighted = dict(zip(breeders, linspace(World.REPRO_SUCCESS_RAMP, 1, len(breeders)))) breeding_pairs = weighted_choice(breed_weighted, 2 * (pop_size - (num_clones + num_rand))) for i in range(0, len(breeding_pairs), 2): new_goombas.append(breed(breeding_pairs[i], breeding_pairs[i + 1])) for i in range(num_rand): new_goombas.append(Goomba(Genome.random_coding(self.seed_meta, randrange(*self.gen_len_range)))) starts = self.start_locations(len(new_goombas)) for gmba, pos in zip(new_goombas, starts): gmba.pos = pos self.goombas = new_goombas
def create_population(self, count): """Create a population of random networks. Args: count (int): Number of networks to generate, aka the size of the population Returns: (list): Population of network objects """ pop = [] i = 0 while i < count: # Initialize a new genome. genome = Genome( self.all_possible_genes, {}, self.ids.get_next_ID(), 0, 0, self.ids.get_Gen() ) # Set it to random parameters. genome.set_genes_random() if i == 0: #this is where we will store all genomes self.master = AllGenomes( genome ) else: # Make sure it is unique.... while self.master.is_duplicate( genome ): genome.mutate_one_gene() # Add the genome to our population. pop.append(genome) # and add to the master list if i > 0: self.master.add_genome(genome) i += 1 #self.master.print_all_genomes() #exit() return pop
def mate(self, genome1, genome2): """ Mate two genomes based on their fitness. Args: genome1: the first parent genome genome2: the second parent genome Returns: genome3: the resultant child genome """ # we want genome1 to be the most fit if genome2.fitness > genome1.fitness: tempGenome = genome1 genome1 = genome2 genome2 = tempGenome assert genome1.fitness >= genome2.fitness genome3 = Genome(INPUTS, OUTPUTS, self.name, None) possible_genes = set(genome1.genes) | set(genome2.genes) for innov in possible_genes: if random.random() < GENE_DOMINANCE: if innov in genome1.genes: genome3.genes[innov] = genome1.genes[innov] else: if innov in genome2.genes: genome3.genes[innov] = genome2.genes[innov] # creating the nodes genome3.generateNodes() genome3.mutate() return genome3
def readGFFs(self, gffFileDir): import os listGFFdir=os.listdir(gffFileDir) GFFListToRead=[] #print listGFFdir #Save files for analysis in listGFFdir count=0 for files in listGFFdir: #WTF maybe? filename=files.replace('.gff','') if filename in self.getFile2GeneDic().keys(): #print filename #print len(self.getFile2GeneDic()[filename]) #print filename #listGFFdir.remove(item) GFFListToRead.append(files) #print len(listGFFdir) #print GFFListToRead #print listGFFdir #only 30!? TODO #setGenomeList=GenomeList(iD=self.ID) setGenomeList=[] for item in GFFListToRead: path=gffFileDir+'/'+item filename=item.replace('.gff','') genome=Genome(name=filename) genome.readGff(path) #reads all genes in file, save in genome #clean genes not present in dictionary listOfGenes=self.getFile2GeneDic()[filename] #print listOfGenes genome.cleanGenome(listOfGenes) #send only the list of values for that file! #print genome setGenomeList.append(genome) self.GenomeList=setGenomeList
def _createInitGenomes(self, seed): ''' Creates the initial set of Genomes in the population @return Genome[] ''' genomes=[] for i in range(self.size): genomes.append(Genome.createRandom(self.sys, self.goal ,seed)) #TODO anything else? return genomes
def __init__(self, origin, caller=None): if type(origin) is Genome: self.subs = [] self.genome = origin self.scored = False self.scores = {} elif type(origin) is Tree: self.genome = Genome(origin.genome) if caller is not None: self.subs = [Tree(sub,(origin,self) ) for sub in origin if sub is not caller[0]] self.subs.append(caller[1]) else: self.subs = [Tree(sub, (origin,self) ) for sub in origin] self.scored = False self.scores = {}
class Creature(object): traits = ["strength", "size", "ferocity", "anger", "health", "penis_length"] def __init__(self, genome=None, ploida=None, ploidb=None, **kw): if genome: self.genome = genome elif ploida and ploidb: self.genome = Genome(ploida, ploidb) else: self.genome = Genome.random(self.traits) self.phenotype = self.genome.make_phenotype() self.__dict__.update(self.phenotype) self.subclass_init(**kw) def subclass_init(self, **kw): pass def __str__(self): if options.show_genes: return str(self.genome) else: return str(self.phenotype)
def __init__(self, tile = None, genome = None, trust_parameter = None, performAction = None): self.performAction = performAction if performAction == None: self.performAction = defaultPerformAction if trust_parameter == None: trust_parameter = TRUST_PARAMETER self.trust_parameter = trust_parameter if LEARN_TRUST: self.trust = Trust() self.genome = genome self.tile = None if genome == None: # then generate your own genome self.genome = Genome() # self.statistics = self.genome.statistics self.name = self.genome.name self.available_moves = GAMES_PER_ITER self.fitness = 0 self.ID = returnRandomID() self.parents = [] self.games_played = 0 if tile != None: tile.acceptAgent(self) self.initializeStatistics()
class Organism(Entity): MAX_HEALTH = 500 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 update(self, screen_bounds, organisms, food): super().update(screen_bounds) self.health = max(0, self.health - 1) self._handle_food_eaten(food) self._look_for_food(food) if self.health <= 0: self.kill() def _look_for_food(self, food): def my_collide_circle(left, right): return pygame.sprite.collide_circle(left, right) orig_rad = self.radius self.radius = self.genome.get("inner_vision_rad") food_inside_inner_rad = pygame.sprite.spritecollide( self, food, False, my_collide_circle) self.radius = self.genome.get("outer_vision_rad") food_inside_outer_rad = pygame.sprite.spritecollide( self, food, False, my_collide_circle) food_seen = [ food for food in food_inside_outer_rad if food not in food_inside_inner_rad ] self.radius = orig_rad if food_seen: food_vectors = [ Vector(*f.rect.center).mult( -1 if self.genome.get("food_pref") < 0 else 1) for f in food_seen if not f.poisonous ] poison_vectors = [ Vector(*f.rect.center).mult( -1 if self.genome.get("poison_pref") < 0 else 1) for f in food_seen if f.poisonous ] if food_vectors: food_force = Vector.steer_forces(food_vectors, Vector(*self.rect.center), self.velocity, self.max_force, self.genome.get("max_speed")) self.apply_force( food_force.mult(abs(self.genome.get("food_pref")))) if poison_vectors: poison_force = Vector.steer_forces( poison_vectors, Vector(*self.rect.center), self.velocity, self.max_force, self.genome.get("max_speed")) self.apply_force( poison_force.mult(abs(self.genome.get("poison_pref")))) def _handle_food_eaten(self, food): food_eaten = pygame.sprite.spritecollide(self, food, False, pygame.sprite.collide_circle) health_diff = 0 for food_entity in food_eaten: health_diff += food_entity.value food_entity.kill() self.health = min(self.MAX_HEALTH, self.health + health_diff) new_color = self._calc_health_color() self.change_color(new_color) def _calc_health_color(self): blue = 0 red = int(255 * ((self.MAX_HEALTH - self.health) / self.MAX_HEALTH)) green = int(255 * (self.health / self.MAX_HEALTH)) return max(min(red, 255), 0), max(min(green, 255), 0), blue def change_color(self, color): self.image = self.get_sprite_img(self.size(), color) @classmethod def get_sprite_img(cls, size, color): sprite_img = pygame.Surface((size, size), pygame.SRCALPHA) pygame.gfxdraw.aacircle(sprite_img, int(size / 2), int(size / 2), int(size / 2), color) pygame.gfxdraw.filled_circle(sprite_img, int(size / 2), int(size / 2), int(size / 2), color) return sprite_img
class Insect: 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 setGenome(self, genome): self.genome = genome self.brain: InsectBrain = InsectBrain(genome) def update(self, food_coords: list): # check if intersecting food coordinates food_to_remove = [] for food in food_coords: # approximating insect body as a square, cannot use sympy due to bug intersects = False if (int(self.position.x) - 5) <= food.x <= (int(self.position.x) + 5): if (int(self.position.y) - 5) <= food.y <= (int(self.position.y) + 5): intersects = True if intersects: self.fitness += 1 food_to_remove.append(food) # for to_remove in food_to_remove: # food_coords.remove(to_remove) # sensors sensor_input = [0, 0, 0, 0, 0] SENSOR_RANGE = 50 for food in food_coords: delta_x = food.x - int(self.position.x) delta_y = food.y - int(self.position.y) vec_to_food = Vector().setXY(delta_x, delta_y) if vec_to_food is None: continue if vec_to_food.mag > SENSOR_RANGE: continue angle = self.vector.clockwiseAngleDeg(vec_to_food) sensor_val = SENSOR_RANGE - float(vec_to_food.mag) / SENSOR_RANGE if -90 >= angle < -45.5: sensor_input[0] += sensor_val elif -45.5 >= angle < -22.5: sensor_input[1] += sensor_val elif -22.5 >= angle < 0 or 0 >= angle < 22.5: sensor_input[2] += sensor_val elif 22.5 >= angle < 45: sensor_input[3] += sensor_val elif 45 >= angle < 90: sensor_input[4] += sensor_val # normalize sensor output max_sensor_val = None min_sensor_val = None for val in sensor_input: if max_sensor_val is None or val > max_sensor_val: max_sensor_val = float(val) if min_sensor_val is None or val < min_sensor_val: min_sensor_val = float(val) max_sensor_val -= min_sensor_val for index, val in enumerate(sensor_input): if max_sensor_val == 0: continue sensor_input[index] = (val - min_sensor_val) / max_sensor_val steering = self.brain.evaluate(np.array(sensor_input).reshape(1, 5)) delta_angle = 10 * steering #print("Sensor: {} deltaDeg: {}".format([round(val, 1) for val in sensor_input], delta_angle)) self.vector.addDeg(delta_angle) # updating position self.prevPosition = self.position self.position = Point( int(self.position.x) + int(self.vector.x), int(self.position.y) + int(self.vector.y)) return food_to_remove def draw(self, canvas: tkinter.Canvas): if self.position is None: return x0, y0 = int(self.position.x) - 5, int(self.position.y) - 5 x1, y1 = int(self.position.x) + 5, int(self.position.y) + 5 if self.canvas_sphere is None: self.canvas_sphere = canvas.create_oval(x0, y0, x1, y1, fill='blue', width=0) if self.canvas_line is not None: canvas.delete(self.canvas_line) self.canvas_line = canvas.create_line( int(self.position.x), int(self.position.y), int(self.position.x) + int(self.vector.x), int(self.position.y) + int(self.vector.y), fill="yellow") canvas.coords(self.canvas_sphere, x0, y0, x1, y1)
def __init__(self, genome): self.genome = Genome(genome) self.policy = Network(self.genome) self.evals = list()
from genome import Genome if __name__ == '__main__': wanted_species = ['Aedes_albopictus', 'Ahrosomya_megacephala', 'Anopheles_funestus', 'Anopheles_albitarsis', 'Anopheles_darlingi', 'Anopheles_gambiae', 'Chrysomya_megacephala', 'Drosophila_melanogaster', 'Drosophila_santomea', 'Drosophila_simulans', 'Drosophila_yakuba', 'Lucilia_cuprina', 'Musca_domestica'] species_objects = [Genome(r'sequences_2\{}.txt'.format(specie)) for specie in wanted_species] for specie in species_objects: specie.print_lengths() # print(species_objects[0].get_list_of_sequences()) # print(species_objects[0].get_just_nucleotides())
def breed(self, mom, dad): """Make two children from parental genes. Args: mother (dict): genome parameters father (dict): genome parameters Returns: (list): Two network objects """ children = [] #where do we recombine? 0, 1, 2, 3, 4... N? #with four genes, there are three choices for the recombination # ___ * ___ * ___ * ___ #0 -> no recombination, and N == length of dictionary -> no recombination #0 and 4 just (re)create more copies of the parents #so the range is always 1 to len(all_possible_genes) - 1 pcl = len(self.all_possible_genes) recomb_loc = random.randint(1,pcl - 1) #for _ in range(2): #make _two_ children - could also make more child1 = {} child2 = {} #enforce defined genome order using list #keys = ['nb_neurons', 'nb_layers', 'activation', 'optimizer'] keys = list(self.all_possible_genes) keys = sorted(keys) #paranoia - just to make sure we do not add unintentional randomization #*** CORE RECOMBINATION CODE **** for x in range(0, pcl): if x < recomb_loc: child1[keys[x]] = mom.geneparam[keys[x]] child2[keys[x]] = dad.geneparam[keys[x]] else: child1[keys[x]] = dad.geneparam[keys[x]] child2[keys[x]] = mom.geneparam[keys[x]] # Initialize a new genome # Set its parameters to those just determined # they both have the same mom and dad genome1 = Genome( self.all_possible_genes, child1, self.ids.get_next_ID(), mom.u_ID, dad.u_ID, self.ids.get_Gen() ) genome2 = Genome( self.all_possible_genes, child2, self.ids.get_next_ID(), mom.u_ID, dad.u_ID, self.ids.get_Gen() ) #at this point, there is zero guarantee that the genome is actually unique # Randomly mutate one gene if self.mutate_chance > random.random(): genome1.mutate_one_gene() if self.mutate_chance > random.random(): genome2.mutate_one_gene() #do we have a unique child or are we just retraining one we already have anyway? while self.master.is_duplicate(genome1): genome1.mutate_one_gene() self.master.add_genome(genome1) while self.master.is_duplicate(genome2): genome2.mutate_one_gene() self.master.add_genome(genome2) children.append(genome1) children.append(genome2) return children
def validate(self): Genome.validate(self)
folder = '' #If a folder is specified, no networks will be generated, it will try and take them from there. mode = 'TRAINING' nGenerations = 90 LOG_FILENAME = './logs/ui.log' logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO, format='%(asctime)s, %(message)s', datefmt='%d/%m/%Y %H:%M:%S') logger = logging.getLogger('main') logger.info(f'Programa lanzado. Datos: Número de genes: {numGenes}, ' f'Probabilidad de mutación: {mutationProb}, Genes seleccionados: {selection}') logger.info(f'Se ha elegido el modo {mode}') logger.info('Loading genome...') genome = Genome(numGenes, mutationProb, selection, folder, nGenerations) logger.info('The genome was successfully loaded') game = Game(mode, nGenerations, numGenes) if mode == 'TRAINING': logger.info(f'Training will last {nGenerations} generations.') for i in range(1, nGenerations): with keyboard.Listener(on_press=keys.on_press) as listener: if not keys.break_program: #End key to stop a running program by the end of the generation. genome.execute_generation(game) if (i % 5 == 0 and i != 0) or i == nGenerations: genome.save_all() genome.kill_and_reproduce()
def test_genome(self): genome = Genome() genome.mutate()
def find_closest_specie(self, genome: Genome, specie_list: List[Specie]) -> Specie: return min([(self.distance_metric.measure_distance( genome.get_position(), b.centroid), b) for b in specie_list], key=lambda x: x[0])[1]
def __init__(self, n, input_size, output_size, evaluate, parent_selection, train, cross_over=crossover, name=None, elitism_rate=0.1, min_species_size=5, n_generations_no_change=5, tol=1e-5, mutate_speed=1, min_species=1, max_species=10, epochs=2, reward_epochs=10, load=None, save_mode="elites", monitor=None, load_params=True): # Evolution parameters self.evaluate = evaluate self.parent_selection = parent_selection self.crossover = cross_over self.train = train self.epochs = epochs self.reward_epochs = reward_epochs self.min_species_size = min_species_size self.min_species = min_species self.max_species = max_species self.elitism_rate = elitism_rate self.mutate_speed = mutate_speed self.n_generations_no_change = n_generations_no_change self.tol = tol # Plotting and tracking training progress self.monitor = monitor self.polygons = dict() # Species centers calculated after first clustering self.species_repr = None self.converged = False # What to save: save_genomes =1 saves elites =2 saves all genomes self.save_genomes, self.save_genes = { "all": [2, True], "elites": [1, True], "genomes": [2, False], "bare": [1, False], "None": [0, False] }[save_mode] # Load if a checkpoint is given if load is not None: self.load_checkpoint(*load, load_params=load_params) else: # Historical markers starting at 5 self.id_generator = itertools.count(5) self.species_id_generator = itertools.count(1) self.input_size = input_size self.output_size = output_size # Begin with min species of nearly same size self.n = n self.species = { i: [ Genome(self) for _ in range(n // min_species if i > 0 else (n // min_species) + (n % min_species)) ] for i in range(min_species) } self.best_genome = self.species[0][0].copy() self.top_acc = 0 self.history = [] # Metadata self.generation = 1 self.checkpoint_name = name or time.strftime("%d.%m-%H:%M") self.check_args()
def combine_genomes(self, genome1, genome2): # print("Empezando prro") matching_edges = [] disjoint_edges_fitter = [] excess_edges_fitter = [] g_fitter = None g_other = None print("parent1: " + str(genome1.calculated_fitness)) print("parent2: " + str(genome2.calculated_fitness)) if genome1.calculated_fitness >= genome2.calculated_fitness: g_fitter = genome1 g_other = genome2 else: g_fitter = genome2 g_other = genome1 rr = rnd.random() r2 = rnd.random() for u, vs in g_fitter.get_edge_iter(): matching_count = 0 for v, eatt1 in vs.items(): data1 = eatt1['inN'] ## find in the for u2, vs2 in g_other.get_edge_iter(): for v2, eatt2 in vs2.items(): data2 = eatt2['inN'] if data2 == data1: matching_count = matching_count + 1 # print("matching!!") ## tiene el mismo numero de inovacion, elegimos aleatoriamente un edge #rr = rnd.random() r2 = rnd.random() atts_inh = None #print("atts p1:") #print(eatt1) #print("atts p2:") #print(eatt2) if r2 < 0.5: atts_inh = eatt1 else: atts_inh = eatt2 #print("heredado") #print(atts_inh) matching_edges.append((u, v, atts_inh)) #else: #print("no matching prro") if matching_count == 0: # no hubo conteos positivos #print( eatt1) """ if g_fitter.inovationNumber <= g_other.inovationNumber: # disjoint disjoint_edges_fitter.append( ( u ,v , eatt1 ) ) else: # exess excess_edges_fitter.append( (u,v, eatt1) ) ## no coinciden """ excess_edges_fitter.append((u, v, eatt1)) """ print(matching_edges) print(len(matching_edges )) print(len( excess_edges_fitter ) ) print(len( disjoint_edges_fitter ) ) """ Gnew = Genome() Gnew.clear() Gnew.add_nodes_from(g_fitter.get_input_genes(), is_input=True) Gnew.add_nodes_from(g_fitter.get_output_genes(), is_output=True) Gnew.add_edges_from(matching_edges) Gnew.add_edges_from(disjoint_edges_fitter) Gnew.add_edges_from(excess_edges_fitter) Gnew.set_inovation_number(g_fitter.get_inovation_number()) #Gnew.set_specie( g_fitter.specie ) Gnew.HLayers = g_fitter.HLayers #Gnew.re_add() return Gnew
def new(cls, genome_length): return cls(Genome.generate(genome_length))
def eval_genomes(genomes, config): for _, genome in genomes: genome.fitness = 0. net = neat.nn.FeedForwardNetwork.create(genome, config) gen = Genome(net) genome.fitness = gen.get_fitness()
def main(): POP_SIZE = 100 generations = 500 # No of generations Model = list() best_individual = Genome() best_fitness = -1 # Initially the best score is 0 # There are 2 types of score to maintain # 1- The current_best score # 2- The global best score # Hence there will be corrensponding individuals for this score global_best_fitness = -1 current_best_fitness = -1 global_best = Genome() # Best individual of all generations current_best = Genome() # Current best individual of the lot c1 = 2 #np.random.randint(0,2) c2 = 2 #np.random.randint(0,2) initPygame(" AI Learning Flappy ") IP = list() for i in range(POP_SIZE): # Training the first set of batch genome = Genome() #genome.mutate() genome = playGame(genome) # here also the intial population has to start playing the game IP.append(genome) P = IP # Setting Population to initial population print(" Training of initial Population is successfull ") #time.sleep(10) maximum_fitness = -1 average_fitness = -1 average_fitness_list = np.array([]) maximum_fitness_list = np.array([]) all_generations = np.array([]) #****************************STARTING THE GENERATION LOOP*************************************# for g in range(generations): # The iteration for each generation #print(" Printing generation number ",g) #time.sleep(10) metric = list() for m in range(POP_SIZE): # training each model from the population #print(type(m)) #print(type(P[m])) P[m] = playGame(P[m]) #instead of train , the model has to play metric.append([m, P[m].fitness ]) # instead of test_error , append its fitness if current_best_fitness < P[m].fitness: current_best_fitness = P[m].fitness current_best_weights = P[m].get_weights() current_best.set_weights(current_best_weights) informationforscreen = { 'generation': g, 'birdnumber': m, 'lastfitness': P[m].fitness, 'lastgenerationaveragefitness': average_fitness, 'bestfitness': current_best_fitness } updateScreen(informationforscreen) values = np.array(metric) results = values.reshape(values.shape[0], values.shape[1]) # setting up all the results average_fitness = np.sum( results[:, 1], axis=0) / 100 # calculating the average fitness #maximum_fitness = np.amax(results[:,1],axis = 0) # calcualting the maximum fitness individuals = results if global_best_fitness < current_best_fitness: global_best_fitness = current_best_fitness global_best_weights = current_best.get_weights() global_best.set_weights(global_best_weights) average_fitness_list = np.append(average_fitness_list, average_fitness) maximum_fitness_list = np.append(maximum_fitness_list, current_best_fitness) all_generations = np.append(all_generations, g) print(" The score of all people is ") print(individuals) progeny = list() #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UPDATE THE FLOCK OF BIRDS IN PSO ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# for pop in P: #new_guy = Genome() ran1 = random.randrange(0, 2) ran2 = random.randrange(0, 2) init_weights = pop.get_weights() global_weights = global_best.get_weights() current_weights = current_best.get_weights() weight_diff_from_global = list( map(sub, global_weights, init_weights)) weight_diff_from_current = list( map(sub, current_weights, init_weights)) z1 = c1 * ran1 z2 = c2 * ran2 new_guy_weights_additive = list( map(add, map(lambda x: x * z1, weight_diff_from_global), map(lambda x: x * z2, weight_diff_from_current))) new_guy_weights = list( map(add, init_weights, new_guy_weights_additive)) #new_guy.set_weights(new_guy_weights) pop.set_weights(new_guy_weights) #progeny.append(new_guy) #children = progeny # NO MUTATION IN PSO #r1 = np.random.randint(1,1000) #r2 = np.random.randint(0,5) #if(r1 < 5) : # children[r2].mutate() # call a random genome of child and mutate it #P = children metric_final = list() Model = P ############################# END OF GENERATION LOOP ################################################## for m in range(len(P)): P[m] = playGame(P[m]) #instead of train , the model has to play metric_final.append([m, P[m].fitness ]) # instead of test_error , append its fitness values_final = np.array(metric_final) results_final = values_final.reshape(values_final.shape[0], values_final.shape[1]) individuals_final = results_final #ranked_individuals_final = individuals_final[individuals_final[:,1].argsort()] #print(" Final ranked individuals ") #print(ranked_individuals_final) #print(type(ranked_individuals_final)) #best_individual = int(ranked_individuals_final[0][0]) #`````````````````````````````` PLOTTING THE CONVERGENCE OF ALL THE BIRDS ``````````````````````` f_handle = open('max_pso.txt', 'w') np.save('max_pso.npy', maximum_fitness_list) f_handle.close() f_handle = open('avg_pso.txt', 'w') np.save('avg_pso.npy', average_fitness_list) f_handle.close() f_handle = open('generations.txt', 'w') np.save('gen.npy', all_generations) f_handle.close() plt.figure(2) #plt.subplot(211) plt.plot(all_generations, maximum_fitness_list, color='red', label='maximum fitness') plt.plot(all_generations, average_fitness_list, color='blue', label='average fitness') plt.xlabel("Generation number") red_patch = mpatches.Patch(color='red', label='minimum fitness') blue_patch = mpatches.Patch(color='blue', label='average fitness') plt.legend(handles=[red_patch, blue_patch]) plt.ylabel("Fitness") #plt.subplot(212) #plt.plot(X,Y,color = 'green') #plt.xlabel("X-value") #plt.ylabel("Y = exp(-3x) + sin(6*pi*x)") plt.show()
def write_to_genome(self): gene = Genome(self.model.get_weights()) return gene
from genome import Genome # TODO: train "iseq" model MODELS = ["novaseq", "miseq", "hiseq"] # We're committed to uniform. UNIFORM_ABUNDANCE = "uniform" # 2x this for a pair READ_SIZE = 150 TOP_6_ID_GENOMES = [ Genome("bacteria", "klebsiella_pneumoniae_HS11286", [("subspecies", 1125630), ("species", 573), ("genus", 570), ("family", 543)], ["NC_016845.1"]), Genome("fungi", "aspergillus_fumigatus", [("subspecies", 330879), ("species", 746128), ("genus", 5052), ("family", 1131492)], [ "NC_007194.1", "NC_007195.1", "NC_007196.1", "NC_007197.1", "NC_007198.1", "NC_007199.1", "NC_007200.1", "NC_007201.1" ], "https://www.ncbi.nlm.nih.gov/genome/18?genome_assembly_id=22576"), Genome("viruses", "rhinovirus_c", [("subspecies", 0), ("species", 463676), ("genus", 12059), ("family", 12058)], ["MG148341.1"]), Genome("viruses", "chikungunya", [("subspecies", 0), ("species", 37124), ("genus", 11019), ("family", 11018)], ["MG049915.1"]), Genome(
def get_new_population(self, adjusted_species_sizes, remaining_species, species_set, generation_tracker, backprop_mutation): """ Creates the dictionary of the new genomes for the next generation population :param: genetation_tracker: :param adjusted_species_sizes: :param remaining_species: :param species_set: :param new_population: :return: """ new_population = {} for species_size, species in zip(adjusted_species_sizes, remaining_species): # TODO: Uncomment if you removed min_species_size # assert (species_size > 0) if species_size > 0: # List of old species members old_species_members = list(species.members.values()) # Reset the members for the current species species.members = {} # Save the species in the species set object species_set.species[species.key] = species # Sort the members into the descending fitness old_species_members.sort(reverse=True, key=lambda x: x.fitness) # Double check that it is descending if len(old_species_members) > 1: assert (old_species_members[0].fitness >= old_species_members[1].fitness) # If we have specified a number of genomes to carry over, carry them over to the new population num_genomes_without_crossover = int( round(species_size * self.config.chance_for_mutation_without_crossover)) if num_genomes_without_crossover > 0: for member in old_species_members[: num_genomes_without_crossover]: # Check if we should carry over a member un-mutated or not if not self.config.keep_unmutated_top_percentage: child = copy.deepcopy(member) child.mutate( reproduction_instance=self, innovation_tracker=self.innovation_tracker, config=self.config, backprop_mutation=backprop_mutation) if not child.check_connection_enabled_amount( ) and not child.check_num_paths( only_add_enabled_connections=True): raise Exception( 'This child has no enabled connections') new_population[child.key] = child self.ancestors[child.key] = () # new_population[member.key] = member species_size -= 1 assert (species_size >= 0) else: # Else we just add the current member to the new population new_population[member.key] = member species_size -= 1 assert (species_size >= 0) # If there are no more genomes for the current species, then restart the loop for the next species if species_size <= 0: continue # Only use the survival threshold fraction to use as parents for the next generation. reproduction_cutoff = int( math.ceil( (1 - self.config.chance_for_mutation_without_crossover) * len(old_species_members))) # Need at least two parents no matter what the previous result reproduction_cutoff = max(reproduction_cutoff, 2) old_species_members = old_species_members[:reproduction_cutoff] # Randomly choose parents and choose whilst there can still be additional genomes for the given species while species_size > 0: species_size -= 1 # TODO: If you don't allow them to mate with themselves then it's a problem because if the species previous # TODO: size is 1, then how can you do with or without crossover? parent_1 = copy.deepcopy( random.choice(old_species_members)) parent_2 = copy.deepcopy( random.choice(old_species_members)) # Has to be a deep copy otherwise the connections which are crossed over are also modified if mutation # occurs on the child. parent_1 = copy.deepcopy(parent_1) parent_2 = copy.deepcopy(parent_2) self.genome_indexer += 1 genome_id = self.genome_indexer child = Genome(key=genome_id) # TODO: Save the parent_1 and parent_2 mutation history as well as what connections they had # Create the genome from the parents num_connections_enabled = child.crossover( genome_1=parent_1, genome_2=parent_2, config=self.config) # If there are no connections enabled we forget about this child and don't add it to the existing # population if num_connections_enabled: child.mutate( reproduction_instance=self, innovation_tracker=self.innovation_tracker, config=self.config, generation_tracker=generation_tracker, backprop_mutation=backprop_mutation) if not child.check_connection_enabled_amount( ) and not child.check_num_paths( only_add_enabled_connections=True): raise Exception( 'This child has no enabled connections') new_population[child.key] = child self.ancestors[child.key] = (parent_1.key, parent_2.key) else: # Else if the crossover resulted in an invalid genome. assert num_connections_enabled == 0 species_size += 1 self.genome_indexer -= 1 return new_population
def generateRandomGenome(): net = Network(Config.networkArchitecture) genome = Genome(net) return genome
def test_combine_two_constant(self): g1 = Constant(15.1) g2 = Constant(20.2) self.assertEqual(20.2, Genome.combine(g1, g2))
lenght_result = ((len(genome) - window_lenght) / stride) + 1 for i in range(int(lenght_result)): nb_motif_checked = 0 for s in seq: if s in genome[i:window_lenght + i]: nb_motif_checked += 1 if thersehold == nb_motif_checked: list_result.append((i, window_lenght + i)) return list_result if __name__ == "__main__": g = Genome('rmark3.fa') g.read_fasta() genome = g.dict_genome['rmark1'].seq sequence = ["AUG"] t1 = time() for pattern in sequence: print(scan_genome_given_seq(genome, pattern)) t2 = time() print('Elapsed time is %f seconds.' % (t2 - t1))
def test_combine_two_multiply(self): g1 = Multiplier(0.23) g2 = Multiplier(4.2) self.assertEqual(1, Genome.combine(g1, g2))
class Player: MAX_ROTATION = 25 IMGS = [pygame.transform.scale2x(pygame.image.load( os.path.join("imgs", "bird" + str(x) + ".png"))) for x in range(1, 4)] ROT_VEL = 20 ANIMATION_TIME = 5 genome_inputs = 3 # Flappy-Bird Vision! If you change vision parameter in look(), change here too! genome_outputs = 1 # To jump or not to jump! def __init__(self, x, y): self.dead = False self.x = x self.y = y self.tilt = 0 self.tick_count = 0 self.vel = 0 self.height = self.y self.img_count = 0 self.img = self.IMGS[0] self.fitness = 1 self.unadjusted_fitness = 0 self.lifespan = 0 self.best_score = 0 #self.replay = False self.score = 1 #self.gen = 0 self.vision = [0 for _ in range(self.genome_inputs)] self.decision = 0 self.brain = Genome(self.genome_inputs, self.genome_outputs) def draw(self, win): self.img_count += 1 if self.img_count <= self.ANIMATION_TIME: self.img = self.IMGS[0] elif self.img_count <= self.ANIMATION_TIME*2: self.img = self.IMGS[1] elif self.img_count <= self.ANIMATION_TIME*3: self.img = self.IMGS[2] elif self.img_count <= self.ANIMATION_TIME*4: self.img = self.IMGS[1] elif self.img_count == self.ANIMATION_TIME*4 + 1: self.img = self.IMGS[0] self.img_count = 0 if self.tilt <= -80: self.img = self.IMGS[1] self.img_count = self.ANIMATION_TIME*2 self.blitRotateCenter(win, self.img, (self.x, self.y), self.tilt) def blitRotateCenter(self, surf, image, topleft, angle): rotated_image = pygame.transform.rotate(image, angle) new_rect = rotated_image.get_rect( center=image.get_rect(topleft=topleft).center) surf.blit(rotated_image, new_rect.topleft) def get_mask(self): return pygame.mask.from_surface(self.img) def jump(self): self.vel = -10.5 self.tick_count = 0 self.height = self.y def move(self): self.tick_count += 1 displacement = self.vel*(self.tick_count) + 0.5 * \ (3)*(self.tick_count)**2 if displacement >= 16: displacement = (displacement/abs(displacement)) * 16 if displacement < 0: displacement -= 2 self.y = self.y + displacement if displacement < 0 or self.y < self.height + 50: if self.tilt < self.MAX_ROTATION: self.tilt = self.MAX_ROTATION else: if self.tilt > -90: self.tilt -= self.ROT_VEL def clone(self): clone = Player(230, randint(200,400)) clone.brain = self.brain.clone() clone.fitness = self.fitness clone.unadjusted_fitness = self.unadjusted_fitness clone.brain.generate_network() clone.best_score = self.best_score return clone def calculate_fitness(self): self.unadjusted_fitness = self.score*self.score self.fitness = self.unadjusted_fitness def crossover(self, parent_2): child = Player(230, randint(200,400)) child.brain = self.brain.crossover(parent_2.brain) child.brain.generate_network() return child def look(self, pipes, pipe_ind): self.vision[0] = self.y self.vision[1] = abs(self.y - pipes[pipe_ind].height) self.vision[2] = abs(self.y - pipes[pipe_ind].bottom) def think_and_act(self): decision = self.brain.feed_forward(self.vision)[0] if decision > 0: self.jump() #def increment_counters(self): # self.lifespan += 1 # # if self.lifespan % 5 == 0: # self.score += 1 def increment_score_by(self, number): self.score += number
def main(): """Parse option""" if len(sys.argv) != 1: #Evaluate a single genome if str(sys.argv[1]) == "-evaluate": initPygame("FlapAI: Evaluating") print str(sys.argv[2]) net = load(str(sys.argv[2])) genome = Genome(net) global savestat savestat = False fitness = [] for i in xrange(100): fit = playGame(genome) fitness.append(fit.fitness) print "fitness : %s " % fit.fitness average = sum(fitness) / float(len(fitness)) printc("Average fitness : %s" % average, "red") pygame.quit() sys.exit() #Show the stat of an experiment if str(sys.argv[1]) == "-stats": showStat(str(sys.argv[2])) #No argument, starting FlapAI initPygame("FlapAI: Learning") """Init the population""" bestFitness = 0 population = Population() population.generateRandomPopulation() generation = 1 maxgeneration = Config.maxGeneration lastgenerationaveragefitness = 0 #Main Loop while generation <= maxgeneration: birdnmbr = 1 for i in xrange(population.size()): genome = playGame(population.getGenome(i)) population.setGenomeFitness(i, genome.fitness) informationforscreen = { 'generation': generation, 'birdnumber': birdnmbr, 'lastfitness': genome.fitness, 'lastgenerationaveragefitness': lastgenerationaveragefitness, 'bestfitness': bestFitness } updateScreen(informationforscreen) if genome.fitness > bestFitness: global bestFitness bestFitness = genome.fitness genome.network.save(today + "/bestfitness.json") birdnmbr += 1 global fitnessovergeneration fitnessovergeneration.append(population.averageFitness()) lastgenerationaveragefitness = population.averageFitness() global fittestovergeneration fittestovergeneration.append(population.findFittest().fitness) #Evolve the population population.evolvePopulation() generation += 1
def random_goombas(cls, dimensions, num_goombas, seed_meta, gen_len_range, gen_time=200): """Generate a world containing a number of goombas with random coding genomes.""" gens = [Genome.random_coding(seed_meta, randrange(*gen_len_range)) for _ in range(num_goombas)] gen_seqs = [genome.sequences() for genome in gens] return cls(dimensions, gen_seqs, seed_meta, gen_len_range, gen_time)
def SetFromFilename(self, filename): self.winnergen = 0 self.highest_fitness = 0.0 self.highest_last_changed = 0 self.cur_node_id = 0 self.cur_innov_num = 0.0 curwordnum = 0 dprint(DEBUG_FILEINPUT, "Opening population file %s." % (filename, )) iFile = open(filename, "r") if not iFile: dprint(DEBUG_ERROR, "Can't open genomes file for input %s." % (filename, )) return md = False metadata = "" #//Loop until file is finished, parsing each line while True: curline = iFile.readline() if not curline: break dprint(DEBUG_FILEINPUT, "line: %s." % (curline.strip(), )) words = curline.strip().split() dprint(DEBUG_FILEINPUT, "words: %s." % (":".join(words), )) if len(words) == 0: continue if re.search('^\s*#', words[0]): continue if words[0] == "genomestart": dprint(DEBUG_FILEINPUT, "genomestart") idcheck = int(words[1]) if not md: metadata = "" md = False new_genome = Genome() new_genome.SetFromFile(idcheck, iFile) new_organism = Organism() new_organism.SetFromGenome(0., new_genome, 1, metadata) self.organisms.append(new_organism) if self.cur_node_id < new_genome.get_last_node_id(): self.cur_node_id = new_genome.get_last_node_id() if self.cur_innov_num < new_genome.get_last_gene_innovnum(): self.cur_innov_num = new_genome.get_last_gene_innovnum() elif words[0] == "/*": #// New metadata possibly, so clear out the metadata metadata = "" word_i = 1 while words[word_i] != "*/": #// If we've started to form the metadata, put a space in the front if md: metadata += " " metadata += words[word_i] md = True word_i += 1 # end of conditional processing # end of while True dprint(DEBUG_FILEINPUT, "End of file.") iFile.close() self.speciate() dprint(DEBUG_FILEINPUT, "Done.") return
def __init__(self, name): self._name = name self._genome = Genome(self._name) self._dbroot = TrackDbRoot() self._genome.add_trackdb(self._dbroot)
import sys from genome import Genome from organism import Organism PREFIX = '/home/koppejan/projects/helicopter/ghh09/policies/' POLICIES = [Genome.open(PREFIX + 'mdp%i.net' % i) for i in range(10) if i != 2] class Agent: """ Model-Free agent, chooses best policy from a set of pre-trained policies. """ def __init__(self): """ Initialize agent. """ self.pool = [Organism(genome) for genome in POLICIES] self.episode = 0 def start(self): """ Start a new episode """ self.set_policy() self.episode += 1 self.reward = 0 self.steps = 0 def step(self, state, reward): """ Choose an action based on the current state. """ self.steps += 1 self.reward += reward return self.org.policy.propagate(state, 1) def end(self, reward): """ Ends the current episode """
def test_make_gamete_calls_reproduce(self): flagger = Mock(**{'reproduce.return_value': "FLAG"}) test_genome = Genome({"a": flagger}, {"a": flagger}) test_gamete = test_genome.make_gamete() self.assertEqual(test_gamete.values()[0], "FLAG")
def breed(self, mom, dad): """Make two children from parental genes. Args: mother (dict): genome parameters father (dict): genome parameters Returns: (list): Two network objects """ children = [] # where do we recombine? 0, 1, 2, 3, 4... N? # with four genes, there are three choices for the recombination # ___ * ___ * ___ * ___ # 0 -> no recombination, and N == length of dictionary -> no recombination # 0 and 4 just (re)create more copies of the parents # so the range is always 1 to len(all_possible_genes) - 1 pcl = len(self.all_possible_genes) recomb_loc = random.randint(1, pcl - 1) # for _ in range(2): #make _two_ children - could also make more child1 = {} child2 = {} # enforce defined genome order using list #keys = ['nb_neurons', 'nb_layers', 'activation', 'optimizer'] keys = list(self.all_possible_genes) # paranoia - just to make sure we do not add unintentional randomization keys = sorted(keys) # *** CORE RECOMBINATION CODE **** for x in range(0, pcl): if x < recomb_loc: child1[keys[x]] = mom.geneparam[keys[x]] child2[keys[x]] = dad.geneparam[keys[x]] else: child1[keys[x]] = dad.geneparam[keys[x]] child2[keys[x]] = mom.geneparam[keys[x]] # Initialize a new genome # Set its parameters to those just determined # they both have the same mom and dad genome1 = Genome(self.all_possible_genes, child1, self.ids.get_next_ID( ), mom.u_ID, dad.u_ID, self.ids.get_Gen()) genome2 = Genome(self.all_possible_genes, child2, self.ids.get_next_ID( ), mom.u_ID, dad.u_ID, self.ids.get_Gen()) # at this point, there is zero guarantee that the genome is actually unique # Randomly mutate one gene if self.mutate_chance > random.random(): genome1.mutate_one_gene() if self.mutate_chance > random.random(): genome2.mutate_one_gene() # do we have a unique child or are we just retraining one we already have anyway? while self.master.is_duplicate(genome1): genome1.mutate_one_gene() self.master.add_genome(genome1) while self.master.is_duplicate(genome2): genome2.mutate_one_gene() self.master.add_genome(genome2) children.append(genome1) children.append(genome2) return children
def test_combine_half_and_half(self): g1 = Multiplier(0.333) g2 = Constant(20.2) self.assertEqual(6.7266, Genome.combine(g1, g2)) # order shouldn't matter self.assertEqual(6.7266, Genome.combine(g2, g1))
def test_crossover(): a = Genome() for i in range(3): a.add_node(Node(NodeType.inp, i + 1)) a.add_node(Node(NodeType.out, 4)) a.add_node(Node(NodeType.hidden, 5)) a.add_connection(Connection(1, 4, 1.0, True, 1)) a.add_connection(Connection(2, 4, 1.0, False, 2)) a.add_connection(Connection(3, 4, 1.0, True, 3)) a.add_connection(Connection(2, 5, 1.0, True, 4)) a.add_connection(Connection(5, 4, 1.0, True, 5)) a.add_connection(Connection(1, 5, 1.0, True, 8)) a.conn_innovation = 9 a.node_innovation = 6 b = Genome() for i in range(3): b.add_node(Node(NodeType.inp, i + 1)) b.add_node(Node(NodeType.out, 4)) b.add_node(Node(NodeType.hidden, 5)) b.add_node(Node(NodeType.hidden, 6)) b.add_connection(Connection(1, 4, 1.0, True, 1)) b.add_connection(Connection(2, 4, 1.0, False, 2)) b.add_connection(Connection(3, 4, 1.0, True, 3)) b.add_connection(Connection(2, 5, 1.0, True, 4)) b.add_connection(Connection(5, 4, 1.0, False, 5)) b.add_connection(Connection(5, 6, 1.0, True, 6)) b.add_connection(Connection(6, 4, 1.0, True, 7)) b.add_connection(Connection(3, 5, 1.0, True, 9)) b.add_connection(Connection(1, 6, 1.0, True, 10)) a.conn_innovation = 11 a.node_innovation = 7 c = Genome.crossover(b, a) a.render("render/crossover_a.gv") b.render("render/crossover_b.gv") c.render("render/crossover_c.gv")
class Tree( object ): def __init__(self, origin, caller=None): if type(origin) is Genome: self.subs = [] self.genome = origin self.scored = False self.scores = {} elif type(origin) is Tree: self.genome = Genome(origin.genome) if caller is not None: self.subs = [Tree(sub,(origin,self) ) for sub in origin if sub is not caller[0]] self.subs.append(caller[1]) else: self.subs = [Tree(sub, (origin,self) ) for sub in origin] self.scored = False self.scores = {} def __iter__( self ): return iter( self.subs ) def __getitem__( self, key): return self.subs[key] def setGenome(self, g): self.scored = False self.scores = {} self.genome = g def isBinary( self, caller=None): return True def allNodes( self, caller=None): s = self.genome.getName() s += " | " for sub in self: s += sub.genome.getName() s += ", " s += "\n" for sub in self: if sub is not caller: s += sub.allNodes(self) return s def isLeaf(self): return len(self.subs) in (0,1) def addConnection(self, other, caller=None ): self.scored = False self.scores = {} if type(other) is Tree: self.subs.append( other ) if other is not caller: other.addConnection( self, self) elif type(other) is Genome: t = Tree( other ) self.addConnection( t ) else: raise Exception("Tried to add an invalid connection to a tree.") def breakConnection(self, other, caller=None): self.scored = False self.scores = {} self.subs.remove( other ) if other is not caller: other.breakConnection( self, self ) def getScore( self, caller=None): if self.scored and caller in self.scores: return self.scores[caller] else: if self.isLeaf() and caller is not None: return 0 elif self.isLeaf() and caller is None: return self.subs[0].getScore() else: grimm = Grimm() s = 0 for sub in self: if sub is not caller: s += sub.getScore( self ) s += grimm.getDistance( self.genome, sub.genome) self.scores[caller] = s self.scored = True return s def getTips(self): tips = [] def rTipFinder( t, tips, caller=None ): if t.isLeaf() and caller is not None: tips.append( t ) elif t.isLeaf() and caller is None: tips.append(t) if not len(t.subs) == 0: rTipFinder( t.subs[0],tips, t) else: for sub in t: if sub is not caller: rTipFinder( sub , tips, t) rTipFinder(self , tips) return tips def __len__( self ): size = [0,] def rSizeFinder( t, size, caller=None ): size[0] += 1 for sub in t: if sub is not caller: rSizeFinder( sub, size, t) rSizeFinder(self ,size) return size[0] def toTuple( self, caller=None): if self.isLeaf() and caller is not None: return self elif self.isLeaf() and caller is None: return self.subs[0].toTuple() elif caller is None: return ( self.subs[0].toTuple(self), self.toTuple( self.subs[0] )) else: return tuple([sub.toTuple(self) for sub in self if sub is not caller]) def __str__( self ): def rStringFinder( t, caller=None): if t.isLeaf() and caller is not None: return t.genome.getName() elif t.isLeaf() and caller is None: return rStringFinder( t.subs[0]) elif caller is None: return "(" + rStringFinder(self.subs[0], self) + ", " + rStringFinder(self, self.subs[0]) + ")" else: s = "(" for sub in t: if sub is not caller: s += rStringFinder( sub, t ) s += ", " s = s[:-2] s += ")" return s return "Tree: " + rStringFinder(self ) def genomeHash(self): return hash( self.genome ) def getName(self): return self.genome.getName()
def test_weight_mut(): a = Genome() a.add_node(Node(NodeType.inp, 1)) a.add_node(Node(NodeType.inp, 2)) a.add_node(Node(NodeType.out, 3)) a.add_connection(Connection(1, 3, 0.5, True, 1)) a.add_connection(Connection(2, 3, 1.0, True, 2)) a.conn_innovation = 3 a.node_innovation = 4 a.render("render/weight_begin.gv") a.mutate_weights() a.render("render/weight_mutated.gv")
def reproduce_with_species(self, species_set, pop_size, generation): ''' Creates and speciates genomes. species_set -- set of current species pop_size -- population size generation -- current generation ''' all_fitnesses = [] remaining_species = [] # Traverse species and grab fitnesses from non-stagnated species for sid, species, species_is_stagnant in self.stagnation.update( species_set, generation): if species_is_stagnant: print("!!! Species {} Stagnated !!!".format(sid)) # self.reporters.species_stagnant(sid, species) pass else: # Add fitnesses of members of current species all_fitnesses.extend(member.fitness for member in itervalues(species.members)) remaining_species.append(species) # No species if not remaining_species: species_set.species = {} return {} # Find min/max fitness across entire population min_population_fitness = min(all_fitnesses) max_population_fitness = max(all_fitnesses) # Do not allow the fitness range to be zero, as we divide by it below. population_fitness_range = max( 1.0, max_population_fitness - min_population_fitness) # Compute adjusted fitness and record minimum species size for species in remaining_species: # Determine current species average fitness mean_species_fitness = mean( [member.fitness for member in itervalues(species.members)]) max_species_fitness = max( [member.fitness for member in itervalues(species.members)]) # Determine current species adjusted fitness and update it species_adjusted_fitness = ( mean_species_fitness - min_population_fitness) / population_fitness_range species.adjusted_fitness = species_adjusted_fitness species.max_fitness = max_species_fitness adjusted_fitnesses = [ species.adjusted_fitness for species in remaining_species ] avg_adjusted_fitness = mean(adjusted_fitnesses) # Compute the number of new members for each species in the new generation. previous_sizes = [ len(species.members) for species in remaining_species ] min_species_size = max(2, self.species_elitism) spawn_amounts = self.compute_species_sizes(adjusted_fitnesses, previous_sizes, pop_size, min_species_size) new_population = {} species_set.species = {} for spawn, species in zip(spawn_amounts, remaining_species): # If elitism is enabled, each species always at least gets to retain its elites. spawn = max(spawn, self.species_elitism) assert spawn > 0 # The species has at least one member for the next generation, so retain it. old_species_members = list(iteritems(species.members)) # Update species with blank slate species.members = {} # Update species in species set accordingly species_set.species[species.key] = species # Sort old species members in order of descending fitness. old_species_members.sort(reverse=True, key=lambda x: x[1].fitness) # Clone elites to new generation. if self.species_elitism > 0: for member_key, member in old_species_members[:self. species_elitism]: new_population[member_key] = member spawn -= 1 # If the species only has room for the elites, move onto next species if spawn <= 0: continue # Only allow fraction of species members to reproduce reproduction_cutoff = int( ceil(self.species_reproduction_threshold * len(old_species_members))) # Use at least two parents no matter what the threshold fraction result is. reproduction_cutoff = max(reproduction_cutoff, 2) old_species_members = old_species_members[:reproduction_cutoff] # Randomly choose parents and produce the number of offspring allotted to the species. # NOTE: Asexual reproduction for now while spawn > 0: spawn -= 1 parent1_key, parent1 = random.choice(old_species_members) # parent2_key, parent2 = random.choice(old_species_members) child_key = next(self.genome_indexer) child = Genome(child_key) # child.crossover(parent1, parent2) child.copy(parent1, generation) child.mutate(generation) new_population[child_key] = child return new_population
def _get_genome(self): if os.path.isfile(self.genome_filename): return Genome(self.genome_filename) else: return None
import sys from genome import Genome from organism import Organism PREFIX = '/home/koppejan/projects/helicopter/ghh09/policies/' POLICIES = [Genome.open(PREFIX + 'mdp%i.net' % i) for i in range(10) if i != 2] class Agent: """ Model-Free agent, chooses best policy from a set of pre-trained policies. """ def __init__(self): """ Initialize agent. """ self.pool = [Organism(genome) for genome in POLICIES] self.episode = 0 def start(self): """ Start a new episode """ self.set_policy() self.episode += 1 self.reward = 0 self.steps = 0 def step(self, state, reward): """ Choose an action based on the current state. """ self.steps += 1 self.reward += reward return self.org.policy.propagate(state, 1) def end(self, reward):
def genome_template(self): """ Get a template of the genome, all chromosomes will have empty genes :return: """ genome = Genome() chromosome = Chromosome(chromosome_id='SingleTrue', encoding_type=EncodingType.BOOLEAN, n_genes=1) chromosome.init_genes() genome.add_chromosome(chromosome) chromosome = Chromosome(chromosome_id='SingleFalse', encoding_type=EncodingType.BOOLEAN, n_genes=1) chromosome.init_genes() genome.add_chromosome(chromosome) chromosome = Chromosome(chromosome_id='Fixed10True', encoding_type=EncodingType.BOOLEAN, n_genes=10) chromosome.init_genes() genome.add_chromosome(chromosome) chromosome = Chromosome(chromosome_id='Fixed10False', encoding_type=EncodingType.BOOLEAN, n_genes=10) chromosome.init_genes() genome.add_chromosome(chromosome) chromosome = Chromosome(chromosome_id='Variable10True', encoding_type=EncodingType.BOOLEAN) chromosome.init_genes() genome.add_chromosome(chromosome) chromosome = Chromosome(chromosome_id='Variable10False', encoding_type=EncodingType.BOOLEAN) chromosome.init_genes() genome.add_chromosome(chromosome) chromosome = Chromosome(chromosome_id='Alternating', encoding_type=EncodingType.BOOLEAN) chromosome.init_genes() genome.add_chromosome(chromosome) return genome