Skip to content

Commit

Permalink
Final
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Zander committed Sep 7, 2018
1 parent 9545f3c commit 5659eb9
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 177 deletions.
159 changes: 102 additions & 57 deletions analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import matplotlib.pyplot as plt
import pickle

no = "5_2_4"
no = "4_3_4"
path = "Evaluation/"+ str(no) + ".pkl"

with open(path, 'rb') as handle:
record_attachment_probabilities = pickle.load(handle)


title = "Transactions = " + str(10000) + \
", " + r'$\lambda$' + " = " + str(50) + ", " + r'$\alpha$' + " = " + str(0.001)#+ \
title = "Transactions = " + str(22000) + \
", " + r'$\lambda$' + " = " + str(50) + ", " + r'$\alpha$' + " = " + str(0.1)#+ \
# ", " + r'$d$' + " = " + str(self.distances[1][0])


plt.figure(figsize=(20, 10))

plt.subplot(1, 2, 1)
# plt.subplot(1, 2, 1)

print(record_attachment_probabilities)

Expand All @@ -26,79 +26,124 @@

# print(self.record_attachment_probabilities)

labels = ["Agent " + str(i) for i in range(len(y))]
# labels = ["Agent " + str(i) for i in range(len(y))]

# ax = plt.axes()
# ax.set_color_cycle([plt.cm.tab20c(i) for i in np.linspace(0, 1, len(y))])
plt.plot(x,y)
# plt.ylim(0, 0.6)
plt.plot(x,y,label="Attachment probability sub-Tangle branch")
plt.ylim(0, 0.6)



print(len(y))
alpha = 0.005
index = 0
for i in list(y):
if i < 0.5 - alpha or i > 0.5 + alpha:
index = list(y).index(i)
break

print(index)
list = list(x)
start_average = list[index]
plt.axvline(x=start_average, color='silver', linestyle=":")


x = x[index:51]
y = y[index:51]

x_mean = [i for i in x]
y_mean = [np.mean(y) for i in y]

print(np.mean(y))
print(np.std(y))
plt.plot(x_mean, y_mean,\
label="Average <20,000 transactions", linestyle='-')


# plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)),\
# label="Best Fit", linestyle='--')
#
# x_mean = [i for i in x]
# y_mean = [np.mean(y) for i in y]
# print(np.mean(y))
# print(np.std(y))
# plt.plot(x_mean, y_mean,\
# label="Average", linestyle='-')

# lower_bound_95_confidence_interval = st.t.interval(0.80, len(partitioning_values)-1, loc=np.mean(partitioning_values), scale=st.sem(partitioning_values))[0]
# upper_bound_95_confidence_interval = st.t.interval(0.80, len(partitioning_values)-1, loc=np.mean(partitioning_values), scale=st.sem(partitioning_values))[1]
# plt.axhline(y=lower_bound_95_confidence_interval, color='r', linestyle='-')
# plt.axhline(y=upper_bound_95_confidence_interval, color='r', linestyle='-')

plt.xlabel("Transactions")
# plt.xticks([])
plt.ylabel("Probability to attach to sub-Tangle branch")
plt.legend(labels, loc="upper right", ncol=2)
# plt.legend(labels, loc='upper right')
textstr = '\n'.join((
r'mean = %.3f' % (np.mean(y), ),
r'std = %.3f' % (np.std(y), )))
print(textstr)

plt.subplot(1, 2, 2)
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

data = []
# place a text box in upper left in axes coords
plt.text(start_average+(max(x)-start_average)/2, 0.5, textstr, fontsize=12,
verticalalignment='top', bbox=props)

for agent in range(10):
agent_data = [i[1][agent] for i in record_attachment_probabilities]
data.append(agent_data)
print(str(agent) + " " + str(agent_data))
######################

print(data)

x = np.squeeze([i[0] for i in record_attachment_probabilities])
y = np.squeeze([i[1] for i in record_attachment_probabilities])
counter = 0
for i in x:
if i == 20000:
index = counter
print(index)
break
counter += 1

start_average = 20000
plt.axvline(x=start_average, color='silver', linestyle=":")

# labels = ["Agent " + str(i) for i in range(len(y))]

# ax = plt.axes()
# ax.set_color_cycle([plt.cm.tab20c(i) for i in np.linspace(0, 1, len(y))])
plt.boxplot(data, 0, '+')
# plt.ylim(0, 0.6)
x = x[50:]
y = y[50:]

x_mean = [i for i in x]
y_mean = [np.mean(y) for i in y]

print(np.mean(y))
print(np.std(y))
plt.plot(x_mean, y_mean,\
label="Average >20,000 transactions", linestyle='-')


# plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)),\
# label="Best Fit", linestyle='--')
#
# x_mean = [i for i in x]
# y_mean = [np.mean(y) for i in y]
# print(np.mean(y))
# print(np.std(y))
# plt.plot(x_mean, y_mean,\
# label="Average", linestyle='-')

