def integration_MxN(n_candidates, n_voters, voting_scheme, seed=None): """ @param voting_scheme: one of the four voting schemes """ if seed != None: np.random.seed(seed) candidates_num = range(65, 65+n_candidates) candidates = [str(chr(i)) for i in candidates_num] pref_list = list(permutations(candidates_num)) pref_mat = np.zeros((n_candidates, n_voters), dtype=np.uint8) for i in range(n_voters): rand = np.random.randint(0, len(pref_list)) pref_mat[:, i] = np.array(pref_list[rand]) params = {"num_voters": n_voters, "num_candidates": n_candidates, "candidate_list": candidates, "pref_mat": pref_mat, "scheme": voting_scheme} pc = PC(**params) vsr = VSR() vsr.voting_simulation(pc.pref_mat, pc.scheme) tv = TV(pref_mat=pc.pref_mat, voting_outcome=vsr.results, scheme=pc.scheme) return pc, vsr, tv
def simple_test_scheme(): pc = PC() pc.num_voters = 4 pc.num_candidates = 4 pc.candidate_list = ["A", "B", "C", "D"] pc.pref_mat = np.array([[65, 65, 65, 65], [66, 66, 66, 66], [67, 67, 67, 67], [68, 68, 68, 68]]) return pc
def test_get_voter_cadidates(self): pc = PC() pc.num_voters = 1 pc.num_candidates = 1 pc.candidate_list = ["A"] pc.pref_mat = np.zeros((1, 1)) with mock.patch("builtins.input", return_value="A"): pc.get_voter_candidates() self.assertEqual(pc.pref_mat, np.array([65]).reshape(1, 1))
def test_get_num_voters_succ(self): pc = PC() num_voters = 2 with mock.patch("builtins.input", return_value=str(num_voters)): pc.get_num_voters() self.assertEqual(pc.num_voters, num_voters) with mock.patch("builtins.input", return_value="a"): with self.assertRaises(ValueError): pc.get_num_voters()
def test_get_num_candidates(self): pc = PC() num_candidates = 4 with mock.patch("builtins.input", return_value=num_candidates): pc.get_num_candidates() self.assertEqual(pc.num_candidates, num_candidates) self.assertEqual(pc.candidate_list, ["A", "B", "C", "D"]) self.assertTrue((pc.pref_mat == np.array([]).reshape(4, 0)).all()) with mock.patch("builtins.input", return_value="a"): with self.assertRaises(ValueError): pc.get_num_candidates()
def integration_voting_for_two(): """Integration test for voting_for_two""" params = {"num_voters": 3, "num_candidates": 2, "candidate_list": ["A", "B"], "pref_mat": np.array([[65, 66, 65], [66, 65, 66]]), "scheme": 1} # scheme is not 2 as in manual selection pc = PC(**params) vsr = VSR() vsr.voting_simulation(pc.pref_mat, pc.scheme) tv = TV(pref_mat=pc.pref_mat, voting_outcome=vsr.results, scheme=pc.scheme) """Expected outcome: Bullet voting as well as compromising possible for all""" return pc, vsr, tv
import integration_tests as IT if __name__ == "__main__": modes = { 1: "Manual input", 2: "Integration test: 2 candidates, 3 voters, voting for two", 3: "Integration test: M candidates, N voters, pick a voting scheme, random preferences" } for k, v in modes.items(): print("{}: {}".format(k, v)) mode = input("\nPlease choose one of the options listed [1/2/3]\n") if mode == "1": pc = PC() vsr = VSR() pc.get_preferences() vsr.voting_simulation(pc.pref_mat, pc.scheme) tv = TacticalVoting(pc.pref_mat, vsr.results, pc.scheme) elif mode == "2": pc, vsr, tv = IT.integration_voting_for_two() elif mode == "3": M = int(input("Enter number of candidates: ")) if not (isinstance(M, int)) or (not M > 0): print("Select an Integer > 0") exit() N = int(input("Enter number of voters: ")) if not (isinstance(N, int)) or (not N > 0): print("Select an Integer > 0") exit()
def test_get_voting_schemes(self): pc = PC() with mock.patch("builtins.input", return_value=1): pc.get_voting_schemes() self.assertEqual(pc.scheme, 0)