-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.py
More file actions
264 lines (210 loc) · 8.01 KB
/
problem.py
File metadata and controls
264 lines (210 loc) · 8.01 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 5 09:38:54 2016
@author: nicolas
"""
'''
Fair Division of Indivisible Goods
Companion code for the chapter 12 of the Handbook of CSC
'''
import random
def generateUtilities(n,resources,culture):
'''
@n: number of agents
@resources: resources
@culture: type of culture (uniform,borda,normalized,gauss)
'''
u_min=1
u_max=99
utilities = {r:0 for r in resources}
intrinsic_utilities = {r:0 for r in resources}
all_utilities = []
if culture=='gauss':
for r in resources:
intrinsic_utilities[r]=random.randrange(u_min,u_max)
for i in range(n):
if culture == 'uniform': #
for r in resources:
u = random.randrange(u_min,u_max)
utilities[r]=u
elif culture == 'borda': # utilities = rank
random.shuffle(resources)
for r in resources:
utilities[r]=resources.index(r)+1
elif culture == 'normalized': # quick-and-dirty normalisation via rounding
for r in resources:
u = random.randrange(u_min,u_max)
utilities[r]=u
sum_of_utilities = sum([utilities[r] for r in resources])
for r in resources:
utilities[r]=round(utilities[r]/sum_of_utilities,3)
elif culture == 'gauss': # picks an intrinsic value for resources
for r in resources:
pass
#TODO: draw with gauss from intrinsic
elif culture == 'empty':
pass
all_utilities += utilities
return utilities
#TODO: adapt when calling since now all agents utilities are produced
def generateAllocation(n,resources,alloc):
'''
returns a dictionary resource:agent
'''
if alloc == 'random':
#allocates each resource uniformly at random
allocation = {r:random.randint(0,n-1) for r in resources}
elif alloc == 'auctioneer':
#allocates all resources to agent 0
allocation = {r:0 for r in resources}
return allocation
def generateTopology(n,topo):
# complete, centralised, circle
visibility_graph = {i:[] for i in range(n)}
exchange_graph = {i:[] for i in range(n)}
if topo == 'complete':
for i in range(n):
for j in range(n):
if i!=j:
visibility_graph[i].append(j)
exchange_graph[i].append(j)
elif topo =='centralized':
for i in range(1,n):
exchange_graph[0].append(i)
for j in range(1,n):
if j!=i:
visibility_graph[i].append(j)
return visibility_graph, exchange_graph
###############################################################################
class Problem(object):
''' a fair division problem is defined by:
- a set of m resources
- a set of n agents,
- utilities over the resources
- an initial allocation of resources
- a visibility topology (symmetric)
- an exchange topology (directed)
'''
def __init__(self,n,m,culture,centralized=True):
'''
@n: number of agents
@m: number of resources
@culture: generation of utilities
@predef_model:
we provide the following pre-defined MARA models:
- centralized: agent 0 gets all resources,
- complete topology, random initial allocation
- ...
'''
self.n=n
self.m=m
self.agent = []
# creating the resources
resources = []
for i in range(m):
resources.append("r"+str(i))
self.resources= resources
self.centralized = centralized
if centralized:
# calling the function generating initial allocation
allocation = generateAllocation(n,resources,'auctioneer')
#print (allocation)
# calling the function generating topologies
visibility, exchange = generateTopology(n,'centralized')
else: # decentrlized
# calling the function generating initial allocation
allocation = generateAllocation(n,resources,'random')
#print (allocation)
# calling the function generating topologies
visibility, exchange = generateTopology(n,'complete')
self.visibility_graph = visibility
self.exchange_graph = exchange
# calling the function generating the utilities
utilities = {}
# should be ok via the topology
for i in range(n): # creating the agents
utilities = generateUtilities(n,resources,culture)
self.agent.append(Agent(utilities,[r for (r,j) in allocation.items() if j==i]))
return
def setUtilities(self,utilities):
'''
sets utilities of a problem,
@utilities: list of dictionary
'''
for i in range(self.n):
utility = utilities[i]
self.agent[i].u= utility
return
def setAllocation(self,alloc):
'''
sets the allocation of a problem,
@alloc: a boolean array nxm specifying who gets which resource
'''
for i in range(self.n):
allocs = alloc[i]
self.agent[i].dropItems()
for j in range(self.m):
if allocs[j]:
self.agent[i].getItem('r'+str(j))
return
def cycleReallocation(self,agents):
'''
@agents: list (cycle) of agents [x_1,x_2, ... xn]
reallocate bundle by giving bundle x_2 to x_1, etc.
'''
items_of_first_agent = self.agent[agents[0]].hold
self.agent[agents[0]].dropItems()
for idx,i in enumerate(agents[:-1]): # solving the cycle
items_of_next_agent = self.agent[agents[(idx+1)%(len(agents))]].hold
self.agent[agents[(idx+1)%(len(agents))]].dropItems()
self.agent[i].getItems(items_of_next_agent)
self.agent[agents[(idx+1)%(len(agents))]].getItems(items_of_first_agent)
return
def __str__(self):
s=""
for i in range(self.n):
s += "agent " + str(i) + str(self.agent[i]) + "\n"
return s
def printAllocation(self):
s="=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"
for i in range(self.n):
s += "agent " + str(i).rjust(2) + str(self.agent[i].hold).rjust(35) + "\t" + str(self.agent[i].current_u).rjust(2)+ "\n"
return (s)
###############################################################################
class Agent(object):
def __init__(self,utility,resources):
'''
'''
self.u= utility # dictionary of utility for resources
self.hold = resources # list of resources held by agent
self.current_u = sum([self.u[r] for r in resources]) # current utility enjoyed by agent
def __str__(self):
return str(self.u)
def getItem(self,r):
'''
@r: a single item
'''
self.hold.append(r)
self.current_u += self.u[r]
return
def getItems(self,lr):
'''
@lr: a list of items
'''
for r in lr:
self.getItem(r)
return
def giveItem(self,r):
if r not in self.hold:
print ("agent ", self, " does not hold ", r, "!!!")
self.hold.remove(r)
self.current_u -= self.u[r]
return
def giveItems(self,lr):
for r in lr:
self.giveItem(r)
return
def dropItems(self):
self.hold=[]
self.current_u = 0
#TODO: replace by dropItems