Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random elections and spatial generation #130

Merged
merged 10 commits into from
Aug 9, 2024

Conversation

kevin-q2
Copy link
Contributor

@kevin-q2 kevin-q2 commented Jul 3, 2024

Initial work on random election mechanisms and spatial generation of preference profiles. Includes new election mechanisms RandomDictator and BoostedRandomDictator as well as Uniform/Clustered spatial generation classes.

@cdonnay cdonnay self-requested a review July 12, 2024 18:21
Copy link
Collaborator

@cdonnay cdonnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Kevin, this is great. My big comment is that I think we can refactor some stuff and add a little more flexibility to these things. Let's Zoom to strategize.

@peterrrock2 , can you take a look at where I tagged you? I'll save you from a bigger review until after we refactor.

src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/elections/election_types.py Outdated Show resolved Hide resolved
src/votekit/elections/election_types.py Outdated Show resolved Hide resolved
src/votekit/elections/election_types.py Show resolved Hide resolved
# Choose via proportional to squares
candidate_votes = {c: 0 for c in remaining}
for ballot in self.state.profile.get_ballots():
top_choice = list(ballot.ranking[0])[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, does list randomize the order of the set?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random.choice(random_ballot.ranking[0])[0]

self.state.profile.ballots, weights=weights, k=1
)[0]
# randomly choose a winner according to first place rankings
winning_candidate = list(random_ballot.ranking[0])[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above, does list randomize the order of the set? @peterrrock2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random.choice(random_ballot.ranking[0])[0]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some googling I believe list does not randomize the order of the set. Unfortunately random.choice() doesn't work on sets either. It could be slow but a good option might be: random.choice(list(random_ballot.ranking[0])). What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the order of the set is determined by the hash of the underlying type. There is an environment variable, PYTHONHASHSEED, that controls this for security reasons. If you want this to be deterministic, you will need to use something other than a set. You can make use of the OrderedSet class in the collections module, or just use a dictionary. Convertiing to a list is also fine here. It doesn't really take up a lot of overhead in the grand scheme of things.

Copy link
Collaborator

@cdonnay cdonnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Keep at it.

src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
@mggg mggg deleted a comment from kevin-q2 Aug 6, 2024
Copy link
Collaborator

@cdonnay cdonnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going great, Kevin!

src/votekit/ballot_generator.py Show resolved Hide resolved
src/votekit/ballot_generator.py Show resolved Hide resolved
src/votekit/ballot_generator.py Show resolved Hide resolved
src/votekit/ballot_generator.py Outdated Show resolved Hide resolved
src/votekit/ballot_generator.py Show resolved Hide resolved
src/votekit/elections/election_types.py Outdated Show resolved Hide resolved
src/votekit/elections/election_types.py Show resolved Hide resolved
src/votekit/elections/election_types.py Outdated Show resolved Hide resolved
cands_elected = [len(s) for s in self.state.winners()]
return sum(cands_elected) < self.seats

def run_step(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay: let's refactor this. Each round should end when one candidate is eliminated.
Then remove them from the profile. Start a new round.

You'll need to ensure that the random order is preserved. Should each ballot have weight 1? Yes: de-coupling the ballots with weight bigger than 1. In the init method, validate that each weight is an integer.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check that a Fraction type is an int, just do int(Fraction(4)) == Fraction(4).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the decoupling in the init method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention in the doc string that you decondense.

src/votekit/metrics/distances.py Outdated Show resolved Hide resolved
@cdonnay cdonnay changed the base branch from main to 3.0.0.dev August 9, 2024 14:02
@cdonnay cdonnay merged commit ebd6ac6 into mggg:3.0.0.dev Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants