def evaluate(self, solution: IntegerSolution) -> IntegerSolution: """ Evaluate the fitness values of the given solution :param solution: :return: """ Yt_pseu = np.array(solution.variables) Y = np.concatenate((self.Ys, Yt_pseu)) F = np.zeros( (self.no_src_instances + self.no_tar_instances, self.no_class)) for index, l in enumerate(Y): F[index][l - 1] = 1.0 # calculate the manifold objective manifold = np.linalg.multi_dot([F.T, self.L, F]).trace() # calculate the discrepancy objective M = self.mmd_matrix(Yt_pseu) discrepancy = np.linalg.multi_dot([F.T, M, F]).trace() # calculate the cross domain error # error = self.cross_domain_error(Yt_pseu) solution.objectives[0] = discrepancy solution.objectives[1] = manifold # solution.objectives[2] = manifold # error return 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 evaluate(self, solution: IntegerSolution) -> IntegerSolution: EvaluationResult = Evaluation.evaluateNetwork(self.network, solution.variables) solution.objectives[0] = EvaluationResult[0] solution.objectives[1] = EvaluationResult[1] # solution.objectives[0] = solution.variables[0] # solution.objectives[1] = h * g return 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.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 evaluate(self, solution: IntegerSolution) -> IntegerSolution: string = solution.variables types = string[:len(code)] locations = string[len(code):] ind = [types, locations] #print(len(solution.objectives)) f1 = get_rt(ind) f2 = get_cost(ind) solution.objectives[0] = f1 solution.objectives[1] = f2 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 evaluate(self, solution: IntegerSolution) -> IntegerSolution: nb_nodes = self.eval_number_nodes(solution) nb_containers_per_node = self.eval_nb_containers_per_node(solution) cohesion, coupling = self.eval_cohesion_coupling(solution) nb_changes = self.eval_nb_changes(solution) solution.objectives[0] = nb_nodes solution.objectives[1] = nb_containers_per_node solution.objectives[2] = -1 * cohesion solution.objectives[3] = coupling solution.objectives[4] = nb_changes return solution
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 execute(self, solution: IntegerSolution) -> IntegerSolution: for i in range(solution.number_of_variables): if random.random() <= self.probability: y = solution.variables[i] yl, yu = solution.lower_bound[i], solution.upper_bound[i] if yl == yu: y = yl else: delta1 = (y - yl) / (yu - yl) delta2 = (yu - y) / (yu - yl) mut_pow = 1.0 / (self.distribution_index + 1.0) rnd = random.random() if rnd <= 0.5: xy = 1.0 - delta1 val = 2.0 * rnd + (1.0 - 2.0 * rnd) * (xy ** (self.distribution_index + 1.0)) deltaq = val ** mut_pow - 1.0 else: xy = 1.0 - delta2 val = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * (xy ** (self.distribution_index + 1.0)) deltaq = 1.0 - val ** mut_pow y += deltaq * (yu - yl) if y < solution.lower_bound[i]: y = solution.lower_bound[i] if y > solution.upper_bound[i]: y = solution.upper_bound[i] solution.variables[i] = int(round(y)) return solution
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, solution: IntegerSolution) -> IntegerSolution: for i in range(solution.number_of_variables): if random.random() <= self.probability: y = solution.variables[i] yl, yu = solution.lower_bound[i], solution.upper_bound[i] if yl == yu: y = yl else: delta1 = (y - yl) / (yu - yl) delta2 = (yu - y) / (yu - yl) mut_pow = 1.0 / (self.distribution_index + 1.0) rnd = random.random() if rnd <= 0.5: xy = 1.0 - delta1 val = 2.0 * rnd + (1.0 - 2.0 * rnd) * (xy**( self.distribution_index + 1.0)) deltaq = val**mut_pow - 1.0 else: xy = 1.0 - delta2 val = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * (xy**( self.distribution_index + 1.0)) deltaq = 1.0 - val**mut_pow y += deltaq * (yu - yl) if y < solution.lower_bound[i]: y = solution.lower_bound[i] if y > solution.upper_bound[i]: y = solution.upper_bound[i] solution.variables[i] = int(round(y)) return 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 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 evaluate(self, solution: IntegerSolution) -> IntegerSolution: variables = np.array(solution.variables) p = calculate_fail_number_GA(variables, self.dec2bin_param, self.group_name, self.program_name, self.algorithm) # the value is negated because we want to maximize "p" using a minimization problem solution.objectives[0] = -p return 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 test_should_default_constructor_create_a_valid_solution(self) -> None: solution = IntegerSolution(2, 3, [0, 5], [1, 2]) self.assertEqual(2, solution.number_of_variables) self.assertEqual(3, solution.number_of_objectives) self.assertEqual(2, len(solution.variables)) self.assertEqual(3, len(solution.objectives)) self.assertEqual([0, 5], solution.lower_bound) self.assertEqual([1, 2], solution.upper_bound)
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 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])
def evaluate(self, solution: IntegerSolution) -> IntegerSolution: self.phenotype +=1 limit = [self.offspring_population_size if self.epoch != 1 else self.population_size] if self.phenotype%(limit[0]+1) == 0: self.epoch += 1 self.phenotype = 1 key = f"{solution.variables}" value = self.dictionary.get(key) if value == None: # Decoding passes = "" for i in range(self.number_of_variables): passes += f" {self.llvm.get_passes()[solution.variables[i]]}" # Optimize and generate resources self.llvm.toIR(self.llvmfiles.get_original_bc(), self.llvmfiles.get_optimized_bc(), passes=passes) self.llvm.toExecutable(self.llvmfiles.get_optimized_bc(), self.llvmfiles.get_optimized_exe()) self.llvm.toAssembly(self.llvmfiles.get_optimized_bc(), self.llvmfiles.get_optimized_ll()) # Get measures self.evaluator.evaluate(source_ll=self.llvmfiles.get_optimized_ll(), source_exe=self.llvmfiles.get_optimized_exe()) solution.objectives[0] = self.evaluator.get_codelines() solution.objectives[1] = self.evaluator.get_tags() solution.objectives[2] = self.evaluator.get_total_jmps() solution.objectives[3] = self.evaluator.get_function_tags() solution.objectives[4] = self.evaluator.get_calls() self.dictionary.update({key: solution.objectives}) self.evaluator.reset() else: # Get stored value solution.objectives[0] = value[0] solution.objectives[1] = value[1] solution.objectives[2] = value[2] solution.objectives[3] = value[3] solution.objectives[4] = value[4] if self.verbose: print("evaluated solution {:3} from epoch {:3} : variables={}, fitness={}"\ .format(self.phenotype,self.epoch,solution.variables,solution.objectives)) return solution
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: IntegerSolution) -> IntegerSolution: string = solution.variables types = string[:len(code)] locations = string[len(code):] ind = [types, locations] f1 = get_rt(ind) f2 = get_cost(ind) solution.objectives[0] = f1 / 1000 + f2 / get_cost( [len(code) * [7], len(code) * [12]]) #solution.objectives[1] = f2 return solution
def execute(self, solution: IntegerSolution) -> IntegerSolution: #En esta parte del operador de mutacion se crea una nueva variable con sus 5 componentes if random.random() < self.probability and solution.attributes[ "configurations"] < self.max_config: for i in range(5): solution.lower_bound.append(solution.lower_bound[i]) solution.upper_bound.append(solution.upper_bound[i]) solution.variables.append( int( random.uniform(solution.lower_bound[i], solution.upper_bound[i]))) solution.attributes["configurations"] += 1 solution.number_of_variables += 5 for i in range(solution.number_of_variables): if random.random() <= self.probability: y = solution.variables[i] yl, yu = solution.lower_bound[i], solution.upper_bound[i] if yl == yu: y = yl else: delta1 = (y - yl) / (yu - yl) delta2 = (yu - y) / (yu - yl) mut_pow = 1.0 / (self.distribution_index + 1.0) rnd = random.random() if rnd <= 0.5: xy = 1.0 - delta1 val = 2.0 * rnd + (1.0 - 2.0 * rnd) * (xy**( self.distribution_index + 1.0)) deltaq = val**mut_pow - 1.0 else: xy = 1.0 - delta2 val = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * (xy**( self.distribution_index + 1.0)) deltaq = 1.0 - val**mut_pow y += deltaq * (yu - yl) if y < solution.lower_bound[i]: y = solution.lower_bound[i] if y > solution.upper_bound[i]: y = solution.upper_bound[i] solution.variables[i] = int(round(y)) return solution
def evaluate(self, solution: IntegerSolution) -> IntegerSolution: """falta evaluar los objetivos""" solution = self.ordenar_solucion(solution) #Ejemplos de evaluación solution.objectives[0] = sum(solution.variables) solution.objectives[1] = solution.variables[1] + solution.variables[2] solution.objectives[2] = solution.variables[1] / ( solution.variables[3]**2 + 1) solution.objectives[3] = sum(solution.variables[:4])**2 solution.objectives[4] = (sum(solution.variables[2:5]) + 1)**2 solution.objectives[5] = sum(solution.variables[4:])**2 return solution
from Utils import Filter startTime = timeit.default_timer() BestTirf = [] BestInterfaceQuantity = [] solutionsResult = [] frontResult = 0 max_evaluations = MAX_EVALUATION Net = Network(folderName="Topologia2") problemOTN = OTNProblem(Net, len(Net.LinkBundles)) # problemOTN = OTNProblemFloat(Net, len(Net.LinkBundles)) stopCriterion = StoppingByEvaluationsCustom(max_evaluations, REFERENCE_POINT) # To topology 1, 200 is enough reference_point = IntegerSolution([LOWER_BOUND], [UPPER_BOUND], problemOTN.number_of_objectives, ) reference_point.objectives = REFERENCE_POINT # Mandatory for HYPE mutation_probability = 0.08 swarm_size = POPULATION_SIZE def configure_experiment(problems: dict, n_run: int): jobs = [] for run in range(n_run): for problem_tag, problem in problems.items(): jobs.append( Job( algorithm=NSGAII( problem=problem, population_size=POPULATION_SIZE,
def test_should_constructor_create_a_non_null_object(self) -> None: solution = IntegerSolution(3, 2, [], []) self.assertIsNotNone(solution)