Example #1
0
class RankingAndCrowdingComparatorTestCases(unittest.TestCase):
    def setUp(self):
        self.comparator = RankingAndCrowdingDistanceComparator()

    def test_should_compare_work_properly_case_1(self):
        """Case 1: solution1.ranking < solution2.ranking"""
        solution1 = Solution(1, 1)
        solution2 = Solution(1, 1)
        solution1.attributes["dominance_ranking"] = 1.0
        solution2.attributes["dominance_ranking"] = 2.0

        self.assertEqual(-1, self.comparator.compare(solution1, solution2))

    def test_should_compare_work_properly_case_2(self):
        """Case 2: solution1.ranking > solution2.ranking"""
        solution1 = Solution(1, 1)
        solution2 = Solution(1, 1)
        solution1.attributes["dominance_ranking"] = 2.0
        solution2.attributes["dominance_ranking"] = 1.0

        self.assertEqual(1, self.comparator.compare(solution1, solution2))

    def test_should_compare_work_properly_case_3(self):
        """Case 3: solution1.ranking == solution2.ranking
        solution1.crowding < solution2.crowding
        """
        solution1 = Solution(1, 1)
        solution2 = Solution(1, 1)
        solution1.attributes["dominance_ranking"] = 1.0
        solution2.attributes["dominance_ranking"] = 1.0
        solution1.attributes["crowding_distance"] = 1.0
        solution2.attributes["crowding_distance"] = 2.0

        self.assertEqual(1, self.comparator.compare(solution1, solution2))

    def test_should_compare_work_properly_case_4(self):
        """Case 4: solution1.ranking == solution2.ranking
        solution1.crowding > solution2.crowding
        """
        solution1 = Solution(1, 1)
        solution2 = Solution(1, 1)
        solution1.attributes["dominance_ranking"] = 1.0
        solution2.attributes["dominance_ranking"] = 1.0
        solution1.attributes["crowding_distance"] = 2.0
        solution2.attributes["crowding_distance"] = 1.0

        self.assertEqual(-1, self.comparator.compare(solution1, solution2))

    def test_should_compare_work_properly_case_5(self):
        """Case 5: solution1.ranking == solution2.ranking
        solution1.crowding == solution2.crowding
        """
        solution1 = Solution(1, 1)
        solution2 = Solution(1, 1)
        solution1.attributes["dominance_ranking"] = 1.0
        solution2.attributes["dominance_ranking"] = 1.0
        solution1.attributes["crowding_distance"] = 2.0
        solution2.attributes["crowding_distance"] = 2.0

        self.assertEqual(0, self.comparator.compare(solution1, solution2))
Example #2
0
def main() -> None:
    class NSGA2b(NSGAII[S, R]):
        def is_stopping_condition_reached(self):
            # Re-define the stopping condition
            reached = [False, True][self.get_current_computing_time() > 4]

            if reached:
                logger.info("Stopping condition reached!")

            return reached

    problem = Fonseca()
    algorithm = NSGA2b[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()))

    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())
Example #3
0
 def test_NSGAII(self):
     NSGAII(
         problem=self.problem,
         population_size=self.population_size,
         offspring_population_size=self.offspring_size,
         mutation=self.mutation,
         crossover=self.crossover,
         selection=BinaryTournamentSelection(comparator=RankingAndCrowdingDistanceComparator()),
         termination_criterion=StoppingByEvaluations(max=1000)
     ).run()
Example #4
0
def configure_experiment(problems: dict, n_run: int):
    jobs = []
    max_evaluations = 25000

    for run in range(n_run):
        for problem_tag, problem in problems.items():
            jobs.append(
                Job(
                    algorithm=NSGAII(
                        problem=problem,
                        population_size=100,
                        offspring_population_size=100,
                        mutation=PolynomialMutation(
                            probability=1.0 / problem.number_of_variables,
                            distribution_index=20),
                        crossover=SBXCrossover(probability=1.0,
                                               distribution_index=20),
                        selection=BinaryTournamentSelection(
                            comparator=RankingAndCrowdingDistanceComparator()),
                        termination_criterion=StoppingByEvaluations(
                            max=max_evaluations)),
                    algorithm_tag='NSGAII',
                    problem_tag=problem_tag,
                    run=run,
                ))
            jobs.append(
                Job(
                    algorithm=GDE3(problem=problem,
                                   population_size=100,
                                   cr=0.5,
                                   f=0.5,
                                   termination_criterion=StoppingByEvaluations(
                                       max=max_evaluations)),
                    algorithm_tag='GDE3',
                    problem_tag=problem_tag,
                    run=run,
                ))
            jobs.append(
                Job(
                    algorithm=SMPSO(
                        problem=problem,
                        swarm_size=100,
                        mutation=PolynomialMutation(
                            probability=1.0 / problem.number_of_variables,
                            distribution_index=20),
                        leaders=CrowdingDistanceArchive(100),
                        termination_criterion=StoppingByEvaluations(
                            max=max_evaluations)),
                    algorithm_tag='SMPSO',
                    problem_tag=problem_tag,
                    run=run,
                ))

    return jobs
Example #5
0
def main() -> None:
    problem = Kursawe()
    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 = BinaryTournament(RankingAndCrowdingDistanceComparator()))

    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())
Example #6
0
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()))

    observer = BasicAlgorithmConsumer(1000)

    algorithm.observable.register(observer=observer)

    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))
Example #7
0
 def setUp(self):
     self.comparator = RankingAndCrowdingDistanceComparator()
Example #8
0
        filename='../../../resources/reference_front/{}.pf'.format(
            problem.get_name()))

    reference_point = [0.8, 0.5]

    max_evaluations = 25000
    algorithm = NSGAII(
        problem=problem,
        population_size=100,
        offspring_population_size=100,
        mutation=PolynomialMutation(probability=1.0 /
                                    problem.number_of_variables,
                                    distribution_index=20),
        crossover=SBXCrossover(probability=1.0, distribution_index=20),
        selection=BinaryTournamentSelection(
            comparator=RankingAndCrowdingDistanceComparator()),
        dominance_comparator=GDominanceComparator(reference_point),
        termination_criterion=StoppingByEvaluations(max=max_evaluations))

    algorithm.observable.register(observer=ProgressBarObserver(
        max=max_evaluations))
    algorithm.observable.register(
        observer=VisualizerObserver(reference_front=problem.reference_front,
                                    reference_point=(reference_point)))

    algorithm.run()
    front = algorithm.get_result()

    # Plot front
    plot_front = Plot(plot_title='Pareto front approximation',
                      axis_labels=problem.obj_labels,
Example #9
0
from jmetal.problem import DTLZ2
from jmetal.util.comparator import RankingAndCrowdingDistanceComparator, DominanceComparator
from jmetal.util.observer import ProgressBarObserver, VisualizerObserver

if __name__ == '__main__':
    problem = DTLZ2()
    problem.reference = '../../resources/reference_front/DTLZ2.3D.pf'

    max_evaluations = 25000
    algorithm = NSGAII(
        problem=problem,
        population_size=100,
        offspring_population_size=100,
        mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20),
        crossover=SBXCrossover(probability=1.0, distribution_index=20),
        selection=BinaryTournamentSelection(comparator=RankingAndCrowdingDistanceComparator()),
        termination_criterion=StoppingByEvaluations(max=max_evaluations),
        dominance_comparator=DominanceComparator()
    )

    algorithm.observable.register(observer=ProgressBarObserver(max=max_evaluations))
    algorithm.observable.register(observer=VisualizerObserver(reference_front=problem.reference_front))

    algorithm.run()
    front = algorithm.get_result()

    label = algorithm.get_name() + "." + problem.get_name()
    algorithm_name = label
    # Plot front
    plot_front = Plot(plot_title='Pareto front approximation', axis_labels=problem.obj_labels)
    plot_front.plot(front, label=label, filename=algorithm_name)