Skip to content

Commit 25deb7c

Browse files
committedFeb 18, 2025
Initial commit of local files
1 parent 6cb34be commit 25deb7c

17 files changed

+1404
-0
lines changed
 

‎.DS_Store

6 KB
Binary file not shown.

‎boltzmann_explr_opin.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import numpy as np
2+
import pdb
3+
4+
def boltzmann_explr_opin(qList, incomingNodeIndex, gList, tempVal, seedIn):
5+
6+
np.random.seed(seedIn)
7+
if incomingNodeIndex != []:
8+
qList[incomingNodeIndex] = 0
9+
maxQ = max(qList)
10+
maxQTmp = maxQ/tempVal
11+
MAXQTMPALLOWED = 500
12+
MAXQALLOWED = MAXQTMPALLOWED*tempVal
13+
if maxQTmp>MAXQTMPALLOWED:
14+
subtractQ = maxQ-MAXQALLOWED
15+
qListNew = [xx-subtractQ for xx in qList]
16+
print("Q", end=' ')
17+
elif maxQ<0:
18+
qListNew = [xx-((maxQ)*tempVal) for xx in qList]
19+
pdb.set_trace()
20+
else:
21+
qListNew = qList
22+
23+
expQList = [np.exp(x/tempVal) for x in qListNew]
24+
pDenom = float(sum(expQList))
25+
26+
#if pDenom == 0: # often encountered in local cooperation case with exp(.) scaling factor
27+
# pDenom = 1 # helps in case of local cooperation with exp(.) scaling factor
28+
# for ii in range(len(expQList)):
29+
# expQList[ii] = 1.0/len(expQList)
30+
31+
pList = [float(x)/pDenom for x in expQList]
32+
33+
rndIdx = np.random.choice(len(pList),1,p=pList)
34+
35+
actionIdx = rndIdx[0]
36+
action = gList[actionIdx]
37+
38+
return [action, actionIdx]

‎compute_prob.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
from normalize_vector import normalize_vector
3+
import pdb
4+
5+
def compute_prob(vecIn1, vecIn2, t, eta, xi):
6+
7+
# find minimum tau
8+
vecIn1Delta = np.add(vecIn1, -t) # vecIn1Delta is negative number
9+
10+
minVecIn2 = np.min(vecIn2)
11+
12+
#noTxTmp = (np.random.rand(1,1)>np.exp(-10*(minVecIn2))) # for debug
13+
#noTxTmp = (np.random.rand(1,1)>np.exp(0*(minVecIn2))) # for debug # this with vecInNeg2 factor = -3 works with pa graph
14+
#noTxTmp = (np.random.rand(1,1)>np.exp(-1*(minVecIn2))) # for debug # this with vecInNeg2 factor = -1 works with pa graph for batchsize of 20
15+
noTxTmp = (np.random.rand(1,1)>np.exp(-eta*(minVecIn2)))
16+
noTx = noTxTmp[0][0]
17+
18+
#noTx = False # for debug # works best
19+
20+
if noTx == False:
21+
22+
#vecInNeg1 = np.multiply(vecIn1Delta, 1) # works best
23+
#vecInNeg2 = np.multiply(vecIn2, 0.) # works best
24+
25+
vecInNeg1 = np.multiply(vecIn1Delta, xi)
26+
vecInNeg2 = np.multiply(vecIn2, eta)
27+
28+
vecInNeg = np.add(vecInNeg1, vecInNeg2)
29+
vecNegExp = np.exp(vecInNeg)
30+
probVec = normalize_vector(vecNegExp)
31+
else:
32+
probVec = []
33+
34+
##if noTx == False and minVecIn2>0 and len(vecIn1)>1:
35+
#if noTx == False and len(vecIn1)>1:
36+
# print(sorted(vecIn1Delta, reverse=True))
37+
38+
#if len(vecIn1)>1:
39+
# pdb.set_trace()
40+
41+
return [probVec, noTx]

‎display_centralities.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import networkx as nx
2+
import pdb
3+
import matplotlib.pyplot as plt
4+
5+
def display_centralities(gnx, srcQ, srcR):
6+
7+
8+
cntraltyEigVec = nx.eigenvector_centrality(gnx, max_iter=100, tol=1e-06, nstart=None, weight='weight')
9+
maxCntraltyEigVec = max(cntraltyEigVec.values())
10+
minCntraltyEigVec = min(cntraltyEigVec.values())
11+
print('srcQNode = ' + str(srcQ) + ', Eigenvector Centrality = ' + str(cntraltyEigVec[srcQ]))
12+
print('srcRNode = ' + str(srcR) + ', Eigenvector Centrality = ' + str(cntraltyEigVec[srcR]))
13+
print('Max = ' + str(maxCntraltyEigVec) + ', Min = ' + str(minCntraltyEigVec))
14+
15+
#print(" ")
16+
#
17+
#cntraltyBtw = nx.betweenness_centrality(gnx, k=None, normalized=True, weight=None, endpoints=False, seed=None)
18+
#maxCntraltyBtw = max(cntraltyBtw.values())
19+
#minCntraltyBtw = min(cntraltyBtw.values())
20+
#print('srcQNode = ' + str(srcQ) + ', Betweenness Centrality = ' + str(cntraltyBtw[srcQ]))
21+
#print('srcRNode = ' + str(srcR) + ', Betweenness Centrality = ' + str(cntraltyBtw[srcR]))
22+
#print('Max = ' + str(maxCntraltyBtw) + ', Min = ' + str(minCntraltyBtw))
23+
24+
return [cntraltyEigVec]
25+
26+

‎dump_graph_script.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from gen_graph import gen_graph
2+
import numpy as np
3+
4+
Params = {};
5+
Params['graphName'] = 'pa'
6+
#Params['graphName'] = 'grid2d'
7+
#Params['graphName'] = 'wxrnd'
8+
#Params['graphName'] = 'rgg'
9+
#Params['graphName'] = 'plclust'
10+
11+
if Params['graphName'] == 'pa':
12+
Params['numNodes'] = 100000
13+
Params['mVal'] = 3
14+
elif Params['graphName'] == 'grid2d':
15+
Params['numNodesRow'] = 10
16+
Params['numNodesCol'] = 10
17+
Params['numNodes'] = Params['numNodesRow']*Params['numNodesCol']
18+
elif Params['graphName'] == 'rgg':
19+
Params['numNodes'] = 1000
20+
#Params['rggRad'] = 0.08
21+
#Params['rggRad'] = 0.17
22+
Params['rggRad'] = 0.07
23+
elif Params['graphName'] == 'plclust':
24+
Params['numNodes'] = 500
25+
Params['mVal'] = 2
26+
Params['pTriangle'] = 0.5
27+
elif Params['graphName'] == 'wxrnd':
28+
Params['numNodes'] = 500
29+
Params['alphaWxRnd'] = 0.045
30+
Params['betaWxRnd'] = 0.7
31+
32+
(g, numNbrDict, gnx) = gen_graph(Params)
33+
34+
dumpVarNames = ['g','numNbrDict','gnx']
35+
dumpVarVals = [g, numNbrDict, gnx]
36+
37+
dumpVar = []
38+
dumpVar.append(dumpVarNames)
39+
dumpVar.append(dumpVarVals)
40+
41+
## save results
42+
savePath = 'graphDump'
43+
graphName = Params['graphName']
44+
45+
if Params['graphName'] == 'pa':
46+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"mVal"+str(Params['mVal'])
47+
elif Params['graphName'] == 'grid2d':
48+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"row"+str(Params['numNodesRow'])+"col"+str(Params["numNodesCol"])
49+
elif Params['graphName'] == 'rgg':
50+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"rggRad"+str(Params['rggRad'])
51+
elif Params['graphName'] == 'plclust':
52+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"mVal"+str(Params['mVal'])+"pTriangle"+str(Params['pTriangle'])
53+
elif Params['graphName'] == 'wxrnd':
54+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"alphaWxRnd"+str(Params['alphaWxRnd'])+"betaWxRnd"+str(Params['betaWxRnd'])
55+
56+
np.save(fileName, dumpVar)
57+

