Last active
August 29, 2015 14:08
-
-
Save prophile/ec2adf0ac8d1a7f00856 to your computer and use it in GitHub Desktop.
Resource distribution brute-force algorithm in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import permutations | |
import yaml | |
import sys | |
#votes = {'a': ['cheese', 'hats', 'faces'], | |
# 'b': ['hats', 'cheese', 'faces'], | |
# 'c': ['hats', 'faces', 'cheese']} | |
votes = {'a': 'CHWESK', | |
'b': 'CESWHK', | |
'c': 'HCWSEK', | |
'd': 'SKEWHC', | |
'e': 'CHEWSK', | |
'f': 'EHWCSK'} | |
def brute(votes, utilitarianism=2): | |
ballots = list(votes.values()) | |
if not ballots: | |
return {} | |
def cost(permutation): | |
return sum((ballot.index(choice) - 1) ** utilitarianism | |
for choice, ballot in zip(permutation, ballots)) | |
selection = min(permutations(sorted(ballots[0])), | |
key=cost) | |
return dict(zip(votes.keys(), selection)) | |
yaml.dump(brute(votes, utilitarianism=3), sys.stdout, default_flow_style=False) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment