-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.py
267 lines (229 loc) · 9.85 KB
/
graph.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
#
# Authors: Joel, Tum
# Date: 2/11/2012
# Harvey Mudd College
# ICM Problem C
#
numNodes = 83
numTopics = 15
DEBUG = 0
DEBUG2 = 1
knownConsp = ['Jean','Alex','Elsie(7)','Paul','Ulf','Yao','Harvey']
knownNotConsp = ['Darlene','Tran','Ellin','Gard','Paige','Este','Chris']
def findLast(L1,L2):
'''Returns the index of the last element of L1 in L2
Assumes L1 is a subset of L2'''
indexMax = 0
for i in L1:
if L2.index(i) > indexMax:
indexMax = L2.index(i)
return indexMax
def findFirst(L1,L2):
'''Returns the index of the firs element of L1 in L2
Assumes L1 is a subset of L2'''
indexMin = len(L2)
for i in L1:
if L2.index(i) < indexMin:
indexMin = L2.index(i)
return indexMin
class Names:
def __init__(self, nameFile):
f = open(nameFile, "r")
self.people = []
for line in f.readlines():
Line = line.split()
self.people.append(Line[1])
def getNum(self, name):
return self.people.index(name)
def getName(self, num):
return self.people[num]
def numsToNames(self, List):
return map(self.getName, List)
def namesToNum(self, List):
return map(self.getNum, List)
class Graph:
def __init__(self,messageFile):
self.fullList = []
self.D = {}
self.notConspSend = -0.25
self.notConspRec = -0.50
self.conspSend = 0.50
self.conspRec = 0.25
self.certaintyNotConsp = [0]*numNodes
self.certaintyConsp = [.5]*numNodes
self.topicCount = [0]*(numTopics+1)
self.topicWeight = [.1, #Not in use\
.1, #1\
.1, #2\
.1, #3\
.1, #4\
.1, #5\
.1, #6\
.95, #7\
.1, #8\
.1, #9\
.1, #10\
.95, #11\
.1, #12\
.95, #13\
.1, #14\
.1] #15
f = open(messageFile, 'r')
for line in f.readlines():
Nums = line.split()
Node1 = int(Nums[0])
Node2 = int(Nums[1])
if not self.D.has_key((Node1,Node2)):
self.D[(Node1,Node2)] = []
for i in Nums[2:]:
self.D[(Node1,Node2)] += [int(i)]
self.topicCount[int(i)]+=1
f.close()
def weightConspirator(self, node, certainty = 1):
'''Changes the weights of the topics based (using a given person)
The effect is scaled with certainty'''
for i in range(numNodes):
if self.D.has_key((node,i)):
for j in self.D[(node,i)]:
effectiveCoeff = self.notConspSend+\
(self.conspSend-self.notConspSend)*certainty
x = 1-self.topicWeight[j]
#self.topicWeight[j]+= certainty * self.conspRec
if(DEBUG):
print "Send: certainty = " + str(certainty)
print "effectiveCoef = " + str(effectiveCoeff)
print " Before: topic " + str(j)+ "'s weight = " + str(self.topicWeight[j])
self.topicWeight[j] += min(self.topicWeight[j],x)*effectiveCoeff
if(DEBUG):
print " After: topic " + str(j)+ "'s weight = " + str(self.topicWeight[j])
#self.topicWeight[j]+= certainty * self.conspSend
if self.D.has_key((i,node)):
for j in self.D[(i,node)]:
effectiveCoeff = self.notConspRec+\
(self.conspRec-self.notConspRec)*certainty
x = 1-self.topicWeight[j]
if(DEBUG):
print "Recieve: certainty = " + str(certainty)
print "effectiveCoef = " + str(effectiveCoeff)
print " Before: topic " + str(j)+ "'s weight = " + str(self.topicWeight[j])
#self.topicWeight[j]+= certainty * self.conspRec
self.topicWeight[j] += min(self.topicWeight[j],x)*effectiveCoeff
if(DEBUG):
print " After: topic " + str(j)+ "'s weight = " + str(self.topicWeight[j])
"""def weightNotConspirator(self, node, certainty = 1):
'''Changes the weights of the topics based (using a given person)
The effect is scaled with certainty'''
for i in range(numNodes):
if self.D.has_key((node,i)):
for j in self.D[(node,i)]:
x = self.topicWeight[j]
self.topicWeight[i] -= x*self.consSend*certainty
#self.topicWeight[j]+= (1-certainty) * self.notConspSend
if self.D.has_key((i,node)):
for j in self.D[(i,node)]:
self.topicWeight[j]+= (1-certainty) * self.notConspRec"""
def numSent(self, node, topic):
'''Returns the number of times a person sent a message pertaining
to the specified topic'''
count = 0
for i in range(numNodes):
if self.D.has_key((node,i)):
for j in self.D[(node,i)]:
if j == topic: count+=1
return count
def numRec(self, node, topic):
'''Returns the number of times a person receives a message pertaining
to the specified topic'''
count = 0
for i in range(numNodes):
if self.D.has_key((i, node)):
for j in self.D[(i, node)]:
if j == topic: count+=1
return count
def runFirstRound(self):
'''Returns an orderd list of people from most suspicious to least'''
for i in knownConsp:
self.weightConspirator(i)
for i in knownNotConsp:
self.weightConspirator(i,0)
L = map(self.scoreReport, range(numNodes))
L.sort(key = lambda X: X[1], reverse = True)
self.fullList = map(lambda X:X[0], L)
return L
def runLaterRound(self):
for i in knownConsp:
self.certaintyConsp[i] = 1
for i in knownNotConsp:
self.certaintyConsp[i] = 0
if(DEBUG2):
print "last = " + str(findLast(knownConsp, self.fullList))
print "first = " + str(findFirst(knownNotConsp, self.fullList))
for i in self.fullList[:findLast(knownConsp, self.fullList)+1]:
#Every possible conspirator
if (knownNotConsp.count(i) == 0 and knownConsp.count(i) == 0):
if(DEBUG):
print "Before: person " + str(i)+ "'s certainty = " + str(self.certaintyConsp[i])
alpha = 0.7
self.certaintyConsp[i] = self.certaintyConsp[i]*(1-alpha) + 1*alpha
if(DEBUG):
print " After: person " + str(i)+ "'s certainty = " + str(self.certaintyConsp[i])
for i in self.fullList[findFirst(knownNotConsp, self.fullList):]:
#Every likely non-conspirator
if (knownNotConsp.count(i) == 0 and knownConsp.count(i) == 0):
if(DEBUG):
print "Before: person " + str(i)+ "'s certainty = " + str(self.certaintyConsp[i])
alpha = 0.1
self.certaintyConsp[i] = self.certaintyConsp[i]*(1-alpha) + 0*alpha
if(DEBUG):
print " After: person " + str(i)+ "'s certainty = " + str(self.certaintyConsp[i])
for i in range(numNodes):
self.weightConspirator(i, self.certaintyConsp[i])
#self.weightNotConspirator(i, self.certaintyConsp[i])
L = map(self.scoreReport, range(numNodes))
#for i in range(numNodes):
# L[i].append(self.certaintyConsp[i])
L.sort(key = lambda X: X[1], reverse = True)
self.fullList = map(lambda X:X[0], L)
return (L, self.topicWeight)
def scoreReport(self, node):
'''Return [node, score]'''
score = 0.0
for topic in range(1, numTopics + 1):
score += self.topicWeight[topic]*(self.numRec(node, topic)+\
self.numSent(node, topic)+0.0)\
/self.topicCount[topic]
return [node, score, self.certaintyConsp[node]]
def nicePrintList(L):
print "{0:5}{1:13}{2:9}{3:7}".format("Rank".ljust(5),"Name".center(13),"Score".center(6),"Certainty".rjust(7)),'\n'
for i in range(len(L)):
print "{0:5}{1:13}{2:6.3f}{3:9.4f}".format(str(i).ljust(5),L[i][0],L[i][1],L[i][2])
def nicePrintTopics(L):
print "{0:5}{1:7}".format("Topic".ljust(5),"Weight".center(7))
for i in range(1,len(L)):
print "{0}{1:5.4f}".format(str(i).ljust(5),L[i])
def main():
global knownConsp
global knownNotConsp
names = Names('names.txt')
graph = Graph('messages.txt')
knownConsp = names.namesToNum(knownConsp)
knownNotConsp = names.namesToNum(knownNotConsp)
L = graph.runFirstRound()
temp = 1
topics = 0
for i in range(5):
if(DEBUG):
print "-----------------------------------------------------"
print "Round " + str(i)
[L, topics] = graph.runLaterRound()
temp = map(lambda X:[names.getName(X[0]),X[1], X[2]] , L)
for j in range(len(temp)):
temp[j][2] = round(temp[j][2],2)
if(DEBUG):
print nicePrintList(temp)
nicePrintList(temp)
print "--------------------------------------------------"
nicePrintTopics(topics)
#print map(lambda X:X[0],temp)
print "--------------------------------------------------"
if __name__ == '__main__': main()