-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_shock_scenarios.py
57 lines (46 loc) · 1.6 KB
/
run_shock_scenarios.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
import multiprocessing as mp
import time
import matplotlib.pyplot as plt
import numpy as np
import network.core.skeleton as ns
from helpers.agent_fisher import get_agents
from network.fin.fin_model import FinNetwork
def per_probability(count, n, pos, arange):
all_deads = []
for k in arange:
networks = [FinNetwork("Net {}".format(y), get_agents(n), net_type=ns.power_law_graph, p=k) for y in
range(count)]
deads = []
for net in networks:
net.apply_shock(pos)
for i in range(10):
net.step()
deads.append(sum([1 for x in net.schedule.agents if x.defaults]))
all_deads.append(sum(deads) / count)
return pos, all_deads
result_list = {}
def log_result(result):
k, v = result
result_list[k] = v
def build_graph(model_):
import networkx as nx
model_graph = nx.DiGraph()
for node in model_.schedule.agents:
model_graph.add_node(node)
for edge in node.edges:
model_graph.add_edge(edge.node_from, edge.node_to)
return model_graph
if __name__ == '__main__':
t_start = time.time()
# pool = mp.Pool(processes=4)
# arange = np.arange(0.0, 1.0, 0.025)
# for x in range(29):
# pool.apply_async(per_probability, args=(2, 29, x, arange,), callback=log_result)
# pool.close()
# pool.join()
# for k, v in result_list.items():
# plt.plot(arange, v)
fin_network = FinNetwork("Net 1", get_agents(10), net_type=ns.power_law_graph, p=.5)
gg = build_graph(fin_network)
print("T1:{}".format(time.time() - t_start))
plt.show()