Skip to content

Commit

Permalink
latest update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo Biswas committed May 5, 2016
1 parent 7f2b30a commit 0bb8d9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
33 changes: 25 additions & 8 deletions datk/core/algs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def get_draw_args(self,network,vals):
node_colors = dict()
edge_colors = None
for p in network.processes:
v = vals[p.UID] # IMPORTANT: check to make sure this is right!
# v = vals[p.UID] # IMPORTANT: check to make sure this is right!
# if self.has(p, "decided"):
if p.state['status'] == "leader":
node_colors[v] = 'ro'
node_colors[p.UID] = 'ro'

elif p.state['status'] == "non-leader": # non-leader
node_colors[v] = 'bo'
node_colors[p.UID] = 'bo'

# algoDrawArgs = AlgorithmDrawArgs(node_colors = node_colors, edge_colors = edge_colors)
return node_colors, edge_colors
Expand All @@ -26,10 +26,9 @@ def get_draw_args(self,network,vals):
node_colors = None
edge_colors = dict()
for p in network.processes:
v = vals[p.UID]
if p.state['parent']:
v_parent = vals[p.state['parent']]
edge_colors[(v,v_parent)] = 'r'
parent_UID = p.state['parent'].UID
edge_colors[(p.UID,parent_UID)] = 'r'

return node_colors, edge_colors

Expand Down Expand Up @@ -78,6 +77,23 @@ def trans_i(self, p, msgs):
if self.r == p.state['n']: p.terminate(self)


def get_draw_args(self,network,vals):
"""network - refers to the network on which the algorithm is running.
vals - the positions of the nodes in the network"""
node_colors = dict()
edge_colors = None
for p in network.processes:
# if self.has(p, "decided"):
if p.state['status'] == "leader":
node_colors[p.UID] = 'ro'

elif p.state['status'] == "non-leader": # non-leader
node_colors[p.UID] = 'bo'

# algoDrawArgs = AlgorithmDrawArgs(node_colors = node_colors, edge_colors = edge_colors)
return node_colors, edge_colors



class AsyncLCR(Asynchronous_Algorithm):
"""The LeLann, Chang and Roberts algorithm for Leader Election in an Asynchronous Ring Network
Expand Down Expand Up @@ -349,6 +365,7 @@ def msgs_i(self, p):

def trans_i(self, p, msgs):
if len(msgs) > 0:

msg = msgs[0] # modify this
if (self.r - 1)/p.state['n'] == msg.content-1 and not self.has(p,"decided"):
self.set(p, 'decided', None)
Expand All @@ -365,7 +382,7 @@ class SynchVariableSpeeds(Synchronous_Algorithm):
pass

#Construct BFS Tree
class SynchBFS(Synchronous_Algorithm):
class SynchBFS(BFS_Synchronous_Algorithm):
"""Constructs a BFS tree with the 'leader' Process at its root
At any point during execution, there is some set of processes that is
"marked," initially just i0. Process i0 sends out a search message at
Expand Down Expand Up @@ -402,7 +419,7 @@ def trans_i(self, p, msgs):
if self.has(p, "recently_marked"): self.delete(p, "recently_marked")
p.terminate(self)

class SynchBFSAck(Synchronous_Algorithm):
class SynchBFSAck(BFS_Synchronous_Algorithm):
"""Constructs a BFS tree with children pointers and the 'leader' Process at its root
Algorithm (Informal):
At any point during execution, there is some set of processes that is
Expand Down
20 changes: 15 additions & 5 deletions datk/core/distalgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def terminate(self, algorithm):
"""Causes the Process to halt execution of algorithm"""
if algorithm in self.algs:
self.algs.remove(algorithm)
print self.state[algorithm]
# print algorithm.has(self,"decided")

def __str__(self):
return "P"+str(self.UID)
Expand All @@ -174,10 +176,15 @@ def __init__(self, n, index_to_UID = None):
"""
self.algs = []
if index_to_UID is None:
self.processes = [Process(i) for i in range(n)]
shuffle(self.processes)
proc_ids = range(n)
shuffle(proc_ids)
process2uid = dict(zip(range(n),proc_ids))
self.processes = [Process(process2uid[i]) for i in range(n)]
self.uid2process = dict(zip(proc_ids,range(n)))
# shuffle(self.processes)
else:
self.processes = [Process(index_to_UID(i)) for i in range(n)]
self.uid2process = dict(zip(range(n),[index_to_UID(i) for i in range(n)]))
for process in self:
process.state['n'] = n

Expand Down Expand Up @@ -238,11 +245,14 @@ def line(v1, v2,color='k'):
for alg in self.algs:
node_colors, edge_colors = alg.get_draw_args(self,vals)
if node_colors:
for node,node_color in node_colors.iteritems():
plt.plot( [node[0]], [node[1]], node_color)
for p_UID,node_color in node_colors.iteritems():
v = vals[self.uid2process[p_UID]]
plt.plot( [v[0]], [v[1]], node_color)

if edge_colors:
for (v1,v2),edge_color in edge_colors.iteritems():
for (p_UID,parent_UID),edge_color in edge_colors.iteritems():
v1 = vals[self.uid2process[p_UID]]
v2 = vals[self.uid2process[parent_UID]]
line(v1,v2,color=edge_color)

plt.show()
Expand Down

0 comments on commit 0bb8d9a

Please sign in to comment.