예제 #1
0
    def test_mwrules_correct_simple(self):
        from preferences import Profile
        import rules_approval

        self.longMessage = True

        profile = Profile(4)
        profile.add_preferences([[0], [1], [2], [3]])
        committeesize = 2

        for rule in rules_approval.MWRULES.keys():
            if rule == "greedy-monroe":  # always returns one committee
                continue
            self.assertEqual(len(
                rules_approval.compute_rule(rule, profile, committeesize)),
                             6,
                             msg=rule + " failed")

        for rule in rules_approval.MWRULES.keys():
            self.assertEqual(len(
                rules_approval.compute_rule(rule,
                                            profile,
                                            committeesize,
                                            resolute=True)),
                             1,
                             msg=rule + " failed with resolute=True")
예제 #2
0
def run_test_instance(unittestinstance, profile, committeesize, tests):
    import rules_approval

    # all rules used?
    for rule in rules_approval.MWRULES:
        unittestinstance.assertTrue(rule in tests.keys())

    for rule in tests.keys():

        output = rules_approval.compute_rule(rule,
                                             profile,
                                             committeesize,
                                             resolute=False)
        unittestinstance.assertEqual(output,
                                     tests[rule],
                                     msg=rules_approval.MWRULES[rule] +
                                     " failed")
        output = rules_approval.compute_rule(rule,
                                             profile,
                                             committeesize,
                                             resolute=True)
        unittestinstance.assertEqual(len(output),
                                     1,
                                     msg=rules_approval.MWRULES[rule] +
                                     " failed with resolute=True")
        unittestinstance.assertTrue(output[0] in tests[rule],
                                    msg=rules_approval.MWRULES[rule] +
                                    " failed with resolute=True")
예제 #3
0
 def runmwtests(tests, committeesize):
     for rule in tests.keys():
         output = rules_approval.compute_rule(rule,
                                              profile,
                                              committeesize,
                                              resolute=False)
         self.assertEqual(output, tests[rule], msg=rule + " failed")
         output = rules_approval.compute_rule(rule,
                                              profile,
                                              committeesize,
                                              resolute=True)
         self.assertEqual(len(output),
                          1,
                          msg=rule + " failed with resolute=True")
         self.assertTrue(output[0] in tests[rule],
                         msg=rule + " failed with resolute=True")
예제 #4
0
    def test_mwrules__toofewcandidates(self):
        from preferences import Profile
        import rules_approval
        profile = Profile(5)
        committeesize = 4
        preflist = [[0, 1, 2], [1], [1, 2], [0]]
        profile.add_preferences(preflist)

        for rule in rules_approval.MWRULES.keys():
            with self.assertRaises(Exception):
                rules_approval.compute_rule(rule, profile, committeesize)
            with self.assertRaises(Exception):
                rules_approval.compute_rule(rule,
                                            profile,
                                            committeesize,
                                            resolute=True)
예제 #5
0
    def test_optphrag_notiebreaking(self):
        from preferences import Profile
        from rules_approval import compute_rule

        self.longMessage = True

        profile = Profile(6)
        profile.add_preferences([[0], [0], [1, 3], [1, 3], [1, 4], [2, 4],
                                 [2, 5], [2, 5]])
        committeesize = 3

        self.assertEqual(
            len(
                compute_rule("optphrag",
                             profile,
                             committeesize,
                             resolute=False)), 12)
예제 #6
0
    def test_mwrules_weightsconsidered(self):
        from preferences import Profile
        from preferences import DichotomousPreferences
        import rules_approval

        self.longMessage = True

        profile = Profile(3)
        profile.add_preferences(DichotomousPreferences([0], 3))
        profile.add_preferences(DichotomousPreferences([1], 3))
        profile.add_preferences(DichotomousPreferences([0]))
        committeesize = 2

        for rule in rules_approval.MWRULES.keys():
            if rule[:6] == "monroe":
                continue  # Monroe only works with unit weights
            self.assertEqual(rules_approval.compute_rule(
                rule, profile, committeesize), [[0, 1]],
                             msg=rule + " failed")
예제 #7
0
    def test_mwrules_weightsconsidered(self):
        from preferences import Profile
        from preferences import DichotomousPreferences
        import rules_approval

        self.longMessage = True

        profile = Profile(3)
        profile.add_preferences(DichotomousPreferences([0]))
        profile.add_preferences(DichotomousPreferences([0]))
        profile.add_preferences(DichotomousPreferences([1], 5))
        profile.add_preferences(DichotomousPreferences([0]))
        committeesize = 1

        for rule in rules_approval.MWRULES.keys():
            if "monroe" in rule or "rule-x" in rule:
                # Monroe and rule x only work with unit weights:
                continue
            result = rules_approval.compute_rule(rule, profile, committeesize)
            self.assertTrue([1] in result, msg=rule + " failed" + str(result))