forked from rolfsimoes/mkl_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pref_attachment.py
68 lines (59 loc) · 2.14 KB
/
Pref_attachment.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
# -*- coding: utf-8 -*-
import random
import pylab
N_slaves = 100 #Para visualizar bem a reta no log-log é preciso um valor grande
p_connect = 1.0
p_disconnect = 0.3
master_of = []
bag_of_masters = []
time_range = 10000 #Isto deve ser bem maior para capturar o comportamento
def setup(params):
global N_slaves, master_of, bag_of_masters, p_connect, p_disconnect, time_range
N_slaves = params["N"]
master_of = [-1 for i in xrange(N_slaves)]
bag_of_masters = []
p_connect = params["connect_prob"]
p_disconnect = params["disconnect_prob"]
time_range = params["time_range"]
def step():
for slave in xrange(N_slaves):
if master_of[slave] == -1:
if random.random() < p_connect:
if len(bag_of_masters) > 0:
choosed_master = random.choice(bag_of_masters)
else:
choosed_master = random.randint(0, N_slaves - 1)
master_of[slave] = choosed_master
bag_of_masters.append(choosed_master)
bag_of_masters.append(slave)
else:
if random.random() < p_disconnect:
bag_of_masters.remove(slave)
bag_of_masters.remove(master_of[slave])
master_of[slave] = -1
def run():
for t in xrange(time_range):
step()
def hist_masters_slave():
master_count = [0 for i in xrange(N_slaves)]
for slave in xrange(N_slaves):
if master_of[slave] != -1:
master_count[master_of[slave]] += 1
hist_result = [0 for i in xrange(max(master_count) + 1)]
for master in xrange(len(master_count)):
hist_result[master_count[master]] += 1
return hist_result
def plot(label):
pylab.plot(hist_masters_slave(), "-o", label=label)
pylab.yscale("log")
pylab.xscale("log")
def experiment(params):
setup(params)
run()
plot("disconnect_prob: " + str(params["disconnect_prob"]))
#######################################################EXPERIMENTS:
for i in xrange(0, 20, 3):
params = {"N": 1000, "time_range": 1000, "connect_prob":1.0, "disconnect_prob":0.05 * i}
experiment(params)
pylab.legend()
pylab.show()