database store graph

This commit is contained in:
2023-03-16 15:07:43 +08:00
parent 9435484e3f
commit 082864814b
11 changed files with 62 additions and 31 deletions

View File

@@ -7,6 +7,7 @@ from firm import FirmAgent
from product import ProductAgent
from orm import db_session, Result
import platform
import json
class Model(ap.Model):
@@ -20,18 +21,8 @@ class Model(ap.Model):
self.dct_lst_remove_firm_prod = self.p.dct_lst_init_remove_firm_prod
# init graph bom
BomNodes = pd.read_csv('BomNodes.csv', index_col=0)
BomNodes.set_index('Code', inplace=True)
BomCateNet = pd.read_csv('BomCateNet.csv', index_col=0)
BomCateNet.fillna(0, inplace=True)
G_bom = nx.from_pandas_adjacency(BomCateNet.T,
create_using=nx.MultiDiGraph())
bom_labels_dict = {}
for code in G_bom.nodes:
bom_labels_dict[code] = BomNodes.loc[code].to_dict()
nx.set_node_attributes(G_bom, bom_labels_dict)
G_bom = nx.adjacency_graph(json.loads(self.p.g_bom))
self.product_network = ap.Network(self, G_bom)
# init graph firm
Firm = pd.read_csv("Firm_amended.csv")
@@ -90,13 +81,8 @@ class Model(ap.Model):
}) for succ_firm in lst_choose_firm]
G_Firm.add_edges_from(lst_add_edge)
self.sample.g_firm = json.dumps(nx.adjacency_data(G_Firm))
self.firm_network = ap.Network(self, G_Firm)
self.product_network = ap.Network(self, G_bom)
out_file = open("myfile.json", "w")
import json
json.dump(nx.adjacency_data(G_Firm), out_file)
out_file.close()
# init product
for ag_node, attr in self.product_network.graph.nodes(data=True):
@@ -151,6 +137,10 @@ class Model(ap.Model):
# draw network
# self.draw_network()
out_file = open("myfile.json", "w")
json.dump(nx.adjacency_data(G_Firm), out_file)
out_file.close()
def update(self):
self.a_lst_total_firms.clean_before_time_step()
# output