Exemplo n.º 1
0
 def test_init_from_iterable(self):
     b = Bitstring("0001")
     b2 = Bitstring([1, 0, 0, 0])
     b3 = Bitstring((True, False, False, False))
     b4 = Bitstring("1")
     self.assertEqual(b, b2)
     self.assertEqual(b, b3)
     self.assertNotEqual(b, b4)
 def test_selected_loci_as_int(self):
     b = Bitstring('110110')
     pos_0 = b.selected_loci_as_int([0])
     self.assertEqual(0, pos_0)
     pos_123 = b.selected_loci_as_int([1, 2, 3])
     self.assertEqual(3, pos_123)
     pos_1 = b.selected_loci_as_int([1])
     self.assertEqual(1, pos_1)
Exemplo n.º 3
0
 def test_selected_loci_as_int(self):
     b = Bitstring('110110')
     pos_0 = b.selected_loci_as_int([0])
     self.assertEqual(0, pos_0)
     pos_123 = b.selected_loci_as_int([1, 2, 3])
     self.assertEqual(3, pos_123)
     pos_1 = b.selected_loci_as_int([1])
     self.assertEqual(1, pos_1)
Exemplo n.º 4
0
 def test_int(self):
     b = Bitstring('110110')
     c = Bitstring('0000')
     d = Bitstring('')
     e = Bitstring('10')
     self.assertEqual(int(b), 54)
     self.assertEqual(int(c), 0)
     self.assertEqual(int(d), 0)
     self.assertEqual(int(e), 2)
Exemplo n.º 5
0
    def test_eq(self):
        b = Bitstring("10")
        b2 = Bitstring("10")
        b3 = Bitstring("100")
        b4 = Bitstring("11")
        self.assertTrue(b == b2)
        self.assertTrue(b != b3)
        self.assertTrue(b != b4)

        s = "10"
        self.assertFalse(b == s)
Exemplo n.º 6
0
 def test_iter(self):
     b = Bitstring("01")
     for pos, expected in zip(b, [True, False]):
         self.assertEqual(pos, expected)
Exemplo n.º 7
0
 def test_hash(self):
     b = Bitstring("000")
     b_ = Bitstring("101")
     self.assertSetEqual({b, b_}, {Bitstring("000"), Bitstring("101")})
Exemplo n.º 8
0
    def __init__(self, *args, **kwargs):
        super(Organism, self).__init__(*args, **kwargs)
        if (isinstance(self, Organism)
                and not isinstance(self.value, Bitstring)):
            raise ValueError("Bitstring Organisms must hold Bitstrings")

    def _mutated_value(self):
        """
        the mutate method of an organism calls the module
        mutate and returns a new organism with the mutation
        note: original organism is unchanged
        """
        return self.value.single_step_mutant()

    def _evaluate_fitness(self):
        """
        fitness of this organism is the hamming distance of
        its bitstring from a bitstring composed of all False's
        """
        return 1 + sum(self.value)


default_organism = Organism(Bitstring(False for _ in range(10)))


def random_organism(length):
    bits = [True, False]
    sequence = [random.choice(bits) for _ in range(length)]
    bs = Bitstring(sequence)
    return Organism(bs)
Exemplo n.º 9
0
 def test_copy(self):
     b = Bitstring("10101")
     b_copy = copy.copy(b)
     self.assertEqual(b, b_copy)
Exemplo n.º 10
0
def random_organism(length):
    bits = [True, False]
    sequence = [random.choice(bits) for _ in range(length)]
    bs = Bitstring(sequence)
    return Organism(bs)
Exemplo n.º 11
0
 def test_hamming_distance(self):
     b = Bitstring("10101")
     b2 = Bitstring("10101")
     b3 = Bitstring("01010")
     self.assertEqual(b.hamming_distance(b2), 0)
     self.assertEqual(b.hamming_distance(b3), 5)
Exemplo n.º 12
0
 def test_repr(self):
     b = Bitstring("10")
     b_as_str = repr(b)
     b2 = eval(b_as_str)
     self.assertEqual(b, b2)
 def test_shift_by_bit(self):
     for _ in range(10):
         b = Bitstring("1010101")
         mutant = mutate.shift_by_bit(b)
         self.assertEqual(b.hamming_distance(mutant), 1)
Exemplo n.º 14
0
 def test_single_step_mutant(self):
     bs = Bitstring("00")
     mutant = bs.single_step_mutant()
     self.assertNotEqual(bs, mutant)
Exemplo n.º 15
0
 def test_init(self):
     b = Bitstring("01")
     self.assertEqual(True, b[0])
     self.assertEqual(False, b[1])
     self.assertEqual(2, len(b))
 def test_single_step_mutant(self):
     bs = Bitstring("00")
     mutant = bs.single_step_mutant()
     self.assertNotEqual(bs, mutant)
 def test_hamming_distance(self):
     b = Bitstring("10101")
     b2 = Bitstring("10101")
     b3 = Bitstring("01010")
     self.assertEqual(b.hamming_distance(b2), 0)
     self.assertEqual(b.hamming_distance(b3), 5)
Exemplo n.º 18
0
 def test_flip_position(self):
     b = Bitstring("00000")
     b_mutated = bitstring.flip_position(b, 0)
     self.assertEqual(b_mutated, Bitstring("00001"))
 def setUp(self):
     self.value_0 = Bitstring("000")
     self.value_1 = Bitstring("001")
     self.value_2 = Bitstring("111")