-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm4.py
276 lines (253 loc) · 10.1 KB
/
algorithm4.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
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
265
266
267
268
269
270
271
272
273
274
275
276
import networkx as nx
import numpy as np
import simulation
from simulation import *
main_sources = set()
def delgo_coupon_allocation(p: CouponProblem, debug=False, pdf=None):
while p.pool_size() >= p.agents:
if p.remaining_coupons_array().any() < 0:
print("bad remaining bug")
return False
sw1 = p.social_welfare()
remaining_array1 = p.remaining_coupons_array()
r = coupon_allocation(p, debug, pdf)
if not r:
return r
remaining_array = p.remaining_coupons_array()
for i in range(len(remaining_array)):
if remaining_array[i] < 0:
print("del item: ", i)
for a in range(p.agents):
p.allocation[a][i] = 0
sw2 = p.social_welfare()
print("sw1: ", sw1, " sw2: ", sw2)
if sw2 <= sw1:
print("social welfare bug")
print(p.valuation_matrix)
return False
return True
def coupon_allocation(p: CouponProblem, debug=False, pdf=None):
global main_sources
nash_set = {-1}
stoped = False
while not stoped:
p.reset_old_allocation()
if debug:
print("remaining: ", p.remaining_coupons_array())
print("before r1: ", p.allocation)
print("val: ", p.valuation_matrix)
draw_graph(p, title="before R1 " + str(p.social_welfare()) + " " + str(p.pool_size()), pdf=pdf)
# Rule #1
nw1 = p.social_welfare()
r1(p)
# Rule 2
remaining_array = p.remaining_coupons_array()
if debug:
print_nash(nash_set, p, "R2 nash: ")
print("pool starting R2: ", p.pool_size())
print("remaining: ", p.remaining_coupons_array())
draw_graph(p, title="after R1 " + str(p.social_welfare()) + " " + str(p.pool_size()), pdf=pdf)
if p.pool_size() < p.agents:
print("pool size: ", p.pool_size(), " < agents: :", p.agents)
# allocation successfully
return True, p
# print("remaining: ", p.remaining_coupons_array())
r2(p, debug, remaining_array, pdf)
if debug:
nw2 = p.social_welfare()
p.reset_old_allocation()
p.create_envy_graph()
if nw2 <= nw1:
print("dec")
# return False
if not p.EFX_evaluate():
print("EFX")
# return False
print(p.allocation)
draw_graph(p, title="end of R2" + str(p.social_welfare()) + " " + str(
p.pool_size()), pdf=pdf)
print("stopped: ", stoped)
p.create_envy_graph()
draw_graph(p, pdf=pdf)
# print("cannot make source not source: ", sources)
def print_nash(nash_set, p, message):
nw = p.social_welfare()
print(message, nw)
def r1(p):
while True:
try:
while safely_add_item(p):
pass
p.create_envy_graph()
cycle = nx.find_cycle(p.nx_graph)
p.eliminate_cycle(cycle)
except:
return
def r2(p, debug, remaining_array, pdf=None):
old_allocation = np.copy(p.allocation)
edited_sources = set()
sources = p.source_nodes()
for s in sources:
stoped = True
if debug:
draw_graph(p,
title="add item to source " + str(s) + " " + str(p.social_welfare()) + " " + str(
p.pool_size()), pdf=pdf)
# print(p.allocation)
is_envy, champion_node = make_source_envied(p, s, remaining_array)
if debug:
# print(p.allocation)
draw_graph(p,
title="item added " + "champion: " + str(champion_node) + " " + str(s) + " " + str(
p.social_welfare()) + " " + str(
p.pool_size()), pdf=pdf)
stoped = not is_envy
self_champ = champion_node == s
if is_envy and not self_champ:
edited_sources.add(s)
if debug:
draw_graph(p, title="finding cycle " + str(p.social_welfare()) + " " + str(p.pool_size()), pdf=pdf)
success = find_eliminate_cycle(p)
if debug:
draw_graph(p, title="cycle found:" + str(success) + " " + str(p.social_welfare()) + " " + str(
p.pool_size()), pdf=pdf)
number_of_added = 1
# while not success and not p.EFX_evaluate():
while not success:
number_of_added += 1
s = p.node2source(champion_node)
if debug:
print(s, " ", champion_node)
draw_graph(p,
title="add item to source " + str(s) + " " + str(p.social_welfare()) + " " + str(
p.pool_size()), pdf=pdf)
# if r1(p):
# print("bug")
is_envy, champion_node = make_source_envied(p, s, remaining_array)
if debug:
draw_graph(p,
title="item added " + "champion: " + str(champion_node) + " " + str(s) + " " + str(
p.social_welfare()) + " " + str(
p.pool_size()), pdf=pdf)
if champion_node == s:
self_champ = True
stoped = False
cycle_nodes = [s]
for sour in p.self_allocation_delusion:
if sour not in cycle_nodes:
p.allocation[sour] = p.self_allocation_delusion[sour]
break
if not is_envy:
print("bbuugg", p.pool_size(), " ", number_of_added)
print("cannot make source not source: ", sources)
# is_envy, champion_node = make_source_envied(p, s)
return False
if debug:
draw_graph(p, title="finding cycle " + str(p.social_welfare()) + " " + str(p.pool_size()), pdf=pdf)
success = find_eliminate_cycle(p)
if debug:
draw_graph(p, title="cycle found:" + str(success) + " " + str(p.social_welfare()) + " " + str(
p.pool_size()), pdf=pdf)
# if np.equal(p.allocation.all(), old_allocation.all()):
# continue
break
if self_champ:
stoped = False
break
def safely_add_item(p):
for a in range(p.agents):
for t in p.order:
if p.allocation[a][t] != 1 and p.remaining_coupon(t) > 0:
p.allocation[a][t] = 1
p.create_envy_graph()
l = np.where(p.envy_graph == 2)
real = (len(l[0]) == 0)
if real is True:
return True
else:
p.allocation[a][t] = 0
return False
def make_source_envied(p: coupon_allocation, s, remaining_array):
global main_sources
if remaining_array.any() < 0:
print("remaining bug")
return False
if p.pool_size() == 0:
return False, None
for coupon in p.order:
# print("remaining: ", remaining_array)
if p.allocation[s][coupon] == 0 and remaining_array[coupon] > 0 and p.remaining_coupon(coupon) > 0:
# print("allocating item ", coupon, " to agent: ", s)
old_source = np.copy(p.allocation[s])
p.add_self_old_allocation(s, old_source)
p.allocation[s][coupon] = 1
remaining_array[coupon] -= 1
str_envying_nodes = set(p.strong_envy_nodes(s))
if len(str_envying_nodes) == 0:
print("bug")
continue
else:
bundle = p.allocation[s].copy()
# p.allocation[s] = old_source
str_envying_nodes.add(s)
champion_node, champion_set = p.champion_of_bundle(str_envying_nodes, s, bundle)
# if p.valuation(0,)
p.allocation[s] = champion_set
p.create_envy_graph()
return True, champion_node
print("del mode")
print(p.valuation_matrix)
order = p.order.copy()
for coupon in order:
if p.allocation[s][coupon] == 0:
old_source = np.copy(p.allocation[s])
p.add_self_old_allocation(s, old_source)
p.allocation[s][coupon] = 1
p.late_order(coupon)
remaining_array[coupon] -= 1
str_envying_nodes = set(p.strong_envy_nodes(s))
if len(str_envying_nodes) == 0:
print("bug")
continue
else:
bundle = p.allocation[s].copy()
# p.allocation[s] = old_source
str_envying_nodes.add(s)
champion_node, champion_set = p.champion_of_bundle(str_envying_nodes, s, bundle)
# if p.valuation(0,)
p.allocation[s] = champion_set
p.create_envy_graph()
return True, champion_node
return False, None
def find_eliminate_cycle(p):
r = False
try:
while True:
# p.create_envy_graph()
cycle = nx.find_cycle(p.nx_graph)
# print("remaining: ", p.remaining_coupons_array())
# print("cycle: ", cycle)
cycle_nodes = [n[0] for n in cycle]
for s in p.self_allocation_delusion:
if s not in cycle_nodes:
p.allocation[s] = p.self_allocation_delusion[s]
p.eliminate_cycle(cycle)
p.create_envy_graph()
# print("remaining: ", p.remaining_coupons_array())
# print("nash welfare cycle eliminated: ", p.nash_welfare())
r = True
break
return r
except:
return r
def find_del_indexes(p, s):
del_indexes = list(np.where(p.allocation[s] == 0)[0])
no_s_alloc = np.copy(p.allocation)
no_s_alloc = np.delete(no_s_alloc, s, axis=0)
r = []
for d in del_indexes:
ar = no_s_alloc[:, d]
x = np.all(ar == 0)
if np.all(no_s_alloc[:, d] == 1):
r.append(d)
return r