before graph firm prod

This commit is contained in:
HaoYizhi 2023-06-05 10:47:37 +08:00
parent af0d1dd2c3
commit 907d6cb179
8 changed files with 108 additions and 4 deletions

3
.vscode/launch.json vendored
View File

@ -13,7 +13,8 @@
"justMyCode": true,
"args": [
"--exp", "test",
"--reset_db", "True"
"--reset_db", "True",
"--job", "1"
]
}
]

Binary file not shown.

Binary file not shown.

View File

@ -93,7 +93,7 @@ class ControllerDB:
# insert exp
df_xv = pd.read_csv("xv.csv", index_col=None)
# read the OA table
df_oa = pd.read_csv("oa_with_exp.csv", index_col=None)
df_oa = pd.read_csv("oa_without_exp.csv", index_col=None)
df_oa = df_oa.iloc[:, 0:9]
for idx_scenario, row in df_oa.iterrows():
dct_exp_para = {}

View File

@ -196,3 +196,6 @@ class FirmAgent(ap.Agent):
def clean_before_time_step(self):
self.dct_n_trial_up_prod_removed = {}
self.a_lst_up_product_removed = ap.AgentList(self.model, [])
def get_firm_network_node(self):
return self.firm_network.positions[self]

104
model.py
View File

@ -146,7 +146,27 @@ class Model(ap.Model):
firm.a_lst_product_removed.append(product)
# draw network
# self.draw_network()
self.draw_network()
print(self.a_lst_total_firms)
print(self.a_lst_total_firms[133])
print(self.a_lst_total_firms[133].code)
print(self.firm_network.positions[self.a_lst_total_firms[133]])
print(
self.firm_network.graph[self.firm_network.positions[self.a_lst_total_firms[133]]])
iter_g = iter(
self.firm_network.graph[self.firm_network.positions[self.a_lst_total_firms[133]]])
f_t = next(iter_g)
print(f_t)
iter_g = iter(self.firm_network.graph[f_t])
print(next(iter_g))
print(self.a_lst_total_firms[133].get_firm_network_node())
print(list(self.dfs_edges_prod(self.firm_network.graph,
self.a_lst_total_firms[133],
self.a_lst_total_firms[133].a_lst_product[0])))
print(self.betweenness_centrality_firm_prod(self.firm_network.graph))
print()
def update(self):
self.a_lst_total_firms.clean_before_time_step()
@ -312,7 +332,7 @@ class Model(ap.Model):
node_label = nx.get_node_attributes(self.firm_network.graph, 'Name')
node_degree = dict(self.firm_network.graph.out_degree())
node_label = {
key: f"{node_label[key]} {node_degree[key]}"
key: f"{key} {node_label[key]} {node_degree[key]}"
for key in node_label.keys()
}
node_size = list(
@ -334,3 +354,83 @@ class Model(ap.Model):
edge_label,
font_size=4)
plt.savefig("network.png")
@ staticmethod
def dfs_edges_prod(G, start_firm, start_prod, depth_limit=None):
assert start_prod in start_firm.a_lst_product, \
f"product {start_prod.code} not in firm {start_firm.code}"
visited = set()
if depth_limit is None:
depth_limit = len(G)
start_node = start_firm.get_firm_network_node()
visited.add(start_firm)
stack = [(start_firm, start_prod, depth_limit, iter(G[start_node]))]
while stack:
parent_firm, parent_prod, depth_now, children_node = stack[-1]
try:
child_node = next(children_node)
child_firm = \
ap.AgentIter(start_firm.model, child_node).to_list()[0]
if child_firm not in visited:
for child_prod in child_firm.a_lst_product:
if child_prod not in parent_prod.a_successors():
continue
yield parent_firm.code, parent_prod.code, child_firm.code, child_prod.code
visited.add(child_firm)
if depth_now > 1:
stack.append((child_firm,
child_prod,
depth_now - 1,
iter(G[child_node])))
except StopIteration:
stack.pop()
@ staticmethod
def betweenness_centrality_firm_prod(G):
def _single_source_shortest_path_basic(G, s):
S = []
P = {}
for v in G:
P[v] = []
sigma = dict.fromkeys(G, 0.0) # sigma[v]=0 for v in G
D = {}
sigma[s] = 1.0
D[s] = 0
Q = [s]
while Q: # use BFS to find shortest paths
v = Q.pop(0)
S.append(v)
Dv = D[v]
sigmav = sigma[v]
for w in G[v]:
if w not in D:
Q.append(w)
D[w] = Dv + 1
if D[w] == Dv + 1: # this is a shortest path, count paths
sigma[w] += sigmav
P[w].append(v) # predecessors
return S, P, sigma
def _accumulate_basic(betweenness, S, P, sigma, s):
delta = dict.fromkeys(S, 0)
while S:
w = S.pop()
coeff = (1 + delta[w]) / sigma[w]
for v in P[w]:
delta[v] += sigma[v] * coeff
if w != s:
betweenness[w] += delta[w]
return betweenness
betweenness = dict.fromkeys(G, 0.0) # b[v]=0 for v in G
nodes = G
for s in nodes:
# single source shortest paths
# use BFS for unweighted g
S, P, sigma = _single_source_shortest_path_basic(G, s)
# accumulation without endpoint
betweenness = _accumulate_basic(betweenness, S, P, sigma, s)
return betweenness

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 MiB

After

Width:  |  Height:  |  Size: 3.5 MiB