def __init__( self, problem: Problem, population_size: int, offspring_population_size: int, mutation: Mutation, crossover: Crossover, termination_criterion: TerminationCriterion = store.default_termination_criteria, population_generator: Generator = store.default_generator, population_evaluator: Evaluator = store.default_evaluator, dominance_comparator: Comparator = store.default_comparator, ): """ :param problem: The problem to solve. :param population_size: Size of the population. :param mutation: Mutation operator (see :py:mod:`jmetal.operator.mutation`). :param crossover: Crossover operator (see :py:mod:`jmetal.operator.crossover`). """ multi_comparator = MultiComparator( [StrengthRanking.get_comparator(), KNearestNeighborDensityEstimator.get_comparator()] ) selection = BinaryTournamentSelection(comparator=multi_comparator) super(SPEA2, self).__init__( problem=problem, population_size=population_size, offspring_population_size=offspring_population_size, mutation=mutation, crossover=crossover, selection=selection, termination_criterion=termination_criterion, population_evaluator=population_evaluator, population_generator=population_generator, ) self.dominance_comparator = dominance_comparator
def test_should_replacement_return_the_right_value_case1(self): """ 5 1 4 2 3 3 2 1 4 0 1 2 3 4 5 List: 1,2,3 OffspringList: 4 Expected result: 4, 1, 3 """ ranking = StrengthRanking() density_estimator = KNearestNeighborDensityEstimator(1) replacement = RankingAndDensityEstimatorReplacement(ranking, density_estimator) solution1 = Solution(2, 2) solution1.objectives = [1, 5] solution2 = Solution(2, 2) solution2.objectives = [2, 4] solution3 = Solution(2, 2) solution3.objectives = [3, 3] solution4 = Solution(2, 2) solution4.objectives = [5, 1] solution_list = [solution1, solution2, solution3] offspring_list = [solution4] result_list = replacement.replace(solution_list, offspring_list) self.assertEqual(3, len(result_list)) self.assertTrue(solution1 in result_list) self.assertTrue(solution3 in result_list) self.assertTrue(solution4 in result_list)
def test_should_replacement_return_the_list_if_the_offspring_list_is_empty(self): """ 5 1 4 2 3 3 2 1 4 0 1 2 3 4 5 """ ranking = StrengthRanking() density_estimator = KNearestNeighborDensityEstimator(1) replacement = RankingAndDensityEstimatorReplacement(ranking, density_estimator) solution1 = Solution(2, 2) solution1.objectives = [1, 5] solution2 = Solution(2, 2) solution2.objectives = [2, 4] solution3 = Solution(2, 2) solution3.objectives = [3, 3] solution4 = Solution(2, 2) solution4.objectives = [5, 1] solution_list = [solution1, solution2, solution3, solution4] result_list = replacement.replace(solution_list, []) self.assertEqual(4, len(result_list)) self.assertEqual(0, solution1.attributes["strength_ranking"]) self.assertEqual(0, solution2.attributes["strength_ranking"]) self.assertEqual(0, solution3.attributes["strength_ranking"]) self.assertEqual(0, solution4.attributes["strength_ranking"])
def replacement(self, population: List[S], offspring_population: List[S]) -> List[List[S]]: ranking = StrengthRanking(self.dominance_comparator) density_estimator = KNearestNeighborDensityEstimator() r = RankingAndDensityEstimatorReplacement(ranking, density_estimator, RemovalPolicyType.SEQUENTIAL) solutions = r.replace(population, offspring_population) front = get_non_dominated_solutions(solutions) objective1 = -1 / self.Uobjecive1 * np.array( [solution.objectives[0] for solution in front]) objective2 = -1 / self.Uobjecive2 * np.array( [solution.objectives[1] for solution in front]) HV = calculateHypervolume(list(zip(objective1, objective2))) print('Hypervolume;', HV) self.hypervolumeByGeneration.append(HV) # DO IGD calculation here IGD = InvertedGenerationalDistance(self.reference_front) igd = IGD.compute( list( zip(-np.array([solution.objectives[0] for solution in front]), -np.array([solution.objectives[1] for solution in front])))) print('IGD1:', igd) # obj1front=1/self.Uobjecive1*self.reference_front[:, 0] # obj2front=1/self.Uobjecive2*self.reference_front[:,1] # igd2 = calculateIGD(list(zip(objective1,objective2)), list(zip(obj1front,obj2front))) # print('IGD2:',igd2) self.IGDbyGeneration.append(igd) return solutions
def test_should_replacement_return_the_right_value_case3(self): """""" points_population = [ [0.13436424411240122, 4.323216008886963], [0.23308445025757263, 4.574937990387161], [0.17300740157905092, 4.82329350808847], [0.9571162814602269, 3.443495331489301], [0.25529404008730594, 3.36387501100745], [0.020818108509287336, 5.1051826661880515], [0.8787178982088466, 3.2716009445324103], [0.6744550697237632, 3.901350307095427], [0.7881164487252263, 3.1796004913916516], [0.1028341459863098, 4.9409270526888935], ] points_offspring_population = [ [0.3150521745650882, 4.369120371847888], [0.8967291504209932, 2.506948771242972], [0.6744550697237632, 3.9361442668874504], [0.9571162814602269, 3.4388386707431433], [0.13436424411240122, 4.741872175943253], [0.25529404008730594, 2.922302861104415], [0.23308445025757263, 4.580180404770213], [0.23308445025757263, 4.591260299892424], [0.9571162814602269, 2.9865495383518694], [0.25529404008730594, 3.875587748122183], ] ranking = FastNonDominatedRanking() density_estimator = KNearestNeighborDensityEstimator(1) population = [] for i in range(len(points_population)): population.append(Solution(2, 2)) population[i].objectives = points_population[i] offspring_population = [] for i in range(len(points_offspring_population)): offspring_population.append(Solution(2, 2)) offspring_population[i].objectives = points_offspring_population[i] replacement = RankingAndDensityEstimatorReplacement(ranking, density_estimator) result_list = replacement.replace(population, offspring_population) self.assertEqual(10, len(result_list)) for solution in result_list[0:4]: self.assertEqual(0, solution.attributes["dominance_ranking"]) for solution in result_list[5:9]: self.assertEqual(1, solution.attributes["dominance_ranking"])
def replacement(self, population: List[S], offspring_population: List[S]) -> List[List[S]]: """This method joins the current and offspring populations to produce the population of the next generation by applying the ranking and crowding distance selection. :param population: Parent population. :param offspring_population: Offspring population. :return: New population after ranking and crowding distance selection is applied. """ ranking = StrengthRanking(self.dominance_comparator) density_estimator = KNearestNeighborDensityEstimator() r = RankingAndDensityEstimatorReplacement(ranking, density_estimator, RemovalPolicyType.SEQUENTIAL) solutions = r.replace(population, offspring_population) return solutions
def test_should_replacement_return_the_right_value_case2(self): """ 5 1 4 2 3 3 2 5 1 4 0 1 2 3 4 5 List: 1,2,4 OffspringList: 3,5 Expected result: 1, 5, 4 """ ranking = StrengthRanking() density_estimator = KNearestNeighborDensityEstimator(1) replacement = RankingAndDensityEstimatorReplacement(ranking, density_estimator) solution1 = Solution(2, 2) solution1.objectives = [1, 5] solution2 = Solution(2, 2) solution2.objectives = [2, 4] solution3 = Solution(2, 2) solution3.objectives = [3, 3] solution4 = Solution(2, 2) solution4.objectives = [5, 1] solution5 = Solution(2, 2) solution5.objectives = [2.5, 2.5] solution_list = [solution1, solution2, solution4] offspring_list = [solution3, solution5] result_list = replacement.replace(solution_list, offspring_list) self.assertEqual(0, solution1.attributes["strength_ranking"]) self.assertEqual(0, solution2.attributes["strength_ranking"]) self.assertEqual(1, solution3.attributes["strength_ranking"]) self.assertEqual(0, solution4.attributes["strength_ranking"]) self.assertEqual(0, solution5.attributes["strength_ranking"]) self.assertEqual(3, len(result_list)) self.assertTrue(solution1 in result_list) self.assertTrue(solution5 in result_list) self.assertTrue(solution4 in result_list)
def setUp(self): self.knn = KNearestNeighborDensityEstimator()
class KNearestNeighborDensityEstimatorTest(unittest.TestCase): def setUp(self): self.knn = KNearestNeighborDensityEstimator() def test_should_the_density_estimator_compute_the_right_distances_case1( self): """ 5 1 4 2 3 3 2 1 4 0 1 2 3 4 5 """ solution1 = Solution(2, 2) solution1.objectives = [1, 5] solution2 = Solution(2, 2) solution2.objectives = [2, 4] solution3 = Solution(2, 2) solution3.objectives = [3, 3] solution4 = Solution(2, 2) solution4.objectives = [5, 1] solution_list = [solution1, solution2, solution3, solution4] self.knn.compute_density_estimator(solution_list) self.assertEqual(sqrt(2), solution1.attributes["knn_density"]) self.assertEqual(sqrt(2), solution2.attributes["knn_density"]) self.assertEqual(sqrt(2), solution3.attributes["knn_density"]) self.assertEqual(sqrt(2 * 2 + 2 * 2), solution4.attributes["knn_density"]) # self.knn.sort(solution_list) def test_should_the_density_estimator_sort_the_solution_list(self): """ 5 1 4 2 3 3 2 5 1 4 0 1 2 3 4 5 List: 1,2,3,4,5 Expected result: 4, 1, 2, 5, 3 """ solution1 = Solution(2, 2) solution1.objectives = [1, 5] solution2 = Solution(2, 2) solution2.objectives = [2, 4] solution3 = Solution(2, 2) solution3.objectives = [3, 3] solution4 = Solution(2, 2) solution4.objectives = [5, 1] solution5 = Solution(2, 2) solution5.objectives = [3, 2] solution_list = [solution1, solution2, solution3, solution4, solution5] self.knn.compute_density_estimator(solution_list) self.knn.sort(solution_list) self.assertEqual(solution_list[0], solution4) self.assertEqual(solution_list[1], solution1) self.assertEqual(solution_list[2], solution2) self.assertEqual(solution_list[3], solution5) def test_should_the_density_estimator_sort_the_solution_list_considering_the_draws( self): """ 5 1 4 2 3 3 2 1 4 0 1 2 3 4 5 Expected result after sort: 4, 3, 1, 2 """ solution1 = Solution(2, 2) solution1.objectives = [1, 5] solution2 = Solution(2, 2) solution2.objectives = [2, 4] solution3 = Solution(2, 2) solution3.objectives = [3, 3] solution4 = Solution(2, 2) solution4.objectives = [5, 1] solution_list = [solution1, solution2, solution3, solution4] self.knn.compute_density_estimator(solution_list) self.knn.sort(solution_list) self.assertEqual(solution_list[0], solution4) self.assertEqual(solution_list[1], solution3) self.assertEqual(solution_list[2], solution1) self.assertEqual(solution_list[3], solution2) def test_should_the_density_estimator_sort_the_solution_list_considering_the_draws_case2( self): """ 0.13436424411240122 4.323216008886963 0.020818108509287336 5.1051826661880515 0.1028341459863098 4.9409270526888935 0.8967291504209932 2.506948771242972 0.25529404008730594 2.922302861104415 """ points = [ [0.13436424411240122, 4.323216008886963], [0.020818108509287336, 5.1051826661880515], [0.1028341459863098, 4.9409270526888935], [0.8967291504209932, 2.506948771242972], [0.25529404008730594, 2.922302861104415], ] population = [] for i in range(len(points)): population.append(Solution(2, 2)) population[i].objectives = points[i] self.knn.compute_density_estimator(population) self.knn.sort(population) self.assertEqual(5, len(population)) self.assertEqual([0.1028341459863098, 4.9409270526888935], population[4].objectives)