Example #1
0
    def test_should_the_solution_remain_unchanged_if_the_probability_is_zero(
            self):
        operator = BitFlipMutation(0.0)
        solution = BinarySolution(number_of_variables=1,
                                  number_of_objectives=1)
        solution.variables[0] = [True, True, False, False, True, False]

        mutated_solution = operator.execute(solution)
        self.assertEqual([True, True, False, False, True, False],
                         mutated_solution.variables[0])
Example #2
0
    def test_should_the_solution_change_all_the_bits_if_the_probability_is_one(
            self):
        operator = BitFlipMutation(1.0)
        solution = BinarySolution(number_of_variables=2,
                                  number_of_objectives=1)
        solution.variables[0] = [True, True, False, False, True, False]
        solution.variables[1] = [False, True, True, False, False, True]

        mutated_solution = operator.execute(solution)
        self.assertEqual([False, False, True, True, False, True],
                         mutated_solution.variables[0])
        self.assertEqual([True, False, False, True, True, False],
                         mutated_solution.variables[1])
Example #3
0
    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))
        )
Example #4
0
    def setUp(self) -> None:
        problem = Knapsack(5, 5, [1, 2, 3, 4, 5], [1, 2, 3, 4, 5])
        self.mother = problem.create_solution()
        self.father = problem.create_solution()

        self.mother.energy = 20
        self.father.energy = 40
        self.reproduction_operator = FractionEnergyReproduction(
            0.5, BitFlipMutation(0.5), SPXCrossover(0.5))
Example #5
0
    def test_should_constructor_create_a_valid_operator_when_adding_two_mutation_operators(
            self):
        polynomial_mutation = PolynomialMutation(1.0, 20.0)
        bit_flip_mutation = BitFlipMutation(0.01)

        operator = CompositeMutation([polynomial_mutation, bit_flip_mutation])

        self.assertIsNotNone(operator)
        self.assertEqual(2, len(operator.mutation_operators_list))
        self.assertTrue(
            issubclass(operator.mutation_operators_list[0].__class__,
                       PolynomialMutation))
        self.assertTrue(
            issubclass(operator.mutation_operators_list[1].__class__,
                       BitFlipMutation))
Example #6
0
 def test_should_constructor_raises_an_exception_is_probability_is_negative(
         self) -> None:
     with self.assertRaises(Exception):
         BitFlipMutation(-1)
Example #7
0
 def test_should_constructor_raise_an_exception_if_the_probability_is_lower_than_zero(
         self):
     with self.assertRaises(Exception):
         BitFlipMutation(-12)
Example #8
0
 def test_should_constructor_raise_an_exception_if_the_probability_is_greater_than_one(
         self):
     with self.assertRaises(Exception):
         BitFlipMutation(2)
Example #9
0
 def test_should_constructor_create_a_valid_operator(self):
     operator = BitFlipMutation(0.5)
     self.assertEqual(0.5, operator.probability)
Example #10
0
 def test_should_constructor_create_a_non_null_object(self):
     solution = BitFlipMutation(1.0)
     self.assertIsNotNone(solution)
Example #11
0
 def test_should_constructor_raises_an_exception_is_probability_is_higher_than_one(
         self) -> None:
     with self.assertRaises(Exception):
         BitFlipMutation(1.01)