I need a generator that gets as input a set of 'agents' and a set of 'items', and generates all partitions in which each agent gets the same number of items. For example:
>>> for p in equalPartitions(["A","B"], [1,2,3,4]): print(p)
{'A': [1, 2], 'B': [3, 4]}
{'A': [1, 3], 'B': [2, 4]}
{'A': [1, 4], 'B': [2, 3]}
{'A': [2, 3], 'B': [1, 4]}
{'A': [2, 4], 'B': [1, 3]}
{'A': [3, 4], 'B': [1, 2]}
For two agents this is easy (assuming the number of items is even):
itemsPerAgent = len(items) // len(agents)
for bundle0 in itertools.combinations(items, itemsPerAgent):
bundle1 = [item for item in items if item not in bundle0]
yield {
agents[0]: list(bundle0),
agents[1]: bundle1
}
For three agents this becomes more complicated:
itemsPerAgent = len(items) // len(agents)
for bundle0 in itertools.combinations(items, itemsPerAgent):
bundle12 = [item for item in items if item not in bundle0]
for bundle1 in itertools.combinations(bundle12, itemsPerAgent):
bundle2 = [item for item in bundle12 if item not in bundle1]
yield {
agents[0]: list(bundle0),
agents[1]: list(bundle1),
agents[2]: bundle2
}
Is there a more general solution, that works for any number of agents?