From 0bb8d9a467b09948de5783800e56fd390b88a1bd Mon Sep 17 00:00:00 2001 From: Nemo Biswas Date: Thu, 5 May 2016 13:55:15 -0400 Subject: [PATCH] latest update --- datk/core/algs.py | 33 +++++++++++++++++++++++++-------- datk/core/distalgs.py | 20 +++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/datk/core/algs.py b/datk/core/algs.py index e23b73a..70644af 100644 --- a/datk/core/algs.py +++ b/datk/core/algs.py @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/datk/core/distalgs.py b/datk/core/distalgs.py index 488732a..1ad25d5 100644 --- a/datk/core/distalgs.py +++ b/datk/core/distalgs.py @@ -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) @@ -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 @@ -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()