-
Notifications
You must be signed in to change notification settings - Fork 13
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
Conversation
There was a problem hiding this 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.
# 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] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going great, Kevin!
cands_elected = [len(s) for s in self.state.winners()] | ||
return sum(cands_elected) < self.seats | ||
|
||
def run_step(self): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.