forked from ariel-research/fairpyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_formats.py
79 lines (48 loc) · 3.02 KB
/
input_formats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#' # Input formats
import fairpyx
divide = fairpyx.divide
#' `fairpyx` allows various input formats, so that you can easily use it on your own data,
#' whether for applications or for research.
#' ## Valuations
#' Suppose you want to divide candies among your children.
#' It is convenient to collect their preferences in a dict of dicts:
valuations = {
"Ami": {"green": 8, "red":7, "blue": 6, "yellow": 5},
"Tami": {"green": 12, "red":8, "blue": 4, "yellow": 2} }
allocation = divide(fairpyx.algorithms.round_robin, valuations=valuations)
#' You can then see the resulting allocation with the agents' real names:
print(allocation)
#' For research, passing a dict of dicts as a parameter may be too verbose.
#' You can call the same algorithm with only the values, or only the value matrix:
print(divide(fairpyx.algorithms.round_robin, valuations={"Ami": [8,7,6,5], "Tami": [12,8,4,2]}))
print(divide(fairpyx.algorithms.round_robin, valuations=[[8,7,6,5], [12,8,4,2]]))
#' For experiments, you can use a numpy random matrix. The code below generates random values for 5 agents and 12 courses:
import numpy as np
valuations = np.random.randint(1,100,[5,12])
print(valuations)
allocation = divide(fairpyx.algorithms.almost_egalitarian_allocation, valuations=valuations)
print(allocation)
#' ## Capacities
#' There are several input formats for agent capacities. You can set the same capacity to all agents:
allocation = divide(fairpyx.algorithms.almost_egalitarian_allocation, valuations=valuations, agent_capacities=2)
print(allocation)
#' Or different capacities to different agents:
allocation = divide(fairpyx.algorithms.almost_egalitarian_allocation, valuations=valuations, agent_capacities=[1,2,3,2,1])
print(allocation)
#' Similarly, you can set the same capacity to all items:
allocation = divide(fairpyx.algorithms.almost_egalitarian_allocation, valuations=valuations, agent_capacities=4, item_capacities=2) # , explanation_logger=fairpyx.ConsoleExplanationLogger()
print(allocation)
#' Or different capacities to different items:
allocation = divide(fairpyx.algorithms.almost_egalitarian_allocation, valuations=valuations, agent_capacities=4, item_capacities=[1,2,1,2,1,2,1,2,1,2,1,2]) # , explanation_logger=fairpyx.ConsoleExplanationLogger()
print(allocation)
#' ## Conflicts
#' You can specify agent_conflicts - a set of items that cannot be allocated to this agent (e.g. due to missing preliminaries):
valuations = {
"Ami": {"green": 8, "red":7, "blue": 6, "yellow": 5},
"Tami": {"green": 12, "red":8, "blue": 4, "yellow": 2} }
allocation = divide(fairpyx.algorithms.round_robin, valuations=valuations, agent_conflicts={"Ami": ["green", "red", "blue"], "Tami": ["red", "blue", "yellow"]})
print(allocation)
#' You can also specify item_conflicts - a set of items that cannot be taken together (e.g. due to overlapping times):
allocation = divide(fairpyx.algorithms.round_robin, valuations=valuations, item_conflicts={"green": ["yellow", "red", "blue"]})
print(allocation)
#' Note that not all algorithms can handle conflicts.