修改部分内容,包括变量名子,化为 小写,能运行的mesa的完整程序,不包括模型更改和结果输出。
This commit is contained in:
		
							parent
							
								
									ede00b0403
								
							
						
					
					
						commit
						356582e2f3
					
				
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							| 
						 | 
					@ -44,9 +44,9 @@ class ControllerDB:
 | 
				
			||||||
        self.fill_sample_table()
 | 
					        self.fill_sample_table()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def fill_experiment_table(self):
 | 
					    def fill_experiment_table(self):
 | 
				
			||||||
        Firm = pd.read_csv("input_data/Firm_amended.csv")
 | 
					        firm = pd.read_csv("input_data/Firm_amended.csv")
 | 
				
			||||||
        Firm['Code'] = Firm['Code'].astype('string')
 | 
					        firm['Code'] = firm['Code'].astype('string')
 | 
				
			||||||
        Firm.fillna(0, inplace=True)
 | 
					        firm.fillna(0, inplace=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # fill dct_lst_init_disrupt_firm_prod
 | 
					        # fill dct_lst_init_disrupt_firm_prod
 | 
				
			||||||
        # 存储 公司-在供应链结点的位置.. 0 :‘1.1’
 | 
					        # 存储 公司-在供应链结点的位置.. 0 :‘1.1’
 | 
				
			||||||
| 
						 | 
					@ -63,7 +63,8 @@ class ControllerDB:
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            # 行索引 (index):这一行在数据帧中的索引值。
 | 
					            # 行索引 (index):这一行在数据帧中的索引值。
 | 
				
			||||||
            # 行数据 (row):这一行的数据,是一个 pandas.Series 对象,包含该行的所有列和值。
 | 
					            # 行数据 (row):这一行的数据,是一个 pandas.Series 对象,包含该行的所有列和值。
 | 
				
			||||||
            for _, row in Firm.iterrows():
 | 
					
 | 
				
			||||||
 | 
					            for _, row in firm.iterrows():
 | 
				
			||||||
                code = row['Code']
 | 
					                code = row['Code']
 | 
				
			||||||
                row = row['1':]
 | 
					                row = row['1':]
 | 
				
			||||||
                for product_code in row.index[row == 1].to_list():
 | 
					                for product_code in row.index[row == 1].to_list():
 | 
				
			||||||
| 
						 | 
					@ -72,18 +73,18 @@ class ControllerDB:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # fill g_bom
 | 
					        # fill g_bom
 | 
				
			||||||
        # 结点属性值 相当于 图上点的 原始 产品名称
 | 
					        # 结点属性值 相当于 图上点的 原始 产品名称
 | 
				
			||||||
        BomNodes = pd.read_csv('input_data/BomNodes.csv', index_col=0)
 | 
					        bom_nodes = pd.read_csv('input_data/BomNodes.csv', index_col=0)
 | 
				
			||||||
        BomNodes.set_index('Code', inplace=True)
 | 
					        bom_nodes.set_index('Code', inplace=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        BomCateNet = pd.read_csv('input_data/BomCateNet.csv', index_col=0)
 | 
					        bom_cate_net = pd.read_csv('input_data/BomCateNet.csv', index_col=0)
 | 
				
			||||||
        BomCateNet.fillna(0, inplace=True)
 | 
					        bom_cate_net.fillna(0, inplace=True)
 | 
				
			||||||
        # 创建 可以多边的有向图 同时 转置操作 使得 上游指向下游结点 也就是 1.1.1 - 1.1 类似这种
 | 
					        # 创建 可以多边的有向图 同时 转置操作 使得 上游指向下游结点 也就是 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())
 | 
					                                         create_using=nx.MultiDiGraph())
 | 
				
			||||||
        # 填充每一个结点 的具体内容 通过 相同的 code 并且通过BomNodes.loc[code].to_dict()字典化 格式类似 格式 { code(0) :  {level: 0 ,name: 工业互联网   }}
 | 
					        # 填充每一个结点 的具体内容 通过 相同的 code 并且通过BomNodes.loc[code].to_dict()字典化 格式类似 格式 { code(0) :  {level: 0 ,name: 工业互联网   }}
 | 
				
			||||||
        bom_labels_dict = {}
 | 
					        bom_labels_dict = {}
 | 
				
			||||||
        for code in g_bom.nodes:
 | 
					        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},
 | 
					        # 分配属性 给每一个结点 获得类似 格式:{1: {'label': 'A', 'value': 10},
 | 
				
			||||||
        nx.set_node_attributes(g_bom, bom_labels_dict)
 | 
					        nx.set_node_attributes(g_bom, bom_labels_dict)
 | 
				
			||||||
        # 改为json 格式
 | 
					        # 改为json 格式
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										4
									
								
								firm.py
								
								
								
								
							
							
						
						
									
										4
									
								
								firm.py
								
								
								
								
							| 
						 | 
					@ -229,7 +229,3 @@ class FirmAgent(Agent):
 | 
				
			||||||
                return False
 | 
					                return False
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            return False
 | 
					            return False
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def step(self):
 | 
					 | 
				
			||||||
        # 在每个时间步进行的操作
 | 
					 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										31
									
								
								my_model.py
								
								
								
								
							
							
						
						
									
										31
									
								
								my_model.py
								
								
								
								
							| 
						 | 
					@ -15,7 +15,7 @@ from product import ProductAgent
 | 
				
			||||||
