class RandomNeighboursTestCase(unittest.TestCase): def setUp(self) -> None: self.solution_a = BinarySolution(number_of_objectives=1, number_of_variables=1, initial_energy=20) self.solution_b = FloatSolution(lower_bound=[0.1], upper_bound=[0.5], number_of_objectives=1, initial_energy=40) self.solution_c = BinarySolution(number_of_objectives=1, number_of_variables=2, initial_energy=30) self.solution_d = BinarySolution(number_of_objectives=1, number_of_variables=2, initial_energy=30) self.neighbours_operator = RandomNeighbours(seed=1) def test_random_neighbours(self): individual = self.solution_a rest = [self.solution_b, self.solution_c, self.solution_d] neigh = self.neighbours_operator.execute((individual, rest)) self.assertEqual(neigh, self.solution_b) def test_no_neighbours_left(self): individual = self.solution_a neigh = self.neighbours_operator.execute((individual, [])) self.assertIsNone(neigh)
def setUp(self) -> None: problem1 = OneMax(number_of_bits=512) self.emas1 = Emas( problem=problem1, initial_population_size=1000, initial_inidividual_energy=10, reproduction_threshold=20, energy_exchange_operator=FractionEnergyExchange(0.5), death_operator=ThresholdDeath(threshold=5, neighbours_operator=RandomNeighbours()), termination_criterion=StoppingByEvaluations(max_evaluations=100000), neighbours_operator=RandomNeighbours(), reproduction_operator=FractionEnergyReproduction(0.5, BitFlipMutation(0.5), SPXCrossover(0.5)) ) problem2 = Sphere(number_of_variables=10) self.emas2 = Emas( problem=problem2, initial_population_size=1000, initial_inidividual_energy=10, reproduction_threshold=20, energy_exchange_operator=FractionEnergyExchange(0.5), death_operator=ThresholdDeath(threshold=5, neighbours_operator=RandomNeighbours()), termination_criterion=StoppingByEvaluations(max_evaluations=50000), neighbours_operator=RandomNeighbours(), reproduction_operator=FractionEnergyReproduction(0.5, PolynomialMutation(0.5), SBXCrossover(0.5)) )
def setUp(self) -> None: self.solution_a = BinarySolution(number_of_objectives=1, number_of_variables=1, initial_energy=20) self.solution_b = FloatSolution(lower_bound=[0.1], upper_bound=[0.5], number_of_objectives=1, initial_energy=40) self.solution_c = BinarySolution(number_of_objectives=1, number_of_variables=2, initial_energy=30) self.solution_d = BinarySolution(number_of_objectives=1, number_of_variables=2, initial_energy=30) self.neighbours_operator = RandomNeighbours(seed=1)
def test_proper_execution_2(self): death_operator = ThresholdDeath( 10, neighbours_operator=RandomNeighbours(seed=1)) total_energy_before = sum([x.energy for x in self.solutions]) after_death_solutions = death_operator.execute(self.solutions) total_energy_after = sum([x.energy for x in after_death_solutions]) self.assertEqual(len(after_death_solutions), 3) self.assertTrue(self.solution_a in after_death_solutions) self.assertTrue(self.solution_b in after_death_solutions) self.assertTrue(self.solution_c in after_death_solutions) self.assertEqual(total_energy_after, total_energy_before) self.assertEqual(self.solution_a.energy, 20) self.assertEqual(self.solution_b.energy, 40) self.assertEqual(self.solution_c.energy, 30)
def test_proper_execution(self): death_operator = ThresholdDeath( 30, neighbours_operator=RandomNeighbours(seed=1)) total_energy_before = sum([x.energy for x in self.solutions]) after_death_solutions = death_operator.execute(self.solutions) total_energy_after = sum([x.energy for x in after_death_solutions]) self.assertEqual(len(after_death_solutions), 2) self.assertTrue(self.solution_b in after_death_solutions) self.assertTrue(self.solution_c in after_death_solutions) self.assertEqual(total_energy_after, total_energy_before) self.assertTrue( (self.solution_b.energy == 60 and self.solution_c.energy == 30) or (self.solution_b.energy == 40 and self.solution_c.energy == 50))
def test_raises_no_alive_individuals(self): death_operator = ThresholdDeath( 50, neighbours_operator=RandomNeighbours(seed=1)) self.assertRaises(NoIndividualsAliveException, death_operator.execute, self.solutions)
def __init__(self, threshold: float, neighbours_operator: Neighbours = RandomNeighbours()): self.threshold = threshold self.neighbours_operator = neighbours_operator