# lower_bound_95_confidence_interval = st.t.interval(0.80, len(partitioning_values)-1, loc=np.mean(partitioning_values), scale=st.sem(partitioning_values))[0]
# upper_bound_95_confidence_interval = st.t.interval(0.80, len(partitioning_values)-1, loc=np.mean(partitioning_values), scale=st.sem(partitioning_values))[1]
# plt.axhline(y=lower_bound_95_confidence_interval, color='r', linestyle='-')
# plt.axhline(y=upper_bound_95_confidence_interval, color='r', linestyle='-')

plt.xlabel("Agents")
plt.xticks(np.arange(1, 11), np.arange(0, 10))
# plt.ylabel("Probability to attach to sub-Tangle branch")
# plt.legend(loc='upper left')
# plt.legend(labels, loc='upper left')
plt.suptitle(title)

plt.tight_layout()
plt.subplots_adjust(top=0.94)
textstr = '\n'.join((
r'mean = %.3f' % (np.mean(y), ),
r'std = %.3f' % (np.std(y), )))
print(textstr)

props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

# place a text box in upper left in axes coords
plt.text(start_average+(max(x)-start_average)/2, 0.4, textstr, fontsize=12,
verticalalignment='top', bbox=props)

######################
plt.xlabel("Transactions")
# plt.xticks([])
plt.ylabel("Probability to attach to sub-Tangle branch")
plt.legend(loc="upper left")
# plt.legend(labels, loc="upper right", ncol=2)
# plt.legend(labels, loc='upper right')

# plt.subplot(1, 2, 2)
#
# data = []
#
# for agent in range(10):
# agent_data = [i[1][agent] for i in record_attachment_probabilities]
# data.append(agent_data)
# print(str(agent) + " " + str(agent_data))
#
# print(data)
#
# plt.boxplot(data, 0, '+')
# plt.xlabel("Agents")
# plt.xticks(np.arange(1, 11), np.arange(0, 10))
# plt.suptitle(title)
plt.title(title)
plt.tight_layout()
# plt.subplots_adjust(top=0.94)
# plt.show()
plt.savefig(str(no) + '.png')

Expand Down
3 changes: 1 addition & 2 deletions simulation/agent.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Agent:
def __init__(self, counter):
self.id = counter

self.visible_transactions = []

#For measuring partitioning
#For analysis
self.agent_average_confirmation_confidence = 0
self.tips = []
self.record_tips = []
Expand Down
90 changes: 48 additions & 42 deletions simulation/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def print_graph(self):
#For genesis take agent 0 as default (always same value)
labels[self.transactions[0]] = str(np.round(self.transactions[0].exit_probability_multiple_agents[self.agents[0]],2))

#pos = graphviz_layout(self.DG, prog="dot", args="")
#col = [['r','b'][int(np.round(transaction.confirmation_confidence,1))] for transaction in self.DG.nodes()] #Color change for 100% confidence

