formatting
This commit is contained in:
parent
83eeeb9d14
commit
936b12b9f6
123
model.py
123
model.py
|
@ -50,10 +50,7 @@ class Model(ap.Model):
|
||||||
|
|
||||||
# init graph firm prod
|
# init graph firm prod
|
||||||
Firm_Prod = pd.read_csv("Firm_amended.csv")
|
Firm_Prod = pd.read_csv("Firm_amended.csv")
|
||||||
# Firm['Code'] = Firm['Code'].astype('string')
|
|
||||||
Firm_Prod.fillna(0, inplace=True)
|
Firm_Prod.fillna(0, inplace=True)
|
||||||
# Firm_attr = Firm.loc[:, ["Code", "Name", "Type_Region", "Revenue_Log"]]
|
|
||||||
# Firm_attr.set_index('Code', inplace=True)
|
|
||||||
firm_prod = pd.DataFrame({'bool': Firm_Prod.loc[:, '1':].stack()})
|
firm_prod = pd.DataFrame({'bool': Firm_Prod.loc[:, '1':].stack()})
|
||||||
firm_prod = firm_prod[firm_prod['bool'] == 1].reset_index()
|
firm_prod = firm_prod[firm_prod['bool'] == 1].reset_index()
|
||||||
firm_prod.drop('bool', axis=1, inplace=True)
|
firm_prod.drop('bool', axis=1, inplace=True)
|
||||||
|
@ -216,9 +213,6 @@ class Model(ap.Model):
|
||||||
self.a_lst_total_firms.select(
|
self.a_lst_total_firms.select(
|
||||||
[firm.code == pro_firm_prod_node['Firm_Code']
|
[firm.code == pro_firm_prod_node['Firm_Code']
|
||||||
for firm in self.a_lst_total_firms])[0]
|
for firm in self.a_lst_total_firms])[0]
|
||||||
|
|
||||||
# print(list(self.firm_prod_network.predecessors(firm_prod_node)))
|
|
||||||
|
|
||||||
lst_shortest_path = \
|
lst_shortest_path = \
|
||||||
list(nx.all_shortest_paths(self.firm_prod_network,
|
list(nx.all_shortest_paths(self.firm_prod_network,
|
||||||
source=init_node,
|
source=init_node,
|
||||||
|
@ -246,16 +240,11 @@ class Model(ap.Model):
|
||||||
sum([True if di_supp_code in path else False
|
sum([True if di_supp_code in path else False
|
||||||
for path in lst_shortest_path]) \
|
for path in lst_shortest_path]) \
|
||||||
/ len(lst_shortest_path)
|
/ len(lst_shortest_path)
|
||||||
# print(lst_shortest_path)
|
|
||||||
# print(sum([True if di_supp_code in path else False
|
|
||||||
# for path in lst_shortest_path]), len(lst_shortest_path))
|
|
||||||
drs = n2n_betweenness / \
|
drs = n2n_betweenness / \
|
||||||
(len(lst_cand) * di_supp_firm.revenue_log)
|
(len(lst_cand) * di_supp_firm.revenue_log)
|
||||||
# print(drs)
|
|
||||||
dct_drs[di_supp_code] = drs
|
dct_drs[di_supp_code] = drs
|
||||||
dct_drs = dict(sorted(
|
dct_drs = dict(sorted(
|
||||||
dct_drs.items(), key=lambda kv: kv[1], reverse=True))
|
dct_drs.items(), key=lambda kv: kv[1], reverse=True))
|
||||||
print(dct_drs)
|
|
||||||
for di_supp_code in dct_drs.keys():
|
for di_supp_code in dct_drs.keys():
|
||||||
di_supp_node = \
|
di_supp_node = \
|
||||||
self.firm_prod_network.nodes[di_supp_code]
|
self.firm_prod_network.nodes[di_supp_code]
|
||||||
|
@ -278,42 +267,12 @@ class Model(ap.Model):
|
||||||
'Product': di_supp_prod.code
|
'Product': di_supp_prod.code
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
|
# change capacity
|
||||||
select_cand.dct_prod_capacity[di_supp_prod] -= 1
|
select_cand.dct_prod_capacity[di_supp_prod] -= 1
|
||||||
|
|
||||||
# draw network
|
# 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))
|
|
||||||
|
|
||||||
t = nx.dfs_successors(self.firm_prod_network, [n for n, v in self.firm_prod_network.nodes(data=True)
|
|
||||||
if v['Firm_Code'] == self.a_lst_total_firms[133].code and
|
|
||||||
v['Product_Code'] == self.a_lst_total_firms[133].a_lst_product[0].code][0])
|
|
||||||
print(t)
|
|
||||||
for key, values in t.items():
|
|
||||||
pred_node = self.firm_prod_network.nodes[key]
|
|
||||||
for value in values:
|
|
||||||
succ_node = self.firm_prod_network.nodes[value]
|
|
||||||
print(pred_node['Firm_Code'], pred_node['Product_Code'],
|
|
||||||
succ_node['Firm_Code'], succ_node['Product_Code'])
|
|
||||||
print()
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.a_lst_total_firms.clean_before_time_step()
|
self.a_lst_total_firms.clean_before_time_step()
|
||||||
# output
|
# output
|
||||||
|
@ -500,83 +459,3 @@ class Model(ap.Model):
|
||||||
edge_label,
|
edge_label,
|
||||||
font_size=4)
|
font_size=4)
|
||||||
plt.savefig("network.png")
|
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
|
|
||||||
|
|
Loading…
Reference in New Issue