class TestOperator(unittest.TestCase):
    def setUp(self):
        self.evolution = op.Evolution(game_path="",
                                      write_path="",
                                      read_path="")
        self.population_mock_1 = [
            LevelIndividual([]),
            LevelIndividual([]),
            LevelIndividual([]),
            LevelIndividual([])
        ]
        self.population_mock_2 = [
            LevelIndividual([]),
            LevelIndividual([]),
            LevelIndividual([]),
            LevelIndividual([])
        ]
        for n in range(len(self.population_mock_1)):
            self.population_mock_1[n].fitness = n
        for n in range(len(self.population_mock_2)):
            self.population_mock_2[n].fitness = n + len(self.population_mock_1)

        self.individual_1 = LevelIndividual([
            BlockGene(type=1, pos=[0, 0], r=0),
            BlockGene(type=1, pos=[0.42, 0.42], r=0),
            BlockGene(type=1, pos=[0.42, 2.5], r=0),
            BlockGene(type=7, pos=[0.5, 1.5], r=2)
        ])
        self.individual_2 = LevelIndividual([
            BlockGene(type=1, pos=[0, 0], r=0),
            BlockGene(type=1, pos=[0.42, 0.42], r=0),
            BlockGene(type=1, pos=[0.42, 2.5], r=0),
            BlockGene(type=7, pos=[0.8, 2.0], r=2)
        ])
        self.merged_blocks = [
            BlockGene(type=1, pos=[0, 0], r=0),
            BlockGene(type=1, pos=[0.42, 0.42], r=0),
            BlockGene(type=1, pos=[0.42, 2.5], r=0),
            BlockGene(type=7, pos=[0.5, 1.5], r=2),
            BlockGene(type=7, pos=[0.8, 2.0], r=2)
        ]

    def test_replacement(self):
        self.evolution.population = self.population_mock_1
        result = self.evolution.elitistReplacement(self.population_mock_2,
                                                   len(self.population_mock_1))
        self.assertEqual(result, self.population_mock_1, "Replacement working")

    def test_fitness(self):  #???
        pass

    def test_merge_blocks(self):
        common = []
        merged = [
            x for x in self.individual_1.blocks() + self.individual_2.blocks()
            if x not in common and (common.append(x) or True)
        ]
        self.assertEqual(merged, self.merged_blocks,
                         "Unique blocks line is working")

    def test_cross(self):

        blocks = random.sample(self.merged_blocks,
                               len(self.individual_1.blocks()))
        with mock.patch('random.sample', lambda x, v: blocks):
            result = self.evolution.crossSampleNoDuplicate(
                [self.individual_1, self.individual_2])[0]

        self.assertTrue(
            np.all(np.asarray([x for x in result.blocks() if x in blocks])))

    def test_mutation(self):  #?
        pass

    def test_selection(self):
        parents = [[self.population_mock_1[0], self.population_mock_2[0]],
                   [self.population_mock_1[1], self.population_mock_1[2]]]
        self.evolution.population = self.population_mock_1 + self.population_mock_2
        with mock.patch('random.sample', lambda x, v: parents.pop(0)):
            result = self.evolution.selectionTournament(0.125)

        self.assertEqual(
            [self.population_mock_1[0], self.population_mock_1[1]], result,
            "Selection tournament correct")

    def test_cross_common(self):
        common = [
            BlockGene(type=1, pos=[0, 0], r=0),
            BlockGene(type=1, pos=[0.42, 0.42], r=0),
            BlockGene(type=1, pos=[0.42, 2.5], r=0)
        ]
        uncommon = [
            BlockGene(type=7, pos=[0.5, 1.5], r=2),
            BlockGene(type=7, pos=[0.8, 2.0], r=2)
        ]

        with mock.patch('random.shuffle', lambda x: uncommon):
            result = self.evolution.crossMaintainCommon(
                [self.individual_1, self.individual_2])

        self.assertTrue(
            np.all(
                np.asarray([
                    x for x in result[0].blocks() if x in common + uncommon[:1]
                ]))
            and np.all(
                np.asarray([
                    x for x in result[1].blocks() if x in common + uncommon[1:]
                ])))
