-
Notifications
You must be signed in to change notification settings - Fork 2
/
Data_analysis_ABM.py
244 lines (183 loc) · 8.1 KB
/
Data_analysis_ABM.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
#!/usr/bin/env python
# coding: utf-8
import Fin4_ABM as model
import matplotlib
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
from matplotlib.ticker import FuncFormatter
import matplotlib.pyplot as plt
import matplotlib.pylab as pl
import numpy as np
import pandas as pd
setup = model
#setup_name = "vs"
df = pd.DataFrame(setup.raw_result)
total_agents = len(setup.raw_result[-1]['agents'])
#print(t_step)
def to_percent(y, position):
# Ignore the passed in position. This has the effect of scaling the default
# tick locations.
#total_agents = len(state['agents'])
s = str(100 * y / total_agents)
# The percent symbol needs escaping in latex
if matplotlib.rcParams['text.usetex'] is True:
return s + r'$\%$'
else:
return s + '%'
#------------------- tokens/PAT -----------------------------
def calculate_total_tokens_per_pat(state):
a = range(0, len(state['PATs']))
distribution = dict((el, 0) for el in a)
database = reconstruct_agent_database(state['agents'])
for ag in database:
for pat, number_of_tokens in ag['token_wallet'].items():
print(pat, number_of_tokens)
if pat != 'reputation':
distribution[pat] += number_of_tokens
print ('distribution: ', distribution)
return distribution
def calculate_total_activity_per_pat(state):
a = range(0, len(state['PATs']))
distribution = dict((el, 0) for el in a)
#database = reconstruct_agent_database(state['agents'])
for pat in state['PATs']:
#for ag in range(0, pat['activity'])):
distribution[pat['name']] = pat['activity']
print ('distribution_activity: ', distribution)
return distribution
def histogram_of_activity_per_PAT(state):
activity_hist_noble = []
activity_hist_opp = []
activity_hist_malicious = []
for pat in state['PATs']:
for i in range(0, pat['activity']):
if pat['purpose'] == 'noble':
activity_hist_noble.append(pat['name'])
if pat['purpose'] == 'opportunistic':
activity_hist_opp.append(pat['name'])
if pat['purpose'] == 'malicious':
activity_hist_malicious.append(pat['name'])
print ('activity_hist_noble: ', activity_hist_noble)
print ('activity_hist_mal: ', activity_hist_malicious)
return activity_hist_noble, activity_hist_opp, activity_hist_malicious
def histogram_of_token_per_PAT(state):
pat_distribution = calculate_total_tokens_per_pat(state)
activity_distribution = calculate_total_activity_per_pat(state)
histogram_list_noble = []
histogram_list_opp = []
histogram_list_malicious = []
Keys = list(pat_distribution.keys())
Values = list(pat_distribution.values())
for v in range(0, len(Values)):
for iter in range(0, Values[v]):
if state['PATs'][v]['purpose'] == 'noble':
histogram_list_noble.append(Keys[v])
if state['PATs'][v]['purpose'] == 'opportunistic':
histogram_list_opp.append(Keys[v])
if state['PATs'][v]['purpose'] == 'malicious':
histogram_list_malicious.append(Keys[v])
#print("Keys: ", Keys)
#print("Values: ", Values)
#print("histog: ", histogram_list)
return histogram_list_noble, histogram_list_opp, histogram_list_malicious
def reconstruct_agent_database(data):
reconstruction = []
for i in range(0, len(data)):
if isinstance(data[i], dict):
reconstruction.append(data[i])
if isinstance(data[i], list):
reconstruction.append(data[i][0])
return reconstruction
def calculate_population_intention_array(state):
intention_array = []
database = reconstruct_agent_database(state['agents'])
for ag in database:
#print("-------------------------")
#print(ag['claimer_PAT_intention'])
intention_type = ag['claimer_PAT_intention']
if intention_type == "noble":
intention_array.append(1)
if intention_type == "opportunistic":
intention_array.append(2)
if intention_type == "malicious":
intention_array.append(3)
#print(intention_array)
return intention_array
def calculate_population_claim_style(state):
style_array = []
database = reconstruct_agent_database(state['agents'])
for ag in database:
#print("-------------------------")
#print(ag['claimer'])
clain_type = ag['claimer']
if clain_type == "compliant":
style_array.append(1)
if clain_type == "opportunistic":
style_array.append(2)
if clain_type == "cheater":
style_array.append(3)
#print(style_array)
return style_array
#------------------population distribution --------------------------
if 1:
n_bins = 3
intent_array = calculate_population_intention_array(setup.raw_result[-1])
intent = np.array(intent_array)
#print("intent: ", intent)
claim_array = calculate_population_claim_style(setup.raw_result[-1])
claimer = np.array(claim_array)
#print("claimer: ", claimer)
formatter = FuncFormatter(to_percent)
fig, axs = pl.subplots(1, 2, sharey=True, tight_layout=True)
#fig.suptitle('Population distribution \n')
axs[0].hist(intent, bins=n_bins, alpha=0.7, histtype='bar', ec='black', rwidth=0.5)
axs[0].set_xlim([1, 3])
axs[0].set_title("Intention")
axs[0].set_xticklabels([])
axs[0].set(xlabel='noble opportunistic malicious')
axs[1].hist(claimer, bins=n_bins, alpha=0.7, histtype='bar', ec='black', rwidth=0.5)
axs[1].set_xlim([1, 3])
axs[1].set_title("Compliance")
axs[1].set_xticklabels([])
axs[1].set(xlabel='compliant opportunistic cheater')
fig.gca().yaxis.set_major_formatter(formatter)
# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
ax.label_outer()
pl.savefig("Population_histograms" + ".png")
pl.show()
#-------------------visualisation-------------------------------------
if 0:
plt1 = pl.scatter(calculate_total_tokens_per_pat(setup.raw_result[-1]).keys(),
calculate_total_tokens_per_pat(setup.raw_result[-1]).values())
pl.xticks(np.arange(0, 5, 1))
pl.xlabel("PATs")
pl.ylabel("nr. of tokens")
pl.savefig("Tokens_per_PAT" + ".png")
pl.show()
#-------------------PAT and activity distribution -------------------------------------
if 1:
bins = np.linspace(0, len(setup.raw_result[-1]["PATs"]) - 1, 2 * len(setup.raw_result[-1]["PATs"]))
pat_array_noble, pat_array_opp, pat_array_malicious = histogram_of_token_per_PAT(setup.raw_result[-1])
activity_array_noble, activity_array_opp, activity_array_malicious = histogram_of_activity_per_PAT(setup.raw_result[-1])
np_pat_array_noble = np.array(pat_array_noble)
np_pat_array_opp = np.array(pat_array_opp)
np_pat_array_malicious = np.array(pat_array_malicious)
#print("np_pat_array_noble: ", np_pat_array_noble)
#print("np_pat_array_opp: ", np_pat_array_opp)
#print("np_pat_array_malicious: ", np_pat_array_malicious)
np_activity_array_noble = np.array(activity_array_noble)
np_activity_array_opp = np.array(activity_array_opp)
np_activity_array_malicious = np.array(activity_array_malicious)
#print("np_activity_array_noble: ", np_activity_array_noble)
#print("np_activity_array_opp: ", np_activity_array_opp)
#print("np_activity_array_malicious: ", np_activity_array_malicious)
plt.hist([np_pat_array_noble, np_activity_array_noble], bins, label=['noble tokens claimed', 'noble actions done'], color=["green", "lightgreen"])
#plt.hist([np_pat_array_opp, np_activity_array_opp], bins, label=["opp tokens claimed", "opp actions done"], color=["orange", "yellow"])
plt.hist([np_pat_array_malicious, np_activity_array_malicious], bins, label=['malicious tokens claimed', 'malicious actions done'], color=["red", "pink"])
plt.legend(loc='upper right')
plt.title("No. tokens and actions per token type created")
plt.xlabel('Token id')
plt.ylabel('No. tokens/actions')
plt.savefig("Token_impact" + ".png")
plt.show()