From 356582e2f3024c8f99396ded01f83bf690d50f5f Mon Sep 17 00:00:00 2001 From: Cricial <2911646453@qq.com> Date: Wed, 18 Sep 2024 16:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=8C=E5=8C=85=E6=8B=AC=E5=8F=98=E9=87=8F=E5=90=8D?= =?UTF-8?q?=E5=AD=90=EF=BC=8C=E5=8C=96=E4=B8=BA=20=E5=B0=8F=E5=86=99?= =?UTF-8?q?=EF=BC=8C=E8=83=BD=E8=BF=90=E8=A1=8C=E7=9A=84mesa=E7=9A=84?= =?UTF-8?q?=E5=AE=8C=E6=95=B4=E7=A8=8B=E5=BA=8F=EF=BC=8C=E4=B8=8D=E5=8C=85?= =?UTF-8?q?=E6=8B=AC=E6=A8=A1=E5=9E=8B=E6=9B=B4=E6=94=B9=E5=92=8C=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E8=BE=93=E5=87=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __pycache__/controller_db.cpython-38.pyc | Bin 8257 -> 8257 bytes __pycache__/my_model.cpython-38.pyc | Bin 10329 -> 10329 bytes controller_db.py | 21 +++++++-------- firm.py | 4 --- my_model.py | 31 ++++++++++------------- product.py | 3 --- 6 files changed, 25 insertions(+), 34 deletions(-) diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index 38608ab2537369902aa40c548d483a8f0b941349..293cecea00c6798f71d96541b5990d671444bb8b 100644 GIT binary patch delta 20 acmX@;aL|D}l$V!_0SFlDUv1I diff --git a/__pycache__/my_model.cpython-38.pyc b/__pycache__/my_model.cpython-38.pyc index 033717c9183a842a9253409fb5b2ce9b85b115de..36151650435222515b599e6493e66453b90b15ef 100644 GIT binary patch delta 1087 zcmZXSPfQa*6vjKduxz)q(xx=oLR)E}wWtJ80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diff --git a/controller_db.py b/controller_db.py index 2ed557b..409056f 100644 --- a/controller_db.py +++ b/controller_db.py @@ -44,9 +44,9 @@ class ControllerDB: self.fill_sample_table() def fill_experiment_table(self): - Firm = pd.read_csv("input_data/Firm_amended.csv") - Firm['Code'] = Firm['Code'].astype('string') - Firm.fillna(0, inplace=True) + firm = pd.read_csv("input_data/Firm_amended.csv") + firm['Code'] = firm['Code'].astype('string') + firm.fillna(0, inplace=True) # fill dct_lst_init_disrupt_firm_prod # 存储 公司-在供应链结点的位置.. 0 :‘1.1’ @@ -63,7 +63,8 @@ class ControllerDB: else: # 行索引 (index):这一行在数据帧中的索引值。 # 行数据 (row):这一行的数据,是一个 pandas.Series 对象,包含该行的所有列和值。 - for _, row in Firm.iterrows(): + + for _, row in firm.iterrows(): code = row['Code'] row = row['1':] for product_code in row.index[row == 1].to_list(): @@ -72,18 +73,18 @@ class ControllerDB: # fill g_bom # 结点属性值 相当于 图上点的 原始 产品名称 - BomNodes = pd.read_csv('input_data/BomNodes.csv', index_col=0) - BomNodes.set_index('Code', inplace=True) + bom_nodes = pd.read_csv('input_data/BomNodes.csv', index_col=0) + bom_nodes.set_index('Code', inplace=True) - BomCateNet = pd.read_csv('input_data/BomCateNet.csv', index_col=0) - BomCateNet.fillna(0, inplace=True) + bom_cate_net = pd.read_csv('input_data/BomCateNet.csv', index_col=0) + bom_cate_net.fillna(0, inplace=True) # 创建 可以多边的有向图 同时 转置操作 使得 上游指向下游结点 也就是 1.1.1 - 1.1 类似这种 - g_bom = nx.from_pandas_adjacency(BomCateNet.T, + g_bom = nx.from_pandas_adjacency(bom_cate_net.T, create_using=nx.MultiDiGraph()) # 填充每一个结点 的具体内容 通过 相同的 code 并且通过BomNodes.loc[code].to_dict()字典化 格式类似 格式 { code(0) : {level: 0 ,name: 工业互联网 }} bom_labels_dict = {} for code in g_bom.nodes: - bom_labels_dict[code] = BomNodes.loc[code].to_dict() + bom_labels_dict[code] = bom_nodes.loc[code].to_dict() # 分配属性 给每一个结点 获得类似 格式:{1: {'label': 'A', 'value': 10}, nx.set_node_attributes(g_bom, bom_labels_dict) # 改为json 格式 diff --git a/firm.py b/firm.py index 561ea80..41aa190 100644 --- a/firm.py +++ b/firm.py @@ -229,7 +229,3 @@ class FirmAgent(Agent): return False else: return False - - def step(self): - # 在每个时间步进行的操作 - pass diff --git a/my_model.py b/my_model.py index cf868b2..1792522 100644 --- a/my_model.py +++ b/my_model.py @@ -15,7 +15,7 @@ from product import ProductAgent class MyModel(Model): def __init__(self, params): - # self.num_agents = N + # 属性 self.is_prf_size = params['prf_size'] self.prf_conn = params['prf_conn'] self.cap_limit_prob_type = params['cap_limit_prob_type'] @@ -35,10 +35,10 @@ class MyModel(Model): self.data_collector = DataCollector( agent_reporters={"Product": "name"} ) + # initialize graph bom self.G_bom = nx.adjacency_graph(json.loads(params['g_bom'])) - # Create the firm-product network graph self.G_FirmProd = nx.MultiDiGraph() # Create the firm network graph @@ -54,13 +54,13 @@ class MyModel(Model): self.int_stop_ts = 0 self.int_n_iter = int(params['n_iter']) self.dct_lst_init_disrupt_firm_prod = params['dct_lst_init_disrupt_firm_prod'] + # external variable self.int_n_max_trial = int(params['n_max_trial']) self.is_prf_size = bool(params['prf_size']) - self.remove_t = int(params['remove_t']) self.int_netw_prf_n = int(params['netw_prf_n']) - + # 方法执行 self.initialize_product_network(params) self.initialize_firm_network() self.initialize_firm_product_network() @@ -76,27 +76,25 @@ class MyModel(Model): print(f"Failed to initialize product network: {e}") def initialize_firm_network(self): - """ Initialize the firm network by reading firm data from a CSV file """ # Read the firm data - Firm = pd.read_csv("input_data/Firm_amended.csv") - Firm['Code'] = Firm['Code'].astype('string') - Firm.fillna(0, inplace=True) - Firm_attr = Firm.loc[:, ["Code", "Type_Region", "Revenue_Log"]] + firm = pd.read_csv("input_data/Firm_amended.csv") + firm['Code'] = firm['Code'].astype('string') + firm.fillna(0, inplace=True) + firm_attr = firm.loc[:, ["Code", "Type_Region", "Revenue_Log"]] firm_product = [] - for _, row in Firm.loc[:, '1':].iterrows(): + for _, row in firm.loc[:, '1':].iterrows(): firm_product.append(row[row == 1].index.to_list()) - Firm_attr.loc[:, 'Product_Code'] = firm_product - Firm_attr.set_index('Code', inplace=True) + firm_attr.loc[:, 'Product_Code'] = firm_product + firm_attr.set_index('Code', inplace=True) - - self.G_Firm.add_nodes_from(Firm["Code"]) + self.G_Firm.add_nodes_from(firm["Code"]) # Assign attributes to the firm nodes - firm_labels_dict = {code: Firm_attr.loc[code].to_dict() for code in self.G_Firm.nodes} + firm_labels_dict = {code: firm_attr.loc[code].to_dict() for code in self.G_Firm.nodes} nx.set_node_attributes(self.G_Firm, firm_labels_dict) - self.Firm = Firm + self.Firm = firm def initialize_firm_product_network(self): """ Initialize the firm-product network """ @@ -244,7 +242,6 @@ class MyModel(Model): # 更新 self.dct_lst_init_disrupt_firm_prod 字典,存储公司及其受干扰的产品 self.dct_lst_init_disrupt_firm_prod = t_dct - # 设置初始受干扰的公司产品状态 for firm, a_lst_product in self.dct_lst_init_disrupt_firm_prod.items(): for product in a_lst_product: diff --git a/product.py b/product.py index 3eaabd6..51181ed 100644 --- a/product.py +++ b/product.py @@ -23,6 +23,3 @@ class ProductAgent(Agent): # 通过 unique_id 查找前驱节点对应的代理对象,直接从 self.product_agents 列表中获取 return [agent for agent in self.model.product_agents if agent.unique_id in predecessors] - def step(self): - # 在每个时间步 进行的操作 - pass