#Coloring of nodes
Expand Down Expand Up @@ -135,12 +134,6 @@ def print_tips_over_time_multiple_agents_with_tangle(self, no_current_transactio
# np.poly1d(np.polyfit(self.arrival_times[cut_off:no_current_transactions-1], no_tips[cut_off:no_current_transactions-1], 1))\
# (np.unique(self.arrival_times[cut_off:no_current_transactions-1])), label="Best Fit Line", linestyle='--')

# no_tips = []
# for i in self.record_tips:
# no_tips.append(len(i))
#
# plt.plot(self.arrival_times[:no_current_transactions-1], no_tips, label="Tips issueing agent")

#Print title
title = "Transactions = " + str(self.no_of_transactions) + \
", " + r'$\lambda$' + " = " + str(self.lam) + \
Expand All @@ -152,7 +145,6 @@ def print_tips_over_time_multiple_agents_with_tangle(self, no_current_transactio
plt.legend(loc='upper left')
plt.title(title)


plt.subplot(2, 1, 2)

#Positioning and text of labels
Expand All @@ -168,7 +160,6 @@ def print_tips_over_time_multiple_agents_with_tangle(self, no_current_transactio
#For genesis take agent 0 as default (always same value)
labels[self.transactions[0]] = str(np.round(self.transactions[0].exit_probability_multiple_agents[self.agents[0]],2))

#pos = graphviz_layout(self.DG, prog="dot", args="")
#col = [['r','b'][int(np.round(transaction.confirmation_confidence,1))] for transaction in self.DG.nodes()] #Color change for 100% confidence

#Coloring of tips
Expand All @@ -177,8 +168,8 @@ def print_tips_over_time_multiple_agents_with_tangle(self, no_current_transactio
# self.DG.node[tip]["node_color"] = '#ffdbb8'
self.DG.node[tip]["node_color"] = self.agent_tip_colors[int(str(tip.agent))]


# col = list(nx.get_node_attributes(self.DG, 'node_color').values()) #Didn't work on Linux
#Didn't work on Linux
# col = list(nx.get_node_attributes(self.DG, 'node_color').values())
col = []
for transaction in self.DG:
col.append(self.DG.node[transaction]["node_color"])
Expand All @@ -188,14 +179,8 @@ def print_tips_over_time_multiple_agents_with_tangle(self, no_current_transactio
nx.draw_networkx(self.DG, pos, with_labels=True, node_size = 100, font_size=5.5, node_color = col)
#nx.draw_networkx_labels(self.DG, lower_pos, labels=labels, font_size=6)

#Print title
# title = "Transactions = " + str(self.no_of_transactions) +\
# ", " + r'$\lambda$' + " = " + str(self.lam)
# if(self.tip_selection_algo == "weighted"):
# title += ", " + r'$\alpha$' + " = " + str(self.alpha)
plt.xlabel("Time (s)")
plt.yticks([])
# plt.title(title)
plt.show()


Expand Down Expand Up @@ -230,12 +215,6 @@ def print_tips_over_time_multiple_agents(self, no_current_transactions):
# np.poly1d(np.polyfit(self.arrival_times[cut_off:no_current_transactions-1], no_tips[cut_off:no_current_transactions-1], 1))\
# (np.unique(self.arrival_times[cut_off:no_current_transactions-1])), label="Best Fit Line", linestyle='--')

# no_tips = []
# for i in self.record_tips:
# no_tips.append(len(i))
#
# plt.plot(self.arrival_times[:no_current_transactions-1], no_tips, label="Tips issueing agent")

#Print title
title = "Transactions = " + str(self.no_of_transactions) + \
", " + r'$\lambda$' + " = " + str(self.lam) + \
Expand All @@ -246,13 +225,10 @@ def print_tips_over_time_multiple_agents(self, no_current_transactions):
plt.ylabel("Number of tips")
plt.legend(loc='upper left')
plt.title(title)


plt.show()



def print_attachment_probabilities(self):
def print_attachment_probabilities_alone(self):

title = "Transactions = " + str(self.no_of_transactions) + \
", " + r'$\lambda$' + " = " + str(self.lam) + \
Expand All @@ -266,17 +242,9 @@ def print_attachment_probabilities(self):

plt.figure(figsize=(14, 8))

print(self.record_attachment_probabilities)

x = np.squeeze([i[0] for i in self.record_attachment_probabilities])
y = np.squeeze([i[1] for i in self.record_attachment_probabilities])

print(self.record_attachment_probabilities)

labels = ["Agent " + str(i) for i in range(len(y))]

# ax = plt.axes()
# ax.set_color_cycle([plt.cm.tab20c(i) for i in np.linspace(0, 1, len(y))])
plt.plot(x,y, label="Attachment probability sub-Tangle branch")
plt.ylim(0, 0.7)

Expand All @@ -290,17 +258,55 @@ def print_attachment_probabilities(self):
plt.plot(x_mean, y_mean,\
label="Average", linestyle='-')

# lower_bound_95_confidence_interval = st.t.interval(0.80, len(partitioning_values)-1, loc=np.mean(partitioning_values), scale=st.sem(partitioning_values))[0]
# upper_bound_95_confidence_interval = st.t.interval(0.80, len(partitioning_values)-1, loc=np.mean(partitioning_values), scale=st.sem(partitioning_values))[1]
# plt.axhline(y=lower_bound_95_confidence_interval, color='r', linestyle='-')
# plt.axhline(y=upper_bound_95_confidence_interval, color='r', linestyle='-')

plt.xlabel("Transactions")
# plt.xticks([])
plt.ylabel("Probability to attach to sub-Tangle branch")
plt.legend(loc='upper left')
#plt.legend(labels, loc='upper left')
plt.title(title)
plt.tight_layout()
# plt.show()
# plt.savefig('graph' + str(title) + '_3' + '.png')


def print_attachment_probabilities_all_agents(self):

title = "Transactions = " + str(self.no_of_transactions) + \
", " + r'$\lambda$' + " = " + str(self.lam) + \
", " + r'$d$' + " = " + str(self.distances[1][0])
if (self.tip_selection_algo == "weighted"):
title += ", " + r'$\alpha$' + " = " + str(self.alpha)

plt.figure(figsize=(20, 10))

#Attachment probabilities
plt.subplot(1, 2, 1)

x = np.squeeze([i[0] for i in self.record_attachment_probabilities])
y = np.squeeze([i[1] for i in self.record_attachment_probabilities])

labels = ["Agent " + str(i) for i in range(len(y))]

#For more than 10 agents
# ax = plt.axes()
# ax.set_color_cycle([plt.cm.tab20c(i) for i in np.linspace(0, 1, len(y))])
plt.plot(x, y)
plt.xlabel("Transactions")
plt.ylabel("Probability to attach to sub-Tangle branch")
plt.legend(labels, loc="upper right", ncol=2)

#Boxplot
plt.subplot(1, 2, 2)

data = []

for agent in range(10):
agent_data = [i[1][agent] for i in self.record_attachment_probabilities]
data.append(agent_data)

plt.boxplot(data, 0, '+')
plt.xlabel("Agents")
plt.xticks(np.arange(1, 11), np.arange(0, 10))
plt.suptitle(title)
plt.tight_layout()
plt.subplots_adjust(top=0.94)
# plt.show()
# plt.savefig(str(no) + '.png')
Loading

0 comments on commit 5659eb9

Please sign in to comment.