修改部分内容,包括变量名子,化为 小写,能运行的mesa的完整程序,不包括模型更改和结果输出。

This commit is contained in:
Cricial 2024-09-18 16:59:32 +08:00
parent ede00b0403
commit 356582e2f3
6 changed files with 25 additions and 34 deletions

Binary file not shown.

View File

@ -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()字典化 格式类似 格式 { code0 : {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 格式

View File

@ -229,7 +229,3 @@ class FirmAgent(Agent):
return False
else:
return False
def step(self):
# 在每个时间步进行的操作
pass

View File

@ -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:

View File

@ -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