‎gen_graph.py

+259
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# Function to generate graphs given topological parameters
2+
# Input: Params
3+
# Outputs: gn - graph as list of neighbors
4+
# numNbrDict - dictionary of number of neighbors of each node
5+
# g - networkx graph
6+
7+
import networkx as nx
8+
import numpy as np
9+
import pdb
10+
import copy
11+
12+
def gen_graph(Params):
13+
# 2D grid graph
14+
if Params['graphName'] == 'grid2d':
15+
#g = nx.grid_2d_graph(Params['numNodesRow'], Params['numNodesCol'])
16+
g = nx.grid_graph(dim=[Params['numNodesRow'], Params['numNodesCol']])
17+
# graph as neighbor list
18+
gn = {}
19+
nnList = []
20+
numNbrDict = {}
21+
gnodes = [xx for xx in g.nodes()]
22+
23+
for ind in range(g.number_of_nodes()):
24+
nodeRow = gnodes[ind][0]
25+
nodeCol = gnodes[ind][1]
26+
nodeIndex = nodeRow*Params['numNodesRow']+nodeCol
27+
neighborList = [xx for xx in nx.neighbors(g, gnodes[ind])]
28+
for indnn in range(len(neighborList)):
29+
nnList.append(neighborList[indnn][0]*Params['numNodesRow']+neighborList[indnn][1])
30+
31+
# check which one is convenient
32+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
33+
gn[nodeIndex] = copy.copy(nnList) # gn as dictionary of lists
34+
numNbrDict[nodeIndex] = len(nnList)
35+
del nnList[:]
36+
37+
# 3D grid graph
38+
elif Params['graphName'] == 'grid3d':
39+
g = nx.grid_graph(dim=[Params['numNodesDim1'], Params['numNodesDim2'], Params['numNodesDim3']])
40+
# graph as neighbor list
41+
gn = {}
42+
nnList = []
43+
numNbrDict = {}
44+
gnodes = [xx for xx in g.nodes()]
45+
for ind in range(g.number_of_nodes()):
46+
nodeDim1 = gnodes[ind][0]
47+
nodeDim2 = gnodes[ind][1]
48+
nodeDim3 = gnodes[ind][2]
49+
nodeIndex = nodeDim1*Params['numNodesDim2']*Params['numNodesDim3']+nodeDim2*Params['numNodesDim3']+nodeDim3
50+
neighborList = nx.neighbors(g, gnodes[ind])
51+
for indnn in range(len(neighborList)):
52+
nnList.append(neighborList[indnn][0]*Params['numNodesDim2']*Params['numNodesDim3']+neighborList[indnn][1]*Params['numNodesDim3']+neighborList[indnn][2])
53+
54+
# check which one is convenient
55+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
56+
gn[nodeIndex] = copy.copy(nnList) # gn as dictionary of lists
57+
numNbrDict[nodeIndex] = len(nnList)
58+
del nnList[:]
59+
60+
# Random Geometric Graph
61+
elif Params['graphName'] == 'rgg':
62+
g = nx.random_geometric_graph(Params['numNodes'], radius = Params['rggRad'])
63+
# graph as neighbor list
64+
gn = {}
65+
nnList = []
66+
numNbrDict = {}
67+
for ind in range(g.number_of_nodes()):
68+
#neighborList = nx.neighbors(g, g.nodes()[ind])
69+
neighborListPrime = nx.neighbors(g, ind)
70+
neighborList = [x for x in neighborListPrime]
71+
for indnn in range(len(neighborList)):
72+
nnList.append(neighborList[indnn])
73+
74+
# check which one is convenient
75+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
76+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
77+
numNbrDict[ind] = len(nnList)
78+
del nnList[:]
79+
80+
# for ind in range(Params['numNodes']):
81+
# print(gn[ind])
82+
83+
# Power-law tree graph
84+
elif Params['graphName'] == 'pltree':
85+
g = nx.random_powerlaw_tree(Params['numNodes'], gamma=Params['pltreegamma'], seed=None, tries = 10000)
86+
# graph as neighbor list
87+
gn = {}
88+
nnList = []
89+
numNbrDict = {}
90+
for ind in range(g.number_of_nodes()):
91+
neighborListPrime = nx.neighbors(g, ind)
92+
neighborList = [x for x in neighborListPrime]
93+
for indnn in range(len(neighborList)):
94+
nnList.append(neighborList[indnn])
95+
96+
# check which one is convenient
97+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
98+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
99+
numNbrDict[ind] = len(nnList)
100+
del nnList[:]
101+
102+
# Preferential attachment graph
103+
elif Params['graphName'] == 'pa':
104+
g = nx.barabasi_albert_graph(n=Params['numNodes'], m=Params['mVal'], seed=None)
105+
# graph as neighbor list
106+
gn = {}
107+
nnList = []
108+
numNbrDict = {}
109+
for ind in range(g.number_of_nodes()):
110+
neighborListPrime = nx.neighbors(g, ind)
111+
neighborList = [x for x in neighborListPrime]
112+
for indnn in range(len(neighborList)):
113+
nnList.append(neighborList[indnn])
114+
115+
# check which one is convenient
116+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
117+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
118+
numNbrDict[ind] = len(nnList)
119+
del nnList[:]
120+
121+
elif Params['graphName'] == 'er':
122+
pEr = (2*np.log(Params['numNodes']))/Params['numNodes']
123+
g = nx.gnp_random_graph(Params['numNodes'], p=pEr, seed=None, directed=False)
124+
# graph as neighbor list
125+
gn = {}
126+
nnList = []
127+
numNbrDict = {}
128+
for ind in range(g.number_of_nodes()):
129+
neighborListPrime = nx.neighbors(g, ind)
130+
neighborList = [x for x in neighborListPrime]
131+
for indnn in range(len(neighborList)):
132+
nnList.append(neighborList[indnn])
133+
134+
# check which one is convenient
135+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
136+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
137+
numNbrDict[ind] = len(nnList)
138+
del nnList[:]
139+
140+
# graph as neighbor list
141+
gn = {}
142+
nnList = []
143+
numNbrDict = {}
144+
for ind in range(g.number_of_nodes()):
145+
neighborListPrime = nx.neighbors(g, ind)
146+
neighborList = [x for x in neighborListPrime]
147+
for indnn in range(len(neighborList)):
148+
nnList.append(neighborList[indnn])
149+
150+
# check which one is convenient
151+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
152+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
153+
numNbrDict[ind] = len(nnList)
154+
del nnList[:]
155+
156+
elif Params['graphName'] == 'wxrnd':
157+
g = nx.waxman_graph(Params['numNodes'], alpha=Params['alphaWxRnd'], beta=Params['betaWxRnd'], L=None, domain=(0,0,1,1))
158+
# graph as neighbor list
159+
gn = {}
160+
nnList = []
161+
numNbrDict = {}
162+
for ind in range(g.number_of_nodes()):
163+
neighborListPrime = nx.neighbors(g, ind)
164+
neighborList = [x for x in neighborListPrime]
165+
for indnn in range(len(neighborList)):
166+
nnList.append(neighborList[indnn])
167+
168+
# check which one is convenient
169+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
170+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
171+
numNbrDict[ind] = len(nnList)
172+
del nnList[:]
173+
174+
# graph as neighbor list
175+
gn = {}
176+
nnList = []
177+
numNbrDict = {}
178+
for ind in range(g.number_of_nodes()):
179+
neighborListPrime = nx.neighbors(g, ind)
180+
neighborList = [x for x in neighborListPrime]
181+
for indnn in range(len(neighborList)):
182+
nnList.append(neighborList[indnn])
183+
184+
# check which one is convenient
185+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
186+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
187+
numNbrDict[ind] = len(nnList)
188+
del nnList[:]
189+
190+
elif Params['graphName'] == 'plclust':
191+
g = nx.powerlaw_cluster_graph(Params['numNodes'], m=Params['mVal'],p=Params['pTriangle'], seed=None)
192+
# graph as neighbor list
193+
gn = {}
194+
nnList = []
195+
numNbrDict = {}
196+
for ind in range(g.number_of_nodes()):
197+
neighborListPrime = nx.neighbors(g, ind)
198+
neighborList = [x for x in neighborListPrime]
199+
for indnn in range(len(neighborList)):
200+
nnList.append(neighborList[indnn])
201+
202+
# check which one is convenient
203+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
204+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
205+
numNbrDict[ind] = len(nnList)
206+
del nnList[:]
207+
208+
# graph as neighbor list
209+
gn = {}
210+
nnList = []
211+
numNbrDict = {}
212+
for ind in range(g.number_of_nodes()):
213+
neighborListPrime = nx.neighbors(g, ind)
214+
neighborList = [x for x in neighborListPrime]
215+
for indnn in range(len(neighborList)):
216+
nnList.append(neighborList[indnn])
217+
218+
# check which one is convenient
219+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
220+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
221+
numNbrDict[ind] = len(nnList)
222+
del nnList[:]
223+
224+
elif Params['graphName'] == 'dgm':
225+
g = nx.dorogovtsev_goltsev_mendes_graph(Params['depthParam'], create_using=None)
226+
# graph as neighbor list
227+
gn = {}
228+
nnList = []
229+
numNbrDict = {}
230+
for ind in range(g.number_of_nodes()):
231+
neighborListPrime = nx.neighbors(g, ind)
232+
neighborList = [x for x in neighborListPrime]
233+
for indnn in range(len(neighborList)):
234+
nnList.append(neighborList[indnn])
235+
236+
# check which one is convenient
237+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
238+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
239+
numNbrDict[ind] = len(nnList)
240+
del nnList[:]
241+
242+
# graph as neighbor list
243+
gn = {}
244+
nnList = []
245+
numNbrDict = {}
246+
for ind in range(g.number_of_nodes()):
247+
neighborListPrime = nx.neighbors(g, ind)
248+
neighborList = [x for x in neighborListPrime]
249+
for indnn in range(len(neighborList)):
250+
nnList.append(neighborList[indnn])
251+
252+
# check which one is convenient
253+
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
254+
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
255+
numNbrDict[ind] = len(nnList)
256+
del nnList[:]
257+
258+
return (gn, numNbrDict, g)
259+

