def update_external_archive(self): feasible_solutions = [] for solution in self.solutions: if is_feasible(solution): feasible_solutions.append(copy.deepcopy(solution)) if len(feasible_solutions) > 0: feasible_solutions = feasible_solutions + self.archive ranking = FastNonDominatedRanking() ranking.compute_ranking(feasible_solutions) first_rank_solutions = ranking.get_subfront(0) if len(first_rank_solutions) <= self.population_size: self.archive = [] for solution in first_rank_solutions: self.archive.append(copy.deepcopy(solution)) else: crowding_distance = CrowdingDistance() while len(first_rank_solutions) > self.population_size: crowding_distance.compute_density_estimator( first_rank_solutions) first_rank_solutions = sorted( first_rank_solutions, key=lambda x: x.attributes['crowding_distance'], reverse=True) first_rank_solutions.pop() self.archive = [] for solution in first_rank_solutions: self.archive.append(copy.deepcopy(solution))
def execute(self, front: List[S]) -> List[S]: if front is None: raise Exception('The front is null') elif len(front) == 0: raise Exception('The front is empty') ranking = FastNonDominatedRanking(self.dominance_comparator) crowding_distance = CrowdingDistance() ranking.compute_ranking(front) ranking_index = 0 new_solution_list = [] while len(new_solution_list) < self.max_population_size: if len(ranking.get_subfront(ranking_index)) < (self.max_population_size - len(new_solution_list)): new_solution_list = new_solution_list + ranking.get_subfront(ranking_index) ranking_index += 1 else: subfront = ranking.get_subfront(ranking_index) crowding_distance.compute_density_estimator(subfront) sorted_subfront = sorted(subfront, key=lambda x: x.attributes['crowding_distance'], reverse=True) for i in range((self.max_population_size - len(new_solution_list))): new_solution_list.append(sorted_subfront[i]) return new_solution_list
def execute(self, front: List[S]) -> List[S]: if front is None: raise Exception('The front is null') elif len(front) == 0: raise Exception('The front is empty') ranking = FastNonDominatedRanking(self.dominance_comparator) ranking.compute_ranking(front) ranking_index = 0 new_solution_list = [] while len(new_solution_list) < self.max_population_size: if len(ranking.get_subfront(ranking_index)) < self.max_population_size - len(new_solution_list): subfront = ranking.get_subfront(ranking_index) new_solution_list = new_solution_list + subfront ranking_index += 1 else: subfront = ranking.get_subfront(ranking_index) parameter_K = len(subfront) - (self.max_population_size - len(new_solution_list)) while parameter_K > 0: subfront = self.compute_hypervol_fitness_values(subfront, self.reference_point, parameter_K) subfront = sorted(subfront, key=lambda x: x.attributes['fitness'], reverse=True) subfront = subfront[:-1] parameter_K = parameter_K - 1 new_solution_list = new_solution_list + subfront return new_solution_list
def execute(self, solution_list: List[S]) -> List[S]: ranking = FastNonDominatedRanking() crowding_distance = CrowdingDistance() ranking.compute_ranking(solution_list) ranking_index = 0 new_solution_list = [] while len(new_solution_list) < self.max_population_size: if len(ranking.get_subfront(ranking_index) ) < self.max_population_size - len(new_solution_list): new_solution_list = new_solution_list + ranking.get_subfront( ranking_index) ranking_index += 1 else: subfront = ranking.get_subfront(ranking_index) crowding_distance.compute_density_estimator(subfront) sorted_subfront = sorted( subfront, key=lambda x: x.attributes["crowding_distance"], reverse=True) for i in range( (self.max_population_size - len(new_solution_list))): new_solution_list.append(sorted_subfront[i]) return new_solution_list
def replacement(self, population: List[S], offspring_population: List[S]) -> List[S]: """ Implements NSGA-III selection as described in * Deb, K., & Jain, H. (2014). An Evolutionary Many-Objective Optimization Algorithm Using Reference-Point-Based Nondominated Sorting Approach, Part I: Solving Problems With Box Constraints. IEEE Transactions on Evolutionary Computation, 18(4), 577–601. doi:10.1109/TEVC.2013.2281535. """ # Algorithm 1 steps 4--8 ranking = FastNonDominatedRanking(self.dominance_comparator) ranking.compute_ranking(population + offspring_population) ranking_index = 0 pop = [] while len(pop) < self.population_size: if len(ranking.get_subfront( ranking_index)) < self.population_size - len(pop): pop += ranking.get_subfront(ranking_index) ranking_index += 1 else: break # complete selected individuals using the reference point based approach selection = EnvironmentalSelection( number_of_objectives=self.problem.number_of_objectives, k=self.population_size - len(pop)) pop += selection.execute(ranking.get_subfront(ranking_index)) return pop
def get_result(self) -> R: ranking = FastNonDominatedRanking(self.dominance_comparator) ranking.compute_ranking(self.solutions) try: non_dominated = ranking.get_nondominated() except IndexError: non_dominated = None return non_dominated if non_dominated else self.solutions
def get_result(self): """ Return only non dominated solutions.""" ranking = FastNonDominatedRanking(self.dominance_comparator) ranking.compute_ranking(self.solutions, k=self.population_size) for solution in self.solutions: # print(solution.objectives, self.target_pattern) goal_flag = np.zeros((7), dtype=int) for j in range(7): if solution.objectives[j] < self.target_value_threshold[j]: goal_flag[j] = 1 else: goal_flag[j] = 0 if (goal_flag == np.array(self.target_pattern)).all(): # print(goal_flag, self.target_pattern) self.problem_solved = True return ranking.get_subfront(0)
def replacement(self, population: List[S], offspring_population: List[S]) -> List[S]: # print("replacement") """ Implements NSGA-III environmental selection based on reference points as described in: * Deb, K., & Jain, H. (2014). An Evolutionary Many-Objective Optimization Algorithm Using Reference-Point-Based Nondominated Sorting Approach, Part I: Solving Problems With Box Constraints. IEEE Transactions on Evolutionary Computation, 18(4), 577–601. doi:10.1109/TEVC.2013.2281535. """ F = np.array([s.objectives for s in population]) # find or usually update the new ideal point - from feasible solutions # note that we are assuming minimization here! self.ideal_point = np.min(np.vstack((self.ideal_point, F)), axis=0) self.worst_point = np.max(np.vstack((self.worst_point, F)), axis=0) # calculate the fronts of the population ranking = FastNonDominatedRanking(self.dominance_comparator) ranking.compute_ranking(population + offspring_population, k=self.population_size) fronts, non_dominated = ranking.ranked_sublists, ranking.get_subfront(0) # find the extreme points for normalization self.extreme_points = get_extreme_points(F=np.array([s.objectives for s in non_dominated]), n_objs=self.problem.number_of_objectives, ideal_point=self.ideal_point, extreme_points=self.extreme_points) # find the intercepts for normalization and do backup if gaussian elimination fails worst_of_population = np.max(F, axis=0) worst_of_front = np.max(np.array([s.objectives for s in non_dominated]), axis=0) nadir_point = get_nadir_point(extreme_points=self.extreme_points, ideal_point=self.ideal_point, worst_point=self.worst_point, worst_of_population=worst_of_population, worst_of_front=worst_of_front) # consider only the population until we come to the splitting front pop = np.concatenate(ranking.ranked_sublists) F = np.array([s.objectives for s in pop]) # update the front indices for the current population counter = 0 for i in range(len(fronts)): for j in range(len(fronts[i])): fronts[i][j] = counter counter += 1 last_front = np.array(fronts[-1]) # associate individuals to niches niche_of_individuals, dist_to_niche = associate_to_niches(F=F, niches=self.reference_directions, ideal_point=self.ideal_point, nadir_point=nadir_point) # if we need to select individuals to survive if len(pop) > self.population_size: # if there is only one front if len(fronts) == 1: until_last_front = np.array([], dtype=np.int) niche_count = np.zeros(len(self.reference_directions), dtype=np.int) n_remaining = self.population_size # if some individuals already survived else: until_last_front = np.concatenate(fronts[:-1]) niche_count = compute_niche_count(len(self.reference_directions), niche_of_individuals[until_last_front]) n_remaining = self.population_size - len(until_last_front) S_idx = niching(pop=pop[last_front], n_remaining=n_remaining, niche_count=niche_count, niche_of_individuals=niche_of_individuals[last_front], dist_to_niche=dist_to_niche[last_front]) survivors_idx = np.concatenate((until_last_front, last_front[S_idx].tolist())) pop = pop[survivors_idx] return list(pop)
def get_result(self): """ Return only non dominated solutions.""" ranking = FastNonDominatedRanking(self.dominance_comparator) ranking.compute_ranking(self.solutions, k=self.population_size) return ranking.get_subfront(0)
class FastNonDominatedRankingTestCases(unittest.TestCase): def setUp(self): self.ranking = FastNonDominatedRanking() def test_should_constructor_create_a_valid_object(self): self.assertIsNotNone(self.ranking) def test_should_compute_ranking_of_an_emtpy_solution_list_return_a_empty_list_of_subranks(self): solution_list = [] self.assertEqual(0, len(self.ranking.compute_ranking(solution_list))) def test_should_compute_ranking_return_a_subfront_if_the_solution_list_contains_one_solution(self): solution = Solution(2, 3) solution_list = [solution] ranking = self.ranking.compute_ranking(solution_list) self.assertEqual(1, self.ranking.get_number_of_subfronts()) self.assertEqual(solution, ranking[0][0]) def test_should_compute_ranking_return_a_subfront_if_the_solution_list_contains_two_nondominated_solutions(self): solution = Solution(2, 2) solution.objectives = [1, 2] solution2 = Solution(2, 2) solution2.objectives = [2, 1] solution_list = [solution, solution2] ranking = self.ranking.compute_ranking(solution_list) self.assertEqual(1, self.ranking.get_number_of_subfronts()) self.assertEqual(2, len(self.ranking.get_subfront(0))) self.assertEqual(solution, ranking[0][0]) self.assertEqual(solution2, ranking[0][1]) def test_should_compute_ranking_work_properly_case1(self): """ The list contains two solutions and one of them is dominated by the other one. """ solution = Solution(2, 2) solution.objectives = [2, 3] solution2 = Solution(2, 2) solution2.objectives = [3, 6] solution_list = [solution, solution2] ranking = self.ranking.compute_ranking(solution_list) self.assertEqual(2, self.ranking.get_number_of_subfronts()) self.assertEqual(1, len(self.ranking.get_subfront(0))) self.assertEqual(1, len(self.ranking.get_subfront(1))) self.assertEqual(solution, ranking[0][0]) self.assertEqual(solution2, ranking[1][0]) def test_should_ranking_of_a_population_with_three_dominated_solutions_return_three_subfronts(self): solution = Solution(2, 2) solution.objectives = [2, 3] solution2 = Solution(2, 2) solution2.objectives = [3, 6] solution3 = Solution(2, 2) solution3.objectives = [4, 8] solution_list = [solution, solution2, solution3] ranking = self.ranking.compute_ranking(solution_list) self.assertEqual(3, self.ranking.get_number_of_subfronts()) self.assertEqual(1, len(self.ranking.get_subfront(0))) self.assertEqual(1, len(self.ranking.get_subfront(1))) self.assertEqual(1, len(self.ranking.get_subfront(2))) self.assertEqual(solution, ranking[0][0]) self.assertEqual(solution2, ranking[1][0]) self.assertEqual(solution3, ranking[2][0]) def test_should_ranking_of_a_population_with_five_solutions_work_properly(self): solution1 = Solution(2, 2) solution2 = Solution(2, 2) solution3 = Solution(2, 2) solution4 = Solution(2, 2) solution5 = Solution(2, 2) solution1.objectives[0] = 1.0 solution1.objectives[1] = 0.0 solution2.objectives[0] = 0.5 solution2.objectives[1] = 0.5 solution3.objectives[0] = 0.0 solution3.objectives[1] = 1.0 solution4.objectives[0] = 0.6 solution4.objectives[1] = 0.6 solution5.objectives[0] = 0.7 solution5.objectives[1] = 0.5 solutions = [solution1, solution2, solution3, solution4, solution5] ranking = self.ranking.compute_ranking(solutions) self.assertEqual(2, self.ranking.get_number_of_subfronts()) self.assertEqual(3, len(self.ranking.get_subfront(0))) self.assertEqual(2, len(self.ranking.get_subfront(1))) self.assertEqual(solution1, ranking[0][0]) self.assertEqual(solution2, ranking[0][1]) self.assertEqual(solution3, ranking[0][2]) self.assertEqual(solution4, ranking[1][0]) self.assertEqual(solution5, ranking[1][1])
def dtlz_test(p: FloatProblemGD, label: str = '', experiment: int = 50): # problem.reference_front = read_solutions(filename='resources/reference_front/DTLZ2.3D.pf') max_evaluations = 25000 # references = ReferenceDirectionFromSolution(p) algorithm = NSGA3C( problem=p, population_size=100, reference_directions=UniformReferenceDirectionFactory(p.instance_.n_obj, n_points=92), mutation=PolynomialMutation(probability=1.0 / p.number_of_variables, distribution_index=20), crossover=SBXCrossover(probability=1.0, distribution_index=30), termination_criterion=StoppingByEvaluations(max_evaluations=max_evaluations) ) bag = [] total_time = 0 for i in range(experiment): algorithm = NSGA3C( problem=p, population_size=92, reference_directions=UniformReferenceDirectionFactory(p.instance_.n_obj, n_points=91), mutation=PolynomialMutation(probability=1.0 / p.number_of_variables, distribution_index=20), crossover=SBXCrossover(probability=1.0, distribution_index=30), termination_criterion=StoppingByEvaluations(max_evaluations=max_evaluations) ) progress_bar = ProgressBarObserver(max=max_evaluations) algorithm.observable.register(progress_bar) algorithm.run() total_time += algorithm.total_computing_time bag = bag + algorithm.get_result() print(len(bag)) print('Total computing time:', total_time) print('Average time: ', str(total_time / experiment)) print_solutions_to_file(bag, DIRECTORY_RESULTS + 'Solutions.bag._class_' + label + algorithm.label) ranking = FastNonDominatedRanking() ranking.compute_ranking(bag) front_ = ranking.get_subfront(0) print('Front 0 size : ', len(front_)) alabels = [] for obj in range(p.number_of_objectives): alabels.append('Obj-' + str(obj)) plot_front = Plot(title='Pareto front approximation' + ' ' + label, axis_labels=alabels) plot_front.plot(front_, label=label + 'F0 ' + algorithm.label, filename=DIRECTORY_RESULTS + 'F0_class_' + 'original_' + label + algorithm.label, format='png') class_fronts = [[], [], [], []] for s in front_: _class = problem.classifier.classify(s) if _class[0] > 0: class_fronts[0].append(s) elif _class[1] > 0: class_fronts[1].append(s) elif _class[2] > 0: class_fronts[2].append(s) else: class_fronts[3].append(s) print(len(class_fronts[0]), len(class_fronts[1]), len(class_fronts[2]), len(class_fronts[3])) _front = class_fronts[0] + class_fronts[1] if len(_front) == 0: _front = class_fronts[2] + class_fronts[3] print('Class : ', len(_front)) # Save results to file print_solutions_to_file(_front, DIRECTORY_RESULTS + 'Class_F0' + label + algorithm.label) print(f'Algorithm: ${algorithm.get_name()}') print(f'Problem: ${p.get_name()}') plot_front = Plot(title=label + 'F_' + p.get_name(), axis_labels=alabels) plot_front.plot(_front, label=label + 'F_' + p.get_name(), filename=DIRECTORY_RESULTS + 'Class_F0' + label + p.get_name(), format='png')
def validate_interclass(problem): with open('/home/thinkpad/Documents/jemoa/experiments/dtlz_preferences/Class_F0DTLZ1_P3.out') as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] solutions = [] print(problem.get_preference_model(0)) for idx, line in enumerate(content): split = line.split('*')[1].split(',') _s = problem.generate_existing_solution([float(x) for x in split],is_objectives=True) _s.bag = 'java' solutions.append(_s) with open('/home/thinkpad/PycharmProjects/jMetalPy/results/Class_F0enfoque_frontsNSGAIII_custom.DTLZ1P_3.csv') as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] for idx, line in enumerate(content): if not 'variables' in line: split = line.split('*')[1].split(',') _s = problem.generate_existing_solution([float(x) for x in split], is_objectives=True) _s.bag = 'python' solutions.append(_s) ranking = FastNonDominatedRanking() ranking.compute_ranking(solutions) front_ = ranking.get_subfront(0) print('Solutions:',len(solutions)) print('Front 0:', len(front_)) class_fronts = [[], [], [], []] fjava = 0 fpython = 0 classifier = InterClassNC(problem) for s in front_: if s.bag == 'java': fjava += 1 else: fpython += 1 # problem.evaluate(s) _class = classifier.classify(s) if _class[0] > 0: class_fronts[0].append(s) elif _class[1] > 0: class_fronts[1].append(s) elif _class[2] > 0: class_fronts[2].append(s) else: class_fronts[3].append(s) print('Java solutions:', (fjava / len(front_))) print('Python solutions:', (fpython / len(front_))) print('HSat : ', len(class_fronts[0]), ', Sat : ', len(class_fronts[1]), ', Dis : ', len(class_fronts[2]), ', HDis : ', len(class_fronts[3])) _sat = class_fronts[0] + class_fronts[1] fjava = 0 fpython = 0 for s in _sat: if s.bag == 'java': fjava += 1 else: fpython += 1 print('Sat Java solutions:', (fjava / len(_sat))) print('Sat Python solutions:', (fpython / len(_sat))) plot_front = Plot(title='Sat and HSat Front') plot_front.plot(_sat, label= 'Sat and HSat Front', filename=DIRECTORY_RESULTS + 'SatFront' + problem.get_name(), format='png')
if __name__ == '__main__': # random.seed(145600) random.seed(1) instance = DTLZInstance() path = '/home/thinkpad/Documents/jemoa/src/main/resources/DTLZ_INSTANCES/DTLZ7_Instance.txt' # path = '/home/thinkpad/PycharmProjects/jMetalPy/resources/DTLZ_INSTANCES/DTLZ1P_10.txt' instance.read_(path) isObjective = False problem = DTLZ7P(instance) _best = problem.generate_existing_solution(problem.instance_.attributes['best_compromise'][0], isObjective) for s in problem.instance_.initial_solutions: problem.evaluate(s) print(s) fndr = FastNonDominatedRanking() fndr.compute_ranking(problem.instance_.initial_solutions) print(fndr.get_number_of_subfronts()) classifier = InterClassNC(problem) print('Best compromise:', _best.objectives,classifier.classify(_best)) # validate_interclass(problem) # loadDataWithClass(problem, # '/home/thinkpad/PycharmProjects/jMetalPy/results/Solutions.bag._class_enfoque_fronts_NSGAIII_custom.DTLZ1P_10.csv', # 'enfoque_fronts_') # load_objectives_from_gdm_file(problem, '/home/thinkpad/PycharmProjects/jMetalPy/resources/DTLZ_INSTANCES/objetivos_nelson.csv') # dm_generator(obj, 14, obj * [Interval(0, (9 / 8) * k * 100)]) # dtlz_test(problem, 'enfoque_fronts') print(problem) reference_set(problem,is_objective=isObjective) # looking_for_compromise(problem) # test_classifier()
problem.number_of_variables, distribution_index=20), crossover=SBXCrossover(probability=1.0, distribution_index=30), termination_criterion=StoppingByEvaluations( max_evaluations=max_evaluations)) progress_bar = ProgressBarObserver(max=max_evaluations) algorithm.observable.register(progress_bar) algorithm.run() bag += algorithm.get_result() print(len(bag)) print_solutions_to_file( bag, DIRECTORY_RESULTS + 'Solutions.bag.' + algorithm.label) ranking = FastNonDominatedRanking() ranking.compute_ranking(bag) front = ranking.get_subfront(0) print(len(front)) # Save results to file print_solutions_to_file(front, DIRECTORY_RESULTS + 'front0.' + algorithm.label) plot_front = Plot(title='Pareto front approximation', axis_labels=['x', 'y', 'z']) plot_front.plot(front, label='NSGAII-ZDT7', filename=DIRECTORY_RESULTS + 'NSGAII-ZDT1P', format='png') print(f'Algorithm: ${algorithm.get_name()}') print(f'Problem: ${problem.get_name()}') print(f'Computing time: ${algorithm.total_computing_time}')
def get_result(self): ranking = FastNonDominatedRanking(self.dominance_comparator) ranking.compute_ranking(self.solutions) return ranking.get_subfront(0)
problem = mt.MultiTransferProblem(normalize=False, gfk_dim=20) no_evaluations = Paras.N_GEN*Paras.N_IND algorithm = NSGAII_MEDA( problem=problem, population_size=Paras.N_IND, offspring_population_size=Paras.N_IND, mutation=IntegerPolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.8, distribution_index=20), termination_criterion=StoppingByEvaluations(max_evaluations=no_evaluations) ) progress_bar = ProgressBarObserver(max=no_evaluations) algorithm.observable.register(progress_bar) algorithm.run() solutions = algorithm.get_result() non_dominated_ranking = FastNonDominatedRanking(algorithm.dominance_comparator) non_dominated_ranking.compute_ranking(solutions) non_dominated_sols = non_dominated_ranking.get_nondominated() plot_front = Plot(title='Pareto front approximation', axis_labels=['Dis', 'Mani']) plot_front.plot(non_dominated_sols, label='NSGAII-ZDT1') list_labels = [] for sol in non_dominated_sols: list_labels.append(sol.variables) dis = sol.objectives[0] man = sol.objectives[1] # train_acc = sol.objectives[2] Yt_sol = sol.variables acc = (np.sum(Yt_sol == problem.Yt)+0.0)/len(Yt_sol) print("Dis: %.5f, man: %.5f, acc: %.5f" %(dis, man, acc)) # print("Dis: %.5f, man: %.5f, train acc: %.5f, acc: %.5f" % (dis, man, train_acc ,acc))