调整比例

This commit is contained in:
Cricial 2024-12-27 16:32:57 +08:00
parent 2d359c80f1
commit 11b7b738f9
11 changed files with 290 additions and 31 deletions

View File

@ -136,6 +136,13 @@
</Attribute>
</value>
</entry>
<entry key="\input_data\产品消耗制造比例.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\output_result\risk\count.csv">
<value>
<Attribute>

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

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

View File

@ -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
# 设备补货阙值 可选

View File

@ -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
1 IndustryID MaterialID Quantity
2 38 47 1.0
3 39 49 1.0
4 40 44 1.0
5 41 15 0.02
6 41 18 0.1
7 41 20 0.4
8 41 22 0.15
9 41 23 0.04
10 41 25 0.02
11 41 31 0.25
12 41 36 1.0
13 42 15 0.02
14 42 18 0.1
15 42 20 0.4
16 42 22 0.15
17 42 23 0.04
18 42 25 0.02
19 42 31 0.25
20 43 46 1.0
21 44 7 1.4
22 44 15 0.02
23 44 18 0.1
24 44 20 0.4
25 44 22 0.15
26 44 23 0.04
27 44 25 0.02
28 44 27 0.25
29 45 7 1.4
30 45 15 0.02
31 45 18 0.1
32 45 20 0.4
33 45 22 0.15
34 45 23 0.04
35 45 25 0.02
36 45 27 0.25
37 46 15 0.02
38 46 18 0.1
39 46 20 0.4
40 46 22 0.15
41 46 23 0.04
42 46 25 0.02
43 46 27 0.25
44 46 32 1.3
45 47 15 0.02
46 47 18 0.1
47 47 20 0.4
48 47 22 0.15
49 47 23 0.04
50 47 25 0.02
51 47 27 0.25
52 47 33 1.0
53 48 15 0.02
54 48 18 0.1
55 48 20 0.4
56 48 22 0.15
57 48 23 0.04
58 48 25 0.02
59 48 27 0.25
60 48 34 1.3
61 49 15 0.02
62 49 18 0.1
63 49 20 0.4
64 49 22 0.15
65 49 23 0.04
66 49 25 0.02
67 49 27 0.25
68 49 35 1.3
69 50 19 0.005
70 50 20 0.4
71 50 22 0.15
72 50 23 0.04
73 50 25 0.02
74 50 28 0.25
75 50 29 0.03
76 50 27 0.01
77 50 40 1.0
78 51 19 0.005
79 51 20 0.4
80 51 22 0.15
81 51 23 0.04
82 51 25 0.02
83 51 28 0.25
84 51 29 0.03
85 51 27 0.01
86 51 38 1.0
87 52 19 0.005
88 52 20 0.4
89 52 22 0.15
90 52 23 0.04
91 52 25 0.02
92 52 28 0.25
93 52 29 0.03
94 52 27 0.01
95 52 41 1.0
96 53 19 0.005
97 53 20 0.4
98 53 22 0.15
99 53 23 0.04
100 53 25 0.02
101 53 28 0.25
102 53 29 0.03
103 53 27 0.01
104 53 39 1.0
105 54 19 0.005
106 54 20 0.4
107 54 22 0.15
108 54 23 0.04
109 54 25 0.02
110 54 28 0.25
111 54 29 0.03
112 54 27 0.01
113 54 43 1.0
114 55 19 0.005
115 55 20 0.4
116 55 22 0.15
117 55 23 0.04
118 55 25 0.02
119 55 28 0.25
120 55 29 0.03
121 55 27 0.01
122 55 42 1.0
123 90 7 1.7
124 90 8 0.07
125 90 9 0.07
126 90 10 0.02
127 90 11 0.04
128 90 17 0.1
129 90 19 0.005
130 90 20 0.4
131 90 21 0.04
132 90 23 0.04
133 90 24 0.07
134 90 25 0.02
135 90 28 0.03
136 90 31 0.25
137 90 44 0.2
138 90 45 0.1
139 91 8 0.07
140 91 9 0.07
141 91 10 0.02
142 91 11 0.04
143 91 17 0.1
144 91 18 0.1
145 91 19 0.005
146 91 20 0.4
147 91 23 0.04
148 91 24 0.07
149 91 28 0.03
150 91 31 0.25
151 92 8 0.07
152 92 9 0.07
153 92 10 0.02
154 92 11 0.04
155 92 17 0.1
156 92 18 0.1
157 92 19 0.005
158 92 20 0.4
159 92 23 0.04
160 92 24 0.07
161 92 28 0.03
162 92 31 0.26
163 93 8 0.07
164 93 9 0.07
165 93 10 0.02
166 93 11 0.04
167 93 17 0.1
168 93 18 0.1
169 93 19 0.005
170 93 20 0.4
171 93 23 0.04
172 93 24 0.07
173 93 28 0.03
174 93 31 0.27
175 94 8 0.07
176 94 9 0.07
177 94 10 0.02
178 94 11 0.04
179 94 17 0.1
180 94 18 0.1
181 94 19 0.005
182 94 20 0.4
183 94 23 0.04
184 94 24 0.07
185 94 28 0.03
186 94 31 0.28
187 95 8 0.1
188 95 9 0.0
189 95 10 7.0
190 95 11 0.02
191 95 12 0.03
192 95 13 0.01
193 95 14 0.01
194 95 15 0.07
195 95 16 0.05
196 95 17 0.003
197 95 18 0.1
198 95 19 0.04
199 95 20 0.01
200 95 21 0.04
201 95 22 0.03
202 95 23 0.02
203 95 24 0.01
204 95 25 0.05
205 95 26 0.01
206 95 27 0.003
207 95 28 0.1
208 95 29 0.05
209 95 30 0.04
210 95 31 0.01
211 95 37 0.1
212 95 44 0.04
213 95 45 0.7
214 95 46 0.07
215 95 47 0.01
216 95 48 0.03
217 95 49 0.01
218 95 50 0.01
219 95 51 0.01
220 95 52 0.2
221 95 53 0.03
222 95 54 0.01
223 95 55 0.01
224 95 90 0.01
225 95 91 0.01
226 95 92 1.0
227 95 93 1.0
228 95 94 1.0
229 96 95 1.0
230 96 101 0.01
231 96 102 0.01
232 96 103 0.02
233 96 104 0.01
234 96 105 0.2
235 96 106 0.1
236 96 107 0.01
237 96 108 0.03
238 96 109 0.02

View File

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

View File

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

View File

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