class MyModel(Model):
 | 
					class MyModel(Model):
 | 
				
			||||||
    def __init__(self, params):
 | 
					    def __init__(self, params):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # self.num_agents = N
 | 
					        # 属性
 | 
				
			||||||
        self.is_prf_size = params['prf_size']
 | 
					        self.is_prf_size = params['prf_size']
 | 
				
			||||||
        self.prf_conn = params['prf_conn']
 | 
					        self.prf_conn = params['prf_conn']
 | 
				
			||||||
        self.cap_limit_prob_type = params['cap_limit_prob_type']
 | 
					        self.cap_limit_prob_type = params['cap_limit_prob_type']
 | 
				
			||||||
| 
						 | 
					@ -35,10 +35,10 @@ class MyModel(Model):
 | 
				
			||||||
        self.data_collector = DataCollector(
 | 
					        self.data_collector = DataCollector(
 | 
				
			||||||
            agent_reporters={"Product": "name"}
 | 
					            agent_reporters={"Product": "name"}
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # initialize graph bom
 | 
					        # initialize graph bom
 | 
				
			||||||
        self.G_bom = nx.adjacency_graph(json.loads(params['g_bom']))
 | 
					        self.G_bom = nx.adjacency_graph(json.loads(params['g_bom']))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Create the firm-product network graph
 | 
					        # Create the firm-product network graph
 | 
				
			||||||
        self.G_FirmProd = nx.MultiDiGraph()
 | 
					        self.G_FirmProd = nx.MultiDiGraph()
 | 
				
			||||||
        # Create the firm network graph
 | 
					        # Create the firm network graph
 | 
				
			||||||
| 
						 | 
					@ -54,13 +54,13 @@ class MyModel(Model):
 | 
				
			||||||
        self.int_stop_ts = 0
 | 
					        self.int_stop_ts = 0
 | 
				
			||||||
        self.int_n_iter = int(params['n_iter'])
 | 
					        self.int_n_iter = int(params['n_iter'])
 | 
				
			||||||
        self.dct_lst_init_disrupt_firm_prod = params['dct_lst_init_disrupt_firm_prod']
 | 
					        self.dct_lst_init_disrupt_firm_prod = params['dct_lst_init_disrupt_firm_prod']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # external variable
 | 
					        # external variable
 | 
				
			||||||
        self.int_n_max_trial = int(params['n_max_trial'])
 | 
					        self.int_n_max_trial = int(params['n_max_trial'])
 | 
				
			||||||
        self.is_prf_size = bool(params['prf_size'])
 | 
					        self.is_prf_size = bool(params['prf_size'])
 | 
				
			||||||
 | 
					 | 
				
			||||||
        self.remove_t = int(params['remove_t'])
 | 
					        self.remove_t = int(params['remove_t'])
 | 
				
			||||||
        self.int_netw_prf_n = int(params['netw_prf_n'])
 | 
					        self.int_netw_prf_n = int(params['netw_prf_n'])
 | 
				
			||||||
 | 
					        # 方法执行
 | 
				
			||||||
        self.initialize_product_network(params)
 | 
					        self.initialize_product_network(params)
 | 
				
			||||||
        self.initialize_firm_network()
 | 
					        self.initialize_firm_network()
 | 
				
			||||||
        self.initialize_firm_product_network()
 | 
					        self.initialize_firm_product_network()
 | 
				
			||||||
| 
						 | 
					@ -76,27 +76,25 @@ class MyModel(Model):
 | 
				
			||||||
            print(f"Failed to initialize product network: {e}")
 | 
					            print(f"Failed to initialize product network: {e}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def initialize_firm_network(self):
 | 
					    def initialize_firm_network(self):
 | 
				
			||||||
        """ Initialize the firm network by reading firm data from a CSV file """
 | 
					 | 
				
			||||||
        # Read the firm data
 | 
					        # Read the firm data
 | 
				
			||||||
        Firm = pd.read_csv("input_data/Firm_amended.csv")
 | 
					        firm = pd.read_csv("input_data/Firm_amended.csv")
 | 
				
			||||||
        Firm['Code'] = Firm['Code'].astype('string')
 | 
					        firm['Code'] = firm['Code'].astype('string')
 | 
				
			||||||
        Firm.fillna(0, inplace=True)
 | 
					        firm.fillna(0, inplace=True)
 | 
				
			||||||
        Firm_attr = Firm.loc[:, ["Code", "Type_Region", "Revenue_Log"]]
 | 
					        firm_attr = firm.loc[:, ["Code", "Type_Region", "Revenue_Log"]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        firm_product = []
 | 
					        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_product.append(row[row == 1].index.to_list())
 | 
				
			||||||
        Firm_attr.loc[:, 'Product_Code'] = firm_product
 | 
					        firm_attr.loc[:, 'Product_Code'] = firm_product
 | 
				
			||||||
        Firm_attr.set_index('Code', inplace=True)
 | 
					        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
 | 
					        # 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)
 | 
					        nx.set_node_attributes(self.G_Firm, firm_labels_dict)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.Firm = Firm
 | 
					        self.Firm = firm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def initialize_firm_product_network(self):
 | 
					    def initialize_firm_product_network(self):
 | 
				
			||||||
        """ Initialize the firm-product network """
 | 
					        """ 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 字典,存储公司及其受干扰的产品
 | 
				
			||||||
        self.dct_lst_init_disrupt_firm_prod = t_dct
 | 
					        self.dct_lst_init_disrupt_firm_prod = t_dct
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
        # 设置初始受干扰的公司产品状态
 | 
					        # 设置初始受干扰的公司产品状态
 | 
				
			||||||
        for firm, a_lst_product in self.dct_lst_init_disrupt_firm_prod.items():
 | 
					        for firm, a_lst_product in self.dct_lst_init_disrupt_firm_prod.items():
 | 
				
			||||||
            for product in a_lst_product:
 | 
					            for product in a_lst_product:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +23,3 @@ class ProductAgent(Agent):
 | 
				
			||||||
        # 通过 unique_id 查找前驱节点对应的代理对象,直接从 self.product_agents 列表中获取
 | 
					        # 通过 unique_id 查找前驱节点对应的代理对象,直接从 self.product_agents 列表中获取
 | 
				
			||||||
        return [agent for agent in self.model.product_agents if agent.unique_id in predecessors]
 | 
					        return [agent for agent in self.model.product_agents if agent.unique_id in predecessors]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def step(self):
 | 
					 | 
				
			||||||
        # 在每个时间步 进行的操作
 | 
					 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue