-
Notifications
You must be signed in to change notification settings - Fork 0
/
amkstyrant.py
195 lines (157 loc) · 7.2 KB
/
amkstyrant.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
#!/usr/bin/python
# This is a dummy peer that just illustrates the available information your peers
# have available.
# You'll want to copy this file to AgentNameXXX.py for various versions of XXX,
# probably get rid of the silly logging messages, and then add more logic.
from __future__ import division
import random
import logging
from messages import Upload, Request
from util import even_split
from peer import Peer
from collections import deque
class AmksTyrant(Peer):
def post_init(self):
print "post_init(): %s here!" % self.id
self.dummy_state = dict()
self.dummy_state["cake"] = "lie"
self.cap = self.up_bw
self.upload_rates = dict()
self.download_rates = dict()
self.ratios = dict()
self.unchoked = dict()
self.diff_available = dict()
self.last_available = dict()
self.alpha = 0.2
self.gamma = 0.1
self.r = 3
def requests(self, peers, history):
"""
peers: available info about the peers (who has what pieces)
history: what's happened so far as far as this peer can see
returns: a list of Request() objects
This will be called after update_pieces() with the most recent state.
"""
needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece
needed_pieces = filter(needed, range(len(self.pieces)))
np_set = set(needed_pieces) # sets support fast intersection ops.
logging.debug("%s here: still need pieces %s" % (
self.id, needed_pieces))
logging.debug("%s still here. Here are some peers:" % self.id)
for p in peers:
logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces))
logging.debug("And look, I have my entire history available too:")
logging.debug("look at the AgentHistory class in history.py for details")
logging.debug(str(history))
requests = []
# Symmetry breaking is good...
random.shuffle(needed_pieces)
# Sort peers by id. This is probably not a useful sort, but other
# sorts might be useful
peers.sort(key=lambda p: p.id)
piece_directory = {}
piece_to_peer = {}
num_requests = {}
for peer in peers:
num_requests[peer.id] = 0
av_set = set(peer.available_pieces)
isect = av_set.intersection(np_set)
for piece_id in list(isect):
if piece_id in piece_directory:
piece_to_peer[piece_id].append(peer)
piece_directory[piece_id] += 1
else:
piece_to_peer[piece_id] = [peer]
piece_directory[piece_id] = 1
rarest_pieces = []
for key, value in sorted(piece_directory.iteritems(), key=lambda (k,v): (v,k)):
rarest_pieces.append(key)
for piece in rarest_pieces:
for peer in piece_to_peer[piece]:
if num_requests[peer.id] < self.max_requests:
start_block = self.pieces[piece]
r = Request(self.id, peer.id, piece, start_block)
requests.append(r)
num_requests[peer.id] += 1
return requests
def uploads(self, requests, peers, history):
"""
requests -- a list of the requests for this peer for this round
peers -- available info about all the peers
history -- history for all previous rounds
returns: list of Upload objects.
In each round, this will be called after requests().
"""
round = history.current_round()
logging.debug("%s again. It's round %d." % (
self.id, round))
# Initialize u_j and d_j for all peers
if round == 0:
for peer in peers:
self.upload_rates[peer.id] = self.up_bw / 4
self.download_rates[peer.id] = 1
self.unchoked[peer.id] = 0
self.diff_available[peer.id] = 0
self.last_available[peer.id] = 0
if len(requests) == 0:
logging.debug("No one wants my pieces!")
chosen = []
bws = []
else:
chosen = []
bws = []
# Store peers who requested pieces from you
request_ids = []
for request in requests:
request_ids.append(request.requester_id)
# Order peers by decreasing reciprocation likelihood ratio
for peer in peers:
self.ratios[peer.id] = self.download_rates[peer.id] / self.upload_rates[peer.id]
total_up = 0
while total_up < self.cap:
greatest_ratio = max(self.ratios.values())
greatest_list = [key for key,value in self.ratios.items() if value == greatest_ratio]
choice = random.choice(greatest_list)
if (total_up + self.upload_rates[choice]) < self.cap:
if choice in request_ids:
chosen.append(choice)
bws.append(self.upload_rates[choice])
self.ratios.pop(choice)
total_up += self.upload_rates[choice]
# Update which peers have unchoked this agent
downloads_last = {}
if round != 0:
for download in history.downloads[round-1]:
if download.from_id not in downloads_last:
downloads_last[download.from_id] = download.blocks
else:
downloads_last[download.from_id] += download.blocks
for peer in peers:
if peer.id in downloads_last:
self.unchoked[peer.id] += 1
else:
self.unchoked[peer.id] = 0
# Update knowledge of downloads
for peer in peers:
self.diff_available[peer.id] = abs(len(peer.available_pieces) - self.last_available[peer.id])
# Update u_j and d_j for all unchoked peers
for peer in chosen:
if round != 0:
if self.unchoked[peer] == 0:
# Update u_j if peer choked
self.upload_rates[peer] = self.upload_rates[peer] * (1 + self.alpha)
# Update d_j if peer choked
self.download_rates[peer] = self.diff_available[peer]
else:
# Update d_j if peer unchoked
self.download_rates[peer] = downloads_last[peer]
# Update u_j if peer unchoked for less than r periods
if self.unchoked[peer] < self.r:
self.upload_rates[peer] = self.upload_rates[peer] * (1 + self.alpha)
# Update u_j if peer unchoked for more than r periods
else:
self.upload_rates[peer] = self.upload_rates[peer] * (1 - self.gamma)
# create actual uploads out of the list of peer ids and bandwidths
uploads = [Upload(self.id, peer_id, bw)
for (peer_id, bw) in zip(chosen, bws)]
return uploads