def test_should_compare_works_properly_case4(self): """Case 4: solution1.attribute > solution2.attribute (highest is best)""" solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution1.attributes["attribute"] = 1.0 solution2.attributes["attribute"] = 0.0 comparator = SolutionAttributeComparator("attribute", False) self.assertEqual(-1, comparator.compare(solution1, solution2))
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
class SolutionAttributeComparatorTestCases(unittest.TestCase): def setUp(self): self.comparator = SolutionAttributeComparator("attribute") def test_should_compare_return_zero_if_the_first_solution_has_no_the_attribute( self): solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution2.attributes["attribute"] = 1.0 self.assertEquals(0, self.comparator.compare(solution1, solution2)) def test_should_compare_return_zero_if_the_second_solution_has_no_the_attribute( self): solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution1.attributes["attribute"] = 1.0 self.assertEquals(0, self.comparator.compare(solution1, solution2)) def test_should_compare_return_zero_if_none_of_the_solutions_have_the_attribute( self): solution1 = Solution(1, 1) solution2 = Solution(1, 1) self.assertEquals(0, self.comparator.compare(solution1, solution2)) def test_should_compare_return_zero_if_both_solutions_have_the_same_attribute_value( self): solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution1.attributes["attribute"] = 1.0 solution2.attributes["attribute"] = 1.0 self.assertEquals(0, self.comparator.compare(solution1, solution2)) def test_should_compare_works_properly_case1(self): """ Case 1: solution1.attribute < solution2.attribute (lowest is best) """ solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution1.attributes["attribute"] = 0.0 solution2.attributes["attribute"] = 1.0 self.assertEquals(-1, self.comparator.compare(solution1, solution2)) def test_should_compare_works_properly_case2(self): """ Case 2: solution1.attribute > solution2.attribute (lowest is best) """ solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution1.attributes["attribute"] = 1.0 solution2.attributes["attribute"] = 0.0 self.assertEquals(1, self.comparator.compare(solution1, solution2)) def test_should_compare_works_properly_case3(self): """ Case 3: solution1.attribute < solution2.attribute (highest is best) """ comparator = SolutionAttributeComparator("attribute", False) solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution1.attributes["attribute"] = 0.0 solution2.attributes["attribute"] = 1.0 self.assertEquals(1, comparator.compare(solution1, solution2)) def test_should_compare_works_properly_case4(self): """ Case 4: solution1.attribute > solution2.attribute (highest is best) """ solution1 = Solution(1, 1) solution2 = Solution(1, 1) solution1.attributes["attribute"] = 1.0 solution2.attributes["attribute"] = 0.0 comparator = SolutionAttributeComparator("attribute", False) self.assertEquals(-1, comparator.compare(solution1, solution2))