diff --git a/.idea/csv-editor.xml b/.idea/csv-editor.xml index 9b71347..dba7d86 100644 --- a/.idea/csv-editor.xml +++ b/.idea/csv-editor.xml @@ -136,6 +136,13 @@ + + + + + + diff --git a/__pycache__/computation.cpython-38.pyc b/__pycache__/computation.cpython-38.pyc index 409e8fa..cd356de 100644 Binary files a/__pycache__/computation.cpython-38.pyc and b/__pycache__/computation.cpython-38.pyc differ diff --git a/__pycache__/firm.cpython-38.pyc b/__pycache__/firm.cpython-38.pyc index d5d075f..b6388e5 100644 Binary files a/__pycache__/firm.cpython-38.pyc and b/__pycache__/firm.cpython-38.pyc differ diff --git a/__pycache__/my_model.cpython-38.pyc b/__pycache__/my_model.cpython-38.pyc index 2ce3b7a..d4057b2 100644 Binary files a/__pycache__/my_model.cpython-38.pyc and b/__pycache__/my_model.cpython-38.pyc differ diff --git a/__pycache__/product.cpython-38.pyc b/__pycache__/product.cpython-38.pyc index acdf26f..a858e0d 100644 Binary files a/__pycache__/product.cpython-38.pyc and b/__pycache__/product.cpython-38.pyc differ diff --git a/computation.py b/computation.py index 4561f76..6cfa875 100644 --- a/computation.py +++ b/computation.py @@ -148,7 +148,7 @@ class Computation: # 归一化生成 lst_prob lst_prob = [size / sum_pred_firm_size for size in lst_pred_firm_size] # 使用 np.isclose() 确保概率总和接近 1 - if not np.isclose(sum(lst_prob), 1.0): + if not pd.np.isclose(sum(lst_prob), 1.0): # print(f"警告: 概率总和为 {sum(lst_prob)},现在进行修正") lst_prob = [prob / sum(lst_prob) for prob in lst_prob] # 确保没有负值或 0 @@ -234,7 +234,7 @@ class Computation: # 归一化生成 lst_prob lst_prob = [size / sum_pred_firm_size for size in lst_succ_firm_size] # 使用 np.isclose() 确保概率总和接近 1 - if not np.isclose(sum(lst_prob), 1.0): + if not pd.np.isclose(sum(lst_prob), 1.0): # print(f"警告: 概率总和为 {sum(lst_prob)},现在进行修正") lst_prob = [prob / sum(lst_prob) for prob in lst_prob] diff --git a/firm.py b/firm.py index 81f48cf..e66be0f 100644 --- a/firm.py +++ b/firm.py @@ -45,7 +45,7 @@ class FirmAgent(Agent): self.c_consumption = demand_quantity # 设备c购买价格(初始值) # self.c_price = c_price - # 资源r补货库存阈值 + # 资源r补货库存阈值 很重要设置 self.s_r = 40 self.S_r = 120 # 设备补货阙值 可选 diff --git a/input_data/产品消耗制造比例.csv b/input_data/产品消耗制造比例.csv new file mode 100644 index 0000000..0c42e76 --- /dev/null +++ b/input_data/产品消耗制造比例.csv @@ -0,0 +1,238 @@ +IndustryID,MaterialID,Quantity +38,47,1.0 +39,49,1.0 +40,44,1.0 +41,15,0.02 +41,18,0.1 +41,20,0.4 +41,22,0.15 +41,23,0.04 +41,25,0.02 +41,31,0.25 +41,36,1.0 +42,15,0.02 +42,18,0.1 +42,20,0.4 +42,22,0.15 +42,23,0.04 +42,25,0.02 +42,31,0.25 +43,46,1.0 +44,7,1.4 +44,15,0.02 +44,18,0.1 +44,20,0.4 +44,22,0.15 +44,23,0.04 +44,25,0.02 +44,27,0.25 +45,7,1.4 +45,15,0.02 +45,18,0.1 +45,20,0.4 +45,22,0.15 +45,23,0.04 +45,25,0.02 +45,27,0.25 +46,15,0.02 +46,18,0.1 +46,20,0.4 +46,22,0.15 +46,23,0.04 +46,25,0.02 +46,27,0.25 +46,32,1.3 +47,15,0.02 +47,18,0.1 +47,20,0.4 +47,22,0.15 +47,23,0.04 +47,25,0.02 +47,27,0.25 +47,33,1.0 +48,15,0.02 +48,18,0.1 +48,20,0.4 +48,22,0.15 +48,23,0.04 +48,25,0.02 +48,27,0.25 +48,34,1.3 +49,15,0.02 +49,18,0.1 +49,20,0.4 +49,22,0.15 +49,23,0.04 +49,25,0.02 +49,27,0.25 +49,35,1.3 +50,19,0.005 +50,20,0.4 +50,22,0.15 +50,23,0.04 +50,25,0.02 +50,28,0.25 +50,29,0.03 +50,27,0.01 +50,40,1.0 +51,19,0.005 +51,20,0.4 +51,22,0.15 +51,23,0.04 +51,25,0.02 +51,28,0.25 +51,29,0.03 +51,27,0.01 +51,38,1.0 +52,19,0.005 +52,20,0.4 +52,22,0.15 +52,23,0.04 +52,25,0.02 +52,28,0.25 +52,29,0.03 +52,27,0.01 +52,41,1.0 +53,19,0.005 +53,20,0.4 +53,22,0.15 +53,23,0.04 +53,25,0.02 +53,28,0.25 +53,29,0.03 +53,27,0.01 +53,39,1.0 +54,19,0.005 +54,20,0.4 +54,22,0.15 +54,23,0.04 +54,25,0.02 +54,28,0.25 +54,29,0.03 +54,27,0.01 +54,43,1.0 +55,19,0.005 +55,20,0.4 +55,22,0.15 +55,23,0.04 +55,25,0.02 +55,28,0.25 +55,29,0.03 +55,27,0.01 +55,42,1.0 +90,7,1.7 +90,8,0.07 +90,9,0.07 +90,10,0.02 +90,11,0.04 +90,17,0.1 +90,19,0.005 +90,20,0.4 +90,21,0.04 +90,23,0.04 +90,24,0.07 +90,25,0.02 +90,28,0.03 +90,31,0.25 +90,44,0.2 +90,45,0.1 +91,8,0.07 +91,9,0.07 +91,10,0.02 +91,11,0.04 +91,17,0.1 +91,18,0.1 +91,19,0.005 +91,20,0.4 +91,23,0.04 +91,24,0.07 +91,28,0.03 +91,31,0.25 +92,8,0.07 +92,9,0.07 +92,10,0.02 +92,11,0.04 +92,17,0.1 +92,18,0.1 +92,19,0.005 +92,20,0.4 +92,23,0.04 +92,24,0.07 +92,28,0.03 +92,31,0.26 +93,8,0.07 +93,9,0.07 +93,10,0.02 +93,11,0.04 +93,17,0.1 +93,18,0.1 +93,19,0.005 +93,20,0.4 +93,23,0.04 +93,24,0.07 +93,28,0.03 +93,31,0.27 +94,8,0.07 +94,9,0.07 +94,10,0.02 +94,11,0.04 +94,17,0.1 +94,18,0.1 +94,19,0.005 +94,20,0.4 +94,23,0.04 +94,24,0.07 +94,28,0.03 +94,31,0.28 +95,8,0.1 +95,9,0.0 +95,10,7.0 +95,11,0.02 +95,12,0.03 +95,13,0.01 +95,14,0.01 +95,15,0.07 +95,16,0.05 +95,17,0.003 +95,18,0.1 +95,19,0.04 +95,20,0.01 +95,21,0.04 +95,22,0.03 +95,23,0.02 +95,24,0.01 +95,25,0.05 +95,26,0.01 +95,27,0.003 +95,28,0.1 +95,29,0.05 +95,30,0.04 +95,31,0.01 +95,37,0.1 +95,44,0.04 +95,45,0.7 +95,46,0.07 +95,47,0.01 +95,48,0.03 +95,49,0.01 +95,50,0.01 +95,51,0.01 +95,52,0.2 +95,53,0.03 +95,54,0.01 +95,55,0.01 +95,90,0.01 +95,91,0.01 +95,92,1.0 +95,93,1.0 +95,94,1.0 +96,95,1.0 +96,101,0.01 +96,102,0.01 +96,103,0.02 +96,104,0.01 +96,105,0.2 +96,106,0.1 +96,107,0.01 +96,108,0.03 +96,109,0.02 diff --git a/main.py b/main.py index 93d7407..4880349 100644 --- a/main.py +++ b/main.py @@ -44,7 +44,7 @@ if __name__ == '__main__': # 输入参数 parser = argparse.ArgumentParser(description='setting') parser.add_argument('--exp', type=str, default='without_exp') - parser.add_argument('--job', type=int, default='1') + parser.add_argument('--job', type=int, default='3') parser.add_argument('--reset_sample', type=int, default='0') parser.add_argument('--reset_db', type=bool, default=False) diff --git a/my_model.py b/my_model.py index c78cfc1..255b10f 100644 --- a/my_model.py +++ b/my_model.py @@ -316,21 +316,23 @@ class MyModel(Model): for ag_node, attr in self.product_network.nodes(data=True): # 产业种类 + production_ratio = self.data_production_ratio[self.data_production_ratio['IndustryID'] == ag_node]['Production_ratio'] + # 转换为字典 + if not production_ratio.empty: # 检查 Series 是否为空 + production_ratio_dict = production_ratio.iloc[0] # 提取第一个值 + else: + # 如果 Series 是空的,返回一个空字典 + production_ratio_dict = {} - # type2 = self.type2[self.type2["Index"] == ag_node]["产业种类"] - # depreciation ratio 折旧比值 - # product_id = product_id.iloc[0] - product = ProductAgent(ag_node, self, name=attr['Name'], type2=0) + product = ProductAgent(ag_node, self, name=attr['Name'], type2=0, production_ratio=production_ratio_dict) self.add_agent(product) - # self.grid.place_agent(product, ag_node) - ##print(f"Product agent created: {product.name}, ID: {product.unique_id}") for ag_node, attr in self.firm_network.nodes(data=True): a_lst_product = [agent for agent in self.product_agents if agent.unique_id in attr['Product_Code']] - demand_quantity = self.data_consumed[self.data_consumed['Firm_Code'] == ag_node] + demand_quantity = self.data_materials[self.data_materials['Firm_Code'] == ag_node] - production_output = self.data_produced[self.data_consumed['Firm_Code'] == ag_node] + production_output = self.data_produced[self.data_materials['Firm_Code'] == ag_node] # c购买价格? 数据预处理 # c_price = self.Firm.loc[self.Firm['Code'] == ag_node, 'c_price'].values[0] @@ -418,24 +420,26 @@ class MyModel(Model): def j_comp_consumed_produced(self): # 着重修改这 然后考虑逻辑 如何传递值 - data_consumed = pd.read_csv('input_data/input_firm_data/firms_materials.csv') + data_materials = pd.read_csv('input_data/input_firm_data/firms_materials.csv') data_produced = pd.read_csv('input_data/input_firm_data/firms_products.csv') - data_not_consumed = data_consumed.groupby('Firm_Code')[['材料id', '材料数量']] \ + data_production_ratio = pd.read_csv('input_data/产品消耗制造比例.csv') + + data_not_consumed = data_materials.groupby('Firm_Code')[['材料id', '材料数量']] \ .apply(lambda x: dict(zip(x['材料id'], x['材料数量']))) \ .reset_index(name='Material_not_Consumed') - # 这里简单设置为折半 考虑 企业的设备量 - # 可以引入 换算率 也就是 材料——计算产品比例 通过上游产业 现在假设为 2:1 的比例 - data_consumed0 = data_consumed.groupby('Firm_Code')[['材料id', '材料数量']] \ - .apply(lambda x: dict(zip(x['材料id'], x['材料数量'] / 2))) \ - .reset_index(name='Material_not_Consumed') - data_produced = data_consumed.groupby('Firm_Code')[['材料id', '材料数量']] \ - .apply(lambda x: dict(zip(x['材料id'], x['材料数量'] / 4))) \ + data_not_produced = data_produced.groupby('Firm_Code')[['产品id', '产品数量']] \ + .apply(lambda x: dict(zip(x['产品id'], x['产品数量']))) \ .reset_index(name='Material_not_Consumed') - self.data_consumed = data_consumed0 - self.data_produced = data_produced + data_production_ratio = data_production_ratio.groupby('IndustryID')[['MaterialID', 'Quantity']] \ + .apply(lambda x: dict(zip(x['MaterialID'], x['Quantity']))) \ + .reset_index(name='Production_ratio') + + self.data_materials = data_not_consumed + self.data_produced = data_not_produced + self.data_production_ratio = data_production_ratio def step(self): # 1. Remove edge to customer and disrupt customer up product @@ -538,16 +542,25 @@ class MyModel(Model): C_list[2] = C0_list[2] # 消耗资源过程 # 这里需要修改 + k = 0.6 + # 特定的产业 + firm_consumed_nums_dct = {} + for indus_i in firm.indus_i: + print("这是 公司产业 编码:" + indus_i) + print(type(indus_i)) + for p_id, p_nums in firm.P.items(): + print("这是 产品 编码:" + p_id) + print(type(p_id)) + if p_id == indus_i.unique_id: + consumed_nums = p_nums * k + firm_consumed_nums_dct[indus_i] = consumed_nums for r_id, r_nums in firm.R.items(): - for consumed_id, consumed_nums in firm.c_consumption: - if consumed_id == r_id: - r_nums = r_nums - consumed_nums - + for i, value in firm_consumed_nums_dct.items(): + if r_id in i: + r_nums = r_nums - i[r_id] * value # 生产产品过程 for p_id, p_nums in firm.P.items(): - for product_id, product_nums in firm.c_consumption: - if product_id == p_id: - p_nums = p_nums + product_nums + p_nums = p_nums * 1.6 firm.refresh_R() # 刷新 C状态 diff --git a/product.py b/product.py index 24ef514..62c8659 100644 --- a/product.py +++ b/product.py @@ -2,13 +2,14 @@ from mesa import Agent class ProductAgent(Agent): - def __init__(self, unique_id, model, name, type2): + def __init__(self, unique_id, model, name, type2, production_ratio): # 调用超类的 __init__ 方法 super().__init__(unique_id, model) # 初始化代理属性 self.name = name self.product_network = self.model.product_network + self.production_ratio = production_ratio if type2 == 0: self.is_equip = True else: