Created
July 14, 2019 05:46
-
-
Save jcheong0428/ddb8ac453bb9940deba9ac1fdc4fda96 to your computer and use it in GitHub Desktop.
First gist for Hungarian post.
This file contains hidden or 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
| # First gist for Hungarian post. | |
| # Brute force estimation of optimal assignment for given preference. | |
| %matplotlib inline | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| sns.set_context('talk') | |
| sns.set_style('white') | |
| import itertools | |
| import numpy as np | |
| preferences = np.array([[99, 99, 13, 41], | |
| [14, 99, 21, 11], | |
| [31, 95, 21, 51], | |
| [52, 50, 51, 11]]) | |
| maxsums, colixs = [], [] | |
| group_size = np.shape(preferences)[0] | |
| np.random.seed(0) | |
| rowix = list(range(group_size)) # rows don't need to be permuted | |
| # Permute choice of columns. | |
| for colix in itertools.permutations(range(group_size)): | |
| _sum = np.sum(preferences[rowix,colix]) | |
| maxsums.append(_sum) | |
| colixs.append(colix) | |
| maxsum = np.max(maxsums) | |
| maxcolix = colixs[np.argmax(maxsums)] | |
| print("Total preference:", maxsum) | |
| print("Row:",rowix, "Col:", maxcolix) | |
| f,ax = plt.subplots(figsize=(10,5)) | |
| ax.bar(range(len(maxsums)),np.sort(maxsums)[::-1]) | |
| ax.set(xlabel='Permutations',ylabel='Combined Group Preference', title="Preferences for every assignment combination") | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment