Esempio n. 1
0
class Terminator(TerminationCriterion):
    # initialize
    def __init__(self, tolerance, max_evaluation):
        super().__init__()
        # store the parameters
        self.max_evaluation = max_evaluation
        # self.timeout = timeout
        self.tolerance = tolerance
        # prepare members
        self.evaluations = 0
        # self.seconds = 0.0
        self.counter = 0
        self.front = NonDominatedSolutionsArchive()
        self.last_violation = None
        self.last_front_size = None

    # check violation
    def violation_count(self, solutions):
        count = 0
        for solution in solutions:
            if not all([c >= 0.0 for c in solution.constraints]):
                count += 1
        return count

    # update the front
    def update_front(self, solutions):
        new_solution_num = 0
        for solution in solutions:
            # solution.objectives = [round(o, 0) for o in solution.objectives]
            if self.front.add(solution):
                new_solution_num += 1
        return new_solution_num

    # update
    def update(self, *args, **kwargs):
        self.evaluations = kwargs['EVALUATIONS']
        if self.evaluations % 500 == 0:
            print(self.evaluations)
        # self.seconds = kwargs['COMPUTING_TIME']
        solutions = kwargs['SOLUTIONS']
        # check 
        front_size = self.update_front(solutions)
        violation = self.violation_count(solutions)
        if self.last_front_size == self.front.size() and self.last_violation == violation:
            self.counter += 1
        else:
            self.counter = 0
        # update
        self.last_front_size = self.front.size()
        self.last_violation = violation
        print('counter: ', self.counter, ' front_size: ', self.front.size(), ' violation: ', violation)
        # print(self.violation_count(solutions))

    # finish condition
    @property
    def is_met(self):
        # if self.evaluations >= self.max_evaluation:
        #     return True
        # return self.seconds >= self.timeout or self.counter >= self.tolerance
        return self.front.size() == POPULATION_SIZE and self.counter >= self.tolerance
Esempio n. 2
0
class NonDominatedSolutionListArchiveTestCases(unittest.TestCase):
    def setUp(self):
        self.archive = NonDominatedSolutionsArchive()

    def test_should_constructor_create_a_non_null_object(self):
        self.assertIsNotNone(self.archive)

    def test_should_adding_one_solution_work_properly(self):
        solution = Solution(1, 1)
        self.archive.add(solution)
        self.assertEqual(1, self.archive.size())
        self.assertEqual(solution, self.archive.solution_list[0])

    def test_should_adding_two_solutions_work_properly_if_one_is_dominated(self):
        dominated_solution = Solution(1, 2)
        dominated_solution.objectives = [2.0, 2.0]

        dominant_solution = Solution(1, 2)
        dominant_solution.objectives = [1.0, 1.0]

        self.archive.add(dominated_solution)
        self.archive.add(dominant_solution)

        self.assertEqual(1, self.archive.size())
        self.assertEqual(dominant_solution, self.archive.solution_list[0])

    def test_should_adding_two_solutions_work_properly_if_both_are_non_dominated(self):
        solution1 = Solution(1, 2)
        solution1.objectives = [1.0, 0.0]

        solution2 = Solution(1, 2)
        solution2.objectives = [0.0, 1.0]

        self.archive.add(solution1)
        self.archive.add(solution2)

        self.assertEqual(2, self.archive.size())
        self.assertTrue(solution1 in self.archive.solution_list and solution2 in self.archive.solution_list)

    def test_should_adding_four_solutions_work_properly_if_one_dominates_the_others(self):
        solution1 = Solution(1, 2)
        solution1.objectives = [1.0, 1.0]

        solution2 = Solution(1, 2)
        solution2.objectives = [0.0, 2.0]

        solution3 = Solution(1, 2)
        solution3.objectives = [0.5, 1.5]

        solution4 = Solution(1, 2)
        solution4.objectives = [0.0, 0.0]

        self.archive.add(solution1)
        self.archive.add(solution2)
        self.archive.add(solution3)
        self.archive.add(solution4)

        self.assertEqual(1, self.archive.size())
        self.assertEqual(solution4, self.archive.solution_list[0])

    def test_should_adding_three_solutions_work_properly_if_two_of_them_are_equal(self):
        solution1 = Solution(1, 2)
        solution1.objectives = [1.0, 1.0]

        solution2 = Solution(1, 2)
        solution2.objectives = [0.0, 2.0]

        solution3 = Solution(1, 2)
        solution3.objectives = [1.0, 1.0]

        self.archive.add(solution1)
        self.archive.add(solution2)
        result = self.archive.add(solution3)

        self.assertEqual(2, self.archive.size())
        self.assertFalse(result)
        self.assertTrue(solution1 in self.archive.solution_list or solution3 in self.archive.solution_list)