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 evaluate(self, solution: CompositeSolution) -> CompositeSolution: antecedentes = solution.variables[0].variables consequentes = solution.variables[1].variables centroides = solution.variables[2].variables particoes = self.alterar_centroids(centroides) new_regras = self.cromossomo_para_regras(antecedentes, consequentes, self.semente.qtdAntecedenteRegra, particoes) classificacao = Classificacao(particoes, new_regras, self.instancias, self.classes) acuracia, interpretabilidadeCondicoes = classificacao.classificar() solution.objectives[0] = -acuracia solution.objectives[1] = -interpretabilidadeCondicoes return solution
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 evaluate(self, solution: CompositeSolution) -> CompositeSolution: distance_to_n = sum( [abs(self.n - value) for value in solution.variables[0].variables]) distance_to_m = sum( [abs(self.m - value) for value in solution.variables[0].variables]) distance_to_n += sum( [abs(self.n - value) for value in solution.variables[1].variables]) distance_to_m += sum( [abs(self.m - value) for value in solution.variables[1].variables]) solution.objectives[0] = distance_to_n solution.objectives[1] = distance_to_m return solution
def create_solution(self) -> CompositeSolution: # INICIALIZAÇÂO DO PROBLEMA attributes_solution = IntegerSolution(self.int_lower_bound_attribute, self.int_upper_bound_attribute, self.number_of_objectives, self.number_of_constraints) labels_solution = IntegerSolution(self.int_lower_bound_label, self.int_upper_bound_label, self.number_of_objectives, self.number_of_constraints) points_solution = FloatSolution(self.lower_centroids, self.upper_centroids, self.number_of_objectives, self.number_of_constraints) if self.isSeed: attributes_solution.variables = self.antecedentes labels_solution.variables = self.consequentes points_solution.variables = self.centroids self.isSeed = False else: attributes_solution.variables = \ [random.randint(self.int_lower_bound_attribute[i], self.int_upper_bound_attribute[i]) for i in range(len(self.int_lower_bound_attribute))] labels_solution.variables = \ [random.randint(self.int_lower_bound_label[i], self.int_upper_bound_label[i]) for i in range(len(self.int_lower_bound_label))] points_solution.variables = \ [random.uniform(self.lower_centroids[i], self.upper_centroids[i]) for i in range(len(self.lower_centroids))] return CompositeSolution([attributes_solution, labels_solution, points_solution])
def test_should_copy_work_properly(self): number_of_objectives = 3 number_of_constraints = 1 float_solution: FloatSolution = FloatSolution([1.0], [3.0], number_of_objectives, number_of_constraints) integer_solution: IntegerSolution = IntegerSolution( [2], [4], number_of_objectives, number_of_constraints) solution: CompositeSolution = CompositeSolution( [float_solution, integer_solution]) new_solution: CompositeSolution = copy.deepcopy(solution) self.assertEqual(solution.number_of_variables, new_solution.number_of_variables) self.assertEqual(solution.number_of_objectives, new_solution.number_of_objectives) self.assertEqual(solution.number_of_constraints, new_solution.number_of_constraints) self.assertEqual(solution.variables[0].number_of_variables, new_solution.variables[0].number_of_variables) self.assertEqual(solution.variables[1].number_of_variables, new_solution.variables[1].number_of_variables) self.assertEqual(solution.variables[0], new_solution.variables[0]) self.assertEqual(solution.variables[1], new_solution.variables[1]) self.assertEqual(solution.variables[0].variables, new_solution.variables[0].variables) self.assertEqual(solution.variables[1].variables, new_solution.variables[1].variables)
def test_should_constructor_create_a_valid_soltion_composed_of_a_float_and_an_integer_solutions( self): number_of_objectives = 3 number_of_constraints = 1 float_solution: FloatSolution = FloatSolution([1.0], [3.0], number_of_objectives, number_of_constraints) integer_solution: IntegerSolution = IntegerSolution( [2], [4], number_of_objectives, number_of_constraints) solution: CompositeSolution = CompositeSolution( [float_solution, integer_solution]) self.assertIsNotNone(solution) self.assertEqual(2, solution.number_of_variables) self.assertEqual(number_of_objectives, solution.number_of_objectives) self.assertEqual(number_of_constraints, solution.number_of_constraints) self.assertEqual(number_of_objectives, solution.variables[0].number_of_objectives) self.assertEqual(number_of_objectives, solution.variables[1].number_of_objectives) self.assertEqual(number_of_constraints, solution.variables[0].number_of_constraints) self.assertEqual(number_of_constraints, solution.variables[1].number_of_constraints) self.assertTrue(type(solution.variables[0] is FloatSolution)) self.assertTrue(type(solution.variables[1] is IntegerSolution))
def test_should_constructor_raise_an_exception_if_the_number_of_objectives_is_not_coherent( self): float_solution: FloatSolution = FloatSolution([1.0], [3.0], 3) integer_solution: IntegerSolution = IntegerSolution([2], [4], 2) with self.assertRaises(InvalidConditionException): CompositeSolution([float_solution, integer_solution])
def execute(self, solutions: List[CompositeSolution]) -> List[CompositeSolution]: Check.is_not_none(solutions) Check.that(len(solutions) == 2, "The number of parents is not two: " + str(len(solutions))) offspring1 = [] offspring2 = [] number_of_solutions_in_composite_solution = solutions[0].number_of_variables for i in range(number_of_solutions_in_composite_solution): parents = [solutions[0].variables[i], solutions[1].variables[i]] children = self.crossover_operators_list[i].execute(parents) offspring1.append(children[0]) offspring2.append(children[1]) return [CompositeSolution(offspring1), CompositeSolution(offspring2)]
def execute(self, solution: CompositeSolution) -> CompositeSolution: Check.is_not_none(solution) mutated_solution_components = [] for i in range(solution.number_of_variables): mutated_solution_components.append(self.mutation_operators_list[i].execute(solution.variables[i])) return CompositeSolution(mutated_solution_components)
def test_should_execute_raise_and_exception_if_the_types_of_the_solutions_do_not_match_the_operators( self): operator = CompositeMutation( [PolynomialMutation(1.0, 5.0), PolynomialMutation(0.9, 25.0)]) float_solution = FloatSolution([2.0], [3.9], 3) binary_solution = BinarySolution(1, 3, 0) float_solution.variables = [3.0] composite_solution = CompositeSolution( [float_solution, binary_solution]) with self.assertRaises(InvalidConditionException): operator.execute(composite_solution)
def create_solution(self) -> CompositeSolution: integer_solution = IntegerSolution(self.int_lower_bound, self.int_upper_bound, self.number_of_objectives, self.number_of_constraints) float_solution = FloatSolution(self.float_lower_bound, self.float_upper_bound, self.number_of_objectives, self.number_of_constraints) float_solution.variables = \ [random.uniform(self.float_lower_bound[i] * 1.0, self.float_upper_bound[i] * .01) for i in range(len(self.int_lower_bound))] integer_solution.variables = \ [random.uniform(self.float_lower_bound[i], self.float_upper_bound[i]) for i in range(len(self.float_lower_bound))] return CompositeSolution([integer_solution, float_solution])
def test_should_constructor_create_a_valid_not_none_composite_solution_composed_of_a_double_solution( self): composite_solution = CompositeSolution( [FloatSolution([1.0], [2.0], 2)]) self.assertIsNotNone(composite_solution)