Exemplo n.º 1
0
class KarvaTest(unittest.TestCase):
    '''Tests basic functionality of Karva genes'''
    def setUp(self):
        self.gene = KarvaGene([add_op, subtract_op, 'a', 1, 'a'], 2)
    
    
    def testEvaluation(self):
        f = Foo()
        self.assertEqual(1, self.gene(f))
        self.assertTrue(f in getattr(self.gene, self.gene.__call__.memo))
        

    def testCodingLocation(self):
        self.assertEqual(4, self.gene.coding)
        self.assertEqual(0, self.gene.derive([(0, ['a'])]).coding)
    
    
    def testDerivation(self):
        g = self.gene.derive([(0, ['a']), (3, [add_op, add_op])])
        self.assertEqual(['a', subtract_op, 'a', add_op, add_op], g.alleles)
        self.assertEqual(self.gene, self.gene.derive([(2, ['a'])]))
    
    
    def testRepresentation(self):
        self.assertEqual('+-a1a', repr(self.gene))
        
    
    def testTerminalLocations(self):
        self.assertEqual(self.gene._terminals, [('a', [2,4])])
Exemplo n.º 2
0
    def testMultigenic(self):
        class Foo(object):
            a = b = c = d = e = f = 1

        # Representation
        g1 = KarvaGene(['a', 'b', 'c'], 1)
        g2 = KarvaGene(['d', 'e', 'f'], 1)
        c = Computation([g1, g2], 1)
        self.assertEqual('abcdef', repr(c))

        # Evaluation
        self.assertEqual((1, 1), c(Foo()))
Exemplo n.º 3
0
    def testDerivation(self):
        alleles = [subtract_op, '?', '?', 1, 0]
        gene = KarvaGene(alleles, 1, [2, 5])
        self.assertEqual(gene._evaluation, [subtract_op, 5, 2])
        self.assertEqual(gene.alleles, alleles)
        
        # Make sure the RNCs are evaluated correctly
        o = object()
        self.assertEqual(3, gene(o))
        
        # Make sure all caching is done
        self.assertTrue(o in getattr(gene, '___call___memo'))
        self.assertEqual(gene._evaluation, [3, 5, 2])

        # And that changes to the used RNCs eliminate that cache
        gene2 = gene.derive([(4, [1])])
        self.assertNotEqual(gene2._evaluation, gene._evaluation)
        self.assertRaises(AttributeError, getattr, gene2, '___call___memo')
Exemplo n.º 4
0
    def testDerivation(self):
        alleles = [subtract_op, '?', '?', 1, 0]
        gene = KarvaGene(alleles, 1, [2, 5])
        self.assertEqual(gene._evaluation, [subtract_op, 5, 2])
        self.assertEqual(gene.alleles, alleles)

        # Make sure the RNCs are evaluated correctly
        o = object()
        self.assertEqual(3, gene(o))

        # Make sure all caching is done
        self.assertTrue(o in getattr(gene, '___call___memo'))
        self.assertEqual(gene._evaluation, [3, 5, 2])

        # And that changes to the used RNCs eliminate that cache
        gene2 = gene.derive([(4, [1])])
        self.assertNotEqual(gene2._evaluation, gene._evaluation)
        self.assertRaises(AttributeError, getattr, gene2, '___call___memo')
Exemplo n.º 5
0
 def setUp(self):
     self.gene = KarvaGene([add_op, subtract_op, 'a', 1, 'a'], 2)
Exemplo n.º 6
0
 def setUp(self):
     self.gene = KarvaGene([z,z,'y',3,4], 1)
     self.chromosome = Computation([self.gene], 2)