Exemplo n.º 1
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))
Exemplo n.º 2
0
def simulate_election_day(config, number):
    '''
    Simulates an election day at a precinct as defined by a config
    file. The heavy lifting in this function is carried out by methods
    defined in the three above classes

    Inputs: config - a configuration file that contains information about the 
            precinct that will be simulated
            number - the number of booths the precinct being simulated

    Outputs: voter_list - a list of voter instances sorted in increasing order
            by arrival time
    '''
    voter_list = []
    pre = precinct(config, number)
    v_sample = voter_sample(config, number)
    v_sample.precinct.set_booths()
    voter_obj = v_sample.gen_next_voter(config, number)
    t = voter_obj.arrival_time
    
    #loops over times less than the amount of time the precinct is open
    while t < v_sample.precinct._time_open:
        
        voter_list.append(voter_obj)
        voter_obj = v_sample.gen_next_voter(config, number)
        t += voter_obj.voting_gap
        
        #breaks the loop if we have exceeded the number of eligible voters
        if voter_obj.VoterID > pre._num_voters:
            break
    
    return util.print_voters(voter_list)
Exemplo n.º 3
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)
Exemplo n.º 4
0
        voters.append(voter)
    #util.print_voters(voters)
    return voters

def simulate_election_day(config):
    # YOUR CODE HERE.
    # REPLACE [] with a list of voter objects
    return []


if __name__ == "__main__":
    # process arguments
    num_booths = 1

    if len(sys.argv) == 2:
        config_filename = sys.argv[1]
    elif len(sys.argv) == 3:
        config_filename = sys.argv[1]
        num_booths = int(sys.argv[2])
    else:
        s = "usage: python3 {0} <configuration filename>"
        s = s + " [number of voting booths]"
        s = s.format(sys.argv[0])
        print(s)
        sys.exit(0)

    config = util.setup_config(config_filename, num_booths)
    #voters = simulate_election_day(config)
    voters = test_sample(config)
    util.print_voters(voters)