‎get_params_list2plot_qopin.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#srcQNode = 900 # worst case
2+
#srcQNode = 46 # best case - high degree and second highest eigvec centrality
3+
#srcQNode = 79 # high degree - low eigvec centrality
4+
#srcRNode = 5 # Hub
5+
6+
## Low deg, low eigvec
7+
#iterList = [0]
8+
#sim = {}
9+
#savePath = "simRes"
10+
#saveFileNameHead = "2018_04_04"
11+
#numNodesList = [1000,1000]
12+
#numPtsList = [100, 100, 10]
13+
#srcQNodeList = [900, 900,900]
14+
#srcRNodeList = [5,5,5]
15+
#gammaValList = [0.8, 0.8, 0.8]
16+
#blfBatchSizeList = [10,10,10]
17+
#graphNameList = ['pa','pa','pa']
18+
#tempValList = [0.1, 0.1, 0.1]
19+
#learnRateList = [0.2, 0.2, 0.2]
20+
#qLrnEnList = [True, False, False]
21+
#tauMaxList = [10,10,10]
22+
#etaList = [0., 0., 0.]
23+
#xiList = [1., 1., 1.]
24+
#mValList = [3,3,3]
25+
#rggRadList = [0.07,0.07,0.07]
26+
27+
## Mod deg, low eigvec
28+
#iterList = [0]
29+
#sim = {}
30+
#savePath = "simRes"
31+
#saveFileNameHead = "2018_04_04"
32+
#numNodesList = [1000,1000]
33+
#numPtsList = [100, 100, 10]
34+
#srcQNodeList = [128,128,128]
35+
#srcRNodeList = [5,5,5]
36+
#gammaValList = [0.8, 0.8, 0.8]
37+
#blfBatchSizeList = [17,10,10]
38+
#graphNameList = ['pa','pa','pa']
39+
#tempValList = [0.1, 0.1, 0.1]
40+
#learnRateList = [0.2, 0.2, 0.2]
41+
#qLrnEnList = [True, False, False]
42+
#tauMaxList = [10,10,10]
43+
#etaList = [0., 0., 0.]
44+
#xiList = [1., 1., 1.]
45+
#mValList = [3,3,3]
46+
#rggRadList = [0.07,0.07,0.07]
47+
48+
49+
# Mod deg, low eigvec
50+
iterList = [0]
51+
sim = {}
52+
savePath = "simRes"
53+
saveFileNameHead = "2018_04_04"
54+
numNodesList = [1000,1000,1000,1000,1000]
55+
numPtsList = [100, 100, 100,100,100,100]
56+
srcQNodeList = [128,128,128,128,128,128]
57+
srcRNodeList = [5,5,5,5,5,5]
58+
gammaValList = [0.8, 0.8, 0.8,0.8, 0.8, 0.8]
59+
blfBatchSizeList = [10,12,14,15,17]
60+
graphNameList = ['pa','pa','pa','pa','pa','pa']
61+
tempValList = [0.1, 0.1, 0.1,0.1, 0.1, 0.1]
62+
learnRateList = [0.2, 0.2, 0.2,0.2, 0.2, 0.2]
63+
qLrnEnList = [True, True, True, True, True, False]
64+
tauMaxList = [10,10,10,10,10,10]
65+
etaList = [0., 0., 0.,0.,0.,0.]
66+
xiList = [1., 1., 1.,1.,1.,1.]
67+
mValList = [3,3,3,3,3,3]
68+
rggRadList = [0.07,0.07,0.07,0.07,0.07,0.07]
69+
70+
71+
72+
### for test
73+
#iterList = [0]
74+
#sim = {}
75+
#savePath = "simRes"
76+
#saveFileNameHead = "2018_04_04"
77+
#numNodesList = [1000,1000]
78+
#numPtsList = [10, 10, 10]
79+
#srcQNodeList = [79, 79,79]
80+
#srcRNodeList = [5,5,5]
81+
#gammaValList = [0.8, 0.8, 0.8]
82+
#blfBatchSizeList = [5,5,5]
83+
#graphNameList = ['pa','pa','pa']
84+
#tempValList = [0.1, 0.1, 0.1]
85+
#learnRateList = [0.2, 0.2, 0.2]
86+
#qLrnEnList = [True, False, False]
87+
#tauMaxList = [5,5,5]
88+
#etaList = [0., 0., 0.]
89+
#xiList = [1., 1., 1.]
90+
#
91+
## graph specific parameters
92+
#mValList = [3,3,3]
93+
#rggRadList = [0.07,0.07,0.07]
94+
95+
#srcQNode = 900 # worst case
96+
#srcQNode = 46 # best case - high degree and second highest eigvec centrality
97+
#srcQNode = 79 # high degree - low eigvec centrality
98+
#srcRNode = 5 # Hub

