-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_aimd.m
106 lines (87 loc) · 3.89 KB
/
generate_aimd.m
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
function [Tangle, Results] = generate_aimd(Tangle, mcNum, Results)
%% Tangle Simulation
% Create waitbar
f = waitbar(0, num2str(mcNum),'Name','Tangle Iteration Completion Bar',...
'CreateCancelBtn','setappdata(gcbf,''canceling'', 1)');
setappdata(f,'canceling',0);
for t = 0:Tangle.dt:Tangle.simTime-Tangle.dt
% Update waitbar
if getappdata(f,'canceling')
break
end
waitbar(t/Tangle.simTime,f)
% AIMD update every second
if(floor(t)==t && t~=0)
nOrphans = 0;
for n = 1:Tangle.nNodes
Results.alphas(n,t) = Tangle.Nodes(n).alpha;
nodeOrphans = find(([Tangle.Sites.node]==n).*[Tangle.Sites.isTip].*([Tangle.Sites.age]>=Tangle.orphanAge));
for o = nodeOrphans
Tangle.Sites(o).node = 0; % remove association between this node and its orphaned site
end
nOrphans = nOrphans + length(nodeOrphans);
if(t>1)
Results.orphanRate(n,t) = length(nodeOrphans);
Results.avgOrphanRate(n,:) = filter(Tangle.bAvgFilt,1,Results.orphanRate(n,:));
Results.nOrphans(n,t) = Results.nOrphans(n,t-1) + length(nodeOrphans);
else
Results.orphanRate(n,t) = length(nodeOrphans);
Results.avgOrphanRate(n,t) = length(nodeOrphans);
Results.nOrphans(n,t) = length(nodeOrphans);
end
%% AIMD Part
if(Results.nOrphans(n,t)<Tangle.orphanRate) % additive increase
Results.nOrphans(n,t) = 0;
Tangle.Nodes(n).alpha = Tangle.Nodes(n).alpha + Tangle.Nodes(n).alpha_AIMD;
else % multiplicative decrease
Tangle.Nodes(n).alpha = Tangle.Nodes(n).alpha*Tangle.Nodes(n).beta_AIMD;
end
end
% if(nOrphans>0)
% backOffNode = ceil(Tangle.nNodes*rand());
% else
% backOffNode = 0;
% end
%
% for n = 1:Tangle.nNodes
% if(n~=backOffNode) % additive increase
% Tangle.Nodes(n).alpha = Tangle.Nodes(n).alpha + Tangle.Nodes(n).alpha_AIMD;
% else % multiplicative decrease
% Tangle.Nodes(n).alpha = Tangle.Nodes(n).alpha*Tangle.Nodes(n).beta_AIMD;
% end
% end
end
% add new arrivals for this time step by AIMD
for node = 1:Tangle.nNodes
if(t/Tangle.dt < Tangle.simTime)
Tangle = newarrivals(Tangle, Tangle.lambda(ceil((t+Tangle.dt)/Tangle.dt))/Tangle.nNodes, 0, 0, node); % each node has equal proportion of computing power
end
end
for i = 1:Tangle.size
% set new CW as unchanged to begin with
Tangle.Sites(i).cumulativeWeight = [Tangle.Sites(i).cumulativeWeight(2:Tangle.nCW) Tangle.Sites(i).cumulativeWeight(Tangle.nCW)];
% if this site is not yet added to the tangle (pending)
if ~Tangle.Sites(i).isTip && ~Tangle.Sites(i).isSelected
% if the site has found suitable tips to validate - PoW in
% progress
if Tangle.Sites(i).selectedParents
% do PoW
Tangle = doPoW(Tangle, i);
if Tangle.Sites(i).isAttached
continue
end
% else look for some tips to validate
else
n = Tangle.Sites(i).node;
Tangle = selecttips(Tangle, i, Tangle.Nodes(n).alpha, Tangle.Nodes(n).beta, Tangle.Nodes(n).alpha, Tangle.Nodes(n).beta);
Tangle.Sites(i).timePending = Tangle.Sites(i).timePending + Tangle.dt;
end
% else, this has been added to the tangle so increment its age
else
Tangle.Sites(i).age = Tangle.Sites(i).age + Tangle.dt;
end
end
end
Results.FinalTangles(mcNum) = Tangle;
close(f);
delete(f);