Esempio n. 1
0
    def test_views_swap_all_clusters(self):
        orig_all_vals = np.copy(self.indiv2.all_values)

        self.indiv1, self.indiv2 = Genotype.xover_cluster(self.indiv1,
                                                          self.indiv2,
                                                          cxpb=1)

        self.indiv1.recreate_views()
        self.indiv1.resample_values()

        close = np.allclose(self.indiv1.all_values, orig_all_vals)

        self.assertTrue(close)
Esempio n. 2
0
    def test_uniform_crossover_clusters(self):
        rng = np.random.RandomState(42)
        swaps = [True if rng.rand() < 0.5 else False for _ in self.indiv1]

        indiv1_ids = [id(i) for i in self.indiv1]
        indiv2_ids = [id(i) for i in self.indiv2]

        Genotype.global_rng = np.random.RandomState(42)
        self.indiv1, self.indiv2 = Genotype.xover_cluster(
            self.indiv1, self.indiv2)
        # Test whether cluster objects have been swapped or not
        for i, (clust1, clust2,
                swap) in enumerate(zip(self.indiv1, self.indiv2, swaps)):
            with self.subTest(i=i):
                if swap:
                    self.assertEqual(indiv1_ids[i], id(clust2))
                    self.assertEqual(indiv2_ids[i], id(clust1))
                else:
                    self.assertEqual(indiv1_ids[i], id(clust1))
                    self.assertEqual(indiv2_ids[i], id(clust2))