‎get_shrtt_paths_from_src.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Function to get shortest paths from source to all other nodes in the graph
2+
import networkx as nx
3+
import pdb
4+
5+
def get_shrtt_paths_from_src(g, Params):
6+
srcNode = Params['srcNode']
7+
numNodes = Params['numNodes']
8+
9+
# generate networkx graph
10+
gnx = nx.Graph()
11+
12+
for indNode in range(numNodes):
13+
gnx.add_node(indNode)
14+
15+
for indNodeRow in range(numNodes):
16+
for indNodeCol in g[indNodeRow]:
17+
if indNodeCol>indNodeRow:
18+
gnx.add_edge(indNodeRow, indNodeCol)
19+
20+
shrttPathsList = nx.single_source_shortest_path_length(gnx, srcNode)
21+
22+
return shrttPathsList

‎load_graph.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
3+
def load_graph(Params):
4+
savePath = 'graphDump'
5+
graphName = Params['graphName']
6+
7+
if Params['graphName'] == 'pa':
8+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"mVal"+str(Params['mVal'])
9+
elif Params['graphName'] == 'grid2d':
10+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"row"+str(Params['numNodesRow'])+"col"+str(Params["numNodesCol"])
11+
elif Params['graphName'] == 'rgg':
12+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"rggRad"+str(Params['rggRad'])
13+
elif Params['graphName'] == 'plclust':
14+
fileName = savePath+"/"+"nodes"+str(Params['numNodes'])+"mVal"+str(Params['mVal'])+"pTriangle"+str(Params['pTriangle'])
15+
16+
loadGraphVar = np.load(fileName+".npy")
17+
18+
g = loadGraphVar[1][0]
19+
numNbrDict = loadGraphVar[1][1]
20+
gnx = loadGraphVar[1][2]
21+
22+
return (g, numNbrDict, gnx)

‎normalize_vector.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Function to normalize a vector
2+
import numpy as np
3+
4+
def normalize_vector(vecIn):
5+
sumVal = np.sum(vecIn)
6+
vecNormalized = np.divide(vecIn, sumVal)
7+
return vecNormalized

‎plot_qopin.py

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import pdb
4+
import os.path
5+
from get_params_list2plot_qopin import *
6+
7+
markerList = ['k-o','b-s','r-^','k-s','b-^','r-o','k-^','b-o','r-s']
8+
9+
for ind, numNodes in enumerate(numNodesList):
10+
numNodes = numNodesList[ind]
11+
numPts = numPtsList[ind]
12+
srcQNode = srcQNodeList[ind]
13+
srcRNode = srcRNodeList[ind]
14+
learnRate = learnRateList[ind]
15+
gammaVal = gammaValList[ind]
16+
blfBatchSize = blfBatchSizeList[ind]
17+
tempVal = tempValList[ind]
18+
graphName = graphNameList[ind]
19+
mVal = mValList[ind]
20+
rggRad = rggRadList[ind]
21+
qLrnEn = qLrnEnList[ind]
22+
tauMax = tauMaxList[ind]
23+
eta = etaList[ind]
24+
xi = xiList[ind]
25+
26+
saveFileNameHead = saveFileNameHead
27+
28+
if graphName =='rgg':
29+
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(numNodes) \
30+
+"rnd"+str(numRounds)+ "qLrnEn" + str(qLrnEn) + "lrnRate"+ str(learnRate) + "gam" + str(gammaVal) + "gamEx" + str(gammaExtraVal) + "temp" + str(tempVal) + "blfBchSz"+str(blfBatchSize)+"grph"+graphName+"rggRad"+str(rggRad)
31+
32+
elif graphName == 'pa':
33+
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(numNodes) \
34+
+"slt"+str(numPts)+ "lrnRate"+ \
35+
str(learnRate) + "gam" + str(gammaVal) \
36+
+ "temp" + str(tempVal) + "tauMax" + str(tauMax) \
37+
+ "blfBchSz"+str(blfBatchSize)+"grph"+graphName+'mVal'+str(mVal) \
38+
+ "eta"+str(eta)+"xi"+str(xi) \
39+
+ "srcQ"+str(srcQNode) \
40+
+ "srcR"+str(srcRNode) \
41+
+ "qLrnEn" + str(int(qLrnEn))
42+
43+
# if ~os.path.exists(fileName+".npy"):
44+
# continue
45+
46+
dumpVar = np.load(fileName+"0.npy")
47+
48+
varNameList = dumpVar[0]
49+
varValList = dumpVar[1]
50+
nArg = len(varNameList)
51+
dct = {}
52+
53+
# collecting all the variables in dictionary "dct"
54+
for indVar, varName in enumerate(varNameList):
55+
print(indVar)
56+
dct[varName] = varValList[indVar]
57+
58+
opinionList = dct['opinionList']
59+
opinionListOppon= dct['opinionListOppon']
60+
blfMatAllRnd = dct['blfMatAllRnd']
61+
## Opinion vs time slots
62+
63+
# With Q-learning
64+
plt.figure(1)
65+
66+
if(graphName == "rgg"):
67+
graphNameStr = "Random geometric graph: r = "+str(rggRad)
68+
elif(graphName == "pa"):
69+
graphNameStr = "PA graph: m = "+str(mVal)
70+
elif(graphName == 'er'):
71+
graphNameStr = 'ER graph'
72+
73+
if qLrnEn == False:
74+
plt.plot(opinionList, linewidth=4, markersize=10, \
75+
label=" without Q-learning (opinion-1)")
76+
else:
77+
plt.plot(opinionList, linewidth=4, markersize=10, \
78+
label=" with Q-learning (opinion-1)")
79+
80+
plt.ylabel("Sum of opinions", fontsize=24)
81+
plt.xlabel("Round", fontsize=24)
82+
83+
# Without Q-learning
84+
plt.figure(1)
85+
if qLrnEn == False:
86+
plt.plot(opinionListOppon, linewidth=4, markersize=10, \
87+
label=" without Q-learning (opinon-2)")
88+
else:
89+
plt.plot(opinionListOppon, linewidth=4, markersize=10, \
90+
label=" with Q-learning (opinion-2)")
91+
92+
plt.title(graphNameStr, fontsize=20)
93+
plt.ylabel("Sum of opinions", fontsize=24)
94+
plt.xlabel("Time-slot", fontsize=24)
95+
print(" ")
96+
97+
98+
if qLrnEn == True:
99+
# Conditional histogram
100+
plt.figure(2)
101+
blfMatLast = blfMatAllRnd[-1]
102+
muiList = np.divide(blfMatLast[:,1], np.sum(blfMatLast, 1))
103+
muiOpponList = np.divide(blfMatLast[:,0], np.sum(blfMatLast, 1))
104+
plt.title('with Q-learning', fontsize=24)
105+
plt.hist(muiList, bins=20, label='opinion-1')
106+
plt.hist(muiOpponList, bins=20, label='opinion-2')
107+
else:
108+
# Conditional histogram
109+
plt.figure(3)
110+
blfMatLast = blfMatAllRnd[-1]
111+
muiList = np.divide(blfMatLast[:,1], np.sum(blfMatLast, 1))
112+
muiOpponList = np.divide(blfMatLast[:,0], np.sum(blfMatLast, 1))
113+
plt.title('without Q-learning', fontsize=24)
114+
plt.hist(muiList, bins=20, label='opinion-1')
115+
plt.hist(muiOpponList, bins=20, label='opinion-2')
116+
117+
plt.ylabel("Frequency", fontsize=24)
118+
plt.xlabel("Opinion", fontsize=24)
119+
print(" ")
120+
121+
122+
123+
for indFig in range(1,4):
124+
plt.figure(indFig)
125+
plt.xticks(fontsize=20)
126+
plt.yticks(fontsize=20)
127+
128+
129+
plt.xticks(fontsize=24)
130+
plt.yticks(fontsize=24)
131+
plt.grid(True)
132+
plt.legend(fontsize=20)
133+
plt.legend(fontsize=20)
134+
plt.show(block=False)
135+
136+
pdb.set_trace()
137+
138+

