When running an event or teaching a class, you might need to divvy people up into groups. Ask them which group they'd like to be put in. Then, use this script to divvy them up in a way that satisfies everyone's preferences as much as possible.
Put everyone's preferences in prefs.csv. Here's an example of what that might look like:
Alice,group1,group2,group3
Bob,group1,group3,group2
Charlie,group1,group2,group3
This means that Alice put group1 as her first choice, group2 as her second choice, and so on.
How big can the groups get? Write that in capacities.csv. For example:
group1,2
group2,3
group3,4
To run the script:
python sort.py prefs.csv capacities.csv -100,50,100,200
The numbers shown here mean that giving someone their first choice is good (it has a "negative cost" of -100), giving someone their second choice is slightly bad (it has a cost of 50), giving someone their third choice is worse (it has a cost of 100), and giving someone their fourth choice is really bad (cost of 200).
By playing with these numbers you can, for example, choose between a scenario where most people get their first choice but some get their third choice, and a scenario where about half get their first choice and half get their second choice.
You'll need to install networkx
:
pip install networkx
The code is mostly not mine but is taken from https://stackoverflow.com/questions/21317213/sorting-people-into-groups-based-on-votes