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))
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())
def __init__(self, problem: Problem, reference_point: Solution, 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): """ This is an implementation of the Hypervolume Estimation Algorithm for Multi-objective Optimization proposed in: * J. Bader and E. Zitzler. HypE: An Algorithm for Fast Hypervolume-Based Many-Objective Optimization. TIK Report 286, Computer Engineering and Networks Laboratory (TIK), ETH Zurich, November 2008. It uses the Exact Hypervolume-based indicator formulation, which once computed, guides both the environmental selection and the binary tournament selection operator Please note that as per the publication above, the evaluator and replacement should not be changed anyhow. It also requires that Problem() has a reference_point with objective values defined, e.g. problem = ZDT1() reference_point = FloatSolution(problem.number_of_variables,problem.number_of_objectives, [0], [1]) reference_point.objectives = [1., 1.] """ selection = BinaryTournamentSelection( comparator=SolutionAttributeComparator(key='fitness', lowest_is_best=False)) self.ranking_fitness = RankingAndFitnessSelection( population_size, dominance_comparator=dominance_comparator, reference_point=reference_point) self.reference_point = reference_point self.dominance_comparator = dominance_comparator super(HYPE, 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)
def test_should_execute_work_properly_case1(self): solution1 = Solution(3, 2) solution1.objectives = [2, 3] solution2 = Solution(3, 2) solution2.objectives = [1, 4] solution1.attributes["dominance_ranking"] = 1 solution2.attributes["dominance_ranking"] = 1 solution_list = [solution1, solution2] operator = BinaryTournament2Selection[Solution]( [SolutionAttributeComparator("key")]) selection1 = operator.execute(solution_list) self.assertTrue(1, selection1.attributes["dominance_ranking"])
def __init__(self, problem: Problem, population_size: int, offspring_population_size: int, mutation: Mutation, crossover: Crossover, kappa: float, termination_criterion: TerminationCriterion = store.default_termination_criteria, population_generator: Generator = store.default_generator, population_evaluator: Evaluator = store.default_evaluator): """ Epsilon IBEA implementation as described in * Zitzler, Eckart, and Simon Künzli. "Indicator-based selection in multiobjective search." In International Conference on Parallel Problem Solving from Nature, pp. 832-842. Springer, Berlin, Heidelberg, 2004. https://link.springer.com/chapter/10.1007/978-3-540-30217-9_84 IBEA is a genetic algorithm (GA), i.e. it belongs to the evolutionary algorithms (EAs) family. The multi-objective search in IBEA is guided by a fitness associated to every solution, which is in turn controlled by a binary quality indicator. This implementation uses the so-called additive epsilon indicator, along with a binary tournament mating selector. :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`). :param kappa: Weight in the fitness computation. """ selection = BinaryTournamentSelection( comparator=SolutionAttributeComparator(key='fitness', lowest_is_best=False)) self.kappa = kappa super(IBEA, 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 )
def __init__(self, problem: Problem, population_size: int, offspring_population_size: int, mutation: Mutation, crossover: Crossover, selection: Selection = BinaryTournamentSelection( MultiComparator( [SolutionAttributeComparator(key="fitness")])), 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): """ MOGA implementation as described in * K. Deb, A. Pratap, S. Agarwal and T. Meyarivan, "A fast and elitist multiobjective genetic algorithm: NSGA-II," in IEEE Transactions on Evolutionary Computation, vol. 6, no. 2, pp. 182-197, Apr 2002. doi: 10.1109/4235.996017 NSGA-II is a genetic algorithm (GA), i.e. it belongs to the evolutionary algorithms (EAs) family. The implementation of NSGA-II provided in jMetalPy follows the evolutionary algorithm template described in the algorithm module (:py:mod:`jmetal.core.algorithm`). .. note:: A steady-state version of this algorithm can be run by setting the offspring size to 1. :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`). :param selection: Selection operator (see :py:mod:`jmetal.operator.selection`). """ super(MOGA, 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 __init__(self, maximum_size: int): super(CrowdingDistanceArchive, self).__init__( maximum_size=maximum_size, comparator=SolutionAttributeComparator("crowding_distance", lowest_is_best=False), density_estimator=CrowdingDistance())
def get_comparator(cls) -> Comparator: return SolutionAttributeComparator('strength_ranking')
def get_comparator(cls) -> Comparator: return SolutionAttributeComparator('dominance_ranking')
def get_comparator(cls) -> Comparator: return SolutionAttributeComparator("crowding_distance", lowest_is_best=False)
def get_comparator(cls) -> Comparator: return SolutionAttributeComparator("knn_density", lowest_is_best=False)
def setUp(self): self.comparator = SolutionAttributeComparator("attribute")
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.assertEqual(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.assertEqual(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.assertEqual(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.assertEqual(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.assertEqual(-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.assertEqual(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.assertEqual(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.assertEqual(-1, comparator.compare(solution1, solution2))