‎plot_qopin_vs_blfbchsz.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import pdb
4+
import os.path
5+
from get_params_list2plot_qopin import *
6+
7+
markerList = ['k-o','b-s','r-^','k-s','b-^','r-o','k-^','b-o','r-s']
8+
9+
finalOpinionList = []
10+
finalOpinionListOppon = []
11+
12+
for ind, numNodes in enumerate(numNodesList):
13+
numNodes = numNodesList[ind]
14+
numPts = numPtsList[ind]
15+
srcQNode = srcQNodeList[ind]
16+
srcRNode = srcRNodeList[ind]
17+
learnRate = learnRateList[ind]
18+
gammaVal = gammaValList[ind]
19+
blfBatchSize = blfBatchSizeList[ind]
20+
tempVal = tempValList[ind]
21+
graphName = graphNameList[ind]
22+
mVal = mValList[ind]
23+
rggRad = rggRadList[ind]
24+
qLrnEn = qLrnEnList[ind]
25+
tauMax = tauMaxList[ind]
26+
eta = etaList[ind]
27+
xi = xiList[ind]
28+
29+
saveFileNameHead = saveFileNameHead
30+
31+
if graphName =='rgg':
32+
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(numNodes) \
33+
+"rnd"+str(numRounds)+ "qLrnEn" + str(qLrnEn) + "lrnRate"+ str(learnRate) + "gam" + str(gammaVal) + "gamEx" + str(gammaExtraVal) + "temp" + str(tempVal) + "blfBchSz"+str(blfBatchSize)+"grph"+graphName+"rggRad"+str(rggRad)
34+
35+
elif graphName == 'pa':
36+
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(numNodes) \
37+
+"slt"+str(numPts)+ "lrnRate"+ \
38+
str(learnRate) + "gam" + str(gammaVal) \
39+
+ "temp" + str(tempVal) + "tauMax" + str(tauMax) \
40+
+ "blfBchSz"+str(blfBatchSize)+"grph"+graphName+'mVal'+str(mVal) \
41+
+ "eta"+str(eta)+"xi"+str(xi) \
42+
+ "srcQ"+str(srcQNode) \
43+
+ "srcR"+str(srcRNode) \
44+
+ "qLrnEn" + str(int(qLrnEn))
45+
46+
# if ~os.path.exists(fileName+".npy"):
47+
# continue
48+
49+
dumpVar = np.load(fileName+"0.npy")
50+
51+
varNameList = dumpVar[0]
52+
varValList = dumpVar[1]
53+
nArg = len(varNameList)
54+
dct = {}
55+
56+
# collecting all the variables in dictionary "dct"
57+
for indVar, varName in enumerate(varNameList):
58+
print(indVar)
59+
dct[varName] = varValList[indVar]
60+
61+
opinionList = dct['opinionList']
62+
opinionListOppon= dct['opinionListOppon']
63+
64+
finalOpinionList.extend([opinionList[-1]])
65+
finalOpinionListOppon.extend([opinionListOppon[-1]])
66+
67+
plt.figure(1)
68+
if(graphName == "rgg"):
69+
graphNameStr = "Random geometric graph: r = "+str(rggRad)
70+
elif(graphName == "pa"):
71+
graphNameStr = "PA graph: m = "+str(mVal)
72+
elif(graphName == 'er'):
73+
graphNameStr = 'ER graph'
74+
75+
plt.plot(blfBatchSizeList, finalOpinionList, linewidth=4, markersize=10, \
76+
label="opinion-1")
77+
78+
plt.plot(blfBatchSizeList, finalOpinionListOppon, linewidth=4, markersize=10, \
79+
label="opinion-2")
80+
81+
plt.title(graphNameStr, fontsize=20)
82+
plt.ylabel("Sum of opinions", fontsize=24)
83+
plt.xlabel("R_Q", fontsize=24)
84+
print(" ")
85+
86+
plt.xticks(fontsize=20)
87+
plt.yticks(fontsize=20)
88+
89+
90+
plt.xticks(fontsize=24)
91+
plt.yticks(fontsize=24)
92+
plt.grid(True)
93+
plt.legend(fontsize=20)
94+
plt.legend(fontsize=20)
95+
plt.show(block=False)
96+
97+
pdb.set_trace()
98+
99+

‎prune_feed.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
3+
# [[Q or R message],[time of reception],[incoming node]]
4+
def prune_feed(feedList, indNode, timeNow, tauMax):
5+
6+
# find and remove all the old (stale) feeds
7+
lenFeedList = len(feedList[indNode][0])
8+
for indFeed in range(0, lenFeedList):
9+
# check if old feed
10+
if (timeNow-feedList[indNode][1][lenFeedList-indFeed-1])>tauMax:
11+
# remove all the record of lenFeedList-indFeed
12+
del feedList[indNode][0][lenFeedList-indFeed-1]
13+
del feedList[indNode][1][lenFeedList-indFeed-1]
14+
del feedList[indNode][2][lenFeedList-indFeed-1]
15+
del feedList[indNode][3][lenFeedList-indFeed-1]
16+
17+
return feedList

‎qopin_tb.py

+326
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
import numpy as np
2+
import networkx as nx
3+
import matplotlib.pyplot as plt
4+
import pdb
5+
import copy
6+
import random
7+
import sys
8+
9+
from boltzmann_explr_opin import boltzmann_explr_opin
10+
from compute_prob import compute_prob
11+
from gen_graph import gen_graph
12+
from prune_feed import prune_feed
13+
from normalize_vector import normalize_vector
14+
from load_graph import load_graph
15+
from visualize_graph import visualize_graph
16+
from display_centralities import display_centralities
17+
from save_results_qopin import save_results_qopin
18+
19+
def qopin_tb(sim, Params):
20+
21+
#Initialization
22+
seed = sim['iterNum']
23+
random.seed(seed)
24+
25+
rememberingFactor = 1
26+
27+
blfBatchSize = Params['blfBatchSize']
28+
29+
gammVal = Params['gammaVal']
30+
if Params['genGraphFlg'] == True:
31+
(g, numNbrDict, gnx) = gen_graph(Params)
32+
else:
33+
(g, numNbrDict, gnx) = load_graph(Params)
34+
35+
36+
if Params['graphName'] == 'rgg' or Params['graphName'] == 'grid2d':
37+
pos = nx.get_node_attributes(gnx, 'pos')
38+
else:
39+
pos = nx.spectral_layout(gnx, scale=4)
40+
#pos = nx.shell_layout(gnx, scale=2)
41+
42+
########################## initialization begin #######################################
43+
# New params
44+
tauMax = Params['tauMax']
45+
learnRate = Params['learnRate']
46+
47+
actionSet = {} # dict dict list
48+
49+
# initializing src node, Q-table and action sets
50+
srcQNode = Params['srcQNode']
51+
srcRNode = Params['srcRNode']
52+
numNodes = Params['numNodes']
53+
# Display centralities and degrees of the nodes
54+
[cntraltyEigVec] = display_centralities(gnx, srcQNode, srcRNode)
55+
56+
actionSet = g
57+
58+
Q = copy.deepcopy(actionSet)
59+
Qdel = copy.deepcopy(actionSet)
60+
cntMat = copy.deepcopy(actionSet)
61+
62+
for indNode in range(numNodes):
63+
Q[indNode] = [0.5]*len(actionSet[indNode])
64+
cntMat[indNode] = [0]*len(actionSet[indNode])
65+
Qdel[indNode] = [0]*len(actionSet[indNode])
66+
informerDict = {x:[] for x in range(numNodes)}
67+
informeeDict = {x:[] for x in range(numNodes)}
68+
opinionList = []
69+
opinionListOppon = []
70+
feedList = {x:[[],[],[],[]] for x in range(numNodes)} # [[Q or R message],[time of reception],[incoming node], [No. of times msg is Tx]]
71+
infNodesDumpAllRnd = {} # For future use. Collecting stats of the nodes informed at each time slot for all rounds
72+
blfMat = np.ones((numNodes, 2))
73+
sumOpinionQ = np.array(Params['numRounds']*[0])
74+
sumOpinionR = np.array(Params['numRounds']*[0])
75+
76+
#blfMatAllRnd = np.array([])
77+
blfMatAllRnd = list()
78+
79+
#Loop for all rounds
80+
for indRnd in range(Params['numRounds']):
81+
82+
if np.mod(indRnd, 10) == 0:
83+
print("Round", indRnd, end=",")
84+
sys.stdout.flush()
85+
86+
#################### initialization - for each round - begin ####################
87+
qTxNodesList = []
88+
rTxNodesList = []
89+
qChosenFeedList = []
90+
rChosenFeedList = []
91+
infNodesDumpOneRnd = []
92+
infNodesDumpOneRnd.append([srcQNode])
93+
94+
# initialize state sequence
95+
stateSeq = {x:[] for x in range(numNodes)}
96+
# keep a count of the number of occurence of each state-action pair (size of count matrix = size of action set)
97+
98+
del qTxNodesList[:]
99+
del rTxNodesList[:]
100+
del qChosenFeedList[:]
101+
del rChosenFeedList[:]
102+
feedDelDict = {x:[] for x in range(numNodes)}
103+
blfMatPrev = copy.deepcopy(blfMat)
104+
105+
# Loop for all nodes
106+
for indNode in range(numNodes):
107+
# 1. Remove all the messages older than tauMax [TODO]
108+
feedList = prune_feed(feedList, indNode, int(indRnd/blfBatchSize), tauMax)
109+
# 2. If source node (Q or R), then
110+
if indNode == srcQNode:
111+
# collect infQ nodes
112+
qTxNodesList.append(indNode)
113+
qChosenFeedList.append(0)
114+
elif indNode == srcRNode:
115+
# collect infR nodes
116+
rTxNodesList.append(indNode)
117+
rChosenFeedList.append(0)
118+
else:
119+
# check if feedsize>0
120+
if len(feedList[indNode][0]) != 0:
121+
# Choose a message from list w.p. exp(-tau)
122+
# Get probability vector from exp(-tau)
123+
[probFeedVec, noTx] = compute_prob(feedList[indNode][1],feedList[indNode][3],int(indRnd/blfBatchSize), Params['eta'], Params['xi'])
124+
#if len(probFeedVec)>1:
125+
# pdb.set_trace()
126+
127+
if noTx == False:
128+
if len(probFeedVec) == 1:
129+
chosenFeedTmp = [0]
130+
else:
131+
chosenFeedTmp = np.random.choice(len(probFeedVec),1,p=probFeedVec)
132+
133+
chosenFeed = chosenFeedTmp[0]
134+
chosenMsg = feedList[indNode][0][chosenFeed]
135+
136+
# If no message chosen dont collect nodes otherwise collect - Does not occur now. We can consider finite buffer size [TODO]
137+
# Collect qTx nodes or collect rTx nodes
138+
if chosenMsg == 1:
139+
qTxNodesList.append(indNode)
140+
qChosenFeedList.append(chosenFeed)
141+
else:
142+
rTxNodesList.append(indNode)
143+
rChosenFeedList.append(chosenFeed)
144+
145+
146+
# Loop for all qTx nodes
147+
for (loopInd, indNode) in enumerate(qTxNodesList):
148+
149+
# 2. Compute p_q (probability of transmitting message m_q)
150+
probSendMsgBlf = blfMatPrev[indNode][1]/np.sum(blfMatPrev[indNode])
151+
# 3. sample sendMsgFlg using p_q
152+
sendMsgSamp = (np.random.rand(1,1)<=probSendMsgBlf)
153+
154+
if indNode == srcQNode:
155+
sendMsgSamp = True
156+
# 4. If sendMsgFlg == True then
157+
# 4.a. Choose recipient (neighbor) using action-values by Boltzmann exploration rule
158+
# 4.b. Update belief of the chosen recipient
159+
# 4.c. Compute reward, update Q-table
160+
# 4.d. send msg
161+
# 4.e. delete msg
162+
if sendMsgSamp == True:
163+
# 4.a. Choose a neighbor using boltzmann exploration rule
164+
if indNode == srcQNode:
165+
incomingNodeIndex = []
166+
else:
167+
chosenFeed = qChosenFeedList[loopInd] # location of the feed
168+
incomingNode = feedList[indNode][2][chosenFeed] # incoming node for indNode
169+
170+
incomingNodeIndexTmp = np.where(np.array(g[indNode]) == incomingNode)
171+
incomingNodeIndex = incomingNodeIndexTmp[0][0]
172+
173+
if np.mod(indRnd, blfBatchSize) == 0:
174+
feedList[indNode][3][chosenFeed] += 1
175+
176+
seedInBoltzmann = random.randint(1,100000)
177+
[action, actionIdx] = boltzmann_explr_opin(Q[indNode], incomingNodeIndex, g[indNode], Params['tempVal'], seedInBoltzmann)
178+
179+
# 4.b.
180+
#blfMat[action][1] += 1
181+
if np.mod(indRnd, blfBatchSize) == 0:
182+
#blfMat[action][1] = blfMat[action][1] + 1
183+
blfMat[action][1] = blfMat[action][1]*rememberingFactor + 1 # for debug
184+
185+
# 4.c.
186+
if Params['qLrnEn'] == True:
187+
opinion = blfMatPrev[action][1]/np.sum(blfMatPrev[action])
188+
#rwdImm = 10*opinion*(1-opinion)/blfMatPrev[action][1] # for debug
189+
rwdImm = opinion*(1-opinion)/blfMatPrev[action][1]
190+
Qmax = np.max(Q[action])
191+
Q[indNode][actionIdx] = (1-learnRate)*Q[indNode][actionIdx]+learnRate*(rwdImm+gammVal*Qmax)
192+
193+
## 4.e. Delete feed and related records from feedList of indNode
194+
if np.mod(indRnd, blfBatchSize) == 0:
195+
if indNode != srcQNode:
196+
feedDelDict[indNode].append(chosenFeed)
197+
198+
# 4.f Append feed and related records in feedList of action node
199+
feedList[action][0].append(1) # message
200+
feedList[action][1].append(int(indRnd/blfBatchSize)) # time
201+
feedList[action][2].append(indNode) # incoming node
202+
feedList[action][3].append(0) # Number of times msg is Tx
203+
204+
205+
if np.mod(indRnd, blfBatchSize) == 0:
206+
# Loop for all rTx nodes
207+
for (loopInd, indNode) in enumerate(rTxNodesList):
208+
209+
# 2. Compute p_r (probability of transmitting message m_q)
210+
probSendMsgBlf = blfMatPrev[indNode][0]/np.sum(blfMatPrev[indNode])
211+
# 3. sample sendMsgFlg using p_q
212+
sendMsgSamp = (np.random.rand(1,1)<=probSendMsgBlf)
213+
214+
if indNode == srcRNode:
215+
sendMsgSamp = True
216+
217+
if sendMsgSamp == True:
218+
# 4.a. Choose a neighbor using boltzmann exploration rule
219+
if indNode == srcRNode:
220+
incomingNodeIndex = []
221+
else:
222+
chosenFeed = rChosenFeedList[loopInd] # location of the feed
223+
incomingNode = feedList[indNode][2][chosenFeed]
224+
incomingNodeIndexTmp = np.where(np.array(g[indNode]) == incomingNode)
225+
incomingNodeIndex = incomingNodeIndexTmp[0][0]
226+
feedList[indNode][3][chosenFeed] += 1
227+
228+
seedInBoltzmann = random.randint(1,100000)
229+
[action, actionIdx] = boltzmann_explr_opin([1]*numNbrDict[indNode], incomingNodeIndex, g[indNode], Params['tempVal'], seedInBoltzmann)
230+
231+
# 4.b.
232+
#blfMat[action][0] += 1
233+
# no need to check mod(.,.). Because....refer to if
234+
# condition outside the for loop
235+
blfMat[action][0] = blfMat[action][0]*rememberingFactor + 1 # for debug
236+
237+
if indNode != srcRNode:
238+
feedDelDict[indNode].append(chosenFeed)
239+
240+
# 4.f Append feed and related records in feedList of action node
241+
feedList[action][0].append(0) # message
242+
feedList[action][1].append(int(indRnd/blfBatchSize)) # time
243+
feedList[action][2].append(indNode) # incoming node
244+
feedList[action][3].append(0) # Number of times msg is Tx
245+
246+
##deleting feeds
247+
#for indNode in range(numNodes):
248+
# if len(feedDelDict[indNode])>0:
249+
# for ind in sorted(feedDelDict[indNode], reverse=True):
250+
# del feedList[indNode][0][ind]
251+
# del feedList[indNode][1][ind]
252+
# del feedList[indNode][2][ind]
253+
# del feedList[indNode][3][ind]
254+
# del feedDelDict[indNode][:]
255+
256+
257+
if np.mod(indRnd, blfBatchSize) == 0:
258+
muiList = np.divide(blfMat[:,1], np.sum(blfMat, 1))
259+
opinionList.append(np.sum(muiList))
260+
muiOpponList = np.divide(blfMat[:,0], np.sum(blfMat, 1))
261+
opinionListOppon.append(np.sum(muiOpponList))
262+
263+
#if indRnd == 0:
264+
# blfArr = np.atleast_2d(blfMat[:,0])
265+
# blfArr = np.append(blfArr, np.atleast_2d(blfMat[:,1]), axis=1)
266+
# blfMatAllRnd = np.atleast_3d(blfArr)
267+
#else:
268+
# blfArr = np.atleast_2d(blfMat[:,0])
269+
# blfArr = np.append(blfArr, np.atleast_2d(blfMat[:,1]), axis=1)
270+
# blfMatAllRnd = np.append(blfMatAllRnd, np.atleast_3d(blfArr), axis=2)
271+
272+
blfMatAllRnd.append(blfMat)
273+
274+
#if np.mod(indRnd,50*blfBatchSize) == 0 or indRnd == Params['numRounds']-1:
275+
##if indRnd == Params['numRounds']-1:
276+
# visualize_graph(gnx, g, blfMat, pos, Params)
277+
# plt.pause(0.01)
278+
# pdb.set_trace()
279+
280+
281+
dumpVarNames = ['opinionList', 'opinionListOppon','blfMatAllRnd','cntraltyEigVec']
282+
dumpVarVals = [opinionList, opinionListOppon, blfMatAllRnd, cntraltyEigVec]
283+
284+
save_results_qopin(sim, Params, dumpVarNames, dumpVarVals)
285+
286+
plt.figure(2)
287+
plt.plot(opinionList, linewidth=4, markersize=10, label='opinion-1')
288+
plt.plot(opinionListOppon, linewidth=4, markersize=10, label='opinion-2')
289+
plt.ylabel("Sum opinions", fontsize=24)
290+
plt.xlabel("Round", fontsize=24)
291+
292+
if Params['qLrnEn'] == True:
293+
plt.title('with Q-learning', fontsize=24)
294+
else:
295+
plt.title('without Q-learning', fontsize=24)
296+
297+
print(" ")
298+
plt.grid(True)
299+
plt.xticks(fontsize=24)
300+
plt.yticks(fontsize=24)
301+
plt.legend(fontsize=20)
302+
#plt.ylim((140, 360))
303+
plt.show()
304+
plt.show(block=False)
305+
306+
307+
plt.figure(3)
308+
plt.hist(muiList, bins=10, label='opinion-1')
309+
plt.hist(muiOpponList, bins=10, label='opinion-2')
310+
plt.ylabel("Frequency", fontsize=24)
311+
plt.xlabel("Opinion", fontsize=24)
312+
313+
if Params['qLrnEn'] == True:
314+
plt.title('with Q-learning', fontsize=24)
315+
else:
316+
plt.title('without Q-learning', fontsize=24)
317+
318+
print(" ")
319+
plt.grid(True)
320+
plt.xticks(fontsize=24)
321+
plt.yticks(fontsize=24)
322+
plt.legend(fontsize=20)
323+
#plt.ylim((140, 360))
324+
plt.show()
325+
plt.show(block=False)
326+

