示例#1
0
    def test_stability_random(self) -> None:
        num_trial = 100
        max_pair = 50
        for _ in range(0, num_trial):
            a_list = []
            b_list = []
            count = random.randrange(10, max_pair)
            # create candidates in two lists
            for i in range(count):
                a_list.append(Person("a_" + str(i)))
                b_list.append(Person("b_" + str(i)))
            # randomly set prefernces for each candidate
            for a in a_list:
                pref = b_list[:]
                random.shuffle(pref)
                a.set_preferences(pref)
            for b in b_list:
                pref = a_list[:]
                random.shuffle(pref)
                b.set_preferences(pref)

            pairs = generate_stable_pairs(a_list, b_list)
            assert len(pairs) == count
            assert len(set([a for a, b in pairs])) == count
            assert len(set([b for a, b in pairs])) == count
            if not check_stability(pairs):
                dump_dict = {}
                for a in a_list:
                    dump_dict[str(a)] = a.get_preferences()
                for b in b_list:
                    dump_dict[str(b)] = b.get_preferences()
                dump_dict['pairs'] = pairs
                print(dump_dict)
            assert check_stability(pairs)
示例#2
0
    def test_stability_all_cases_stable(self) -> None:
        self._simon.set_preferences([self._jane, self._mary])
        self._peter.set_preferences([self._mary, self._jane])
        self._jane.set_preferences([self._peter, self._simon])
        self._mary.set_preferences([self._simon, self._peter])

        assert check_stability([(self._simon, self._mary),
                                (self._peter, self._jane)])
        assert check_stability([(self._simon, self._jane),
                                (self._peter, self._mary)])
示例#3
0
    def test_stability_simple2(self) -> None:
        self._simon.set_preferences([self._jane, self._mary])
        self._peter.set_preferences([self._jane, self._mary])
        self._jane.set_preferences([self._simon, self._peter])
        self._mary.set_preferences([self._peter, self._simon])

        assert not check_stability([(self._simon, self._mary),
                                    (self._peter, self._jane)])
        assert check_stability([(self._simon, self._jane),
                                (self._peter, self._mary)])
示例#4
0
    def test_stable_result_for_two(self) -> None:
        left = [self.nIsTwo['Simon'], self.nIsTwo['Peter']]
        right = [self.nIsTwo['Jane'], self.nIsTwo['Mary']]
        pairs: Sequence[Pair] = generate_stable_pairs(left, right)

        assert len(pairs) == 2
        assert (self.nIsTwo['Simon'], self.nIsTwo['Jane']) in pairs
        assert (self.nIsTwo['Peter'], self.nIsTwo['Mary']) in pairs
        assert check_stability(pairs)
示例#5
0
 def test_same_seed(self) -> None:
     seed = 42
     prev_result = None
     for _ in range(0, 100):
         pairs = generate_stable_pairs(self.a_list, self.b_list, seed)
         if prev_result:
             assert prev_result == pairs
         prev_result = pairs
         assert len(pairs) == self.count
         assert len(set([a for a, b in pairs])) == self.count
         assert len(set([b for a, b in pairs])) == self.count
         assert check_stability(pairs)
示例#6
0
 def test_stability_empty(self) -> None:
     assert check_stability([])
     assert check_stability(generate_stable_pairs([], []))