Exemple #1
0
def main() -> None:
    problem = ZDT1()
    algorithm = NSGAII[FloatSolution, List[FloatSolution]](
        problem=problem,
        population_size=100,
        max_evaluations=25000,
        mutation=Polynomial(1.0 / problem.number_of_variables,
                            distribution_index=20),
        crossover=SBX(1.0, distribution_index=20),
        #selection=BinaryTournamentSelection(RankingAndCrowdingDistanceComparator()))
        selection=BinaryTournament2Selection([
            SolutionAttributeComparator("dominance_ranking"),
            SolutionAttributeComparator("crowding_distance",
                                        lowest_is_best=False)
        ]))

    algorithm.run()
    result = algorithm.get_result()

    SolutionListOutput[FloatSolution].print_function_values_to_file(
        "FUN." + problem.get_name(), result)

    logger.info("Algorithm (continuous problem): " + algorithm.get_name())
    logger.info("Problem: " + problem.get_name())
    logger.info("Computing time: " + str(algorithm.total_computing_time))
Exemple #2
0
    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))
Exemple #3
0
    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(
        )
Exemple #4
0
 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 main() -> None:
    problem = ZDT1()
    algorithm = NSGAII[FloatSolution, List[FloatSolution]](
        problem,
        population_size=100,
        max_evaluations=25000,
        mutation=Polynomial(1.0/problem.number_of_variables, distribution_index=20),
        crossover=SBX(1.0, distribution_index=20),
        #selection=BinaryTournament(RankingAndCrowdingDistanceComparator()))
        selection = BinaryTournament2([SolutionAttributeComparator("dominance_ranking"),
                                       SolutionAttributeComparator("crowding_distance", lowest_is_best=False)]))

    observer = AlgorithmObserver(animation_speed=1*10e-8)
    algorithm.observable.register(observer=observer)

    algorithm.run()

    logger.info("Algorithm (continuous problem): " + algorithm.get_name())
    logger.info("Problem: " + problem.get_name())
Exemple #6
0
    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"])
Exemple #8
0
    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,
        )
Exemple #9
0
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
Exemple #10
0
    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):

        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 setUp(self):
     self.comparator = SolutionAttributeComparator("attribute")
Exemple #12
0
 def get_comparator(cls) -> Comparator:
     return SolutionAttributeComparator("knn_density", lowest_is_best=False)
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))
Exemple #14
0
 def get_comparator(cls) -> Comparator:
     return SolutionAttributeComparator('dominance_ranking')
Exemple #15
0
 def get_comparator(cls) -> Comparator:
     return SolutionAttributeComparator('strength_ranking')
 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())
Exemple #17
0
 def get_comparator(cls) -> Comparator:
     return SolutionAttributeComparator("crowding_distance",
                                        lowest_is_best=False)