-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_qopin.py
executable file
·138 lines (111 loc) · 4.55 KB
/
plot_qopin.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
import numpy as np
import matplotlib.pyplot as plt
import pdb
import os.path
from get_params_list2plot_qopin import *
markerList = ['k-o','b-s','r-^','k-s','b-^','r-o','k-^','b-o','r-s']
for ind, numNodes in enumerate(numNodesList):
numNodes = numNodesList[ind]
numPts = numPtsList[ind]
srcQNode = srcQNodeList[ind]
srcRNode = srcRNodeList[ind]
learnRate = learnRateList[ind]
gammaVal = gammaValList[ind]
blfBatchSize = blfBatchSizeList[ind]
tempVal = tempValList[ind]
graphName = graphNameList[ind]
mVal = mValList[ind]
rggRad = rggRadList[ind]
qLrnEn = qLrnEnList[ind]
tauMax = tauMaxList[ind]
eta = etaList[ind]
xi = xiList[ind]
saveFileNameHead = saveFileNameHead
if graphName =='rgg':
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(numNodes) \
+"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)
elif graphName == 'pa':
fileName = savePath+"/"+saveFileNameHead+"nodes"+str(numNodes) \
+"slt"+str(numPts)+ "lrnRate"+ \
str(learnRate) + "gam" + str(gammaVal) \
+ "temp" + str(tempVal) + "tauMax" + str(tauMax) \
+ "blfBchSz"+str(blfBatchSize)+"grph"+graphName+'mVal'+str(mVal) \
+ "eta"+str(eta)+"xi"+str(xi) \
+ "srcQ"+str(srcQNode) \
+ "srcR"+str(srcRNode) \
+ "qLrnEn" + str(int(qLrnEn))
# if ~os.path.exists(fileName+".npy"):
# continue
dumpVar = np.load(fileName+"0.npy")
varNameList = dumpVar[0]
varValList = dumpVar[1]
nArg = len(varNameList)
dct = {}
# collecting all the variables in dictionary "dct"
for indVar, varName in enumerate(varNameList):
print(indVar)
dct[varName] = varValList[indVar]
opinionList = dct['opinionList']
opinionListOppon= dct['opinionListOppon']
blfMatAllRnd = dct['blfMatAllRnd']
## Opinion vs time slots
# With Q-learning
plt.figure(1)
if(graphName == "rgg"):
graphNameStr = "Random geometric graph: r = "+str(rggRad)
elif(graphName == "pa"):
graphNameStr = "PA graph: m = "+str(mVal)
elif(graphName == 'er'):
graphNameStr = 'ER graph'
if qLrnEn == False:
plt.plot(opinionList, linewidth=4, markersize=10, \
label=" without Q-learning (opinion-1)")
else:
plt.plot(opinionList, linewidth=4, markersize=10, \
label=" with Q-learning (opinion-1)")
plt.ylabel("Sum of opinions", fontsize=24)
plt.xlabel("Round", fontsize=24)
# Without Q-learning
plt.figure(1)
if qLrnEn == False:
plt.plot(opinionListOppon, linewidth=4, markersize=10, \
label=" without Q-learning (opinon-2)")
else:
plt.plot(opinionListOppon, linewidth=4, markersize=10, \
label=" with Q-learning (opinion-2)")
plt.title(graphNameStr, fontsize=20)
plt.ylabel("Sum of opinions", fontsize=24)
plt.xlabel("Time-slot", fontsize=24)
print(" ")
if qLrnEn == True:
# Conditional histogram
plt.figure(2)
blfMatLast = blfMatAllRnd[-1]
muiList = np.divide(blfMatLast[:,1], np.sum(blfMatLast, 1))
muiOpponList = np.divide(blfMatLast[:,0], np.sum(blfMatLast, 1))
plt.title('with Q-learning', fontsize=24)
plt.hist(muiList, bins=20, label='opinion-1')
plt.hist(muiOpponList, bins=20, label='opinion-2')
else:
# Conditional histogram
plt.figure(3)
blfMatLast = blfMatAllRnd[-1]
muiList = np.divide(blfMatLast[:,1], np.sum(blfMatLast, 1))
muiOpponList = np.divide(blfMatLast[:,0], np.sum(blfMatLast, 1))
plt.title('without Q-learning', fontsize=24)
plt.hist(muiList, bins=20, label='opinion-1')
plt.hist(muiOpponList, bins=20, label='opinion-2')
plt.ylabel("Frequency", fontsize=24)
plt.xlabel("Opinion", fontsize=24)
print(" ")
for indFig in range(1,4):
plt.figure(indFig)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.grid(True)
plt.legend(fontsize=20)
plt.legend(fontsize=20)
plt.show(block=False)
pdb.set_trace()