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 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 step_discrepancy(self, solutions): ''' Perform local search using gradient on the discrepancy objective The step_discrepancy applies the local search to all solutions :param solutions: contains solutions for local search :return: ''' grad_solutions = [] for sol in solutions: Yt_pseu = np.array(sol.variables) M = self.mmd_matrix(Yt_pseu) left = np.dot(self.A + M, self.K) \ + self.eta * np.eye(self.no_src_instances + self.no_tar_instances, self.no_src_instances + self.no_tar_instances) beta = np.dot(np.linalg.inv(left), np.dot(self.A, self.YY)) F = np.dot(self.K, beta) Y_pseu = np.argmax(F, axis=1) + 1 Yt_pseu = Y_pseu[self.no_src_instances:].tolist() new_solution = IntegerSolution(self.lower_bound, self.upper_bound, self.number_of_objectives, self.number_of_constraints) new_solution.variables = [] for _, value in enumerate(Yt_pseu): new_solution.variables.append(value) grad_solutions.append(new_solution) return grad_solutions
def test_should_the_solution_change__if_the_probability_is_one(self): operator = IntegerPolynomial(1.0) solution = IntegerSolution(2, 1, 0, [-5, -5, -5], [5, 5, 5]) solution.variables = [1, 2, 3] mutated_solution = operator.execute(solution) self.assertNotEqual([1, 2, 3], mutated_solution.variables) self.assertEqual([True, True, True], [isinstance(x, int) for x in mutated_solution.variables])
def create_solution(self) -> IntegerSolution: new_solution = IntegerSolution(self.lower_bound, self.upper_bound, self.number_of_objectives, self.number_of_constraints) new_solution.variables = \ [int(random.uniform(self.lower_bound[i]*1.0, self.upper_bound[i]*1.0)) for i in range(self.number_of_variables)] return new_solution
def ordenar_solucion(self, solution: IntegerSolution) -> IntegerSolution: solution.variables = [int(i) for i in solution.variables] for i in range(0, int(solution.number_of_variables / 5)): for j in range(i, int(solution.number_of_variables / 5)): if (solution.variables[j * 5] < solution.variables[i * 5]): aux = solution.variables[j * 5:j * 5 + 5] solution.variables[j * 5:j * 5 + 5] = solution.variables[i * 5:i * 5 + 5] solution.variables[i * 5:i * 5 + 5] = aux[:] return solution
def test_should_the_solution_remain_unchanged_if_the_probability_is_zero( self): operator = IntegerPolynomialMutation(0.0) solution = IntegerSolution([-5, -5, -5], [5, 5, 5], 2) solution.variables = [1, 2, 3] mutated_solution = operator.execute(solution) self.assertEqual([1, 2, 3], mutated_solution.variables) self.assertEqual( [True, True, True], [isinstance(x, int) for x in mutated_solution.variables])
def create_solution(self) -> IntegerSolution: new_solution = IntegerSolution( self.number_of_variables, self.number_of_objectives, self.number_of_constraints, self.lower_bound, self.upper_bound) new_solution.variables = \ [int(random.uniform(self.lower_bound[i]*1.0, self.upper_bound[i]*1.0)) for i in range(self.number_of_variables)] return new_solution
def test_should_copy_work_properly(self) -> None: solution = IntegerSolution(2, 3, [0, 5], [1, 2]) solution.variables = [1, 2] solution.objectives = [0.16, -2.34, 9.25] solution.attributes["attr"] = "value" new_solution = copy.copy(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.variables, new_solution.variables) self.assertEqual(solution.objectives, new_solution.objectives) self.assertEqual(solution.lower_bound, new_solution.lower_bound) self.assertEqual(solution.upper_bound, new_solution.upper_bound) self.assertIs(solution.lower_bound, solution.lower_bound) self.assertIs(solution.upper_bound, solution.upper_bound) self.assertEqual(solution.attributes, new_solution.attributes)
def create_solution(self) -> IntegerSolution: new_solution = IntegerSolution(self.lower_bound, self.upper_bound, self.number_of_objectives, self.number_of_constraints) """ Method to create a solution for JmetalPy based on the initialised vector """ if self.init_index == len(self.init_label_vector): self.init_index = 0 pseu_label = self.init_label_vector[self.init_index] new_solution.variables = [] for _, value in enumerate(pseu_label): new_solution.variables.append(value) self.init_index = self.init_index + 1 return new_solution
def create_solution(self) -> IntegerSolution: new_solution = IntegerSolution(self.lower_bound, self.upper_bound, self.number_of_objectives, self.number_of_constraints) new_solution.number_of_variables = self.number_of_variables new_solution.attributes = { "fitness": [0], "variables": [], "weights": np.zeros(self.number_of_objectives) } new_solution.variables = ([ int(random.uniform(self.lower_bound[j], self.upper_bound[j])) for j in range(0, new_solution.number_of_variables) ]) return new_solution
def test_should_copy_work_properly(self) -> None: solution = IntegerSolution(2, 3, 2, [0, 5], [1, 2]) solution.variables = [1, 2] solution.objectives = [0.16, -2.34, 9.25] solution.attributes["attr"] = "value" new_solution = copy.copy(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, new_solution.variables) self.assertEqual(solution.objectives, new_solution.objectives) self.assertEqual(solution.lower_bound, new_solution.lower_bound) self.assertEqual(solution.upper_bound, new_solution.upper_bound) self.assertIs(solution.lower_bound, solution.lower_bound) self.assertIs(solution.upper_bound, solution.upper_bound) self.assertEqual({}, new_solution.attributes)
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])