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))
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)
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])
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))
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
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
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])
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)
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)
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))
def test_should_constructor_raise_an_exception_if_the_distribution_index_is_negative( self): with self.assertRaises(Exception): SBXCrossover(0.1, -2.0)
def test_should_constructor_raise_an_exception_if_the_probability_is_negative( self): with self.assertRaises(Exception): SBXCrossover(-0.1, 2.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)
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)
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)
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)) )