Esempio n. 1
0
def run_test(precincts_file, num_trials, num_booths, avg_wait_time):
    precincts, seed = util.load_precincts(precincts_file)
    p = precincts[0]

    avg_wt = find_avg_wait_time(p, num_booths, num_trials, initial_seed=seed)

    assert avg_wt == pytest.approx(avg_wait_time)
def run_test(precincts_file):
    precincts, seed = util.load_precincts(precincts_file)
    results_file = precincts_file.replace(".json", ".csv")
    voters = simulate_election_day(precincts, seed)

    with open(results_file) as f:
        reader = csv.DictReader(f)

        results = {}
        for row in reader:
            results.setdefault(row["precinct"], []).append(row)

        for p in precincts:
            pname = p["name"]

            pvoters = voters[pname]
            rvoters = results.get(pname, [])

            assert len(pvoters) == len(
                rvoters
            ), "Incorrect number of voters for precinct '{}' (got {}, expected {}".format(
                pname, len(pvoters), len(rvoters))

            i = 0
            for returned_voter, expected_voter in zip(pvoters, rvoters):
                fcompare(pname, i, "arrival time", returned_voter.arrival_time,
                         float(expected_voter["arrival_time"]))
                fcompare(pname, i, "voting duration",
                         returned_voter.voting_duration,
                         float(expected_voter["voting_duration"]))
                fcompare(pname, i, "start time", returned_voter.start_time,
                         float(expected_voter["start_time"]))
                i += 1
Esempio n. 3
0
def cmd(precincts_file, max_num_booths, target_wait_time, print_voters):
    precincts, seed = util.load_precincts(precincts_file)

    if target_wait_time is None:
        voters = simulate_election_day(precincts, seed)
        print()
        if print_voters:
            for p in voters:
                print("PRECINCT '{}'".format(p))
                util.print_voters(voters[p])
                print()
        else:
            for p in precincts:
                pname = p["name"]
                if pname not in voters:
                    print("ERROR: Precinct file specified a '{}' precinct".
                          format(pname))
                    print(
                        "       But simulate_election_day returned no such precinct"
                    )
                    print()
                    return -1
                pvoters = voters[pname]
                if len(pvoters) == 0:
                    print("Precinct '{}': No voters voted.".format(pname))
                else:
                    pl = "s" if len(pvoters) > 1 else ""
                    closing = p["hours_open"] * 60.
                    last_depart = pvoters[-1].departure_time
                    avg_wt = sum(
                        [v.start_time - v.arrival_time
                         for v in pvoters]) / len(pvoters)
                    print("PRECINCT '{}'".format(pname))
                    print("- {} voter{} voted.".format(len(pvoters), pl))
                    msg = "- Polls closed at {} and last voter departed at {:.2f}."
                    print(msg.format(closing, last_depart))
                    print("- Avg wait time: {:.2f}".format(avg_wt))
                    print()
    else:
        precinct = precincts[0]

        if max_num_booths is None:
            max_num_booths = precinct["num_voters"]

        nb, avg_wt = find_number_of_booths(precinct, target_wait_time,
                                           max_num_booths, 20, seed)

        if nb is 0:
            msg = "The target wait time ({:.2f}) is infeasible"
            msg += " in precint '{}' with {} or less booths"
            print(
                msg.format(target_wait_time, precinct["name"], max_num_booths))
        else:
            msg = "Precinct '{}' can achieve average waiting time"
            msg += " of {:.2f} with {} booths"
            print(msg.format(precinct["name"], avg_wt, nb))
def run_test(precincts_file, num_trials, target_wait_time, max_num_booths,
             num_booths, avg_wait_time):
    precincts, seed = util.load_precincts(precincts_file)
    p = precincts[0]

    nb, avg_wt = find_number_of_booths(p, target_wait_time, max_num_booths,
                                       num_trials, seed)

    assert nb == num_booths
    assert avg_wt == pytest.approx(avg_wait_time)
Esempio n. 5
0
def test_0(precinct):
    '''
    This functions tests whethers you are properly generating voter objects
    '''
    random.seed(1468604453)
    precinct = util.load_precincts(precinct)
    generator = VoterGenerator(precinct[0][0]['voter_distribution']\
        ['arrival_rate'], precinct[0][0]['voter_distribution']\
        ['voting_duration_rate'])
    lst = []
    for i in range(precinct[0][0]['num_voters']):
        lst.append(generator.activate())
    for voter in lst:
        print([voter.arrival_time, voter.voting_duration, voter.start_time, \
            voter.departure_time, voter.voter_id])
Esempio n. 6
0
def test_VoterGenerator(file_name):
    '''
    Tests the VoterGenerator class.
    '''

    (precincts, seed) = util.load_precincts(file_name)
    for p in precincts:
        random.seed(seed)
        num_voters = p["num_voters"]
        arrival_rate = p["voter_distribution"]["arrival_rate"]
        voting_duration_rate = p["voter_distribution"]["voting_duration_rate"]
        v_list = []
        vg = VoterGenerator(arrival_rate, voting_duration_rate, num_voters)
        for v in range(num_voters):
            voter = vg.next_voter()
            if voter is not None:
                v_list.append(voter)

    return util.print_voters(v_list)
Esempio n. 7
0
def run_test(precincts_file, check_start):
    precincts, seed = load_precincts(precincts_file)
    results_file = precincts_file.replace(".json", ".csv")

    voters = {}
    for p in precincts:
        precinct = Precinct(p["name"], p["hours_open"], p["num_voters"],
                            p["voter_distribution"]["arrival_rate"],
                            p["voter_distribution"]["voting_duration_rate"])
        voters[p["name"]] = precinct.simulate(seed, p["num_booths"])

    with open(results_file) as f:
        reader = csv.DictReader(f)

        results = {}
        for row in reader:
            results.setdefault(row["precinct"], []).append(row)

        for p in precincts:
            pname = p["name"]

            pvoters = voters[pname]
            rvoters = results.get(pname, [])

            assert len(pvoters) == len(
                rvoters
            ), "Incorrect number of voters for precinct '{}' (got {}, expected {}".format(
                pname, len(pvoters), len(rvoters))

            i = 0
            for returned_voter, expected_voter in zip(pvoters, rvoters):
                fcompare(pname, i, "arrival time", returned_voter.arrival_time,
                         float(expected_voter["arrival_time"]))
                fcompare(pname, i, "voting duration",
                         returned_voter.voting_duration,
                         float(expected_voter["voting_duration"]))
                if check_start:
                    fcompare(pname, i, "start time", returned_voter.start_time,
                             float(expected_voter["start_time"]))
                i += 1