Пример #2
0
class TestLevelIndividual(unittest.TestCase):
    def setUp(self):
        self.block1 = BlockGene(type=0, pos=[0, 0], r=0)
        self.block2 = BlockGene(type=1, pos=[0, 0], r=1)
        self.individual = LevelIndividual([self.block1])
        self.blocklist1 = [
            BlockGene(type=0, pos=[0, 0], r=0),
            BlockGene(type=0, pos=[0.42, 0.42], r=0),
            BlockGene(type=0, pos=[0.42, 2.5], r=0),
            BlockGene(type=7, pos=[0.5, 1.5], r=2)
        ]
        self.individual2 = LevelIndividual(self.blocklist1)

    def test_should_initialize_individual_OK(self):
        self.assertIsInstance(self.individual, LevelIndividual,
                              "Object created")

    def test_append_block(self):
        self.individual.appendBlock(self.block2)
        self.assertEqual(self.individual.blocks(), [self.block1, self.block2],
                         "Block appended")

    def test_remove_block(self):
        self.individual.removeBlock(0)
        self.assertEqual(self.individual.blocks(), [], "Block removed")

    def test_update_block(self):
        self.individual.updateBlock(0, self.block2)
        self.assertEqual(self.individual.blocks(), [self.block2],
                         "Block changed")

    def test_try_append_block_error(self):
        self.assertTrue(not self.individual.tryAppendBlock(self.block2)
                        and self.individual.blocks() == [self.block1])

    def test_try_append_block_success(self):
        block = BlockGene(type=0, pos=[2, 2], r=0)
        self.assertTrue(
            self.individual.tryAppendBlock(block)
            and self.individual.blocks() == [self.block1, block])

    def test_overlapping_blocks(self):
        self.assertEqual(self.individual2.numberOverlappingBlocks(), 6)

    def test_overlapping_blocks_update(self):
        self.individual2.updateBlock(3, BlockGene(type=4, pos=[0.5, 1.5], r=0))
        self.assertEqual(self.individual2.numberOverlappingBlocks(), 2)

    def test_overlapping_blocks_remove(self):
        self.individual2.removeBlock(3)
        self.assertEqual(self.individual2.numberOverlappingBlocks(), 2)

    def test_overlapping_blocks_append(self):
        self.individual2.appendBlock(BlockGene(type=7, pos=[0.5, 1.5], r=0))
        self.assertEqual(self.individual2.numberOverlappingBlocks(), 8)

    def test_init_random_len(self):
        individual = LevelIndividual([]).initRandom(8)
        self.assertTrue(len(individual.blocks()) == 8)

    def test_init_discrete_len(self):
        individual = LevelIndividual([]).initDiscrete(8)
        self.assertTrue(len(individual.blocks()) == 8)

    def test_init_no_overlapping_len(self):
        individual = LevelIndividual([]).initNoOverlapping(8)
        self.assertTrue(len(individual.blocks()) == 8)

    def test_init_discrete_no_overlapping_len(self):
        individual = LevelIndividual([]).initDiscreteNoOverlapping(8)
        self.assertTrue(len(individual.blocks()) == 8)

    def test_init_no_overlapping(self):
        individual = LevelIndividual([]).initNoOverlapping(8)
        self.assertTrue(individual.numberOverlappingBlocks() == 0)

    def test_init_discrete_no_overlapping(self):
        individual = LevelIndividual([]).initDiscreteNoOverlapping(8)
        self.assertTrue(individual.numberOverlappingBlocks() == 0)

    def test_weird_case(self):
        individual = LevelIndividual([
            BlockGene(type=6, pos=[1.47, -2.88], r=1),
            BlockGene(type=3, pos=[0.0, -2.88], r=1),
            BlockGene(type=4, pos=[2.31, -0.5699999999999998], r=3),
            BlockGene(type=5, pos=[4.62, -1.41], r=3),
            BlockGene(type=5, pos=[1.26, -0.5699999999999998], r=1),
            BlockGene(type=2, pos=[0.0, -0.7799999999999998], r=3),
            BlockGene(type=3, pos=[4.41, -2.25], r=2),
            BlockGene(type=5, pos=[2.31, -3.09], r=0)
        ])

        print(individual.numberOverlappingBlocks())
        individual.calculatePreFitness()
        print(individual.fitness)
        individual.calculateFitness([22.38628, 12.7887])
        print(individual.fitness)
Пример #3
0
 def test_init_discrete_len(self):
     individual = LevelIndividual([]).initDiscrete(8)
     self.assertTrue(len(individual.blocks()) == 8)
Пример #4
0
 def test_init_discrete_no_overlapping_len(self):
     individual = LevelIndividual([]).initDiscreteNoOverlapping(8)
     self.assertTrue(len(individual.blocks()) == 8)
Пример #5
0
 def test_init_random_len(self):
     individual = LevelIndividual([]).initRandom(8)
     self.assertTrue(len(individual.blocks()) == 8)