def test_tournament_distribution(self) -> None: # Test default distribution set_numpy_seed() population = get_dummy_population(-100, -90, 1000) values_def = self.fetch_distribution(population) self.assertListEqual([547, 243, 110, 41, 34, 11, 9, 2, 0, 3], list(values_def))
def test_tournament_distribution_stochastic(self) -> None: # Test stochastic distribution when k=1 set_numpy_seed() population = get_dummy_population(-100, -90, 1000) self.selector.k = 1 values_stochastic = self.fetch_distribution(population) self.selector.__init__() # reinitialise to reset k to default self.assertTrue((20-np.sum(np.log10(values_stochastic))) <= 0.02)
def test_tournament_distribution_deterministic(self) -> None: # Test deterministic tournament selection set_numpy_seed() population = get_dummy_population(-100, -90, 1000) values_def = self.fetch_distribution(population) self.selector.p = 1 # Set p_select for the fittest member in the tournament to 1 values_deterministic = self.fetch_distribution(population) self.selector.__init__() # reinitialise to reset p to default self.assertTrue(values_deterministic[0] > values_def[0])
def test_boltzmann_distribution(self) -> None: # TODO: come up with a better test for distributions # self.skipTest("not_working") population = get_dummy_population(-100, -90, 1000) parents = [] for i in range(500): p1, p2 = self.selector(population) parents.append(p1.cost) parents.append(p2.cost) parent_costs = np.array(parents) values, _ = np.histogram(parent_costs) self.assertListEqual([515, 250, 108, 59, 31, 17, 11, 5, 3, 1], list(values))
def setUp(self) -> None: """Sets the random seed and sets up the dummy population and Rank selector""" set_numpy_seed() self.selector = RankSelector() self.population = get_dummy_population()