Esempio n. 1
0
    def test_should_constructor_create_a_valid_operator_when_adding_a_single_crossover_operator(
            self):
        crossover: Crossover = SBXCrossover(0.9, 20.0)

        operator = CompositeCrossover([crossover])
        self.assertIsNotNone(operator)
        self.assertEqual(1, len(operator.crossover_operators_list))
Esempio n. 2
0
    def test_should_execute_work_properly_with_a_two_crossover_operators(self):
        operator = CompositeCrossover(
            [SBXCrossover(0.9, 20.0),
             IntegerSBXCrossover(0.1, 20.0)])

        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]
        integer_solution1 = IntegerSolution([2], [4], 3)
        integer_solution1.variables = [3.0]
        integer_solution2 = IntegerSolution([2], [7], 3)
        integer_solution2.variables = [4.0]

        composite_solution1 = CompositeSolution(
            [float_solution1, integer_solution1])
        composite_solution2 = CompositeSolution(
            [float_solution2, integer_solution2])

        children = operator.execute([composite_solution1, composite_solution2])

        self.assertIsNotNone(children)
        self.assertEqual(2, len(children))
        self.assertEqual(2, children[0].number_of_variables)
        self.assertEqual(2, children[1].number_of_variables)
Esempio n. 3
0
    def test_should_execute_with_an_invalid_solution_list_size_raise_an_exception(self):
        crossover: SBXCrossover = SBXCrossover(0.1, 20.0)

        solution = FloatSolution([1, 2], [2, 4], 2, 2)
        with self.assertRaises(Exception):
            crossover.execute([solution])

        with self.assertRaises(Exception):
            crossover.execute([solution, solution, solution])
Esempio n. 4
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))
Esempio n. 5
0
 def configurar_NSGAIII(self):
     
     algorithm = NSGAIII(
             problem=self.problema,
             reference_directions = UniformReferenceDirectionFactory(5, n_points=91),
             population_size=self.maxima_poblacion,
             mutation=PolynomialMutation(probability = self.probabilidad , distribution_index=0.20),
             crossover=SBXCrossover(probability= self.probabilidad, distribution_index=20),
             termination_criterion=StoppingByEvaluations(max_evaluations=self.evaluaciones),
             dominance_comparator=DominanceComparator())
     return algorithm
Esempio n. 6
0
    def configurar_SPEA2(self):

        algorithm = SPEA2(
                problem=self.problema,
                population_size=self.maxima_poblacion,
                offspring_population_size=self.maxima_poblacion,
                mutation=PolynomialMutation(probability=self.probabilidad, distribution_index=0.20),
                crossover=SBXCrossover(probability=self.probabilidad, distribution_index=20),
                termination_criterion=StoppingByEvaluations(max_evaluations=self.evaluaciones),
                dominance_comparator=DominanceComparator())
        return algorithm
Esempio n. 7
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])
Esempio n. 8
0
    def test_should_execute_return_the_parents_if_the_crossover_probability_is_zero(self):
        crossover: SBXCrossover = SBXCrossover(0.0, 20.0)

        solution1 = FloatSolution([1, 2], [2, 4], 2, 2)
        solution2 = FloatSolution([1, 2], [2, 4], 2, 2)

        solution1.variables = [1.5, 2.7]
        solution2.variables = [1.7, 3.6]

        offspring = crossover.execute([solution1, solution2])

        self.assertEqual(2, len(offspring))
        self.assertEqual(solution1.variables, offspring[0].variables)
        self.assertEqual(solution2.variables, offspring[1].variables)
Esempio n. 9
0
    def test_should_execute_work_with_a_solution_subclass_of_float_solution(self):
        class NewFloatSolution(FloatSolution):
            def __init__(self, lower_bound: List[float], upper_bound: List[float], number_of_objectives: int,
                         number_of_constraints: int = 0):
                super(NewFloatSolution, self).__init__(lower_bound, upper_bound, number_of_objectives,
                                                       number_of_constraints)

        solution1 = NewFloatSolution([1, 2], [2, 4], 2, 2)
        solution2 = NewFloatSolution([1, 2], [2, 4], 2, 2)

        solution1.variables = [1.5, 2.7]
        solution2.variables = [1.7, 3.6]

        crossover: SBXCrossover = SBXCrossover(0.0, 20.0)
        offspring = crossover.execute([solution1, solution2])

        self.assertEqual(2, len(offspring))
        self.assertEqual(solution1.variables, offspring[0].variables)
        self.assertEqual(solution2.variables, offspring[1].variables)
Esempio n. 10
0
    ncores = sum(client.ncores().values())
    print(f'{ncores} cores available on cluster')

    # creates the problem
    problem = ZDT1()

    # creates the algorithm
    max_evaluations = 25000

    algorithm = DistributedNSGAII(
        problem=problem,
        population_size=100,
        mutation=PolynomialMutation(probability=1.0 /
                                    problem.number_of_variables,
                                    distribution_index=20),
        crossover=SBXCrossover(probability=1.0, distribution_index=20),
        termination_criterion=StoppingByEvaluations(
            max_evaluations=max_evaluations),
        number_of_cores=ncores,
        client=client)

    algorithm.observable.register(observer=ProgressBarObserver(
        max=max_evaluations))
    algorithm.observable.register(observer=VisualizerObserver())

    algorithm.run()
    front = algorithm.get_result()

    print('Computing time: ' + str(algorithm.total_computing_time))
Esempio n. 11
0
 def test_should_constructor_raise_an_exception_if_the_distribution_index_is_negative(
         self):
     with self.assertRaises(Exception):
         SBXCrossover(0.1, -2.0)
Esempio n. 12
0
 def test_should_constructor_raise_an_exception_if_the_probability_is_negative(
         self):
     with self.assertRaises(Exception):
         SBXCrossover(-0.1, 2.0)
Esempio n. 13
0
 def test_should_constructor_raise_an_exception_if_the_probability_is_greater_than_one(
         self):
     with self.assertRaises(Exception):
         SBXCrossover(1.5, 2.0)
Esempio n. 14
0
    def test_should_constructor_assign_the_correct_distribution_index_value(
            self):
        distribution_index = 10.5
        crossover: SBXCrossover = SBXCrossover(0.1, distribution_index)

        self.assertEqual(distribution_index, crossover.distribution_index)
Esempio n. 15
0
    def test_should_constructor_assign_the_correct_probability_value(self):
        crossover_probability = 0.1
        crossover: SBXCrossover = SBXCrossover(crossover_probability, 2.0)

        self.assertEqual(crossover_probability, crossover.probability)
Esempio n. 16
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))
        )