def execute(self, solution_list: List[S]) -> List[S]: ranking = DominanceRanking() 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 __init__(self, maximum_size: int): super(CrowdingDistanceArchive, self).__init__(maximum_size) self.__non_dominated_solution_archive = NonDominatedSolutionListArchive[ S]() self.__comparator = SolutionAttributeComparator("crowding_distance", lowest_is_best=False) self.__crowding_distance = CrowdingDistance() self.solution_list = self.__non_dominated_solution_archive.get_solution_list( )
def __init__(self, maximum_size: int, reference_point: List[float]): super(CrowdingDistanceArchiveWithReferencePoint, self).__init__( maximum_size=maximum_size, reference_point=reference_point, comparator=SolutionAttributeComparator("crowding_distance", lowest_is_best=False), density_estimator=CrowdingDistance())
class CrowdingDistanceArchive(BoundedArchive[S]): def __init__(self, maximum_size: int): super(CrowdingDistanceArchive, self).__init__(maximum_size) self.__non_dominated_solution_archive = NonDominatedSolutionListArchive[ S]() self.__comparator = SolutionAttributeComparator("crowding_distance", lowest_is_best=False) self.__crowding_distance = CrowdingDistance() self.solution_list = self.__non_dominated_solution_archive.get_solution_list( ) def add(self, solution: S) -> bool: success: bool = self.__non_dominated_solution_archive.add(solution) if success: if self.size() > self.get_max_size(): self.compute_density_estimator() worst_solution = self.__find_worst_solution( self.get_solution_list()) self.get_solution_list().remove(worst_solution) return success def compute_density_estimator(self): self.__crowding_distance.compute_density_estimator( self.get_solution_list()) def __find_worst_solution(self, solution_list: List[S]) -> S: if solution_list is None: raise Exception("The solution list is None") elif len(solution_list) is 0: raise Exception("The solution list is empty") worst_solution = solution_list[0] for solution in solution_list[1:]: if self.__comparator.compare(worst_solution, solution) < 0: worst_solution = solution return worst_solution def get_comparator(self): return self.__comparator
def execute(self, front: List[S]) -> List[S]: ranking = FastNonDominatedRanking() 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 setUp(self): self.crowding = CrowdingDistance()
class CrowdingDistanceTestCases(unittest.TestCase): def setUp(self): self.crowding = CrowdingDistance() def test_should_the_crowding_distance_of_an_empty_set_do_nothing(self): solution_list = [] self.crowding.compute_density_estimator(solution_list) def test_should_the_crowding_distance_of_single_solution_be_infinity(self): solution = Solution(3, 3) solution_list = [solution] self.crowding.compute_density_estimator(solution_list) value = solution_list[0].attributes["crowding_distance"] self.assertEqual(float("inf"), value) def test_should_the_crowding_distance_of_two_solutions_be_infinity(self): solution1 = Solution(3, 3) solution2 = Solution(3, 3) solution_list = [solution1, solution2] self.crowding.compute_density_estimator(solution_list) value_from_solution1 = solution_list[0].attributes["crowding_distance"] value_from_solution2 = solution_list[1].attributes["crowding_distance"] self.assertEqual(float("inf"), value_from_solution1) self.assertEqual(float("inf"), value_from_solution2) def test_should_the_crowding_distance_of_three_solutions_correctly_assigned( self): solution1 = Solution(2, 2) solution2 = Solution(2, 2) solution3 = Solution(2, 2) solution1.objectives[0] = 0.0 solution1.objectives[1] = 1.0 solution2.objectives[0] = 1.0 solution2.objectives[1] = 0.0 solution3.objectives[0] = 0.5 solution3.objectives[1] = 0.5 solution_list = [solution1, solution2, solution3] self.crowding.compute_density_estimator(solution_list) value_from_solution1 = solution_list[0].attributes["crowding_distance"] value_from_solution2 = solution_list[1].attributes["crowding_distance"] value_from_solution3 = solution_list[2].attributes["crowding_distance"] self.assertEqual(float("inf"), value_from_solution1) self.assertEqual(float("inf"), value_from_solution2) self.assertEqual(2.0, value_from_solution3) def test_should_the_crowding_distance_of_four_solutions_correctly_assigned( self): solution1 = Solution(2, 2) solution2 = Solution(2, 2) solution3 = Solution(2, 2) solution4 = Solution(2, 2) solution1.objectives[0] = 0.0 solution1.objectives[1] = 1.0 solution2.objectives[0] = 1.0 solution2.objectives[1] = 0.0 solution3.objectives[0] = 0.5 solution3.objectives[1] = 0.5 solution4.objectives[0] = 0.75 solution4.objectives[1] = 0.75 solution_list = [solution1, solution2, solution3, solution4] self.crowding.compute_density_estimator(solution_list) value_from_solution1 = solution_list[0].attributes["crowding_distance"] value_from_solution2 = solution_list[1].attributes["crowding_distance"] value_from_solution3 = solution_list[2].attributes["crowding_distance"] value_from_solution4 = solution_list[3].attributes["crowding_distance"] self.assertEqual(float("inf"), value_from_solution1) self.assertEqual(float("inf"), value_from_solution2) self.assertGreater(value_from_solution3, value_from_solution4)
class CrowdingDistanceTestCases(unittest.TestCase): def setUp(self): self.crowding = CrowdingDistance() def test_should_the_crowding_distance_of_an_empty_set_do_nothing(self): solution_list = [] self.crowding.compute_density_estimator(solution_list) def test_should_the_crowding_distance_of_single_solution_be_infinity(self): solution = Solution(3, 3) solution_list = [solution] self.crowding.compute_density_estimator(solution_list) value = solution_list[0].attributes["crowding_distance"] self.assertEqual(float("inf"), value) def test_should_the_crowding_distance_of_two_solutions_be_infinity(self): solution1 = Solution(3, 3) solution2 = Solution(3, 3) solution_list = [solution1, solution2] self.crowding.compute_density_estimator(solution_list) value_from_solution1 = solution_list[0].attributes["crowding_distance"] value_from_solution2 = solution_list[1].attributes["crowding_distance"] self.assertEqual(float("inf"), value_from_solution1) self.assertEqual(float("inf"), value_from_solution2) def test_should_the_crowding_distance_of_three_solutions_correctly_assigned(self): solution1 = Solution(2, 2) solution2 = Solution(2, 2) solution3 = Solution(2, 2) solution1.objectives[0] = 0.0 solution1.objectives[1] = 1.0 solution2.objectives[0] = 1.0 solution2.objectives[1] = 0.0 solution3.objectives[0] = 0.5 solution3.objectives[1] = 0.5 solution_list = [solution1, solution2, solution3] self.crowding.compute_density_estimator(solution_list) value_from_solution1 = solution_list[0].attributes["crowding_distance"] value_from_solution2 = solution_list[1].attributes["crowding_distance"] value_from_solution3 = solution_list[2].attributes["crowding_distance"] self.assertEqual(float("inf"), value_from_solution1) self.assertEqual(float("inf"), value_from_solution2) self.assertEqual(2.0, value_from_solution3) def test_should_the_crowding_distance_of_four_solutions_correctly_assigned(self): solution1 = Solution(2, 2) solution2 = Solution(2, 2) solution3 = Solution(2, 2) solution4 = Solution(2, 2) solution1.objectives[0] = 0.0 solution1.objectives[1] = 1.0 solution2.objectives[0] = 1.0 solution2.objectives[1] = 0.0 solution3.objectives[0] = 0.5 solution3.objectives[1] = 0.5 solution4.objectives[0] = 0.75 solution4.objectives[1] = 0.75 solution_list = [solution1, solution2, solution3, solution4] self.crowding.compute_density_estimator(solution_list) value_from_solution1 = solution_list[0].attributes["crowding_distance"] value_from_solution2 = solution_list[1].attributes["crowding_distance"] value_from_solution3 = solution_list[2].attributes["crowding_distance"] value_from_solution4 = solution_list[3].attributes["crowding_distance"] self.assertEqual(float("inf"), value_from_solution1) self.assertEqual(float("inf"), value_from_solution2) self.assertGreater(value_from_solution3, value_from_solution4)
def __init__(self, maximum_size: int): super(CrowdingDistanceArchive, self).__init__( maximum_size=maximum_size, comparator=SolutionAttributeComparator("crowding_distance", lowest_is_best=False), density_estimator=CrowdingDistance())