-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
191 lines (156 loc) · 5.43 KB
/
utils.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
"""This module contains utility functions that are used by other modules."""
import collections
import pickle
import socket
import numpy as np
Relatives = collections.namedtuple('Relatives',
'parent pseudoparents children pseudochildren')
def draw_graph(graph, pstree):
"""Draw the tree structure, given a 'graph' and its 'pstree'."""
raise NotImplementedError
def get_agents_info(filepath):
"""
Return a dict with that has all the information extracted from a file like
'agents.txt'.
"""
f = open(filepath)
agents_info = {}
for line in f:
if line.split() == [] or line[0] == '#':
continue
entries = dict([tuple(entry.split("=")) for entry in line.split()])
id = int(entries['id'])
del entries['id']
agents_info[id] = entries
return agents_info
def listen_func(msgs, sock, agent):
"""
Continuously listens on the IP and Port specified in 'sock', and stores the
messages in the dict 'msgs', until an 'exit' message is received. See
comments in the source code for more information.
"""
if agent == None:
agent_id = 'No agent'
else:
agent_id = agent.id
print str(agent_id) + ': Begin listen_func'
while True:
# The 'data' which is received should be the pickled string
# representation of a tuple.
# The first element of the tuple should be the data title, a name given
# to describe the data.
# This first element will become the key of the 'msgs' dict.
# The second element should be the actual data to be passed.
# Loop ends when an exit message is sent.
data, addr = sock.recvfrom(65536)
udata = pickle.loads(data) # Unpickled data
msgs[udata[0]] = udata[1]
print str(agent_id) + ': Msg received, ' + udata[0] + ": " + str(udata[1])
if str(udata[1]) == "exit":
print str(agent_id)+': End listen_func'
return
print str(agent.id) + ': End listen_func'
def combine(*args):
"""Return the combined array, given n numpy arrays and their corresponding
n assignment-nodeid-tuples ('ants')."""
largs = len(args)
arrays = args[:largs/2]
ants = args[largs/2:]
# Calculate the new shape
D = {}
for arr, ant in zip(arrays, ants):
shape = arr.shape
for nodeid, depth in zip(ant, shape):
if nodeid in D:
continue
else:
D[nodeid] = depth
new_shape = tuple([D[key] for key in sorted(D)])
# Calculate the merged ant
merged_ant = set()
for ant in ants:
merged_ant = merged_ant | set(ant)
merged_ant = tuple(sorted(tuple(merged_ant)))
merged_array, _ = expand(arrays[0], ants[0], merged_ant, new_shape)
for array, ant in zip(arrays[1:], ants[1:]):
new_array, _ = expand(array, ant, merged_ant, new_shape)
merged_array += new_array
return (merged_array, merged_ant)
def expand(array, ant, new_ant, new_shape):
"""Return the new numpy array after expanding it so that its ant changes
from 'ant' to 'new_ant'. The value of the new elements created will be
initialized to 0."""
# The values of the nodeids in ant and new_ant must be sorted.
# Insertion sort is used as there is already an inbuilt function in numpy
# to swap axes.
ant = list(ant)
for j in range(len(ant) - 1):
i_min = j
for i in range(j+1, len(ant)):
if ant[i] < ant[i_min]:
i_min = i
if i_min != j:
array = np.swapaxes(array, i_min, j)
ant[j], ant[i_min] = ant[i_min], ant[j]
ant = tuple(ant)
a = array.copy()
x = y = -1
i = j = 0
end_of_ant_reached = False
while(i != len(new_ant)):
x = new_ant[i]
try:
y = ant[j]
except:
end_of_ant_reached = True
if x < y:
a, ant = add_dims(a, ant, j, x, new_shape[i])
i += 1
j += 1
continue
elif x > y:
if not end_of_ant_reached:
j += 1
else:
a, ant = add_dims(a, ant, j, x, new_shape[i])
i += 1
j += 1
continue
else: # x == y
i += 1
j += 1
continue
# Checking if ant has changed properly
assert ant == new_ant
return (a, ant)
def add_dims(array, ant, index, nodeid, depth):
"""Return a numpy array with an additional dimension in the place of index.
The values of all additional elements created are 0. The depth of the
'nodeid' is given by 'depth'."""
assert ant == tuple(sorted(ant))
# a = array.copy()
# a = np.expand_dims(a, axis=index)
# zeros_dim = list(a.shape)
# zeros_dim[index] = depth - 1
# zeros_dim = tuple(zeros_dim)
# zeros = np.zeros(zeros_dim, dtype=int)
# a = np.concatenate((a, zeros), axis=index)
# new_ant = list(ant)
# new_ant.insert(index, nodeid)
# new_ant = tuple(new_ant)
# return (a, new_ant)
a = array.copy()
a = np.expand_dims(a, axis=index)
new_a = a
for _ in range(depth-1):
new_a = np.concatenate((new_a, a), axis=index)
new_ant = list(ant)
new_ant.insert(index, nodeid)
new_ant = tuple(new_ant)
return (new_a, new_ant)
def prod(S):
"""Returns the product of all elements in a sequence S."""
product = 1
for i in S:
product = product * i
return product