‎qopin_wrapper.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from qopin_tb import qopin_tb
2+
import sys
3+
4+
5+
iterList = [0]
6+
7+
sim = {}
8+
Params = {}
9+
sim['savePath'] = "simRes"
10+
sim['saveFileNameHead'] = "2018_04_08"
11+
#Params['graphName'] = 'grid2d'
12+
#Params['graphName'] = 'rgg'
13+
Params['graphName'] = 'pa'
14+
#Params['graphName'] = 'er'
15+
Params['genGraphFlg'] = False
16+
#Params['genGraphFlg'] = True
17+
Params['eta'] = 0.
18+
Params['xi'] = 1.
19+
Params['tauMax'] = 10
20+
Params['blfBatchSize'] = 15
21+
Params['numPts'] = 100
22+
Params['qLrnEn'] = False
23+
#Params['qLrnEn'] = True
24+
25+
26+
print(Params['graphName'])
27+
28+
29+
if Params['graphName'] == 'rgg':
30+
Params['numNodes'] = 1000
31+
Params['rggRad'] = 0.07
32+
Params['numRounds'] = Params['numPts']*Params['blfBatchSize']
33+
Params['srcRNode'] = 700
34+
Params['srcQNode'] = 800
35+
Params['learnRate'] = 0.01
36+
Params['gammaVal'] = 0.9
37+
Params['tempVal'] = 0.1
38+
39+
#Params['numNodes'] = 100
40+
#Params['rggRad'] = 0.17
41+
#Params['numRounds'] = 520
42+
#Params['srcRNode'] = 90
43+
#Params['srcQNode'] = 80
44+
#Params['learnRate'] = 0.05
45+
#Params['gammaVal'] = 0.9
46+
#Params['tempVal'] = 0.1
47+
48+
elif Params['graphName'] == 'er':
49+
# Params['numNodes'] = 1000
50+
# Params['numRounds'] = 1020
51+
# Params['freezeRound'] = 1001
52+
# Params['srcNode'] = 990
53+
# Params['learnRate'] = 0.03
54+
# Params['gammaVal'] = 0.9
55+
# Params['gammaExtraVal']= 0.2
56+
# Params['blfBatchSize'] = 50.
57+
# Params['tempVal'] = 0.2
58+
59+
Params['numNodes'] = 500
60+
Params['numRounds'] = 1020
61+
Params['srcQNode'] = 495
62+
Params['srcRNode'] = 12
63+
Params['learnRate'] = 0.1
64+
Params['gammaVal'] = 0.8
65+
Params['tempVal'] = 0.1
66+
67+
elif Params['graphName'] == 'pa':
68+
# Params['numNodes'] = 1000
69+
# Params['mVal'] = 3
70+
# Params['numRounds'] = Params['numPts']*Params['blfBatchSize']
71+
# #Params['srcQNode'] = 900 # low degree low eigvec centrality
72+
# #Params['srcQNode'] = 46 # best case - high degree and second highest eigvec centrality
73+
# Params['srcQNode'] = 128 # high degree - low eigvec centrality
74+
# Params['srcRNode'] = 5 # Hub
75+
# Params['learnRate'] = 0.2
76+
# Params['gammaVal'] = 0.8
77+
# Params['tempVal'] = 0.1
78+
79+
Params['numNodes'] = 500
80+
Params['mVal'] = 3
81+
Params['numRounds'] = Params['numPts']*Params['blfBatchSize']
82+
Params['srcQNode'] = 400
83+
#Params['srcQNode'] = 495
84+
#Params['srcRNode'] = 12
85+
Params['srcRNode'] = 1
86+
Params['learnRate'] = 0.1
87+
Params['gammaVal'] = 0.8
88+
Params['tempVal'] = 0.1
89+
90+
#For Test
91+
# Params['numNodes'] = 100
92+
# Params['mVal'] = 3
93+
# Params['numRounds'] = 120
94+
# Params['srcQNode'] = 75
95+
# Params['srcRNode'] = 85
96+
# Params['learnRate'] = 0.05
97+
# Params['gammaVal'] = 0.8
98+
# Params['tempVal'] = 0.1
99+
100+
101+
for indIter in iterList:
102+
print("Iteration " + str(indIter))
103+
print("================================================= List of parameters - BEGIN =================================================")
104+
for paramName in Params.keys():
105+
print(paramName+" = ",Params[paramName])
106+
print("------------------------------------------------- List of parameters - END -------------------------------------------------")
107+
108+
sim['iterNum'] = indIter
109+
qopin_tb(sim, Params)
110+
111+

