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_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 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 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 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.ranking:Ranking = StrengthRanking()