Exemple #1
0
    def test_should_the_operator_work_with_a_solution_with_three_binary_variables(
            self, random_call):
        operator = SPXCrossover(1.0)
        solution1 = BinarySolution(number_of_variables=3,
                                   number_of_objectives=1)
        solution1.variables[0] = [True, False, False, True, True, False]
        solution1.variables[1] = [True, False, False, True, False, False]
        solution1.variables[2] = [True, False, True, True, True, True]
        solution2 = BinarySolution(number_of_variables=3,
                                   number_of_objectives=1)
        solution2.variables[0] = [False, True, False, False, True, True]
        solution2.variables[1] = [True, True, False, False, True, False]
        solution2.variables[2] = [True, True, True, False, False, True]

        random_call.return_value = 8
        offspring = operator.execute([solution1, solution2])
        self.assertEqual([True, False, False, True, True, False],
                         offspring[0].variables[0])
        self.assertEqual([True, False, False, False, True, False],
                         offspring[0].variables[1])
        self.assertEqual([True, True, True, False, False, True],
                         offspring[0].variables[2])
        self.assertEqual([False, True, False, False, True, True],
                         offspring[1].variables[0])
        self.assertEqual([True, True, False, True, False, False],
                         offspring[1].variables[1])
        self.assertEqual([True, False, True, True, True, True],
                         offspring[1].variables[2])
Exemple #2
0
    def test_should_the_solution_remain_unchanged_if_the_probability_is_zero(self):
        operator = SPXCrossover(0.0)
        solution1 = BinarySolution(number_of_variables=1, number_of_objectives=1)
        solution1.variables[0] = [True, False, False, True, True, False]
        solution2 = BinarySolution(number_of_variables=1, number_of_objectives=1)
        solution2.variables[0] = [False, True, False, False, True, False]

        offspring = operator.execute([solution1, solution2])
        self.assertEqual([True, False, False, True, True, False], offspring[0].variables[0])
        self.assertEqual([False, True, False, False, True, False], offspring[1].variables[0])
Exemple #3
0
    def test_should_the_operator_work_if_the_third_bit_is_selected(self, random_call):
        operator = SPXCrossover(1.0)
        solution1 = BinarySolution(number_of_variables=1, number_of_objectives=1)
        solution1.variables[0] = [True, False, False, True, True, False]
        solution2 = BinarySolution(number_of_variables=1, number_of_objectives=1)
        solution2.variables[0] = [False, True, False, False, True, True]

        random_call.return_value = 3
        offspring = operator.execute([solution1, solution2])
        self.assertEqual([True, False, False, False, True, True], offspring[0].variables[0])
        self.assertEqual([False, True, False, True, True, False], offspring[1].variables[0])
Exemple #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))
Exemple #5
0
    def test_should_constructor_create_a_valid_operator_when_adding_two_crossover_operators(self):
        sbx_crossover = SBXCrossover(1.0, 20.0)
        single_point_crossover = SPXCrossover(0.01)

        operator = CompositeCrossover([sbx_crossover, single_point_crossover])

        self.assertIsNotNone(operator)
        self.assertEqual(2, len(operator.crossover_operators_list))
        self.assertTrue(issubclass(operator.crossover_operators_list[0].__class__, SBXCrossover))
        self.assertTrue(issubclass(operator.crossover_operators_list[1].__class__, SPXCrossover))
Exemple #6
0
    def test_should_execute_raise_and_exception_if_the_types_of_the_solutions_do_not_match_the_operators(self):
        operator = CompositeCrossover([SBXCrossover(1.0, 5.0), SPXCrossover(0.9)])

        float_solution1 = FloatSolution([2.0], [3.9], 3)
        float_solution1.variables = [3.0]
        float_solution2 = FloatSolution([2.0], [3.9], 3)
        float_solution2.variables = [4.0]
        composite_solution1 = CompositeSolution([float_solution1, float_solution2])
        composite_solution2 = CompositeSolution([float_solution1, float_solution2])

        with self.assertRaises(InvalidConditionException):
            operator.execute([composite_solution1, composite_solution2])
Exemple #7
0
 def test_should_constructor_raise_an_exception_if_the_probability_is_lower_than_zero(
         self):
     with self.assertRaises(Exception):
         SPXCrossover(-12)
Exemple #8
0
 def test_should_constructor_raise_an_exception_if_the_probability_is_greater_than_one(
         self):
     with self.assertRaises(Exception):
         SPXCrossover(2)
Exemple #9
0
 def test_should_constructor_create_a_valid_operator(self):
     operator = SPXCrossover(0.5)
     self.assertEqual(0.5, operator.probability)
Exemple #10
0
 def test_should_constructor_create_a_non_null_object(self):
     solution = SPXCrossover(1.0)
     self.assertIsNotNone(solution)
Exemple #11
0
 def test_should_constructor_raises_an_exception_is_probability_is_higher_than_one(
         self) -> None:
     with self.assertRaises(Exception):
         SPXCrossover(1.01)
Exemple #12
0
 def test_should_constructor_raises_an_exception_is_probability_is_negative(
         self) -> None:
     with self.assertRaises(Exception):
         SPXCrossover(-1)
Exemple #13
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))
        )