‎save_results_qopin.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
import pdb
3+
4+
def save_results_qopin(sim, Params, dumpVarNames, dumpVarVals):
5+
6+
## save results
7+
savePath = sim['savePath']
8+
saveFileNameHead = sim['saveFileNameHead']
9+
graphName = Params['graphName']
10+
11+
if graphName =='rgg': # [TODO]
12+
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(Params['numNodes']) \
13+
+"slt"+str(Params['numPts'])+ "lrnRate"+ \
14+
str(Params['learnRate']) + "gam" + str(Params['gammaVal']) \
15+
+ "temp" + str(Params['tempVal']) \
16+
+ "blfBchSz"+str(Params['blfBatchSize'])\
17+
+"grph"+graphName+"rggRad"+str(Params['rggRad']) \
18+
+ "eta"+str(Params['eta']+"xi"+Params['xi']) \
19+
+ "qLrnEn" + str(Params['qLrnEn'])
20+
elif graphName == 'pa':
21+
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(Params['numNodes']) \
22+
+"slt"+str(Params['numPts'])+ "lrnRate"+ \
23+
str(Params['learnRate']) + "gam" + str(Params['gammaVal']) \
24+
+ "temp" + str(Params['tempVal']) + "tauMax" + str(Params['tauMax']) \
25+
+ "blfBchSz"+str(Params['blfBatchSize'])+"grph"+graphName+'mVal'+str(Params['mVal']) \
26+
+ "eta"+str(Params['eta'])+"xi"+str(Params['xi']) \
27+
+ "srcQ"+str(Params['srcQNode']) \
28+
+ "srcR"+str(Params['srcRNode']) +"qLrnEn"+str(int(Params['qLrnEn']))
29+
30+
dumpVar = []
31+
dumpVar.append(dumpVarNames)
32+
dumpVar.append(dumpVarVals)
33+
34+
np.save(fileName+str(sim['iterNum']), dumpVar)
35+
36+

‎visualize_graph.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import matplotlib.pyplot as plt
2+
import networkx as nx
3+
import numpy as np
4+
import pdb
5+
6+
def visualize_graph(G, g, blfMat, pos, Params):
7+
8+
srcQ = Params['srcQNode']
9+
srcR = Params['srcRNode']
10+
numNodes = Params['numNodes']
11+
graphName = Params['graphName']
12+
13+
#G = nx.random_geometric_graph(200, 0.125)
14+
# position is stored as node attribute data for random_geometric_graph
15+
16+
# find node near center (0.5,0.5)
17+
dmin = 1
18+
ncenter = 0
19+
for n in pos:
20+
x, y = pos[n]
21+
d = (x - 0.5)**2 + (y - 0.5)**2
22+
if d < dmin:
23+
ncenter = n
24+
dmin = d
25+
26+
plt.figure(100, figsize=(8, 8))
27+
plt.gcf().clear()
28+
29+
# q to
30+
31+
32+
# edge thickness according to action-values
33+
#for indNode in range(numNodes)
34+
#for nbr in g[ind]
35+
if graphName == 'grid2d':
36+
pos = dict((n, n) for n in G.nodes())
37+
nx.draw_networkx_edges(G, pos, alpha=0.1)
38+
else: #graphName == 'rgg':
39+
nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.1)
40+
41+
for indNode in range(numNodes):
42+
alphaVal = (blfMat[indNode][0]/(blfMat[indNode][0]+blfMat[indNode][1]))
43+
if graphName == 'grid2d':
44+
indNodeRow = int(indNode/Params['numNodesRow'])
45+
indNodeCol = np.mod(indNode, Params['numNodesRow'])
46+
indNode4Grid = (indNodeRow, indNodeCol)
47+
srcRRow = int(srcR/Params['numNodesRow'])
48+
srcRCol = np.mod(srcR, Params['numNodesRow'])
49+
srcR4Grid = (srcRRow, srcRCol)
50+
nx.draw_networkx_nodes(G,pos, nodelist=[indNode4Grid, srcR4Grid],node_color='r', node_size=100, alpha=0.8*alphaVal)
51+
else: #graphName == 'rgg':
52+
nx.draw_networkx_nodes(G,pos, nodelist=[indNode, srcR],node_color='r', node_size=100, alpha=0.8*alphaVal)
53+
54+
if graphName == 'pa':
55+
plt.xlim(-0.015, 0.015)
56+
plt.ylim(-0.015, 0.015)
57+
plt.axis('off')
58+
elif graphName == 'pa' and numNodes == 1000:
59+
plt.xlim(-0.035, 0.035)
60+
plt.ylim(-0.07, 0.07)
61+
plt.axis('off')
62+
63+
64+
if graphName == 'rgg':
65+
plt.xlim(-0.05, 1.05)
66+
plt.ylim(-0.05, 1.05)
67+
plt.axis('off')
68+
69+
plt.show(block=False)
70+
71+
72+
73+
for indNode in range(numNodes):
74+
alphaVal = (blfMat[indNode][1]/(blfMat[indNode][0]+blfMat[indNode][1]))
75+
if graphName == 'grid2d':
76+
indNodeRow = int(indNode/Params['numNodesRow'])
77+
indNodeCol = np.mod(indNode, Params['numNodesRow'])
78+
indNode4Grid = (indNodeRow, indNodeCol)
79+
srcQRow = int(srcQ/Params['numNodesRow'])
80+
srcQCol = np.mod(srcQ, Params['numNodesRow'])
81+
srcQ4Grid = (srcQRow, srcQCol)
82+
nx.draw_networkx_nodes(G,pos, nodelist=[indNode4Grid, srcQ4Grid],node_color='b', node_size=100, alpha=0.8*alphaVal)
83+
else: #graphName == 'rgg':
84+
nx.draw_networkx_nodes(G,pos, nodelist=[indNode, srcQ],node_color='b', node_size=100, alpha=0.8*alphaVal)
85+
86+
if graphName == 'rgg':
87+
plt.xlim(-0.05, 1.05)
88+
plt.ylim(-0.05, 1.05)
89+
plt.axis('off')
90+
91+
if graphName == 'pa' and numNodes == 500:
92+
plt.xlim(-0.015, 0.015)
93+
plt.ylim(-0.015, 0.015)
94+
plt.axis('off')
95+
elif graphName == 'pa' and numNodes == 1000:
96+
plt.xlim(-0.035, 0.035)
97+
plt.ylim(-0.07, 0.07)
98+
plt.axis('off')
99+
100+
if Params['qLrnEn'] == True:
101+
plt.title('With Q-learning')
102+
else:
103+
plt.title('Without Q-learning')
104+
105+
plt.show(block=False)
106+
107+

0 commit comments

Comments
 (0)
Please sign in to comment.