Back to index

python-biopython  1.60
test_GAOrganism.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """Tests for an Organism in a Genetic Algorithm population.
00003 """
00004 # standard library
00005 import unittest
00006 
00007 # Biopython
00008 from Bio import Alphabet
00009 from Bio.Seq import MutableSeq
00010 
00011 
00012 # local stuff
00013 from Bio.GA import Organism
00014 
00015     
00016 
00017 # -- utility functions
00018 class TestAlphabet(Alphabet.Alphabet):
00019     """Simple alphabet for test purposes.
00020     """
00021     letters = ["1", "2", "3", "4"]
00022 
00023 def genome_generator():
00024     """Generate a genome for testing purposes.
00025     """
00026     return MutableSeq("1234", TestAlphabet())
00027 
00028 def fitness_calculator(genome):
00029     """Calculate fitness for testing purposes.
00030     """
00031     assert isinstance(genome, MutableSeq), "Expected MutableSeq for a genome."
00032 
00033     regular_seq = genome.toseq()
00034     return int(regular_seq.tostring())
00035 
00036 class CreatePopulationTest(unittest.TestCase):
00037     """Tests for utility functions for creating populations.
00038     """
00039     def setUp(self):
00040         self.alphabet = TestAlphabet()
00041 
00042     def test_function_population(self):
00043         """Create a population using a function to generate genomes.
00044         """
00045         num_orgs = 10
00046         new_pop = Organism.function_population(genome_generator,
00047                                                num_orgs, fitness_calculator)
00048 
00049         assert len(new_pop) == num_orgs, "Expected %s organisms, got %s" \
00050                % (num_orgs, len(new_pops))
00051 
00052         for org in new_pop:
00053             assert isinstance(org, Organism.Organism), \
00054                    "Expected to get an organism, got %r" % org
00055 
00056             exp_fit = fitness_calculator(org.genome)
00057             assert org.fitness == exp_fit, \
00058                    "Expected fitness of %s, got %s" % (org.fitness, exp_fit)
00059 
00060     def test_random_population(self):
00061         """Create a population randomly from a alphabet.
00062         """
00063         num_orgs = 10
00064         genome_size = 5
00065         new_pop = Organism.random_population(self.alphabet, genome_size,
00066                                              num_orgs, fitness_calculator)
00067 
00068         assert len(new_pop) == num_orgs, "Expected %s organisms, got %s" \
00069                % (num_orgs, len(new_pops))
00070 
00071         for org in new_pop:
00072             assert isinstance(org, Organism.Organism), \
00073                    "Expected to get an organism, got %r" % org
00074 
00075             exp_fit = fitness_calculator(org.genome)
00076             assert org.fitness == exp_fit, \
00077                    "Expected fitness of %s, got %s" % (org.fitness, exp_fit)
00078 
00079             assert len(org.genome) == genome_size, \
00080                    "Expected genome size of %s, got %s" % (len(org.genome),
00081                                                            genome_size)
00082 
00083     def test_random_population_types(self):
00084         """Creating a random population with different types of alphabets.
00085         """
00086         class DoubleAlphabet:
00087             letters = [1.0, 2.0]
00088 
00089         class CharacterAlphabet:
00090             letters = ["a", "b"]
00091 
00092         class IntegerAlphabet:
00093             letters = [1, 2]
00094 
00095         def test_fitness(genome):
00096             return 2
00097 
00098         all_alphabets = [DoubleAlphabet(), CharacterAlphabet(),
00099                          IntegerAlphabet()]
00100 
00101         for alphabet in all_alphabets:
00102             new_pop = Organism.random_population(alphabet, 5, 10,
00103                                                  test_fitness)
00104 
00105 class OrganismTest(unittest.TestCase):
00106     """Tests for an organism in a GA population.
00107     """
00108     def setUp(self):
00109         self.alphabet = TestAlphabet()
00110         self.genome = MutableSeq("1234", self.alphabet)
00111         self.organism = Organism.Organism(self.genome, fitness_calculator)
00112 
00113     def test_organism_basic(self):
00114         """Exercise basic organism functionality.
00115         """
00116         same_genome = MutableSeq("1234", self.alphabet)
00117         same_organism = Organism.Organism(same_genome, fitness_calculator)
00118 
00119         dif_genome = MutableSeq("1111", self.alphabet)
00120         dif_organism = Organism.Organism(dif_genome, fitness_calculator)
00121 
00122         assert str(self.organism) == str(same_organism), \
00123                "Comparison doesn't work for identical organisms."
00124 
00125         assert str(self.organism) != str(dif_organism), \
00126                "Comparison doesn't work for different organism."
00127 
00128     def test_organism_fitness(self):
00129         """Test the ability to deal with the fitness of the genome.
00130         """
00131         assert self.organism.fitness == 1234, \
00132                "Unexpected fitness %s" % self.organism.fitness
00133         
00134         new_genome = MutableSeq("1111", self.alphabet)
00135         self.organism.genome = new_genome
00136         self.organism.recalculate_fitness()
00137 
00138         assert self.organism.fitness == 1111, \
00139                "Unexpected fitness %s" % self.organism.fitness
00140 
00141     def test_organism_copy(self):
00142         """Test copying of organisms.
00143         """
00144         new_organism = self.organism.copy()
00145 
00146         new_organism.genome.append("1")
00147 
00148         assert str(new_organism.genome) != str(self.organism.genome), \
00149                "Did not provide a copy of the organism."
00150 
00151     def test_provide_fitness(self):
00152         """Test that providing a pre-calculated fitness works.
00153         """
00154         def fitness_calc(genome):
00155             raise ValueError("Should not have been executed.")
00156 
00157         genome = self.organism.genome
00158 
00159         # make sure not supplying fitness works
00160         try:
00161             new_org = Organism.Organism(genome, fitness_calc)
00162             raise AssertionError("Did not calculate fitness when expected.")
00163         except ValueError:
00164             pass
00165 
00166         # make sure supplying fitness works
00167         new_org = Organism.Organism(genome, fitness_calc, 50)
00168 
00169 if __name__ == "__main__":
00170     runner = unittest.TextTestRunner(verbosity = 2)
00171     unittest.main(testRunner=runner)