def find_schemas(self, fitness, num_schemas): """Find the given number of unique schemas using a genetic algorithm Arguments: - fitness - A callable object (ie. function) which will evaluate the fitness of a motif. - num_schemas - The number of unique schemas with good fitness that we want to generate. """ start_population = \ Organism.function_population(self.motif_generator.random_motif, self.initial_population, fitness) finisher = SimpleFinisher(num_schemas, self.min_generations) # set up the evolver and do the evolution evolver = GenerationEvolver(start_population, self.selector) evolved_pop = evolver.evolve(finisher.is_finished) # convert the evolved population into a PatternRepository schema_info = {} for org in evolved_pop: # convert the Genome from a MutableSeq to a Seq so that # the schemas are just strings (and not array("c")s) seq_genome = org.genome.toseq() schema_info[str(seq_genome)] = org.fitness return PatternRepository(schema_info)
def find_schemas(self, fitness, num_schemas): """Find the given number of unique schemas using a genetic algorithm Arguments: o fitness - A callable object (ie. function) which will evaluate the fitness of a motif. o num_schemas - The number of unique schemas with good fitness that we want to generate. """ start_population = \ Organism.function_population(self.motif_generator.random_motif, self.initial_population, fitness) finisher = SimpleFinisher(num_schemas, self.min_generations) # set up the evolver and do the evolution evolver = GenerationEvolver(start_population, self.selector) evolved_pop = evolver.evolve(finisher.is_finished) # convert the evolved population into a PatternRepository schema_info = {} for org in evolved_pop: # convert the Genome from a MutableSeq to a Seq so that # the schemas are just strings (and not array("c")s) seq_genome = org.genome.toseq() schema_info[str(seq_genome)] = org.fitness return PatternRepository(schema_info)
def test_function_population(self): """Create a population using a function to generate genomes. """ num_orgs = 10 new_pop = Organism.function_population(genome_generator, num_orgs, fitness_calculator) self.assertEqual(len(new_pop), num_orgs, "Expected %s organisms, " "got %s" % (num_orgs, len(new_pop))) for org in new_pop: self.assertIsInstance(org, Organism.Organism, "Expected to get an organism, got %r" % org) exp_fit = fitness_calculator(org.genome) self.assertEqual(org.fitness, exp_fit, "Expected fitness of %s, " "got %s" % (org.fitness, exp_fit))
def test_function_population(self): """Create a population using a function to generate genomes. """ num_orgs = 10 new_pop = Organism.function_population(genome_generator, num_orgs, fitness_calculator) assert len(new_pop) == num_orgs, "Expected %s organisms, got %s" \ % (num_orgs, len(new_pops)) for org in new_pop: assert isinstance(org, Organism.Organism), \ "Expected to get an organism, got %r" % org exp_fit = fitness_calculator(org.genome) assert org.fitness == exp_fit, \ "Expected fitness of %s, got %s" % (org.fitness, exp_fit)
def test_function_population(self): """Create a population using a function to generate genomes. """ num_orgs = 10 new_pop = Organism.function_population(genome_generator, num_orgs, fitness_calculator) self.assertEqual( len(new_pop), num_orgs, "Expected %s organisms, " "got %s" % (num_orgs, len(new_pop))) for org in new_pop: self.assertIsInstance(org, Organism.Organism, "Expected to get an organism, got %r" % org) exp_fit = fitness_calculator(org.genome) self.assertEqual( org.fitness, exp_fit, "Expected fitness of %s, " "got %s" % (org.fitness, exp_fit))